1. 如何搭建nfs服務(wù)
#master節(jié)點(diǎn)安裝nfs
yum -y install nfs-utils
#創(chuàng)建nfs目錄
mkdir -p /nfs/data/
#修改權(quán)限
chmod -R 777 /nfs/data
#編輯export文件
vim /etc/exports
/nfs/data *(rw,no_root_squash,sync)
#配置生效
exportfs -r
#查看生效
exportfs
#啟動(dòng)rpcbind、nfs服務(wù)
systemctl restart rpcbind && systemctl enable rpcbind
systemctl restart nfs && systemctl enable nfs
#查看 RPC 服務(wù)的注冊(cè)狀況
rpcinfo -p localhost
#showmount測(cè)試
showmount -e 192.168.92.56
#所有node節(jié)點(diǎn)安裝客戶端
yum -y install nfs-utils
systemctl start nfs && systemctl enable nfs
作為準(zhǔn)備工作,我們已經(jīng)在 k8s-master 節(jié)點(diǎn)上搭建了一個(gè) NFS 服務(wù)器,目錄為 /nfs/data.
修改默認(rèn)storageclass:
kubectl patch storageclass <your-class-name> -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
# nfs環(huán)境搭建報(bào)錯(cuò)clnt_create: RPC: Program not registered
命令:(注意先啟動(dòng)rpc)
rpcbind restart
nfs restart
2. 部署存儲(chǔ)供應(yīng)卷
根據(jù)PVC的請(qǐng)求, 動(dòng)態(tài)創(chuàng)建PV存儲(chǔ).
[root@bogon statefulset]# cat deployment-nfs.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: nfs-client-provisioner
spec:
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccount: nfs-provisioner
containers:
- name: nfs-client-provisioner
image: registry.cn-hangzhou.aliyuncs.com/open-ali/nfs-client-provisioner
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: fuseim.pri/ifs
- name: NFS_SERVER
value: 192.168.64.136
- name: NFS_PATH
value: /nfs/data
volumes:
- name: nfs-client-root
nfs:
server: 192.168.1.136
path: /nfs/data
創(chuàng)建服務(wù)
[root@bogon statefulset]# kubectl create -f deployment-nfs.yaml
[root@bogon statefulset]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nfs-client-provisioner 1 1 1 1 36m
3. 部署storageclass
[root@bogon statefulset]# cat storageclass-nfs.yaml
apiVersion: storage.k8s.io/v1beta1
kind: StorageClass
metadata:
name: managed-nfs-storage
provisioner: fuseim.pri/ifs
創(chuàng)建
[root@bogon statefulset]# kubectl create -f storageclass-nfs.yaml
[root@bogon statefulset]# kubectl get storageclass
NAME PROVISIONER AGE
managed-nfs-storage fuseim.pri/ifs 36m
4.構(gòu)建權(quán)限體系
創(chuàng)建 serviceaccount
[root@bogon statefulset]# cat serviceaccount.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-provisioner
創(chuàng)建 role
[root@bogon statefulset]# cat clusterrole.yaml
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
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: ["watch", "create", "update", "patch"]
- apiGroups: [""]
resources: ["services", "endpoints"]
verbs: ["get"]
- apiGroups: ["extensions"]
resources: ["podsecuritypolicies"]
resourceNames: ["nfs-provisioner"]
verbs: ["use"]
創(chuàng)建:賬戶和角色綁定
[root@bogon statefulset]# cat clusterrolebinding.yaml
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-provisioner
subjects:
- kind: ServiceAccount
name: nfs-provisioner
namespace: default
roleRef:
kind: ClusterRole
name: nfs-provisioner-runner
apiGroup: rbac.authorization.k8s.io
創(chuàng)建:
kubectl create -f serviceaccount.yaml -f clusterrole.yaml -f clusterrolebinding.yaml
5.創(chuàng)建測(cè)試
[root@bogon statefulset]# cat statefulset-nfs.yaml
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: web
spec:
serviceName: "nginx1"
replicas: 1
volumeClaimTemplates:
- metadata:
name: test
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 2Gi
template:
metadata:
labels:
app: nginx1
spec:
serviceAccount: nfs-provisioner
containers:
- name: nginx1
image: nginx:1.7.9
volumeMounts:
- mountPath: "/mnt"
name: test
創(chuàng)建:
$ kubectl create -f statefulset-nfs.yaml
查看生成的pv,pvc,storageclass, deployment.
kubectl get pv
kubectl get pvc
kubectl get sc
kubectl get deploy
6. 參考文章
nfs 和k8s使用參考文章
http://www.itdecent.cn/p/284c999d5717
https://www.cnblogs.com/cuishuai/p/9152277.html
https://www.cnblogs.com/DaweiJ/articles/9131762.html
https://yq.aliyun.com/articles/613036
改變默認(rèn)storageclass
mount -t nfs 172.16.0.158:/srv /nfs