Kubernetes(简称 K8s)是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用程序。MySQL 是一个流行的关系型数据库管理系统。在 Kubernetes 上搭建 MySQL 可以充分利用其强大的容器编排能力,实现数据库的高可用性和可扩展性。
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
my.cnf: |
[mysqld]
bind-address=0.0.0.0
log-bin=mysql-bin
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
spec:
replicas: 3
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "root_password"
volumeMounts:
- name: mysql-config
mountPath: /etc/mysql/my.cnf
subPath: my.cnf
- name: mysql-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-config
configMap:
name: mysql-config
- name: mysql-storage
persistentVolumeClaim:
claimName: mysql-pvc
---
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
ports:
- port: 3306
targetPort: 3306
selector:
app: mysql
领取专属 10元无门槛券
手把手带您无忧上云