K8S 部署seata 1.6.x高可用集群

发布时间 2023-07-03 11:08:21作者: Gshelldon

写在之前

seata 1.6 无法注册到nacos 配置中心 下面有说原因。

2023年7月2日 gshelldon写的博客。

大于 1.4版本之后,配置文件就不是用registry.conf了所以按照官方的文档搭建都是坑。

默认使用application.yml的配置文件进行管理。

都是坑!!!害我排查了几天。

image

所以官方给出的yaml需要修改,如果使用官方的配置文件需要在iimage版本在1.4.x,我测试的是1.4.2可以正常注册并配置,dcoker镜像 seataio/seata-server:1.4.2

创建数据库配置

参考连接

创建数据库 seata

> create database seata;

创建seata-server需要的表

配置yaml

vim 01-cm.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: seata-ha-server-config
data:
  application.yml: |
    server:
      # web管理的端口
      port: 7091
    spring:
      application:
        name: seata-server
    logging:
      config: classpath:logback-spring.xml
      file:
        path: ${user.home}/logs/seata
      extend:
        logstash-appender:
          destination: 127.0.0.1:4560
        kafka-appender:
          bootstrap-servers: 127.0.0.1:9092
          topic: logback_to_logstash
    console:
      # web端管理的用户名和密码
      user:
        username: seata
        password: seata
    seata:
      config:
        type: nacos
        nacos:
          # nacos的地址 k8s内部域名需要在后面加端口号
          server-addr: 192.168.1.20:30848
          # namespace:  默认使用 public
          group: SEATA_GROUP
          username: nacos
          password: nacos
          # 创建的配置文件名称
          data-id: seataServer.properties
      registry:
        type: nacos
        nacos:
          application: seata-server
          server-addr: 192.168.1.20:30848
          group: SEATA_GROUP
          #namespace: 
          cluster: default
          username: nacos
          password: nacos
      #store:
      #  mode: db
      #  db:
      #    datasource: druid
      #    dbType: mysql
      #    driverClassName: com.mysql.jdbc.Driver 
      #    url: jdbc:mysql:/192.168.1.50:3306/seata?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
      #    user: root
      #    password: root
      # seata 服务暴露的地址
      server:
        service-port: 8091
      security:
        secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017
        tokenValidityInMilliseconds: 1800000
        ignore:
          urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/api/v1/auth/login

vim 02-deploy.yaml

apiVersion: v1
kind: Service
metadata:
  name: seata-ha-server
  labels:
    app.kubernetes.io/name: seata-ha-server
  #annotations:
  #  service.beta.kubernetes.io/alibaba-cloud-loadbalancer-id: lb-2zeabcdefghijklmn
  #  service.beta.kubernetes.io/alicloud-loadbalancer-force-override-listeners: 'true'
spec:
  #type: LoadBalancer
  type: NodePort
  ports:
    - port: 8091
      protocol: TCP
      targetPort: 8091
      name: http
    - port: 7091
      protocol: TCP
      targetPort: 7091
      name: web
  selector:
    app.kubernetes.io/name: seata-ha-server
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: seata-ha-server
  labels:
    app.kubernetes.io/name: seata-ha-server
spec:
  serviceName: seata-ha-server
  replicas: 3
  selector:
    matchLabels:
      app.kubernetes.io/name: seata-ha-server
  template:
    metadata:
      labels:
        app.kubernetes.io/name: seata-ha-server
    spec:
      containers:
        - name: seata-ha-server
          image: docker.io/seataio/seata-server:1.6.0
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 8091
              protocol: TCP
            - name: web
              containerPort: 7091
              protocol: TCP
          volumeMounts:
          - mountPath: /seata-server/resources/application.yml
            name: seata-config
            subPath: application.yml
      volumes:
        - name: seata-config
          configMap:
            name: seata-ha-server-config

image

成功注册到注册中心

image

Nacos注册中心的配置

命名空间: namespace

组: SEATA_GROUP

配置名称:seataServer.properties

store.mode=db
#-----db-----
store.db.datasource=druid
store.db.dbType=mysql
# 需要根据mysql的版本调整driverClassName
# mysql8及以上版本对应的driver:com.mysql.cj.jdbc.Driver
# mysql8以下版本的driver:com.mysql.jdbc.Driver
store.db.driverClassName=com.mysql.jdbc.Driver
store.db.url=jdbc:mysql://192.168.1.50:3306/seata?useUnicode=true&characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
store.db.user= root
store.db.password= root
# 数据库初始连接数
store.db.minConn=1
# 数据库最大连接数
store.db.maxConn=200
# 获取连接时最大等待时间 默认5000,单位毫秒
store.db.maxWait=5000
# 全局事务表名 默认global_table
store.db.globalTable=global_table
# 分支事务表名 默认branch_table
store.db.branchTable=branch_table
# 全局锁表名 默认lock_table
store.db.lockTable=lock_table
store.db.distributedLockTable=distributed_lock
# 查询全局事务一次的最大条数 默认100
store.db.queryLimit=100


# undo保留天数 默认7天,log_status=1(附录3)和未正常清理的undo
server.undo.logSaveDays=7
# undo清理线程间隔时间 默认86400000,单位毫秒
server.undo.logDeletePeriod=86400000
# 二阶段提交重试超时时长 单位ms,s,m,h,d,对应毫秒,秒,分,小时,天,默认毫秒。默认值-1表示无限重试
# 公式: timeout>=now-globalTransactionBeginTime,true表示超时则不再重试
# 注: 达到超时时间后将不会做任何重试,有数据不一致风险,除非业务自行可校准数据,否者慎用
server.maxCommitRetryTimeout=-1
# 二阶段回滚重试超时时长
server.maxRollbackRetryTimeout=-1
# 二阶段提交未完成状态全局事务重试提交线程间隔时间 默认1000,单位毫秒
server.recovery.committingRetryPeriod=1000
# 二阶段异步提交状态重试提交线程间隔时间 默认1000,单位毫秒
server.recovery.asynCommittingRetryPeriod=1000
# 二阶段回滚状态重试回滚线程间隔时间  默认1000,单位毫秒
server.recovery.rollbackingRetryPeriod=1000
# 超时状态检测重试线程间隔时间 默认1000,单位毫秒,检测出超时将全局事务置入回滚会话管理器
server.recovery.timeoutRetryPeriod=1000

image

NodePort访问

image