为deployment挂载nfs卷
nfs卷允许将现有的NFS(网络文件系统)共享装载到Pod中。与删除Pod时会擦除的emptyDir不同,nfs卷的内容会保留下来,而卷只是被卸载。这意味着NFS卷可以预先填充数据,并且数据可以在pod之间共享。NFS可以被多个对象同时挂载。
生成脚本文件
kubectl create deployment nfs-deploy --image=nginx --dry-run=client -n openeuler -o yaml > nfs-deploy.yaml
增加volume配置内容
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: nfs-deploy
name: nfs-deploy
namespace: openeuler
spec:
replicas: 1
selector:
matchLabels:
app: nfs-deploy
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: nfs-deploy
spec:
volumes:
- name: v1
nfs:
server: 192.168.26.135
path: /data
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
resources: {}
volumeMounts:
- name: v1
mountPath: /data
status: {}
kubectl apply -f nfs-deploy.yaml
验证结果
kubectl exec -it -n openeuler nfs-deploy-5c644945b7-mh429 -- df -h /data
Filesystem Size Used Avail Use% Mounted on
192.168.26.135:/data 37G 1.7G 33G 5% /data
注意:pod调度的节点需要安装nfs-utils,不然Pod
会一直处于ContainerCreating
状态,查看通过命令查看kubectl describe pods -n openeuler nfs-deploy-75d4bcfbd5-b5g9k
以下提示:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m43s default-scheduler Successfully assigned openeuler/nfs-deploy-75d4bcfbd5-b5g9k to vms129.openeuler.cc
Warning FailedMount 2m10s kubelet Unable to attach or mount volumes: unmounted volumes=[v1], unattached volumes=[v1 kube-api-access-qfgpf]: timed out waiting for the condition
Warning FailedMount 2m4s (x9 over 4m12s) kubelet MountVolume.SetUp failed for volume "v1" : mount failed: exit status 32
Mounting command: mount
Mounting arguments: -t nfs 192.168.26.135:/data /var/lib/kubelet/pods/42efd45e-5675-47c6-a808-4f30268e00f5/volumes/kubernetes.io~nfs/v1
Output: mount: /var/lib/kubelet/pods/42efd45e-5675-47c6-a808-4f30268e00f5/volumes/kubernetes.io~nfs/v1: bad option; for several filesystems (e.g. nfs, cifs) you might need a /sbin/mount.<type> helper program.
dmesg(1) may have more information after failed mount system call.