在Kubernetes(K8s)環境中,我們可以通過部署MySQL主從架構來實現數據的高可用性。接下來,我們將介紹如何搭建K8s MySQL主從。
1.創建MySQL服務憑證:
$ kubectl create secret generic mysql-pass --from-literal=password=yourpassword
2.創建MySQL服務:
apiVersion: v1 kind: Service metadata: name: mysql labels: app: mysql spec: ports: - port: 3306 selector: app: mysql clusterIP: None
3.創建MySQL主從部署:
apiVersion: apps/v1beta1 kind: Deployment metadata: name: mysql-master labels: app: mysql spec: replicas: 1 template: metadata: labels: app: mysql role: master spec: containers: - name: mysql image: mysql:5.7 env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-pass key: password ports: - containerPort: 3306 volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-pv-claim --- apiVersion: apps/v1beta1 kind: Deployment metadata: name: mysql-slave labels: app: mysql spec: replicas: 1 template: metadata: labels: app: mysql role: slave spec: containers: - name: mysql image: mysql:5.7 env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-pass key: password ports: - containerPort: 3306 volumeMounts: - name: mysql-persistent-storage mountPath: /var/lib/mysql volumes: - name: mysql-persistent-storage persistentVolumeClaim: claimName: mysql-pv-claim
4.創建MySQL主從服務:
apiVersion: v1 kind: Service metadata: name: mysql-slave spec: ports: - port: 3306 selector: app: mysql role: slave
5.配置MySQL主從:
CHANGE MASTER TO MASTER_HOST='mysql-master.mysql', MASTER_USER='root', MASTER_PASSWORD='root', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107; START SLAVE;
6.檢查MySQL主從狀態:
SHOW MASTER STATUS; SHOW SLAVE STATUS\G;
通過以上步驟,我們就成功搭建了K8s MySQL主從架構,實現了數據的高可用性。