1. 完成nfs服务的搭建:
见我的简书nfs的安装与使用 记录
2. 建立PersistentVolume
- 文件:mysql-pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
path: /nfsdata/mysql-pv
server: 172.26.195.120
- 创建并查看:
root@ali005:~/mysql-volume# kubectl create -f mysql-pv.yml
persistentvolume/mysql-pv created
root@ali005:~/mysql-volume# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql-pv 1Gi RWO Retain Available nfs 12s
注意:此时status是Avaliable状态
2. 建立PersistentVolumeClaim
- 文件:mysql-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs
- 创建并查看
root@ali005:~/mysql-volume# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pvc Bound mysql-pv 1Gi RWO nfs 8s
root@ali005:~/mysql-volume# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql-pv 1Gi RWO Retain Bound default/mysql-pvc nfs 3m46s
注意:此时pv的state是:Bound
,同时CLAIM选项成为default/mysql-pvc
3. 建立service和deployment
- 文件:mysql.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
replicas: 3
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: abc
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
---
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
- 创建并查看
root@ali005:~/mysql-volume# kubectl create -f mysql.yml
deployment.apps/mysql created
service/mysql created
root@ali005:~/mysql-volume# kubectl get pod
NAME READY STATUS RESTARTS AGE
mysql-bd4895546-5c897 1/1 Running 1 54s
mysql-bd4895546-r8jgs 1/1 Running 1 54s
mysql-bd4895546-wrmwk 1/1 Running 2 54s
网友评论