2026-04-26T03:02:43.035ZK8s云原生Seata
K8s搭建 Seata + DB 模式,无注册中心!
K8s搭建Seata + DB模式
Apache Seata™ (incubating) 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。

去掉注册中心的好处:
- 减少组件数量 :不使用注册中心可以减少系统的复杂度,简化架构,降低维护成本。
- 部署简单 :只需配置 Seata Server 和数据库,无需额外配置注册中心。
- 集中配置 :所有的配置和服务都集中在 Seata Server 和数据库中,方便管理和监控。
- 减少故障点 :注册中心作为一个独立的服务可能会成为单点故障,不使用注册中心可以降低系统的故障风险。
- 性能优化 :在 DB 模式下,Seata 直接使用数据库进行事务管理,减少了网络通信的延迟,适合对性能要求较高的场景。
- 数据持久化 :事务信息、锁信息等都存储在数据库中,便于进行数据的持久化和查询。
适用场景
- 中型项目 :对于小型或中小型项目,DB 模式足以覆盖需求,使用注册中心就显得过于复杂。
- 内部服务 :在内部网络中,服务之间的调用延迟较小,注册中心的必要性降低。
- 对性能要求高的场景 :如金融、在线支付等领域,直接与数据库交互可以提高系统的整体性能。
部署前置条件: 1. 数据库 2. k8s集群
- 下载db模式需要在数据库创建对应的表结构
https://github.com/apache/incubator-seata/tree/develop/script/server/db
- 准备部署文件
apiVersion: v1
kind: Secret
metadata:
name: seata-db-secret
namespace: default
type: Opaque
stringData:
username: seata
password: "请改为强密码"
apiVersion: v1
kind: ConfigMap
metadata:
name: seata-server-config
namespace: default
data:
application.yml: |
server:
port: 7091
spring:
application:
name: seata-server
logging:
config: classpath:logback-spring.xml
file:
path: ${log.home:${user.home}/logs/seata}
console:
user:
username: seata
password: seata
seata:
config:
type: file
registry:
type: file
store:
mode: db
session:
mode: db
lock:
mode: db
db:
datasource: druid
db-type: mysql
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://<数据库地址>:3306/seata?rewriteBatchedStatements=true&useUnicode=true&characterEncoding=utf8&connectTimeout=5000&socketTimeout=3000
user: ${SEATA_DB_USER}
password: ${SEATA_DB_PASSWORD}
min-conn: 5
max-conn: 100
global-table: global_table
branch-table: branch_table
lock-table: lock_table
security:
secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
tokenValidityInMilliseconds: 1800000
ignore:
urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login
apiVersion: apps/v1
kind: Deployment
metadata:
name: seata-server
namespace: default
labels:
app: seata-server
spec:
replicas: 1
selector:
matchLabels:
app: seata-server
template:
metadata:
labels:
app: seata-server
spec:
containers:
- name: seata-server
image: docker.io/apache/seata-server:latest
imagePullPolicy: IfNotPresent
env:
- name: SEATA_PORT
value: "8091"
- name: STORE_MODE
value: "db"
- name: SEATA_DB_USER
valueFrom:
secretKeyRef:
name: seata-db-secret
key: username
- name: SEATA_DB_PASSWORD
valueFrom:
secretKeyRef:
name: seata-db-secret
key: password
ports:
- name: http
containerPort: 8091
protocol: TCP
- name: http
containerPort: 7091
protocol: TCP
volumeMounts:
- name: seata-config
mountPath: /seata-server/resources/application.yml
subPath: application.yml
volumes:
- name: seata-config
configMap:
name: seata-server-config
apiVersion: v1
kind: Service
metadata:
name: seata-server
namespace: default
labels:
k8s-app: seata-server
spec:
type: NodePort
ports:
- port: 8091
nodePort: 30091
protocol: TCP
name: http
selector:
k8s-app: seata-server
- 创建服务
kubectl apply -f *
- 查看服务日志,是否正常启动
