容器云平台

kubernetes部署Percona XtraDB Clust

2020-09-16  本文已影响0人  空壳先生

PXC介绍

全称percona-xtradb-cluster,提供了MySQL高可用的一种实现方法。PXC集群以节点组成(推荐至少3节点,便于故障恢复),每个节点都是基于常规的 MySQL Server,意味着你可以从集群中分离出某节点单独使用,集群中每个节点都包含完整的数据。

PXC特性

架构图如下所示

image.png

部署PXC集群到kubernetes

1、下载部署文件

git clone -b v1.5.0 https://github.com/percona/percona-xtradb-cluster-operator

2、创建CRD

[root@k8s-master001 deploy]# kubectl  apply -f crd.yaml
Warning: apiextensions.k8s.io/v1beta1 CustomResourceDefinition is deprecated in v1.16+, unavailable in v1.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 created

3、创建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.

4、创建RBAC

[root@k8s-master001 deploy]# kubectl apply -f rbac.yaml
Warning: rbac.authorization.k8s.io/v1beta1 Role is deprecated in v1.17+, unavailable in v1.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 deprecated in v1.17+, unavailable in v1.22+; use rbac.authorization.k8s.io/v1 RoleBinding
rolebinding.rbac.authorization.k8s.io/service-account-percona-xtradb-cluster-operator created

5、创建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

6、创建secrets

创建root密码,并修改secrets.yaml中的root字段为新生成的密码
[root@k8s-master001 deploy]# echo -n 'pxcadmin' | base64
cHhjYWRtaW4=

[root@k8s-master001 deploy]# kubectl  apply -f secrets.yaml
secret/my-cluster-secrets created

7、创建Percona XtraDB Cluster
修改配置

 updateStrategy: OnDelete
 
指定存储类和存储大小
rook-ceph是k8s的存储类,可以通过kubectl get sc获取
       persistentVolumeClaim:
        storageClassName: rook-ceph
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 6Gi

执行部署

[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

8、验证:

运行一个percona-client
kubectl run -i --rm --tty percona-client --image=percona:5.7 --restart=Never -- bash -il

查看3306代理,这里官方用的是haproxy
sh-4.2$ env|grep 3306  
CLUSTER1_HAPROXY_SERVICE_PORT=3306
CLUSTER1_HAPROXY_REPLICAS_PORT_3306_TCP_PORT=3306
CLUSTER1_HAPROXY_PORT=tcp://10.106.76.114:3306

连接数据库,查看集群信息,wsrep_cluster_size=3,表示现在集群有3个节点 ,更多信息自己查看输出信息
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:PREORDERED:STREAMING:NBO:                       |
| wsrep_provider_name              | Galera                                                                                                                                                               |
| wsrep_provider_vendor            | Codership Oy <info@codership.com>                                                                                                                                    |
| wsrep_provider_version           | 4.3(r752664d)                                                                                                                                                        |
| wsrep_ready                      | ON                                                                                                                                                                   |
| wsrep_thread_count               | 3                                                                                                                                                                    |
+----------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
75 rows in set (0.01 sec)

9、说明

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            43m
  1. 从以上输出可以看到,运行了三个pxc节点,这里可以视为物理机三个mysql节点,除此之外,还有三个haproxy。说明使用haproxy来为pxc集群提供代理服务,进入到cluster1-haproxy-0 可以看到如下信息
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
  1. 再来看service,创建了cluster1-haproxy,如果在kubernetes集群内部,可以直接通过CLUSTER-IP+PORT访问到数据库

  2. 如果想在集群外部访问到数据库,可以使用前文介绍的Ingress来暴露TCP服务,如果测试,也可使创建一个NodePort来暴露服。务下一篇我们就来就使用前文介绍过的Ingress来把mysql的服务暴露出去,顺便介绍Nginx-Ingress。


    image.png

Tips: 更多好文章,请关注首发微信公众号“菜鸟运维杂谈”!!!

上一篇下一篇

猜你喜欢

热点阅读