关于pv和pvc 详细介绍 点这里
主要说明静态pvc如何关联到pv
创建应该PV:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-nfs
spec:
capacity:
storage: 50Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.7.206
path: "/opt/dbdata"
创建应该PVC
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-nfs
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 50Gi
以上创建之后的pvc会自动去匹配满足accessModes,storage 条件的pv,如果有多个相同绑定到其中一个
2、指定pvc绑定到指定的pv
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-nfs
pv: mysql-nfs
spec:
capacity:
storage: 50Gi
accessModes:
- ReadWriteMany
storageClassName: slow
nfs:
server: 192.168.7.206
path: "/opt/dbdata"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
namespaces: dev
name: mysql-nfs
spec:
accessModes:
- ReadWriteMany
storageClassName: slow
resources:
requests:
storage: 50Gi
selector:
matchLabels:
pv: mysql-nfs
配置:storageClassName: slow。此配置用于绑定PVC和PV。这表明这个PVC希望使用storageClassName=slow的PV。接下来是accessModes =ReadWriteMany。这表明这个PV希望使用storageClassName=slow,并且accessModes =ReadWriteManye的PV。在上述条件都满足后,PVC还可以指定PV必须满足的Label,如matchLabels:pv: mysql-nfs。这表明此PVC希望使用storageClassName=slow,accessModes =
ReadWriteMany 并且拥有Label:
pv: mysql-nfs 的PV。最后是storage: 50Gi。这表明此PVC希望使用50G的Volume资源。
完整的部署mysql单点yaml文件
cat mysql/mysql-deploy.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-nfs
spec:
capacity:
storage: 50Gi
accessModes:
- ReadWriteMany
nfs:
server: 192.168.7.206
path: "/opt/dbdata"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-nfs
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 50Gi
---
apiVersion: v1
kind: Service
metadata:
name: dev-mysql
labels:
app: dev-mysql
spec:
type: NodePort
ports:
- port: 3306
targetPort: 3306
protocol: TCP
nodePort: 32066
selector:
app: dev-mysql
tier: mysql
---
apiVersion: extensions/v1beta1 # for k8s versions before 1.9.0 use apps/v1beta2 and before 1.8.0 use extensions/v1beta1
kind: Deployment
metadata:
name: dev-mysql
labels:
app: dev-mysql
spec:
selector:
matchLabels:
app: dev-mysql
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: dev-mysql
tier: mysql
spec:
containers:
- image: 192.168.19.111/baseimages/mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
value: P12345678
livenessProbe:
tcpSocket:
port: 3306
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: tz-config
mountPath: /etc/localtime
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-nfs
- name: tz-config
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
claimName: mysql-nfs对应pvc名称