文章目录
- 一、storageClass自动创建PV
- 1.1 安装NFS
- 1.2 创建nfs storageClass
- 1.3 测试自动创建pv
一、storageClass自动创建PV
这里使用NFS实现
1.1 安装NFS
安装nfs-server: sh nfs_install.sh /mnt/data03 10.60.41.0/24
nfs_install.sh
#!/bin/bash### How to install it? ###
### 安装nfs-server,需要两个参数:1、挂载点 2、允许访问nfs-server的网段 ###### How to use it? ###
### Client节点`yum -y install nfs-utils rpcbind`,然后挂载nfs-server目录到本地 ###
### 如:echo "192.168.0.20:/mnt/data01 /mnt/data01 nfs defaults 0 0" >> /etc/fstab && mount -a ###mount_point=$1
subnet=$2function nfs_server() {systemctl stop firewalldsystemctl disable firewalldsetenforce 0sed -i 's/^SELINUX.*/SELINUX\=disabled/' /etc/selinux/configyum -y install nfs-utils rpcbindmkdir -p $mount_pointecho "$mount_point ${subnet}(rw,sync,no_root_squash)" >> /etc/exportssystemctl start rpcbind && systemctl enable rpcbindsystemctl restart nfs-server && systemctl enable nfs-serverchown -R nfsnobody:nfsnobody $mount_point
}function usage() {
echo "Require 2 argument: [mount_point] [subnet]
eg: sh $0 /mnt/data01 192.168.10.0/24"
}declare -i arg_nums
arg_nums=$#
if [ $arg_nums -eq 2 ];thennfs_server
elseusageexit 1
fi
1.2 创建nfs storageClass
创建: kubectl apply -f storageclass_deploy.yaml
storageclass_deploy.yaml
apiVersion: v1
kind: ServiceAccount
metadata:name: nfs-provisionernamespace: devops
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:name: nfs-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: ["create", "update", "patch"]
- apiGroups: [""]resources: ["services", "endpoints"]verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: run-nfs-provisioner
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: nfs-provisioner-runner
subjects:
- kind: ServiceAccountname: nfs-provisionernamespace: devops
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:name: leader-locking-nfs-provisionernamespace: devops
rules:
- apiGroups: [""]resources: ["endpoints"]verbs: ["get", "list", "watch", "create", "update", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:name: leader-locking-nfs-provisionernamespace: devops
roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: leader-locking-nfs-provisioner
subjects:
- kind: ServiceAccountname: nfs-provisionernamespace: devops
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nfs-provisioner
spec:selector:matchLabels:app: nfs-provisionerreplicas: 1strategy:type: Recreatetemplate:metadata:labels:app: nfs-provisionerspec:serviceAccountName: nfs-provisionercontainers:- name: nfs-provisionerimage: docker.io/gmoney23/nfs-client-provisioner:latestvolumeMounts:- name: nfs-client-rootmountPath: /persistentvolumesenv:- name: PROVISIONER_NAMEvalue: example.com/nfs- name: NFS_SERVERvalue: 10.60.41.248- name: NFS_PATHvalue: /mnt/data03volumes:- name: nfs-client-rootnfs:server: 10.60.41.248path: /mnt/data03
---
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:name: nfs
provisioner: example.com/nfs
1.3 测试自动创建pv
创建: kubectl apply -f test_auto_generate_pv.yaml
创建pvc之后发现pv自动创建了,在/mnt/data03/ 目录下
test_auto_generate_pv.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: test-claim1namespace: devops
spec:accessModes:- ReadWriteManyresources:requests:storage: 1GistorageClassName: nfs
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deployment
spec:replicas: 1selector:matchLabels:name: nginx-podtemplate:metadata:labels:name: nginx-podnamespace: myappspec:containers:- name: nginximage: docker.io/library/nginx:latestimagePullPolicy: IfNotPresentvolumeMounts:- name: nfs-pvcmountPath: /usr/share/nginx/htmlvolumes:- name: nfs-pvcpersistentVolumeClaim:claimName: test-claim1