天天看点

基于Kubernetes的框架和应用系列之二:在Kubernetes部署NFS作为Persistent Volume

一、前言

Kubernetes支持NFS作为外部永久存储分配给容器使用,这种简易方式适用于小范围低强度的文件共享和持久化。

为了使得NFS能够和Kubernetes进行云原生的结合,需要一个nfs-provisioner来进行粘合,比如nfs-client-provisioner。

转载自https://blog.csdn.net/cloudvtech

二、安装NFS服务器

2.1 安装软件 

mkdir /home/nfs
cd /home/nfs/
yum install nfs-utils
systemctl enable rpcbind
systemctl enable nfs
systemctl start rpcbind
systemctl start nfs
firewall-cmd --zone=public --permanent --add-service=rpc-bind
firewall-cmd --zone=public --permanent --add-service=mountd
firewall-cmd --zone=public --permanent --add-service=nfs
firewall-cmd --reload
chmod 755  /home/nfs/
           

2.2 配置 /etc/exports

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

systemctl restart nfs

showmount -e localhost
Export list for localhost:
/home/nfs *
           

2.3 在客户端测试

yum install nfs-utils
 systemctl enable rpcbind
 systemctl start rpcbind
 showmount -e 172.2.2.13
 mkdir /mnt/nfs
 mount -t nfs 172.2.2.13:/home/nfs /mnt/nfs/
 mount
 touch /mnt/nfs/test
 ls /mnt/nfs/test  -l
           

转载自https://blog.csdn.net/cloudvtech

三、安装nfs-client-provisioner

通过helm安装

helm install stable/nfs-client-provisioner --set nfs.server=172.2.2.13 --set nfs.path=/home/nfs 
NAME:   quiet-toucan
LAST DEPLOYED: Wed Oct 10 09:21:19 2018
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/StorageClass
NAME        PROVISIONER                                        AGE
nfs-client  cluster.local/quiet-toucan-nfs-client-provisioner  31s
==> v1/ServiceAccount
NAME                                 SECRETS  AGE
quiet-toucan-nfs-client-provisioner  1        31s
==> v1/ClusterRole
NAME                                        AGE
quiet-toucan-nfs-client-provisioner-runner  31s
==> v1/ClusterRoleBinding
NAME                                     AGE
run-quiet-toucan-nfs-client-provisioner  31s
==> v1/Role
NAME                                                AGE
leader-locking-quiet-toucan-nfs-client-provisioner  31s
==> v1/RoleBinding
NAME                                                AGE
leader-locking-quiet-toucan-nfs-client-provisioner  31s
==> v1/Deployment
NAME                                 DESIRED  CURRENT  UP-TO-DATE  AVAILABLE  AGE
quiet-toucan-nfs-client-provisioner  1        1        1           0          31s
==> v1/Pod(related)
NAME                                                 READY  STATUS             RESTARTS  AGE
quiet-toucan-nfs-client-provisioner-88d4c9564-vccgr  0/1    ContainerCreating  0         31s
           

确认POD和StorageClass

[[email protected] nfs-client]# kubectl get pod | grep nfs
quiet-toucan-nfs-client-provisioner-88d4c9564-vccgr   1/1       Running     0          18m

[[email protected] nfs-client]# kubectl get sc
NAME                  PROVISIONER                                         AGE
nfs-client            cluster.local/quiet-toucan-nfs-client-provisioner   18m
           

转载自https://blog.csdn.net/cloudvtech

四、使用StorageClass based NFS

PVC文件:test-claim.yaml

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-claim
  annotations:
    volume.beta.kubernetes.io/storage-class: "nfs-client"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Mi
           

使用PVC:test-pod.yaml

kind: Pod
apiVersion: v1
metadata:
  name: test-pod
spec:
  containers:
  - name: test-pod
    image: gcr.io/google_containers/busybox:1.24
    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
           

创建PVC和POD

kubectl create -f deploy/test-claim.yaml -f deploy/test-pod.yaml

查看在NFS共享目录下由Kubernetes自动创建的PVC目录

ls /home/nfs//default-test-claim-pvc-879d441b-cc70-11e8-9135-fa163ebda1b8/
SUCCESS
           
kubectl describe pv pvc-879d441b-cc70-11e8-9135-fa163ebda1b8
Name:            pvc-879d441b-cc70-11e8-9135-fa163ebda1b8
Labels:          <none>
Annotations:     pv.kubernetes.io/provisioned-by=cluster.local/quiet-toucan-nfs-client-provisioner
Finalizers:      [kubernetes.io/pv-protection]
StorageClass:    nfs-client
Status:          Bound
Claim:           default/test-claim
Reclaim Policy:  Delete
Access Modes:    RWX
Capacity:        1Mi
Node Affinity:   <none>
Message:         
Source:
    Type:      NFS (an NFS mount that lasts the lifetime of a pod)
    Server:    172.2.2.13
    Path:      /home/nfs/default-test-claim-pvc-879d441b-cc70-11e8-9135-fa163ebda1b8
    ReadOnly:  false
Events:        <none>
           
kubectl describe pvc test-claim
Name:          test-claim
Namespace:     default
StorageClass:  nfs-client
Status:        Bound
Volume:        pvc-879d441b-cc70-11e8-9135-fa163ebda1b8
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed=yes
               pv.kubernetes.io/bound-by-controller=yes
               volume.beta.kubernetes.io/storage-class=nfs-client
               volume.beta.kubernetes.io/storage-provisioner=cluster.local/quiet-toucan-nfs-client-provisioner
Finalizers:    [kubernetes.io/pvc-protection]
Capacity:      1Mi
Access Modes:  RWX
Events:
  Type    Reason                 Age              From                                                                                                                                        Message
  ----    ------                 ----             ----                                                                                                                                        -------
  Normal  Provisioning           7m               cluster.local/quiet-toucan-nfs-client-provisioner_quiet-toucan-nfs-client-provisioner-88d4c9564-vccgr_52eca4e7-cc6d-11e8-a267-8a77c6fda26c  External provisioner is provisioning volume for claim "default/test-claim"
  Normal  ProvisioningSucceeded  7m               cluster.local/quiet-toucan-nfs-client-provisioner_quiet-toucan-nfs-client-provisioner-88d4c9564-vccgr_52eca4e7-cc6d-11e8-a267-8a77c6fda26c  Successfully provisioned volume pvc-879d441b-cc70-11e8-9135-fa163ebda1b8
  Normal  ExternalProvisioning   4m (x3 over 4m)  persistentvolume-controller                                                                                                                 waiting for a volume to be created, either by external provisioner "cluster.local/quiet-toucan-nfs-client-provisioner" or manually created by system administrator
           

在POD内查看

kubectl exec -it test-pod sh

/ # mount | grep nfs

172.2.2.13:/home/nfs/default-test-claim-pvc-27b0d062-8cda-11e9-b430-fa163ebda1b8 on /mnt type nfs4 (rw,relatime,vers=4.1,rsize=1048576,wsize=1048576,namlen=255,hard,proto=tcp,port=0,timeo=600,retrans=2,sec=sys,clientaddr=172.2.2.11,local_lock=none,addr=172.2.2.13)

/ # ls -l /mnt/

total 0

-rw-r--r--    1 root     root             0 Jun 12 06:21 SUCCESS

转载自https://blog.csdn.net/cloudvtech