內容概述
MySQL Operator 是由 Oracle 官方開發的 Kubernetes Operator,專為管理 MySQL InnoDB Cluster 設計。它能自動化部署、升級、備份與監控等工作,讓開發者與系統管理員能更輕鬆地在 Kubernetes 上運行高可用的 MySQL 叢集。
應用場景
- 雲原生應用:需要在 Kubernetes 上部署可擴展且高可用的 MySQL 資料庫。
- DevOps 團隊:希望透過 CI/CD 流程自動化資料庫部署與管理。
- 多租戶平台:需要為不同租戶提供獨立且可管理的 MySQL 叢集。
技術特點
- 完整生命週期管理:自動處理部署、升級、備份與恢復等任務。
- 支援 InnoDB Cluster:利用 MySQL Group Replication 提供高可用性。
- 整合 MySQL Router:簡化應用程式的連線管理。
- 自動 TLS 憑證管理:支援自簽憑證,提升安全性。
- 可擴展性:透過 Helm 或 YAML 自訂部署配置。
其他整合
- Helm Chart:官方提供 Helm Chart,簡化部署流程。
- 私有映像庫支援:可從私有映像庫部署 Operator 與叢集。
- 備份與還原:支援備份 MySQL 資料並從備份中還原。
安裝步驟
使用 kubectl 安裝
1. 部署 CRDs(Custom Resource Definitions):
Bash
kubectl apply -f https://raw.githubusercontent.com/mysql/mysql-operator/9.3.0-2.2.4/deploy/deploy-crds.yaml
2. 部署 MySQL Operator:
Bash
kubectl apply -f https://raw.githubusercontent.com/mysql/mysql-operator/9.3.0-2.2.4/deploy/deploy-operator.yaml
3. 確認 Operator 是否運行:
Bash
kubectl get deployment -n mysql-operator mysql-operator
使用 Helm 安裝(推薦)
1. 新增 Helm 倉庫並更新:
Bash
helm repo add mysql-operator https://mysql.github.io/mysql-operator/ helm repo update
2. 安裝 MySQL Operator:
Bash
helm install mysql-operator mysql-operator/mysql-operator --namespace mysql-operator --create-namespace
存取和操作
建立 MySQL InnoDB Cluster
1. 建立包含 root 使用者憑證的 Secret:
Bash
kubectl create secret generic mypwds \
--from-literal=rootUser=root \
--from-literal=rootHost=% \
--from-literal=rootPassword="sakila"
2. 建立 InnoDBCluster 資源(mycluster.yaml):
YAML
apiVersion: mysql.oracle.com/v2
kind: InnoDBCluster
metadata:
name: mycluster
spec:
secretName: mypwds
tlsUseSelfSigned: true
instances: 3
router:
instances: 1
3. 部署叢集:
Bash
kubectl apply -f mycluster.yaml
4. 監控叢集狀態:
Bash
kubectl get innodbcluster --watch
連接至 MySQL 叢集
Operator 會自動建立 Kubernetes Service,供應用程式連接:
Bash
kubectl get service mycluster
您可以使用 kubectl port-forward
或 MySQL Router 進行連接。
Bash
kubectl port-forward service/mycluster mysql
Forwarding from 127.0.0.1:3306 -> 6446
Forwarding from [::1]:3306 -> 6446
mysql -h127.0.0.1 -P3306 -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
...
小技巧
- 備份與還原:使用官方提供的工具進行資料備份與還原,確保資料安全。
- 自訂配置:透過修改 Helm values.yaml 或 YAML 檔案,自訂叢集配置。
- 監控與日誌:整合 Prometheus 與 Grafana,監控叢集健康狀態。