第一步:创建pv
kind: PersistentVolume
apiVersion: v1
metadata:
name: mysql-pv
namespace: kube-public
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
path: /storage/nfsdata/mysql # nfs创建的目录
server: 192.168.194.104 # nfs的地址
第二步:创建pvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pvc
namespace: kube-public
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs
第三步:创建pod (安装nfs的机器,防火墙记得关闭)
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
namespace: kube-public
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: 192.168.194.104:8080/library/mysql:5.7.32 # 私有镜像仓库
env:
- name: MYSQL_ROOT_PASSWORD
value: "123456" # 数据库root的密码
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
第四步:创建service
apiVersion: v1
kind: Service
metadata:
name: mysql
namespace: kube-public
spec:
type: NodePort
ports:
- port: 3306
targetPort: 3306
nodePort: 30011 # 对外暴露的接口
selector:
app: mysql
第五步:验证