Kubernetes nfs动态storageclass

Posted by BlueFat on Monday, November 23, 2020

nfs安装

yum install -y nfs-utils
systemctl start nfs-server
mkdir /data/nfs/k8s -p
echo "/data/nfs *(rw,sync,no_subtree_check,no_root_squash)" > /etc/exports
exportfs -r
systemctl reload nfs-server

静态持久卷

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: nfs-slow
  mountOptions:
    - hard
    - nfsvers=4.1
  nfs:
    path: /data/nfs/k8s
    server: 192.168.10.228
EOF
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  volumeMode: Filesystem
  resources:
    requests:
      storage: 2Gi
  storageClassName: nfs-slow
EOF
cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: nfs-pod-test
spec:
  containers:
  - image: nginx:latest
    name: nginx
    volumeMounts:
    - mountPath: /mnt
      name: nfs-volume
  volumes:
  - name: nfs-volume
    persistentVolumeClaim:
      claimName: nfs-pvc
EOF

测试

[root@ha ~]# kubectl get pvc
NAME         STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
nfs-pvc      Bound    nfs-pv                                     5Gi        RWX            nfs-slow       10s

[root@ha ~]# kubectl get pod
NAME           READY   STATUS    RESTARTS   AGE
nfs-pod-test   1/1     Running   0          2m13s

删除

kubectl delete pod nfs-pod-test
kubectl delete pvc nfs-pvc
kubectl delete pv nfs-pv

动态持久卷

nfs-subdir-external-provisioner

下载镜像到私有仓库

export https_proxy="192.168.10.250:7890"
image=k8s.gcr.io/sig-storage/nfs-subdir-external-provisioner:v4.0.2
skopeo copy docker://${image} docker://registry.sundayhk.com/k8s/${image#*/}
unset https_proxy

helm安装

helm repo add nfs-subdir-external-provisioner https://kubernetes-sigs.github.io/nfs-subdir-external-provisioner/

helm install nfs-subdir-external-provisioner nfs-subdir-external-provisioner/nfs-subdir-external-provisioner \
    --set nfs.server=192.168.10.228 \
    --set nfs.path=/data/nfs/k8s \
    --set image.repository=registry.sundayhk.com/k8s/sig-storage/nfs-subdir-external-provisioner \
    -n nfs-provisioner \
    --create-namespace

测试

kubectl create -f https://raw.githubusercontent.com/kubernetes-sigs/nfs-subdir-external-provisioner/master/deploy/test-claim.yaml 

# test-claim.yaml
cat <<EOF | kubectl apply -f -
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-claim
spec:
  storageClassName: nfs-client
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Mi
EOF
kubectl create -f https://raw.githubusercontent.com/kubernetes-sigs/nfs-subdir-external-provisioner/master/deploy/test-pod.yaml

# test-pod.yaml
cat <<EOF | kubectl apply -f -
kind: Pod
apiVersion: v1
metadata:
  name: test-pod
spec:
  containers:
  - name: test-pod
    image: busybox:1.28
    command:
      - "/bin/sh"
    args:
      - "-c"
      - "touch /mnt/SUCCESS && exit 0 || exit 1"
    volumeMounts:
      - name: nfs-pvc
        mountPath: "/mnt"
  restartPolicy: "Never"
  volumes:
    - name: nfs-pvc
      persistentVolumeClaim:
        claimName: test-claim
EOF

nfs查看

[root@harbor /]# ls -l /data/nfs/k8s/default-test-claim-pvc-15fe2f96-7fc4-4064-8ab4-19fb9eec1f8c/
总用量 0
-rw-r--r-- 1 root root 0 11月 21 03:41 SUCCESS

设置为默认storageclass

kubectl patch storageclass nfs-client -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'