PXC introduction
Full name percona-xtradB-cluster, provides a high availability of MySQL implementation method. PXC clusters are made up of nodes (at least 3 are recommended for easy recovery), and each node is based on regular MySQL Server, which means you can use a separate node from the cluster, and each node in the cluster contains complete data.
PXC features
- Synchronous replication
- Supports multi-master replication
- Parallel replication
- As a high availability solution, implementation is relatively simple
The architecture diagram is shown below
Deploy the PXC cluster to Kubernetes
1. Download the deployment file
git clone- b v1.5.0 https://github.com/percona/percona-xtradb-cluster-operatorCopy the code
2. Create a CRD
[root@k8s-master001 deploy]# kubectl apply -f crd.yaml
Warning: apiextensions.k8s.io/v1beta1 CustomResourceDefinition is deprecated inV1.16 +, unavailableinV1.22 +; use apiextensions.k8s.io/v1 CustomResourceDefinition customresourcedefinition.apiextensions.k8s.io/perconaxtradbclusters.pxc.percona.com created customresourcedefinition.apiextensions.k8s.io/perconaxtradbclusterbackups.pxc.percona.com created customresourcedefinition.apiextensions.k8s.io/perconaxtradbclusterrestores.pxc.percona.com created customresourcedefinition.apiextensions.k8s.io/perconaxtradbbackups.pxc.percona.com createdCopy the code
3. Create a namespace
[root@k8s-master001 deploy]# kubectl create namespace pxc
namespace/pxc created
[root@k8s-master001 deploy]# kubectl config set-context $(kubectl config current-context) --namespace=pxc
Context "kubernetes-admin@kubernetes" modified.
Copy the code
4. Create RBAC
[root@k8s-master001 deploy]# kubectl apply -f rbac.yaml
Warning: rbac.authorization.k8s.io/v1beta1 Role is deprecated inV1.17 +, unavailableinV1.22 +; use rbac.authorization.k8s.io/v1 Role role.rbac.authorization.k8s.io/percona-xtradb-cluster-operator created serviceaccount/percona-xtradb-cluster-operator created serviceaccount/percona-xtradb-cluster-operator-workload created Warning: rbac.authorization.k8s.io/v1beta1 RoleBinding is deprecatedinV1.17 +, unavailableinV1.22 +; use rbac.authorization.k8s.io/v1 RoleBinding rolebinding.rbac.authorization.k8s.io/service-account-percona-xtradb-cluster-operator createdCopy the code
5. Create operator
[root@k8s-master001 deploy]# kubectl apply -f operator.yaml
deployment.apps/percona-xtradb-cluster-operator created
[root@k8s-master001 deploy]# kubectl get po -n pxc
NAME READY STATUS RESTARTS AGE
percona-xtradb-cluster-operator-54d9b7c858-hkmsp 1/1 Running 0 2m19s
Copy the code
6. Create secrets
Yaml create the root password and change the root field in secrets.yaml to the new generated password [root@k8s-master001 deploy]# echo -n 'pxcadmin' | base64
cHhjYWRtaW4=
[root@k8s-master001 deploy]# kubectl apply -f secrets.yaml
secret/my-cluster-secrets created
Copy the code
7. Create a Percona XtraDB Cluster to modify the configuration
updateStrategy: OnDelete
Specify the storage class and storage size
Rook-ceph is a storage class for K8S, available via Kubectl get Sc to obtain
persistentVolumeClaim:
storageClassName: rook-ceph
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 6Gi
Copy the code
Perform the deployment
[root@k8s-master001 deploy]# kubectl apply -f cr.yaml
perconaxtradbcluster.pxc.percona.com/cluster1 created
[root@k8s-master001 deploy]# kubectl get po -n pxc
NAME READY STATUS RESTARTS AGE
cluster1-haproxy-0 2/2 Running 0 17m
cluster1-haproxy-1 2/2 Running 0 15m
cluster1-haproxy-2 2/2 Running 0 13m
cluster1-pxc-0 1/1 Running 0 17m
cluster1-pxc-1 1/1 Running 0 6m28s
cluster1-pxc-2 1/1 Running 0 3m33s
Copy the code
8. Verification:
Kubectl run -i --rm --tty percona-client --image=percona:5.7 --restart=Never -- bash-il Official use here is haproxy sh - 4.2 $env | grep CLUSTER1_HAPROXY_SERVICE_PORT = 3306 3306 CLUSTER1_HAPROXY_REPLICAS_PORT_3306_TCP_PORT=3306 CLUSTER1_HAPROXY_PORT= TCP :// 10.106.76.118:3306 Connect to the database, Wsrep_cluster_size =3 indicates that the cluster has three nodes. For more information, run the following command: sh-4.2$mysql-h10.106.76.114 -uroot -ppxcadmin Type'help; ' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show status like 'wsrep%'; +----------------------------------+------------------------------------------------------------------------------------ ----------------------------------------------------------------------------------+ | Variable_name | Value | +----------------------------------+------------------------------------------------------------------------------------ ----------------------------------------------------------------------------------+ | wsrep_local_state_uuid | 703296d1-f72b-11ea-93ec-5267957b341b | | wsrep_cluster_capabilities | | | wsrep_cluster_conf_id | 5 | | wsrep_cluster_size | 3 | | wsrep_cluster_state_uuid | 703296d1-f72b-11ea-93ec-5267957b341b | | wsrep_cluster_status | Primary | | wsrep_connected | ON | | wsrep_local_bf_aborts | 0 | | wsrep_local_index | 2 | | wsrep_provider_capabilities | :MULTI_MASTER:CERTIFICATION:PARALLEL_APPLYING:TRX_REPLAY:ISOLATION:PAUSE:CAUSAL_READS:INCREMENTAL_WRITESET:UNORDERED:PRE ORDERED:STREAMING:NBO: | | wsrep_provider_name | Galera | | wsrep_provider_vendor | Codership Oy <[email protected]> | | Wsrep_provider_version 4.3 (r752664d) | | | wsrep_ready | ON | | wsrep_thread_count | 3 | +----------------------------------+------------------------------------------------------------------------------------ ----------------------------------------------------------------------------------+ 75 rowsin set (0.01 sec)
Copy the code
9, description,
kubectl get po -n pxc NAME READY STATUS RESTARTS AGE cluster1-haproxy-0 2/2 Running 0 39m cluster1-haproxy-1 2/2 Running 0 36m cluster1-haproxy-2 2/2 Running 0 35m cluster1-pxc-0 1/1 Running 1 39m cluster1-pxc-1 1/1 Running 0 28m cluster1-pxc-2 1/1 Running 0 25m percona-client 1/1 Running 0 16m percona-xtradb-cluster-operator-54d9b7c858-hkmsp 1/1 Running 0 65m NAME TYPE cluster-ip external-ip PORT(S) AGE service/cluster1-haproxy ClusterIP 10.106.76.114 <none> 3306/TCP,3309/TCP 43M service/cluster1-haproxy-replicas ClusterIP 10.104.239.57 <none> 3306/TCP 43M service/cluster1-pxc ClusterIP None <none> 3306/TCP 43m service/cluster1-pxc-unready ClusterIP None <none> 3306/TCP 43mCopy the code
- As you can see from the output above, there are three PXC nodes running, which can be considered as three mysql nodes on the physical machine, in addition to three HaProxies. Haproxy is used to provide proxy services for the PXC cluster. If you go to Cluster1-haproxy-0, you can view the following information
server cluster1-pxc-0 cluster1-pxc-0.cluster1-pxc.pxc.svc.cluster.local:3306 check inter 10000 rise 1 fall 2 weight 1
server cluster1-pxc-1 cluster1-pxc-1.cluster1-pxc.pxc.svc.cluster.local:3306 check inter 10000 rise 1 fall 2 weight 1
server cluster1-pxc-2 cluster1-pxc-2.cluster1-pxc.pxc.svc.cluster.local:3306 check inter 10000 rise 1 fall 2 weight 1
Copy the code
-
Look at the service, to create the cluster1 – haproxy, if within kubernetes CLUSTER, can be directly through the CLUSTER – IP + PORT access to the database
-
If you want to access the database outside the cluster, you can expose the TCP service using the Ingress described above, or if you test, you can create a NodePort to expose the service. In the next article, we will use the Ingress to expose mysql services, and also introduce nginx-ingress.
Tips: for more good articles, please pay attention to the first wechat public number “Rookie operation and maintenance talk”!!