mysql Operator 学习

姬裕
2023-12-01

先来四个网站供学习:

1. Oracle MySQL
 https://github.com/mysql/mysql-operator
 
2. Percona MySQL
 https://github.com/percona/percona-xtradb-cluster-operator
 
3. presslabs/mysql-operator
https://github.com/bitpoke/mysql-operator
 
4. Operator社区网站:https://operatorhub.io/?category=Database

一、部署Operator
 https://github.com/mysql/mysql-operator
 可以直接参考官方文档,就是以下步骤

kubectl apply -f https://raw.githubusercontent.com/mysql/mysql-operator/trunk/deploy/deploy-crds.yaml
kubectl apply -f  https://raw.githubusercontent.com/mysql/mysql-operator/trunk/deploy/deploy-operator.yaml

# 查看
kubectl get pods -n mysql-operator
 
二、部署InnoDB Cluster
1、secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: mypwds
stringData:
  rootUser: root
  rootHost: '%'
  rootPassword: "dsfg"

2、sample-cluster.yaml

apiVersion: mysql.oracle.com/v2alpha1
kind: InnoDBCluster
metadata:
  name: mycluster
spec:
  secretName: mypwds
  instances: 3
  router:
    instances: 2

3、手动创建PV(创建三个PV,metadata.name不同就行)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume-0
  labels:
    type: local
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/mycluster"

 # 创建

 kubectl apply -f  secret.yaml
 kubectl apply -f  sample-cluster.yaml

 kubectl apply -f  pv01.yaml
 kubectl apply -f  pv02.yaml
 kubectl apply -f  pv03.yaml

# 注意: Operator会自动创建PVC,官方示例中没有提示要先准备好创建PV,但是这步是必须的,一般这里是最容易出错的,最好查看下PVC状态,看看有没有绑定
# kubectl get pvc
NAME                  STATUS   VOLUME              CAPACITY   ACCESS MODES   STORAGECLASS   AGE
datadir-mycluster-0   Bound    mysql-pv-volume-0   2Gi        RWO                           2h
datadir-mycluster-1   Bound    mysql-pv-volume-1   2Gi        RWO                           2h
datadir-mycluster-2   Bound    mysql-pv-volume-2   2Gi        RWO                           2h
三、访问测试

# 查看service ip
kubectl get service mycluster

mysql -h10.106.187.34 -P6446 -uroot -p
 

登录后复制 
 # 查看MGR集群成员状态

mysql> SELECT MEMBER_HOST,MEMBER_STATE,MEMBER_ROLE,MEMBER_VERSION FROM performance_schema.replication_group_members;
+-----------------------------------------------------------+--------------+-------------+----------------+
| MEMBER_HOST | MEMBER_STATE | MEMBER_ROLE | MEMBER_VERSION |
+-----------------------------------------------------------+--------------+-------------+----------------+
| mycluster-1.mycluster-instances.default.svc.cluster.local | ONLINE | SECONDARY | 8.0.25 |
| mycluster-2.mycluster-instances.default.svc.cluster.local | ONLINE | SECONDARY | 8.0.25 |
| mycluster-0.mycluster-instances.default.svc.cluster.local | ONLINE | PRIMARY | 8.0.25 |
+-----------------------------------------------------------+--------------+-------------+----------------+
3 rows in set (0.00 sec)
-----------------------------------

--------------------------------------------------------------------------------------------------------------

PXC Operator由Percona基于SDK创建,文档详细,有很好的维护,是MySQL Operator很好的参考案例,这里只做简单的部署实验。详细信息查看官方文档:

  https://github.com/percona/percona-xtradb-cluster-operator
  https://www.percona.com/doc/kubernetes-operator-for-pxc/kubernetes.html
 
 
1、部署Opeartor
# 下载代码
git clone -b v1.9.0 https://github.com/percona/percona-xtradb-cluster-operator
cd percona-xtradb-cluster-operator

kubectl apply -f deploy/crd.yaml
kubectl create namespace pxc
kubectl config set-context $(kubectl config current-context) --namespace=pxc
kubectl apply -f deploy/rbac.yaml
kubectl apply -f deploy/operator.yaml


2、部署PXC

kubectl create -f deploy/secrets.yaml
cp deploy/cr.yaml deploy/pxc.yaml
编辑 deploy/pxc.yaml

    volume:
          persistentVolumeClaim:
            storageClassName: standard   # 写上你自己的SC名字
            accessModes: [ "ReadWriteOnce" ]
            resources:
              requests:
                storage: 6G      

 

# 如果没有SC可以自己创建三个PV做测试用,metadata.name不同就行(上面修改过SC就可以省略了)

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pxc-pv-volume-0
  labels:
    type: local
spec:
  capacity:
    storage: 6Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/data/pxc"

登录后复制 
mkdir /data/pxc
chown 99:1001 /data/pxc -R
chmod 775 /data/pxc -R

注意:按理说,这里不用创建目录并且赋予权限,可是如果不提前加的话,pxc-init初始化容器
会报install: cannot create regular file '/var/lib/mysql/pxc-entrypoint.sh': Permission denied

kubectl apply -f deploy/pxc.yaml
 


 

 类似资料: