k8s部署mysql主从
时间: 2023-09-20 15:07:36 浏览: 134
MySQL主从安装部署
你可以按照以下步骤使用Kubernetes(k8s)部署MySQL主从架构:
1. 创建一个名为`mysql-pvc.yaml`的持久卷声明(PVC)文件,用于存储MySQL数据。示例文件内容如下:
```yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
```
2. 创建一个名为`mysql-master.yaml`的MySQL主节点部署文件,示例文件内容如下:
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-master
spec:
replicas: 1
selector:
matchLabels:
app: mysql-master
template:
metadata:
labels:
app: mysql-master
spec:
containers:
- name: mysql-master
image: mysql:latest
env:
- name: MYSQL_ROOT_PASSWORD
value: your_password
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
```
请替换`your_password`为您要设置的MySQL根密码。
3. 创建一个名为`mysql-slave.yaml`的MySQL从节点部署文件,示例文件内容如下:
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-slave
spec:
replicas: 2
selector:
matchLabels:
app: mysql-slave
template:
metadata:
labels:
app: mysql-slave
spec:
containers:
- name: mysql-slave
image: mysql:latest
env:
- name: MYSQL_ROOT_PASSWORD
value: your_password
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
```
4. 使用以下命令应用和创建主节点和从节点的部署:
```
kubectl apply -f mysql-pvc.yaml
kubectl apply -f mysql-master.yaml
kubectl apply -f mysql-slave.yaml
```
这样就完成了MySQL主从架构的部署。请确保您已经正确配置了Kubernetes环境,并且使用了适当的MySQL镜像和密码。
阅读全文