第一步: 创建 service、pvc、deployment
# cat mysql-deployment.yml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
clusterIP: None # 创建Headless services,DNS直接解析service的name为POD的IP, 当service只有一个pod时,这种方式最简单。
---
apiVersion: v1
kind: PersistentVolumeClaim # 创建的前提是有可用的PV。
metadata:
name: mysql-pv-claim
spec:
accessModes:
- ReadWriteOnce # 申请能在一个节点上进行读写的数据卷
resources:
requests:
storage: 20Gi
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate # 因为底层数据卷不支持多pod同时读写,所以先stop第一个pod,才创建新的。
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
# Use secret in real usage
- name: MYSQL_ROOT_PASSWORD
value: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
$ kubectl create -f ./mysql-deployment.yml
第二步: 测试部署成功
# 这里可以直接通过service的name访问POD
$ kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword
网友评论