在Kubernetes集群中部署MySQL主从(2)

# cat mysql-master-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
 name: mysql-master
 labels:
  name: mysql-master
spec:
  replicas: 1
  selector:
  name: mysql-master
  template:
  metadata:
    labels:
    name: mysql-master
  spec:
    containers:
    - name: mysql-master
      image: registry.fjhb.cn/mysql-master:0.1
      volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysql-master-data
      ports:
      - containerPort: 3306
      env:
      - name: MYSQL_ROOT_PASSWORD
        value: "12345678"
      - name: MYSQL_REPLICATION_USER
        value: "repl"
      - name: MYSQL_REPLICAITON_PASSWORD
        value: "12345678"
    volumes:
    - name: mysql-master-data
      persistentVolumeClaim:
        claimName: pv-nfs-mysql-master

# cat mysql-master-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql-master
  labels:
  name: mysql-master
spec:
  type: NodePort
  ports:
  - port: 3306
    targetPort: 3306
    name: http
    nodePort: 30066
  selector:
    name: mysql-master

# kubectl create -f mysql-master-rc.yaml
# kubectl create -f mysql-master-svc.yaml

在kubernetes集群中部署mysql主从


在kubernetes集群中部署mysql主从


使用mysql客户端连接master测试

在kubernetes集群中部署mysql主从


六、根据yaml文件创建mysql slave ReplicationController和services

# cat mysql-slave-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
 name: mysql-slave
 labels:
  name: mysql-slave
spec:
  replicas: 1
  selector:
  name: mysql-slave
  template:
  metadata:
    labels:
    name: mysql-slave
  spec:
    containers:
    - name: mysql-slave
      image: registry.fjhb.cn/mysql-slave:0.1
      volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysql-slave-data
      ports:
      - containerPort: 3306
      env:
      - name: MYSQL_ROOT_PASSWORD
        value: "12345678"
      - name: MYSQL_REPLICATION_USER
        value: "repl"
      - name: MYSQL_REPLICAITON_PASSWORD
        value: "12345678"
    volumes:
    - name: mysql-slave-data
      persistentVolumeClaim:
        claimName: pv-nfs-mysql-slave

# cat mysql-slave-svc.yaml
apiVersion: v1
kind: Service
metadata:
  name: mysql-slave
  labels:
  name: mysql-slave
spec:
  type: NodePort
  ports:
  - port: 3306
    targetPort: 3306
    name: http
    nodePort: 30067
  selector:
    name: mysql-slave

# kubectl create -f mysql-slave-rc.yaml
# kubectl create -f mysql-slave-svc.yaml

七、测试与排错
通过mysql命令行连接slave查看复制的状态,发现状态是connecting

在kubernetes集群中部署mysql主从


使用slave 容器连接master,发现使用root账号可以正常连接,而使用repl账号无法正常连接,提示access deny

在kubernetes集群中部署mysql主从


修改master上的repl用户密码

mysql> alter user repl@'%' identified by "12345678";
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/024586b61cc5887de734363c9485e7d4.html