关于k8s:Kubernetes使用nfs做为动态存储

6次阅读

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

机器环境筹备

应用 StorageClass 为 k8s 作为动静存储,大规模集群中可能会有很多 PV,如果这些 PV 都须要运维手动来解决这也是一件很繁琐的事件,所以就有了动静供应概念,也就是 Dynamic Provisioning。而咱们下面的创立的 PV 都是动态供应形式,也就是 Static Provisioning。而动静供应的要害就是 StorageClass,它的作用就是创立 PV 模板,进步工作效率, 创 PVC 时会动静主动创立 PV。

服务器 用处
10.4.2.104 nfs 服务器
10.4.2.100-105 k8s 集群

搭建 nfs 服务器

  • 10.4.2.104 上搭建 nfs 服务器
yum install rpcbind
yum -y install nfs-utils
k
cat << EOF >> /etc/hosts
/volumes 10.0.0.0/8(rw,no_root_squash,anonuid=998,anongid=994)
EOF
exportfs -rv
systemctl restart rpcbind
systemctl restart nfs
systemctl enable nfs
  • k8s 所有节点装置 nfs 客户端
yum -y install nfs-utils

K8S 装置 nfs-client

  • 创立 RABCServiceAccount
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: storage-class
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner
rules:
  - 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: storage-class
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: storage-class
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
  namespace: storage-class
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    # replace with namespace where provisioner is deployed
    namespace: storage-class
roleRef:
  kind: Role
  name: leader-locking-nfs-client-provisioner
  apiGroup: rbac.authorization.k8s.io
kubectl apply -f rabc.yaml
  • 装置 nfs-client
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nfs-client-provisioner
  labels:
    app: nfs-client-provisioner
  # replace with namespace where provisioner is deployed
  namespace: storage-class
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-client-provisioner
  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: harbor-k8s.iwgame.com/containers/nfs-client-provisioner:latest
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes/
          env:
            - name: PROVISIONER_NAME
              value: g.iwgame.com/nfs
            - name: NFS_SERVER
              value: 10.4.2.104
            - name: NFS_PATH
              value: /volumes
      volumes:
        - name: nfs-client-root
          nfs:
            server: 10.4.2.104
            path: /volumes
kubectl apply -f deployment.yaml

创立 StorageClass

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

创立 PVC 测试

正文完
 0