一、helm部署mysql主从复制
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
helm pull bitnami/mysql
解压后编辑values.yaml文件,修改如下(storageclass已设置默认类)
117 ## @param architecture MySQL architecture (`standalone` or `replication`)118 ##119 architecture: replication120 ## MySQL Authentication parameters121 ##122 auth:123 ## @param auth.rootPassword Password for the `root` user. Ignored if existing secret is provided124 ## ref: https://github.com/bitnami/containers/tree/main/bitnami/mysql#setting-the-root-password-on-first-run125 ##126 rootPassword: "abc123456"127 ## @param auth.createDatabase Whether to create the .Values.auth.database or not128 ## ref: https://github.com/bitnami/containers/tree/main/bitnami/mysql#creating-a-database-on-first-run129 ##130 createDatabase: true131 ## @param auth.database Name for a custom database to create132 ## ref: https://github.com/bitnami/containers/tree/main/bitnami/mysql#creating-a-database-on-first-run133 ##134 database: "my_database"135 ## @param auth.username Name for a custom user to create136 ## ref: https://github.com/bitnami/containers/tree/main/bitnami/mysql#creating-a-database-user-on-first-run137 ##138 username: ""139 ## @param auth.password Password for the new user. Ignored if existing secret is provided140 ##141 password: ""142 ## @param auth.replicationUser MySQL replication user143 ## ref: https://github.com/bitnami/containers/tree/main/bitnami/mysql#setting-up-a-replication-cluster144 ##145 replicationUser: replicator146 ## @param auth.replicationPassword MySQL replication user password. Ignored if existing secret is provided147 ##148 replicationPassword: "abc123456"
安装
helm install mysql ./mysql -n mysql --create-namespace
二、添加ingress控制器tcp配置
[root@k8s-master01 mysql]# kubectl get daemonset,pods -n ingress-nginx
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
daemonset.apps/ingress-nginx-controller 1 1 1 1 1 ingress=true,kubernetes.io/os=linux 20hNAME READY STATUS RESTARTS AGE
pod/ingress-nginx-controller-xcn5q 1/1 Running 0 9m46s
这里使用的是daemonset方式部署的,如果是deployment,需要将daemonset修改成deployment
kubectl edit daemonset ingress-nginx-controller -n ingress-nginx
添加tcp配置
- --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
三、编写相应的configmap
查看当前mysql服务
[root@k8s-master01 mysql]# kubectl get pods,svc -n mysql
NAME READY STATUS RESTARTS AGE
pod/mysql-primary-0 1/1 Running 0 73m
pod/mysql-secondary-0 1/1 Running 0 73mNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mysql-primary ClusterIP 10.102.29.24 <none> 3306/TCP 73m
service/mysql-primary-headless ClusterIP None <none> 3306/TCP 73m
service/mysql-secondary ClusterIP 10.106.87.104 <none> 3306/TCP 73m
service/mysql-secondary-headless ClusterIP None <none> 3306/TCP 73m
编写configmap,注意当前namespace为mysql
vim mysql-tcp-cm.yaml
kind: ConfigMap
apiVersion: v1
metadata:name: tcp-servicesnamespace: ingress-nginx
data:"30306": "mysql/mysql-primary:3306""30307": "mysql/mysql-secondary:3306"
应用configmap
kubectl apply -f mysql-tcp-cm.yaml
四、测试连接