Kubernetes部署系列:高可用MySQL集群

Percona XtraDB Cluster(PXC)是一个开源的MySQL高可用性(HA)解决方案,它建立在Percona Server for MySQL之上,是一个基于Galera Cluster的分布式数据库集群。

1、创建MySQL的Secret对象 mysql-secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: mysql-secret
type: Opaque
data:
  MYSQL_ROOT_PASSWORD: 

替换为经过base64编码的MySQL root密码。 

echo -n 'your-password' | base64

2、创建MySQL的StatefulSet mysql-statefulset.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  serviceName: "mysql"
  replicas: 3
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: percona/percona-xtradb-cluster:latest
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secret
                  key: MYSQL_ROOT_PASSWORD
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
  volumeClaimTemplates:
    - metadata:
        name: mysql-persistent-storage
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 1Gi

3、创建MySQL的Service  mysql-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  clusterIP: None
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 3306

4、创建PXC集群的Service pxc-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: pxc-cluster
spec:
  selector:
    app: mysql
  ports:
    - protocol: TCP
      port: 4567
    - protocol: TCP
      port: 4568
    - protocol: TCP
      port: 4444
  type: ClusterIP

5、部署PXC集群

kubectl apply -f mysql-secret.yaml
kubectl apply -f mysql-statefulset.yaml
kubectl apply -f mysql-service.yaml
kubectl apply -f pxc-service.yaml

6、验证部署

kubectl get pods
kubectl exec -it mysql-0 -- mysql -u root -p
SHOW STATUS LIKE 'wsrep_cluster_size';

你可能感兴趣的:(Kubernetes系列,kubernetes,mysql,容器)