关于kubernetes:实战用廉价的NFS作为K8S后端存储

1次阅读

共计 6376 个字符,预计需要花费 16 分钟才能阅读完成。

K8S 后端存储:NFS

大家都晓得,NFS 是一种基于网络的文件系统协定,容许在不同的机器之间共享文件系统资源。在 K8S 中,能够应用 NFS 作为后端存储,以提供长久化存储和共享存储卷。但是否适宜在生产环境应用 NFS 作为后端存储,这取决于具体的应用程序和应用场景。如果应用程序对性能和可靠性要求比拟高,可能须要抉择其余更适宜的存储计划,比方 ceph。如果只是在测试或者开发环境中,我感觉应用 NFS 能够更不便地实现共享存储卷,进步测试或者开发的效率。

搭建 NFS

# step1 装置 yum install nfs-utils -y
yum install nfs-utils -y

# step2 创立 NFS 共享目录
mkdir /data/nfs_k8s_storage_share

# step3 配置 NFS 共享,编辑 /etc/exports 文件,将要共享的目录增加到文件中
/data/nfs_k8s_storage_share *(rw,sync,no_root_squash) # 这将容许任何客户端以读写模式访问共享目录。# step4 从新加载 NFS 配置
exportfs -r

# step5 启动 NFS 服务
systemctl start nfs-server
systemctl enable nfs-server

# step6 确认 NFS 服务正在运行
systemctl status nfs-server

# step7 留神防火墙,能够敞开或增加容许策略
firewall-cmd --permanent --zone=public --add-service=nfs
firewall-cmd --reload

# step8 验证是否共享胜利
showmount -e 192.168.11.254

k8s 工作节点装置 nfs-utils

装置完即可,无需做任何配置

yum install nfs-utils -y

具体在哪个工作节点装置得看在上面的 deployment.yaml 中拉起的 NFS 客户端配置程序 pod 跑在哪个节点上,能够让该 pod 调度到指定的节点,那么该节点就要装置好 nfs-utils。如果不干预调度行为,或者思考到后续要跑多个正本就能够将全副工作节点都装置好 nfs-utils。

在 k8s master 节点上装置 nfs 动静供应插件

仓库:https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner

相干 yaml 下载链接:https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/blob/master/deploy/rbac.yaml https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/blob/master/deploy/deployment.yaml https://github.com/kubernetes-sigs/nfs-subdir-external-provisioner/blob/master/deploy/class.yaml

  • rbac.yaml:受权拜访 ApiServer
  • deployment.yaml:部署插件,部署之前要批改外面的指向的 nfs 服务器地址和共享目录
  • class.yaml:创立存储类
开始部署插件
  1. 部署 rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner
rules:
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["get", "list", "watch"]
  - 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: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-runner
  apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
rules:
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: leader-locking-nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: default
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io
kubectl create -f rbac.yaml
  1. 部署 deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: default
spec:
  replicas: 1
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: nfs-client-provisioner
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: tantianran/nfs-subdir-external-provisioner:v4.0.1 # 应用到镜像曾经转存到我的仓库
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: k8s-sigs.io/nfs-subdir-external-provisioner
            - name: NFS_SERVER
              value: 192.168.11.254 #此处批改成 nfs 服务器的 ip 地址
            - name: NFS_PATH
              value: /data/nfs_k8s_storage_share # 此处批改成 nfs 的共享目录
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.11.254 #此处批改成 nfs 服务器的 ip 地址
            path: /data/nfs_k8s_storage_share # 此处批改成 nfs 的共享目录 
kubectl create -f deployment.yaml
  1. 创立存储类 class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-client
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
  archiveOnDelete: "false"
kubectl create -f class.yaml
  1. 查看
# 查看受权
kubectl get sa

# 查看存储类
kubectl get sc

应用

  1. 创立一个 PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  storageClassName: nfs-client

在这里,PVC 名称为 my-pvc,申请 5GB 的存储空间,并将存储类设置为 nfs-client

  1. 查看 pvc
# pvc 已创立
[root@k8s-a-master nfs-storage-yaml]# kubectl get pvc
NAME     STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
my-pvc   Bound    pvc-ce18ae0a-d4b4-4fa9-8241-b8b9551baa61   5Gi        RWO            nfs-client     2m6s

# 对应的 pv 曾经主动创立
[root@k8s-a-master nfs-storage-yaml]# kubectl get pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM            STORAGECLASS   REASON   AGE
pvc-ce18ae0a-d4b4-4fa9-8241-b8b9551baa61   5Gi        RWO            Delete           Bound    default/my-pvc   nfs-client              2m7s
[root@k8s-a-master nfs-storage-yaml]# 
  1. 创立一个利用 pod
apiVersion: v1
kind: Pod
metadata:
  name: test-nginx-pod
spec:
  containers:
  - name: test-nginx-container
    image: nginx
    volumeMounts:
    - name: storage
      mountPath: /data
  volumes:
  - name: storage
    persistentVolumeClaim:
      claimName: my-pvc
  1. 查看 pod 是否挂载胜利
[root@k8s-a-master nfs-storage-yaml]# kubectl exec -it test-nginx-pod -- df -h
...                                                                                            3.9G     0  3.9G   0% /sys/fs/cgroup
192.168.11.254:/data/nfs_k8s_storage_share/default-my-pvc-pvc-ce18ae0a-d4b4-4fa9-8241-b8b9551baa61  500G  1.8G  499G   1% /data
...

本文转载于 WX 公众号:不背锅运维(喜爱的盆友关注咱们):https://mp.weixin.qq.com/s/f8eCJNxztMFc3uQ_YJzwgg

正文完
 0