在Kubernetes下部署MySQL容器集群可以使用StatefulSet來管理。下面是一個示例:
mysql-configmap.yaml
,其中包含MySQL的配置信息:apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
my.cnf: |
[mysqld]
server-id=1
log-bin=mysql-bin
binlog_format=row
skip_name_resolve=ON
mysql-service.yaml
,用于提供訪問MySQL的服務:apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
selector:
app: mysql
clusterIP: None
ports:
- port: 3306
mysql-statefulset.yaml
,包含MySQL容器的定義和副本數量:apiVersion: apps/v1
kind: StatefulSet
metadata:
name: mysql
spec:
serviceName: mysql-service
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:latest
env:
- name: MYSQL_ROOT_PASSWORD
value: "password"
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
volumeClaimTemplates:
- metadata:
name: mysql-pvc
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 10Gi
kubectl apply -f mysql-configmap.yaml
,kubectl apply -f mysql-service.yaml
,kubectl apply -f mysql-statefulset.yaml
。這樣就創建了一個包含3個MySQL容器的集群。每個MySQL容器都有一個持久化存儲卷,用于存儲數據。MySQL容器使用配置文件中定義的配置來啟動和運行。
注意:在部署集群之前,需要確保已經創建了一個Persistent Volume和Persistent Volume Claim,用于持久化存儲。