Kubernetes 动态PV使用

Kubernetes支持动态供给的存储插件:
https://kubernetes.io/docs/concepts/storage/storage-classes/

  • Dynamic Provisioning机制工作的核心在于StorageClass的API对象。
  • StorageClass声明存储插件,用于自动创建PV。

Kubernetes 动态PV使用-LMLPHP

创建动态PVStorageClass

1、创建storageclass相关文件
1.1、vim storageclass-nfs.yaml:标识插件创建storageclass名称

apiVersion: storage.k8s.io/v1beta1
kind: StorageClass
metadata:
# StorageClass名称
name: managed-nfs-storage
# 默认不支持nfs存储,添加支持web插件标识
provisioner: fuseim.pri/ifs

1.2、vim deployment-nfs.yaml:创建nfs相关存储指定服务名称

apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nfs-client-provisioner
spec:
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
imagePullSecrets:
- name: registry-pull-secret
# 绑定角色定义的名称
serviceAccount: nfs-client-provisioner
containers:
# 镜像拉取
- name: nfs-client-provisioner
image: lizhenliang/nfs-client-provisioner:v2.0.0
# 自定义变量格式处理
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
# 指定标识插件的值
value: fuseim.pri/ifs
- name: NFS_SERVER
# nfs地址
value: 192.168.1.115
- name: NFS_PATH
# 挂在路径
value: /data/nfs
volumes:
- name: nfs-client-root
nfs:
# nfs地址
server: 192.168.1.115
# 共享路径
path: /data/nfs

1.3、vim rbac.yaml:创建rbac授权apiserver

apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner --- kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: nfs-client-provisioner-runner
# 角色中可以访问的权限
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["list", "watch", "create", "update", "patch"] --- # 角色绑定
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: run-nfs-client-provisioner
subjects:
# 绑定角色 ServiceAccount
- kind: ServiceAccount
name: nfs-client-provisioner
namespace: default
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io

2、创建文件

kubectl apply -f storageclass-nfs.yaml
kubectl apply -f rbac.yaml
kubectl apply -f deployment-nfs.yaml

3、查看创建的storageclass
kubectl get storageclass

NAME PROVISIONER AGE
managed-nfs-storage fuseim.pri/ifs 57s

4、查看创建的nfs容器
kubectl get pods

NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-565b4456f6-v9b97 1/1 Running 0 67s

使用动态 PV StorageClass  案例一

环境:部署mysql
1、创建yaml配置文件。vim mysql.yaml

apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
name: mysql
# 创建service为无头服务,标识容器
clusterIP: None
selector:
app: mysql-public --- apiVersion: apps/v1beta1
kind: StatefulSet
# 名称
metadata:
name: db
spec:
# 指定service名称
serviceName: "mysql"
# 标签选择器
template:
metadata:
labels:
app: mysql-public
spec:
# 镜像容器编辑
containers:
- name: mysql
image: mysql:5.7
env:
# 创建数据库用户密码
- name: MYSQL_ROOT_PASSWORD
value: ""
# 创建数据库
- name: MYSQL_DATABASE
value: test
# 启用端口
ports:
- containerPort: 3306
# 数据卷
volumeMounts:
# 挂在容器目录
- mountPath: "/var/lib/mysql"
# 使用来源
name: mysql-data
# 使用数据卷来源
volumes:
# 数据卷名称
- name: mysql-data
# 指定数据卷动态供给
persistentVolumeClaim:
# pvc动态供给名称
claimName: mysql-pvc --- apiVersion: v1
kind: PersistentVolumeClaim
metadata:
# pvc名称
name: mysql-pvc
spec:
# 读写权限
accessModes:
- ReadWriteMany
# 使用的存储类
storageClassName: managed-nfs-storage
# 定义容量
resources:
requests:
storage: 5Gi

2、创建容器

kubectl apply -f mysql.yaml

3、查看持久卷
kubectl get PersistentVolumeClaim

NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-public-mysql-public-0 Bound default-mysql-public-mysql-public-0-pvc-2b4979b7-c89a-11e9-8b0e-000c29400317 2Gi RWO managed-nfs-storage 39m
mysql-pvc Bound default-mysql-pvc-pvc-b8584af2-c89d-11e9-9db0-000c292e28d6 5Gi RWX managed-nfs-storage 14m

使用动态 PV StorageClass  案例二

1、创建文件
vim sts.yaml

apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx --- apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: nginx-statefulset
namespace: default
spec:
serviceName: nginx
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "managed-nfs-storage"
resources:
requests:
storage: 1Gi

2、创建容器

kubectl create -f sts.yaml 

3、查看pod
kubectl get pods

NAME READY STATUS RESTARTS AGE
nginx-statefulset-0 1/1 Running 0 3m21s
nginx-statefulset-1 1/1 Running 0 3m16s
nginx-statefulset-2 1/1 Running 0 3m11s

4、查看动态pv,pvc存储kubectl get pv,pvc
kubectl get pv,pvc

NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/default-mysql-public-mysql-public-0-pvc-2b4979b7-c89a-11e9-8b0e-000c29400317 2Gi RWO Delete Bound default/mysql-public-mysql-public-0 managed-nfs-storage 63m
persistentvolume/default-www-nginx-statefulset-0-pvc-8063e4f9-c8a1-11e9-8b0e-000c29400317 1Gi RWO Delete Bound default/www-nginx-statefulset-0 managed-nfs-storage 11m
persistentvolume/default-www-nginx-statefulset-1-pvc-836c1466-c8a1-11e9-8b0e-000c29400317 1Gi RWO Delete Bound default/www-nginx-statefulset-1 managed-nfs-storage 11m
persistentvolume/default-www-nginx-statefulset-2-pvc-868a4a51-c8a1-11e9-8b0e-000c29400317 1Gi RWO Delete Bound default/www-nginx-statefulset-2 managed-nfs-storage 11m
persistentvolume/my-pv 5Gi RWX Retain Bound default/my-pvc 133m NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/my-pvc Bound my-pv 5Gi RWX 133m
persistentvolumeclaim/mysql-public-mysql-public-0 Bound default-mysql-public-mysql-public-0-pvc-2b4979b7-c89a-11e9-8b0e-000c29400317 2Gi RWO managed-nfs-storage 63m
persistentvolumeclaim/www-nginx-statefulset-0 Bound default-www-nginx-statefulset-0-pvc-8063e4f9-c8a1-11e9-8b0e-000c29400317 1Gi RWO managed-nfs-storage 11m
persistentvolumeclaim/www-nginx-statefulset-1 Bound default-www-nginx-statefulset-1-pvc-836c1466-c8a1-11e9-8b0e-000c29400317 1Gi RWO managed-nfs-storage 11m
persistentvolumeclaim/www-nginx-statefulset-2 Bound default-www-nginx-statefulset-2-pvc-868a4a51-c8a1-11e9-8b0e-000c29400317 1Gi RWO managed-nfs-storage 11m

5、nfs服务器会自动创建pv数据
[root@localhost nfs]# ls

default-www-nginx-statefulset-0-pvc-8063e4f9-c8a1-11e9-8b0e-000c29400317
default-www-nginx-statefulset-1-pvc-836c1466-c8a1-11e9-8b0e-000c29400317
default-www-nginx-statefulset-2-pvc-868a4a51-c8a1-11e9-8b0e-000c29400317
05-18 05:07