概述
pxc為mysql的一種集群模型荆针,我們結(jié)合operator和k8s 完成pxc的部署和擴(kuò)容
硬盤使用local卷,如何管理local卷請翻閱 我的另一篇文章
http://www.reibang.com/p/bfa204cef8c0
英文文檔詳情 https://percona.github.io/percona-xtradb-cluster-operator/configure/backups
下載git
git clone-brelease-0.3.0 https://github.com/percona/percona-xtradb-cluster-operator
cd percona-xtradb-cluster-operator
安裝自定義資源CRD
kubectl apply-fdeploy/crd.yaml
創(chuàng)建pxc namespace
kubectl create namespace pxc
kubectl config set-context $(kubectl config current-context) -n pxc
創(chuàng)建k8s用戶和權(quán)限
kubectl apply-fdeploy/rbac.yaml
部署operator
kubectl apply-fdeploy/operator.yaml
配置初始密碼
生成加密字符串
echo -n 'plain-text-password' | base64
vi deploy/secrets.yaml
apiVersion: v1
kind: Secret
metadata:
? name: my-cluster-secrets
type: Opaque
data:
? root: 加密字符
? xtrabackup: 加密字符
? monitor: 加密字符
? clustercheck: 加密字符
? proxyuser: 加密字符
? proxyadmin: 加密字符
? pmmserver: 加密字符
部署
kubectl apply-fdeploy/secrets.yaml
部署集群
自定義pxc參數(shù)
vi deploy/cr.yaml
apiVersion: "pxc.percona.com/v1alpha1"
kind: "PerconaXtraDBCluster"
metadata:
? #集群名稱
? name: "cluster1"
? finalizers:
? ? - delete-pxc-pods-in-order
? ? # - delete-proxysql-pvc
? ? # - delete-pxc-pvc
spec:
? secretsName: my-cluster-secrets
? pxc:
??? #集群節(jié)點(diǎn)數(shù)量
? ? size: 3
? ? image: perconalab/pxc-openshift:0.2.0
??? #資源信息
? ? resources:
? ? ? requests:
? ? ? ? memory: 1G
? ? ? ? cpu: 600m
? ? ? limits:
? ? ? ? memory: 1G
? ? ? ? cpu: "1"
? ? volumeSpec:
????? #存儲卷信息
? ? ? storageClass: local-storage
? ? ? accessModes: [ "ReadWriteOnce" ]
????? #大小
? ? ? size: 6Gi
? ? affinity:
? ? ? topologyKey: "kubernetes.io/hostname"
? ? ? # advanced:
? ? # nodeSelector:
? ? #? disktype: ssd
? ? # tolerations:
? ? # - key: "node.alpha.kubernetes.io/unreachable"
? ? #? operator: "Exists"
? ? #? effect: "NoExecute"
? ? #? tolerationSeconds: 6000
? ? # priorityClassName: high-priority
? ? # annotations:
? ? #? iam.amazonaws.com/role: role-arn
? ? # imagePullSecrets:
? ? #? - name: private-registry-credentials
? ? # labels:
? ? #? rack: rack-22
? proxysql:
??? #是否部署proxy來完成讀寫分離
? ? enabled: true
??? #proxysql幾點(diǎn)數(shù)量
? ? size: 1
? ? image: perconalab/proxysql-openshift:0.2.0
? ? resources:
? ? ? requests:
? ? ? ? memory: 1G
? ? ? ? cpu: 600m
? ? ? # limits:
? ? ? #? memory: 1G
? ? ? #? cpu: 700m
? ? volumeSpec:
? ? ? storageClass: ssd-local-storage
? ? ? accessModes: [ "ReadWriteOnce" ]
? ? ? size: 2Gi
? ? # affinity:
? ? #? topologyKey: "failure-domain.beta.kubernetes.io/zone"
? ? #? # advanced:
? ? # nodeSelector:
? ? #? disktype: ssd
? ? # tolerations:
? ? # - key: "node.alpha.kubernetes.io/unreachable"
? ? #? operator: "Exists"
? ? #? effect: "NoExecute"
? ? #? tolerationSeconds: 6000
? pmm:
??? #是否開啟pmm
? ? enabled: false
? ? image: perconalab/pmm-client:1.17.0
? ? serverHost: monitoring-service
? ? serverUser: pmm
? backup:
? ? image: perconalab/backupjob-openshift:0.2.0
? ? # imagePullSecrets:
? ? #? - name: private-registry-credentials
? ? schedule:
? ? ? - name: "sat-night-backup"
??????? #crontab計(jì)劃
? ? ? ? schedule: "0 0 * * 6"
??????? #保存幾份備份
? ? ? ? keep: 3
? ? ? ? volume:
? ? ? ? ? storageClass: local-storage
? ? ? ? ? size: 6Gi
kubectl apply -f deploy/cr.yaml
查看pxc集群
kubectl -n pxc get po --show-labels
查看service
kubectl -n pxc get svc
將nodePORT
kubectl -n pxc edit svc cluster1-pxc-proxysql
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
? creationTimestamp: "2019-03-25T10:58:37Z"
? labels:
? ? app: pxc
? ? cluster: cluster1
? name: cluster1-pxc-proxysql
? namespace: pxc
? ownerReferences:
? - apiVersion: pxc.percona.com/v1alpha1
? ? controller: true
? ? kind: PerconaXtraDBCluster
? ? name: cluster1
? ? uid: f0a9a0ba-4eec-11e9-ba71-005056ac2dbb
? resourceVersion: "2771712"
? selfLink: /api/v1/namespaces/pxc/services/cluster1-pxc-proxysql
? uid: f0c3c844-4eec-11e9-9e18-005056ac20e9
spec:
? clusterIP: 10.99.239.180
? externalTrafficPolicy: Cluster
? ports:
? - name: mysql
? ? nodePort: 31239
? ? port: 3306
? ? protocol: TCP
? ? targetPort: 3306
? - name: proxyadm
? ? nodePort: 31531
? ? port: 6032
? ? protocol: TCP
? ? targetPort: 6032
? selector:
? ? component: cluster1-pxc-proxysql
? sessionAffinity: None
? type: NodePort
status:
? loadBalancer: {}
再次查看
連接mysql
mysql -h 10.16.16.119 -P 31239 -u root -p
連接proxyadmin
mysql -h 10.16.16.119 -P 31531 -u proxyadmin -p
擴(kuò)容
將3節(jié)點(diǎn)擴(kuò)容到5節(jié)點(diǎn)
需要注意必須為奇數(shù)節(jié)點(diǎn)础芍,
而且po分布在不同的k8snode上 所以k8s node必須大于pxc節(jié)點(diǎn),不然po會一直處于pend狀態(tài)
kubectl -n pxc get pxc/cluster1 -o yaml | sed -e 's/size: 3/size: 5/' | kubectl -n pxc apply -f -
查看讀寫分離,新節(jié)點(diǎn)已經(jīng)加入
縮減節(jié)點(diǎn)
kubectl -n pxc get pxc/cluster1 -o yaml | sed -e 's/size: 4/size: 3/' | kubectl -n pxc apply -f -
創(chuàng)建用戶和同步用戶到proxysql
mysql -u root -p -P 31239 -h node1
create database database1;
GRANT ALL PRIVILEGES ON database1.* TO 'user1'@'%' IDENTIFIED BY 'password1';
同步proxy
kubectl -n pxc exec -it cluster1-pxc-proxysql-0 -- proxysql-admin --config-file=/etc/proxysql-admin.cnf --syncusers?
測試
備份和恢復(fù)
crontab 已經(jīng)在創(chuàng)建集群的時候創(chuàng)建完畢
手動backup
cat <<EOF | kubectl -n pxc apply -f-
apiVersion: "pxc.percona.com/v1alpha1"
kind: "PerconaXtraDBBackup"
metadata:
? name: "backup20190326"
spec:
? pxcCluster: "cluster1"
? volume:
? ? storageClass: local-storage
? ? size: 6G
EOF
查看備份
kubectl get pxc-backup
查看pxc集群
kubectl get pxc
從備份恢復(fù)數(shù)據(jù)
查看備份
kubectl get pxc-backup
查看pxc集群
kubectl get pxc
將指定備份恢復(fù)到指定集群
./deploy/backup/restore-backup.sh <備份名稱> <集群名稱>
將備份copy到本地
./deploy/backup/copy-backup.sh <備份名稱> path/to/dir
安裝PMM監(jiān)控
helm repo add percona https://percona-charts.storage.googleapis.com
helm repo update
查看pmmserver 參數(shù)
helm inspect percona/pmm-server
安裝
helm install percona/pmm-server --name pcx-monitoring --namespace=pxc --set platform=kubernetes --set persistence.storageClass=local-storage --set credentials.username=pmm
如果下載有問題可以代理下載
https://percona-charts.storage.googleapis.com/pmm-server-1.17.1.tgz
然后放到本機(jī)
tar -xvf ~/pmm-server-1.17.1.tgz
更改:
vi ./pmm-server/templates/statefulset.yaml?
115行加入
storageClassName: {{ .Values.persistence.storageClass }}
類似下圖
helm install ./pmm-server --name pcx-monitoring --namespace=pxc --set platform=kubernetes --set persistence.storageClass=local-storage --set credentials.username=pmm --set "credentials.password=密碼"
這里的密碼必須和my-cluster-secrets中的pmmserver一致
如果忘記kubectl -n pxc get secret 查看
kubectl -n pxc edit secret 密碼文件 修改
查看
開啟客戶端pmm
kubectl -n pxc get pxc
kubectl -n pxc get svc 獲取pmmserver名稱
kubectl -n pxc edit pxc cluster1
騎寵serverHost為kubectl -n pxc get svc 獲取pmmserver的名稱
serverUser為賬號