天天看點

在Kubernetes上安裝Percona XtraDB叢集

官方文檔位址:https://www.percona.com/doc/kubernetes-operator-for-pxc/kubernetes.html

一、簡介

Percona XtraDB Cluster是MySQL高可用性和可擴充性的解決方案,Percona XtraDB Cluster提供的特性如下:

1).同步複制,事務要麼在所有節點送出或不送出。

2).多主複制,可以在任意節點進行寫操作。

3).在從伺服器上并行應用事件,真正意義上的并行複制。

4).節點自動配置。

5).資料一緻性,不再是異步複制。

二、相容性

Percona XtraDB Cluster完全相容MySQL和Percona Server,表現在:

1).資料的相容性

2).應用程式的相容性:無需更改應用程式

1).叢集是有節點組成的,推薦配置至少3個節點,但是也可以運作在2個節點上

2).每個節點都是普通的mysql/percona伺服器,可以将現有的資料庫伺服器組成叢集,反之,也可以将叢集拆分成單獨的伺服器。

3).每個節點都包含完整的資料副本

PXC叢集主要由兩部分組成:Percona Server with XtraDB和Write Set Replication patches(使用了Galera library,一個通用的用于事務型應用的同步、多主複制插件)

三、優缺點

優點如下:

1).當執行一個查詢時,在本地節點上執行。因為所有資料都在本地,無需 遠端通路

2).無需集中管理。可以在任何時間點失去任何節點,但是叢集将照常工作, 不受影響

3).良好的讀負載擴充,任意節點都可以查詢

缺點如下:

1).加入新節點,開銷大。需要複制完整的資料

2).不能有效的解決寫縮放問題,所有的寫操作都将發生在所有節點上

3).有多少個節點就有多少重複的資料

在Kubernetes上安裝Percona XtraDB叢集

四、部署方式

示例在k8s叢集中部署Percona XtraDB Cluster叢集

  • clone項目
git clone -b v1.7.0 https://gitee.com/Aaron-23/percona-xtradb-cluster-operator.git
cd percona-xtradb-cluster-operator
           
  • 準備存儲

目前環境具有三個k8s node節點,每個node節點/var/lib/data目錄有100GB存儲空間

所有存儲均使用本地存儲

建立storageclass

cd storage
kubectl create -f local-sc.yaml
           

建立pv

spec.local.path路徑中的檔案手動建立,修改values值為各節點ip

kubectl create -f pv-data-172.yaml
kubectl create -f pv-data-173.yaml
kubectl create -f pv-data-174.yaml
kubectl create -f pv-cron-172.yaml
kubectl create -f pv-cron-173.yaml
kubectl create -f pv-cron-174.yaml
           
  • 建立crd資源
kubectl apply -f deploy/crd.yaml
           
  • 建立ns
kubectl create namespace pxc
 kubectl config set-context $(kubectl config current-context) --namespace=pxc
           
  • 賦予rbac權限
kubectl apply -f deploy/rbac.yaml
           
  • 在Kubernetes中啟動operator
kubectl apply -f deploy/operator.yaml
           
  • 使用secrets.管理Percona XtraDB叢集相關秘鑰

需要設定資料庫密碼的修改secrets中密碼即可

kubectl create -f deploy/secrets.yaml
           
  • 建立Percona XtraDB Cluster
kubectl apply -f deploy/cr.yaml
           

建立過程将花費一些時間。當po和副本數都達到“Running”狀态時,該過程結束:

$ kubectl get pods
NAME                                              READY   STATUS    RESTARTS   AGE
cluster1-haproxy-0                                1/1     Running   0          5m
cluster1-haproxy-1                                1/1     Running   0          5m
cluster1-haproxy-2                                1/1     Running   0          5m
cluster1-pxc-0                                    1/1     Running   0          5m
cluster1-pxc-1                                    1/1     Running   0          4m
cluster1-pxc-2                                    1/1     Running   0          2m
percona-xtradb-cluster-operator-dc67778fd-qtspz   1/1     Running   0          6m
           
  • 檢查與新建立叢集的連接配接
$ kubectl run -i --rm --tty percona-client --image=percona:8.0 --restart=Never -- bash -il
percona-client:/$ mysql -h cluster1-haproxy -uroot -proot_password
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1976
Server version: 8.0.19-10 Percona XtraDB Cluster (GPL), Release rel10, Revision 727f180, WSREP version 26.4.3

Copyright (c) 2009-2020 Percona LLC and/or its affiliates
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.