k8s部署NFS持久化存储

NFS

安装NFS Server

yum install nfs-utils rpcbind

编辑/etc/exports

/nfs *(insecure,rw,sync,no_root_squash,no_all_squash)

创建nfs目录

mkdir -p /nfs

启动服务

systemctl enable nfs-server rpcbind
systemctl start nfs-server rpcbind

添加DNS域名服务器

echo "nameserver 192.168.1.21" >>/etc/resolv.conf

测试访问NFS

$ showmount -e rke02.nfs.com
Export list for rke02.nfs.com:
/nfs *

k8s

编辑coredns configmap添加dns解析

.:53 {
......
} # STUBDOMAINS - Rancher specific change
k8s.nfs.com:53 {
errors
cache 30
forward . 192.168.1.21 {
prefer_udp
}
}

安装nfs客户端

yum install nfs-utils rpcbind

下载代码

git clone https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner.git

rbac授权

cd nfs-subdir-external-provisioner/deploy/
kubectl apply -f rbac.yaml

创建StorageClass

编辑class.yaml修改metata.namenfs-provisioner,新增allowVolumeExpansion: true;支持pv扩容和设置默认storageclass;编辑provisioner;

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-client
annotations:
storageclass.beta.kubernetes.io/is-default-class: 'true'
provisioner: nfs-client-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
archiveOnDelete: "false"
allowVolumeExpansion: true

执行以下命令创建storageclass

kubectl apply -f class.yaml

部署nfs-client-provisioner
编辑deployment.yaml,修改NFS_SERVER、NFS_PATH、PROVISIONER_NAME,PROVISIONER_NAME需要和storageclass中的provisioner保持一致

spec:
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: registry.k8s.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: nfs-client-provisioner
- name: NFS_SERVER
value: cluster.nfs.com
- name: NFS_PATH
value: /nfs
volumes:
- name: nfs-client-root
nfs:
server: cluster.nfs.com
path: /nfs

执行以下命令部署NFS Client

kubectl apply -f deployment.yaml

查看容器状态

$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nfs-client-provisioner-66cb44d4f5-fdh6v 1/1 Running 0 2m48s

创建PVC

kubectl apply -f test-claim.yaml

创建pod

编辑test-pod.yaml

kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: 192.168.1.21:81/library/busybox:latest
command: ['tail']
args: ['-f', '/dev/null']
volumeMounts:
- name: nfs-pvc
mountPath: "/mnt"
restartPolicy: "Never"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-claim

部署pod

kubectl apply -f test-po.yaml

验证NFS挂载

$ kubectl exec -it test-pod -- sh
/ #
/ # df -h|grep nfs
192.168.229.133:/nfs/default-test-claim-pvc-7a2497e5-f93c-4deb-b2b0-71451ec09647
/ #
文章作者: 慕容峻才
文章链接: https://www.acaiblog.top/k8s部署NFS持久化存储/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 阿才的博客
微信打赏
支付宝打赏