在Kubernetes(K8s)中进行MySQL的容器编排主要涉及创建Deployment、Service、Persistent Volume(PV)和Persistent Volume Claim(PVC)等资源。以下是一个基本的步骤指南,帮助你完成MySQL在Kubernetes上的容器编排:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: root-password
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
targetPort: 3306
selector:
app: mysql
type: ClusterIP
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: standard
通过以上步骤和配置示例,你可以在Kubernetes集群中成功部署MySQL,并确保其高可用性和数据持久性。记得根据实际情况调整配置,例如存储容量、副本数量等。