目录
一、搭建Redis集群
1、安装redis
2、测试连接
二、搭建RocketMq
1、下载资源
2、安装
3、查看dashboard
三、搭建Elasticseach
1、es-service.yaml
2、es-statefulset.yaml
3、创建资源
四、搭建mongodb
1、mongodb.yaml
2、创建资源
五、搭建Mysql集群
1、init-nacos-configmap.yaml
2、mysql-configmap.yaml
3、mysql-secret.yaml
4、mysql-service.yaml
5、mysql-statefulset.yaml
6、创建资源
六、搭建Nacos集群
1、nacos-quick-start.yaml
2、创建资源
七、搭建Seata
1、seata-server.yaml
八、附录
1、Redis
2、RocketMQ
3、参考
一、搭建Redis集群
具体安装请参考本人下面这篇文章
Kubernetes实战——基于Helm安装Redis主从模式_redis helm 部署-CSDN博客
1、安装redis
1、创建namespace
kubectl create ns redis2、安装redis、helm install redis ./redis/ -n redis3、查看资源
kubectl get po,svc -n redis
2、测试连接
二、搭建RocketMq
1、下载资源
1、添加helm仓库helm repo add rocketmq-repo https://helm-charts.itboon.top/rocketmqhelm repo update rocketmq-repo2、查看仓库
helm search repo rocketmq3、拉取资源,这里选择集群版本helm pull rocketmq-repo/rocketmq-cluster 4、解压文件
tar -zxf rocketmq-cluster-12.3.2.tgz
2、安装
1、创建namespace
kubectl create ns rocketmq2、安装rocketmq
helm install rocketmq ./rocketmq-cluster/ -n rocketmq3、查看资源
kubectl get po,svc -n rocketmq
3、查看dashboard
三、搭建Elasticseach
1、es-service.yaml
apiVersion: v1
kind: Service
metadata:name: elasticsearchnamespace: elastic
spec:clusterIP: Noneports:- name: elasticsearch-inport: 9300protocol: TCPtargetPort: 9300- name: elasticsearch-outport: 9200protocol: TCPtargetPort: 9200selector:app: elasticsearchtype: ClusterIP
2、es-statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:name: elasticsearchnamespace: elastic
spec:replicas: 3selector:matchLabels:app: elasticsearchserviceName: elasticsearchtemplate:metadata:labels:app: elasticsearchspec:containers:- name: elasticsearchenv:- name: ES_JAVA_OPTSvalue: -Xms2048m -Xmx2048m- name: node.datavalue: "true"- name: node.mastervalue: "true"- name: path.datavalue: /usr/share/elasticsearch/data- name: cluster.namevalue: es-cluster- name: node.namevalueFrom:fieldRef:fieldPath: metadata.name- name: cluster.initial_master_nodesvalue: "elasticsearch-0"- name: discovery.zen.minimum_master_nodesvalue: "1"- name: discovery.seed_hostsvalue: "elasticsearch"image: elasticsearch:7.17.25imagePullPolicy: IfNotPresentlifecycle:postStart:exec:command:- /bin/sh- -c- |sysctl -w vm.max_map_count=262144ulimit -l unlimitedulimit -n 65536chown -R elasticsearch:elasticsearch /usr/share/elasticsearch/dataports:- containerPort: 9200name: 9200tcp2protocol: TCP- containerPort: 9300name: 9300tcp2protocol: TCPresources:limits:cpu: "1"memory: 4Girequests:cpu: "1"memory: 2GivolumeMounts:- name: elasticsearch-datamountPath: /usr/share/elasticsearch/datatolerations: #添加容忍,可以部署到master节点上,实际生产不要这样- key: "node-role.kubernetes.io/master"operator: "Exists"effect: "NoSchedule"volumeClaimTemplates:- apiVersion: v1kind: PersistentVolumeClaimmetadata:name: elasticsearch-datanamespace: elasticspec:accessModes:- ReadWriteManyresources:requests:storage: 2GistorageClassName: manager-nfs-storage
3、创建资源
1、创建资源
kubectl create -f elasticsearch/2、查看资源
kubectl get po,svc -n elastic
四、搭建mongodb
1、mongodb.yaml
---
apiVersion: v1
kind: Service
metadata:name: mongodbnamespace: mongodblabels:app: mongodb
spec:type: NodePortports:- name: mongoport: 27017nodePort: 30017protocol: TCPselector:app: mongodb
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: mongodbnamespace: mongodb
spec:serviceName: "mongodb"replicas: 3selector:matchLabels:app: mongodbtemplate:metadata:labels:app: mongodbspec:containers:- name: mongodbimage: mongo:6.0.8imagePullPolicy: IfNotPresentports:- containerPort: 27017name: web # 该端口配置的名字volumeClaimTemplates:- metadata:name: mongodbnamespace: mongodbspec:storageClassName: manager-nfs-storageaccessModes:- ReadWriteManyresources:requests:storage: 1Gi
2、创建资源
kubectl create -f mongodb.yaml kubectl get po,svc -n mongodb
五、搭建Mysql集群
1、init-nacos-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: init-sqlnamespace: mysql
data:nacos-mysql.sql: |/******************************************//* 数据库全名 = nacos_config *//* 表名称 = config_info *//* 由于需要安装nacos,初始化数据库的时候将SQL语句直接执行出来*//******************************************/CREATE DATABASE IF NOT EXISTS nacos_config DEFAULT CHARSET utf8 COLLATE utf8_general_ci;DROP TABLE IF EXISTS config_info;CREATE TABLE `config_info` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',`data_id` varchar(255) NOT NULL COMMENT 'data_id',`group_id` varchar(255) DEFAULT NULL,`content` longtext NOT NULL COMMENT 'content',`md5` varchar(32) DEFAULT NULL COMMENT 'md5',`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',`src_user` text COMMENT 'source user',`src_ip` varchar(20) DEFAULT NULL COMMENT 'source ip',`app_name` varchar(128) DEFAULT NULL,`tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',`c_desc` varchar(256) DEFAULT NULL,`c_use` varchar(64) DEFAULT NULL,`effect` varchar(64) DEFAULT NULL,`type` varchar(64) DEFAULT NULL,`c_schema` text,PRIMARY KEY (`id`),UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info';/******************************************//* 数据库全名 = nacos_config *//* 表名称 = config_info_aggr *//******************************************/DROP TABLE IF EXISTS config_info_aggr;CREATE TABLE `config_info_aggr` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',`data_id` varchar(255) NOT NULL COMMENT 'data_id',`group_id` varchar(255) NOT NULL COMMENT 'group_id',`datum_id` varchar(255) NOT NULL COMMENT 'datum_id',`content` longtext NOT NULL COMMENT '内容',`gmt_modified` datetime NOT NULL COMMENT '修改时间',`app_name` varchar(128) DEFAULT NULL,`tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',PRIMARY KEY (`id`),UNIQUE KEY `uk_configinfoaggr_datagrouptenantdatum` (`data_id`,`group_id`,`tenant_id`,`datum_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='增加租户字段';/******************************************//* 数据库全名 = nacos_config *//* 表名称 = config_info_beta *//******************************************/DROP TABLE IF EXISTS config_info_beta;CREATE TABLE `config_info_beta` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',`data_id` varchar(255) NOT NULL COMMENT 'data_id',`group_id` varchar(128) NOT NULL COMMENT 'group_id',`app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',`content` longtext NOT NULL COMMENT 'content',`beta_ips` varchar(1024) DEFAULT NULL COMMENT 'betaIps',`md5` varchar(32) DEFAULT NULL COMMENT 'md5',`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',`src_user` text COMMENT 'source user',`src_ip` varchar(20) DEFAULT NULL COMMENT 'source ip',`tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',PRIMARY KEY (`id`),UNIQUE KEY `uk_configinfobeta_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_beta';/******************************************//* 数据库全名 = nacos_config *//* 表名称 = config_info_tag *//******************************************/DROP TABLE IF EXISTS config_info_tag;CREATE TABLE `config_info_tag` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',`data_id` varchar(255) NOT NULL COMMENT 'data_id',`group_id` varchar(128) NOT NULL COMMENT 'group_id',`tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',`tag_id` varchar(128) NOT NULL COMMENT 'tag_id',`app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',`content` longtext NOT NULL COMMENT 'content',`md5` varchar(32) DEFAULT NULL COMMENT 'md5',`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',`src_user` text COMMENT 'source user',`src_ip` varchar(20) DEFAULT NULL COMMENT 'source ip',PRIMARY KEY (`id`),UNIQUE KEY `uk_configinfotag_datagrouptenanttag` (`data_id`,`group_id`,`tenant_id`,`tag_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info_tag';/******************************************//* 数据库全名 = nacos_config *//* 表名称 = config_tags_relation *//******************************************/DROP TABLE IF EXISTS config_tags_relation;CREATE TABLE `config_tags_relation` (`id` bigint(20) NOT NULL COMMENT 'id',`tag_name` varchar(128) NOT NULL COMMENT 'tag_name',`tag_type` varchar(64) DEFAULT NULL COMMENT 'tag_type',`data_id` varchar(255) NOT NULL COMMENT 'data_id',`group_id` varchar(128) NOT NULL COMMENT 'group_id',`tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',`nid` bigint(20) NOT NULL AUTO_INCREMENT,PRIMARY KEY (`nid`),UNIQUE KEY `uk_configtagrelation_configidtag` (`id`,`tag_name`,`tag_type`),KEY `idx_tenant_id` (`tenant_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_tag_relation';/******************************************//* 数据库全名 = nacos_config *//* 表名称 = group_capacity *//******************************************/DROP TABLE IF EXISTS group_capacity;CREATE TABLE `group_capacity` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',`group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID,空字符表示整个集群',`quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',`usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',`max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',`max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数,,0表示使用默认值',`max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',`max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',PRIMARY KEY (`id`),UNIQUE KEY `uk_group_id` (`group_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='集群、各Group容量信息表';/******************************************//* 数据库全名 = nacos_config *//* 表名称 = his_config_info *//******************************************/DROP TABLE IF EXISTS his_config_info;CREATE TABLE `his_config_info` (`id` bigint(64) unsigned NOT NULL,`nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT,`data_id` varchar(255) NOT NULL,`group_id` varchar(128) NOT NULL,`app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',`content` longtext NOT NULL,`md5` varchar(32) DEFAULT NULL,`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,`gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,`src_user` text,`src_ip` varchar(20) DEFAULT NULL,`op_type` char(10) DEFAULT NULL,`tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',PRIMARY KEY (`nid`),KEY `idx_gmt_create` (`gmt_create`),KEY `idx_gmt_modified` (`gmt_modified`),KEY `idx_did` (`data_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='多租户改造';/******************************************//* 数据库全名 = nacos_config *//* 表名称 = tenant_capacity *//******************************************/DROP TABLE IF EXISTS tenant_capacity;CREATE TABLE `tenant_capacity` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',`tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID',`quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',`usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',`max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',`max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数',`max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',`max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',`gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',`gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',PRIMARY KEY (`id`),UNIQUE KEY `uk_tenant_id` (`tenant_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='租户容量信息表';DROP TABLE IF EXISTS tenant_info;CREATE TABLE `tenant_info` (`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',`kp` varchar(128) NOT NULL COMMENT 'kp',`tenant_id` varchar(128) default '' COMMENT 'tenant_id',`tenant_name` varchar(128) default '' COMMENT 'tenant_name',`tenant_desc` varchar(256) DEFAULT NULL COMMENT 'tenant_desc',`create_source` varchar(32) DEFAULT NULL COMMENT 'create_source',`gmt_create` bigint(20) NOT NULL COMMENT '创建时间',`gmt_modified` bigint(20) NOT NULL COMMENT '修改时间',PRIMARY KEY (`id`),UNIQUE KEY `uk_tenant_info_kptenantid` (`kp`,`tenant_id`),KEY `idx_tenant_id` (`tenant_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tenant_info';DROP TABLE IF EXISTS users;CREATE TABLE `users` (`username` varchar(50) NOT NULL PRIMARY KEY,`password` varchar(500) NOT NULL,`enabled` boolean NOT NULL);DROP TABLE IF EXISTS roles;CREATE TABLE `roles` (`username` varchar(50) NOT NULL,`role` varchar(50) NOT NULL,UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE);DROP TABLE IF EXISTS permissions;CREATE TABLE `permissions` (`role` varchar(50) NOT NULL,`resource` varchar(512) NOT NULL,`action` varchar(8) NOT NULL,UNIQUE INDEX `uk_role_permission` (`role`,`resource`,`action`) USING BTREE);INSERT INTO users (username, password, enabled) VALUES ('nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', TRUE);INSERT INTO roles (username, role) VALUES ('nacos', 'ROLE_ADMIN');
2、mysql-configmap.yaml
# 主从配置,此处只是简单的演示,还可以添加其他的参数配置
apiVersion: v1
kind: ConfigMap
metadata:name: mysqlnamespace: mysqllabels:app: mysql
data:master.cnf: |[client]default-character-set=utf8# Master[mysqld]character-set-server=utf8log-bin=master-binskip-name-resolveslave.cnf: |[client]default-character-set=utf8# Slave[mysqld]character-set-server=utf8super-read-onlyskip-name-resolvelog-bin=slave-bin#此处是忽略不需要同步的数据库replicate-ignore-db=mysql
3、mysql-secret.yaml
apiVersion: v1
kind: Secret
metadata:name: mysql-secretnamespace: mysqllabels:app: mysql
type: Opaque
data:password: MTIzNDU2 #此密码为123456 经过base64转码后的值
4、mysql-service.yaml
apiVersion: v1
kind: Service
metadata:name: mysql-writenamespace: mysqllabels:app: mysql
spec:type: NodePortports:- name: mysqlport: 3306nodePort: 30002 #对外暴露的端口selector:app: mysqlstatefulset.kubernetes.io/pod-name: mysql-0
---
apiVersion: v1
kind: Service
metadata:name: mysqlnamespace: mysqllabels:app: mysql
spec:ports:- name: mysqlport: 3306selector:app: mysql
---
apiVersion: v1
kind: Service
metadata:name: mysql-readnamespace: mysqllabels:app: mysql
spec:type: NodePortports:- name: mysqlport: 3306nodePort: 30003selector:app: mysql
5、mysql-statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:name: mysqlnamespace: mysqllabels:app: mysql
spec:selector:matchLabels:app: mysqlserviceName: mysqlreplicas: 2 # 副本数量,集群中的数量template:metadata:labels:app: mysqlspec:initContainers:- name: init-mysqlimage: mysql:5.7.33env:- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:name: mysql-secretkey: passwordcommand:- bash- "-c"- |set -ex# 从 Pod 的序号,生成 server-id[[ $(hostname) =~ -([0-9]+)$ ]] || exit 1ordinal=${BASH_REMATCH[1]}echo [mysqld] > /mnt/conf.d/server-id.cnf# 由于 server-id 不能为 0,因此给 ID 加 100 来避开它echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf# 如果 Pod 的序号为 0,说明它是 Master 节点,从 ConfigMap 里把 Master 的配置文件拷贝到 /mnt/conf.d 目录下# 否则,拷贝 ConfigMap 里的 Slave 的配置文件if [[ ${ordinal} -eq 0 ]]; thencp /mnt/config-map/master.cnf /mnt/conf.delsecp /mnt/config-map/slave.cnf /mnt/conf.dfivolumeMounts:- name: timezonemountPath: /etc/localtime- name: confmountPath: /mnt/conf.d- name: config-mapmountPath: /mnt/config-map- name: clone-mysql#image: gcr.io/google-samples/xtrabackup:1.0image: registry.cn-shenzhen.aliyuncs.com/jbjb/csi:xtrabackup-1.0env:- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:name: mysql-secretkey: passwordcommand:- bash- "-c"- |set -ex# 拷贝操作只需要在第一次启动时进行,所以数据已经存在则跳过[[ -d /var/lib/mysql/mysql ]] && exit 0# Master 节点(序号为 0)不需要这个操作[[ $(hostname) =~ -([0-9]+)$ ]] || exit 1ordinal=${BASH_REMATCH[1]}[[ $ordinal == 0 ]] && exit 0# 使用 ncat 指令,远程地从前一个节点拷贝数据到本地ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql# 执行 --prepare,这样拷贝来的数据就可以用作恢复了xtrabackup --prepare --target-dir=/var/lib/mysqlvolumeMounts:- name: datamountPath: /var/lib/mysqlsubPath: mysql- name: confmountPath: /etc/mysql/conf.dcontainers:- name: mysqlimage: mysql:5.7.33env:
# - name: MYSQL_ALLOW_EMPTY_PASSWORD
# value: "1"- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:name: mysql-secretkey: passwordports:- name: mysqlcontainerPort: 3306volumeMounts:- name: timezonemountPath: /etc/localtime- name: datamountPath: /var/lib/mysqlsubPath: mysql- name: confmountPath: /etc/mysql/conf.d- name: init-sqlmountPath: /mnt/initresources:requests:cpu: 500mlifecycle:postStart:exec:command:- bash- "-c"- |set -ex# 只有文件存在才进行初始化操作if [[ ! -f "/var/lib/mysql/nacos_inited" ]]; then[[ $(hostname) =~ -([0-9]+)$ ]] || exit 1ordinal=${BASH_REMATCH[1]}# 如果是 0 代表 master 节点,准备初始化 nacosif [[ ${ordinal} -eq 0 ]]; thenecho "Waiting for mysqld to be ready(accepting connections)"until mysql -h 127.0.0.1 -uroot -p${MYSQL_ROOT_PASSWORD} -e "SELECT 1"; do sleep 1; doneecho "Initializing replication from clone position"# nacos 初始化mysqladmin -uroot -p${MYSQL_ROOT_PASSWORD} create nacos_configmysql -uroot -p${MYSQL_ROOT_PASSWORD} nacos_config < /mnt/init/nacos-mysql.sql# 初始化完成后标记为初始化touch /var/lib/mysql/nacos_initedfifilivenessProbe:exec:command: ["mysqladmin", "ping", "-uroot", "-p${MYSQL_ROOT_PASSWORD}"]initialDelaySeconds: 30periodSeconds: 10timeoutSeconds: 5readinessProbe:exec:command: ["mysqladmin", "ping", "-uroot", "-p${MYSQL_ROOT_PASSWORD}"]initialDelaySeconds: 5periodSeconds: 2timeoutSeconds: 1#lifecycle:# postStart:# exec:# command: ["/bin/sh", "-c", "mysql -uroot -p${MYSQL_ROOT_PASSWORD} < /var/lib/mysql/nacos/nacos-mysql.sql"]- name: xtrabackup#image: gcr.io/google-samples/xtrabackup:1.0image: registry.cn-shenzhen.aliyuncs.com/jbjb/csi:xtrabackup-1.0ports:- name: xtrabackupcontainerPort: 3307env:- name: MYSQL_ROOT_PASSWORDvalueFrom:secretKeyRef:name: mysql-secretkey: passwordcommand:- bash- "-c"- |set -excd /var/lib/mysql# 从备份信息文件里读取 MASTER_LOG_FILE 和 MASTER_LOG_POS 这 2 个字段的值,用来拼装集群初始化 SQLif [[ -f xtrabackup_slave_info ]]; then# 如果 xtrabackup_slave_info 文件存在,说明这个备份数据来自于另一个 Slave 节点# 这种情况下,XtraBackup 工具在备份的时候,就已经在这个文件里自动生成了 "CHANGE MASTER TO" SQL 语句# 所以,只需要把这个文件重命名为 change_master_to.sql.in,后面直接使用即可mv xtrabackup_slave_info change_master_to.sql.in# 所以,也就用不着 xtrabackup_binlog_info 了rm -f xtrabackup_binlog_infoelif [[ -f xtrabackup_binlog_info ]]; then# 如果只是存在 xtrabackup_binlog_info 文件,说明备份来自于 Master 节点,就需要解析这个备份信息文件,读取所需的两个字段的值[[ $(cat xtrabackup_binlog_info) =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1rm xtrabackup_binlog_info# 把两个字段的值拼装成 SQL,写入 change_master_to.sql.in 文件echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.infi# 如果存在 change_master_to.sql.in,就意味着需要做集群初始化工作if [[ -f change_master_to.sql.in ]]; then# 但一定要先等 MySQL 容器启动之后才能进行下一步连接 MySQL 的操作echo "Waiting for mysqld to be ready(accepting connections)"until mysql -h 127.0.0.1 -uroot -p${MYSQL_ROOT_PASSWORD} -e "SELECT 1"; do sleep 1; doneecho "Initializing replication from clone position"# 将文件 change_master_to.sql.in 改个名字# 防止这个 Container 重启的时候,因为又找到了 change_master_to.sql.in,从而重复执行一遍初始化流程mv change_master_to.sql.in change_master_to.sql.orig# 使用 change_master_to.sql.orig 的内容,也就是前面拼装的 SQL,组成一个完整的初始化和启动 Slave 的 SQL 语句mysql -h 127.0.0.1 -uroot -p${MYSQL_ROOT_PASSWORD} << EOF$(< change_master_to.sql.orig),MASTER_HOST='mysql-write.mysql',MASTER_USER='root',MASTER_PASSWORD='${MYSQL_ROOT_PASSWORD}',MASTER_CONNECT_RETRY=10;START SLAVE;EOFfi# 使用 ncat 监听 3307 端口。# 它的作用是,在收到传输请求的时候,直接执行 xtrabackup --backup 命令,备份 MySQL 的数据并发送给请求者exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \"xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root --password=${MYSQL_ROOT_PASSWORD}"volumeMounts:- name: datamountPath: /var/lib/mysqlsubPath: mysql- name: confmountPath: /etc/mysql/conf.d- name: timezonemountPath: /etc/localtimevolumes:- name: timezonehostPath:path: /usr/share/zoneinfo/Asia/Shanghai- name: confemptyDir: {}- name: config-mapconfigMap:name: mysql- name: init-sqlconfigMap:name: init-sqlvolumeClaimTemplates:- metadata:name: data#annotations:#volume.beta.kubernetes.io/storage-class: nfs-csispec:accessModes:- "ReadWriteOnce"storageClassName: manager-nfs-storage #配置的storageclassresources:requests:storage: 2Gi
6、创建资源
1、创建资源 ,mysql为上面文件所在的目录kubectl create -f mysql2、查看资源
kubectl get po,svc -n mysql
六、搭建Nacos集群
1、nacos-quick-start.yaml
---
apiVersion: v1
kind: Service
metadata:name: nacos-headlessnamespace: nacoslabels:app: nacos-headless
spec:type: NodePortports:- port: 8848name: servertargetPort: 8848- port: 9848name: client-rpctargetPort: 9848- port: 9849name: raft-rpctargetPort: 9849## 兼容1.4.x版本的选举端口- port: 7848name: old-raft-rpctargetPort: 7848selector:app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:name: nacos-cmnamespace: nacos
data:mysql.host: "mysql-write.mysql" #这里写servicename.namespacemysql.db.name: "nacos_config"mysql.port: "3306"mysql.user: "root"mysql.password: "123456"mysql.db.param: "characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&serverTimezone=UTC"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:name: nacosnamespace: nacos
spec:serviceName: nacos-headlessreplicas: 3template:metadata:labels:app: nacosannotations:pod.alpha.kubernetes.io/initialized: "true"spec:affinity:podAntiAffinity:requiredDuringSchedulingIgnoredDuringExecution:- labelSelector:matchExpressions:- key: "app"operator: Invalues:- nacostopologyKey: "kubernetes.io/hostname"containers:- name: nacosimagePullPolicy: Alwaysimage: nacos/nacos-server:v2.4.3 #最新稳定版本resources:requests:memory: "2Gi"cpu: "500m"ports:- containerPort: 8848name: client- containerPort: 9848name: client-rpc- containerPort: 9849name: raft-rpc- containerPort: 7848name: old-raft-rpcenv:- name: NACOS_REPLICASvalue: "3"- name: MYSQL_SERVICE_HOSTvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.host- name: MYSQL_SERVICE_DB_NAMEvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.db.name- name: MYSQL_SERVICE_PORTvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.port- name: MYSQL_SERVICE_USERvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.user- name: MYSQL_SERVICE_PASSWORDvalueFrom:configMapKeyRef:name: nacos-cmkey: mysql.password- name: SPRING_DATASOURCE_PLATFORMvalue: "mysql"- name: NACOS_SERVER_PORTvalue: "8848"- name: NACOS_APPLICATION_PORTvalue: "8848"- name: PREFER_HOST_MODEvalue: "hostname"- name: NACOS_AUTH_ENABLE #开启鉴权value: "true"- name: NACOS_AUTH_TOKENvalue: SecretKey012345678901234567890123456789012345678901234567890123456789- name: NACOS_AUTH_IDENTITY_KEYvalue: wssnail-key- name: NACOS_AUTH_IDENTITY_VALUEvalue: wssnail-value- name: NACOS_SERVERSvalue: "nacos-0.nacos-headless.nacos.svc.cluster.local:8848 nacos-1.nacos-headless.nacos.svc.cluster.local:8848 nacos-2.nacos-headless.nacos.svc.cluster.local:8848"tolerations: #添加容忍,可以部署到master节点上,实际生产不要这样- key: "node-role.kubernetes.io/master"operator: "Exists"effect: "NoSchedule"selector:matchLabels:app: nacos
2、创建资源
#创建资源
kubectl create -f nacos-quick-start.yaml#查看
kubectl get po,svc -n nacos
七、搭建Seata
1、seata-server.yaml
apiVersion: v1
kind: Service
metadata:name: seata-headlessnamespace: seatalabels:k8s-app: seata-server
spec:type: NodePortports:- port: 8091protocol: TCPname: port-8091- port: 7091protocol: TCPname: port-7091selector:k8s-app: seata-server---apiVersion: apps/v1
kind: Deployment
metadata:name: seata-servernamespace: seatalabels:k8s-app: seata-server
spec:replicas: 3selector:matchLabels:k8s-app: seata-servertemplate:metadata:labels:k8s-app: seata-serverspec:containers:- name: seata-serverimage: apache/seata-server:2.2.0imagePullPolicy: IfNotPresentenv:- name: SEATA_PORTvalue: "8091"ports:- name: servicecontainerPort: 8091protocol: TCP- name: consolecontainerPort: 7091protocol: TCPvolumeMounts:- name: seata-configmountPath: /seata-server/resources/application.ymlsubPath: application.ymlvolumes:- name: seata-configconfigMap:name: seata-server-config
---
apiVersion: v1
kind: ConfigMap
metadata:name: seata-server-confignamespace: seata
data:application.yml: |server:port: 7091spring:application:name: seata-serverlogging:config: classpath:logback-spring.xmlfile:path: ${log.home:${user.home}/logs/seata}extend:logstash-appender:destination: 127.0.0.1:4560kafka-appender:bootstrap-servers: 127.0.0.1:9092topic: logback_to_logstashconsole:user:username: seatapassword: seataseata:config:# support: nacos, consul, apollo, zk, etcd3type: nacosnacos:server-addr: nacos-headless.nacos:8848group: SEATA_GROUPusername: nacospassword: nacosdata-id: seataServer.propertiesregistry:# support: nacos, eureka, redis, zk, consul, etcd3, sofatype: nacosnacos:application: seata-serverserver-addr: nacos-headless.nacos:8848group: SEATA_GROUPcluster: defaultusername: nacospassword: nacosstore:# support: file 、 db 、 redismode: dbdb:datasource: druiddb-type: mysqldriver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://mysql-write.mysql:3306/seatauser: rootpassword: 123456min-conn: 10max-conn: 100global-table: global_tablebranch-table: branch_tablelock-table: lock_tabledistributed-lock-table: distributed_lockvgroup-table: vgroup_tablequery-limit: 1000max-wait: 5000security:secretKey: SeataSecretKey0c382ef121d778043159209298fd40bf3850a017tokenValidityInMilliseconds: 1800000csrf-ignore-urls: /metadata/v1/**ignore:urls: /,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.jpeg,/**/*.ico,/api/v1/auth/login,/version.json,/health,/error,/vgroup/v1/**
八、附录
1、Redis
链接: https://pan.baidu.com/s/1nixWxMDlDhrPxolrCG2PkA?pwd=c7pn 提取码: c7pn
2、RocketMQ
链接: https://pan.baidu.com/s/1RQV7y8hly6EPuhKEDyZ5dw?pwd=f9bc 提取码: f9bc
3、参考 :
https://nacos.io/docs/latest/quickstart/quick-start/
https://seata.apache.org/zh-cn/docs/user/quickstart/