在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,用于持久化存储。