服务器安装NFS

nfs服务器配置比较简单,安装之后设置挂载路径即可:

yum install -y nfs-utils
mkdir -p /data/k8s #设置挂载路径
# 打开文件
vim /etc/exports
# 添加如下内容
/data/k8s *(rw,no_root_squash)
systemctl start nfs-server # 启动服务

K8s集群节点安装nfs

yum install -y nfs-utils

K8s配置存储

配置Client

kind: Deployment
apiVersion: apps/v1
metadata:
  name: nfs-client-provisioner
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nfs-client-provisioner
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          image: quay.io/external_storage/nfs-client-provisioner:latest
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs
            - name: NFS_SERVER
              value: 192.168.2.100 #nfs服务器ip
            - name: NFS_PATH
              value: /data/k8s
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.2.100 #nfs服务器ip
            path: /data/k8s #nfs服务器上创建的目录

环境变量 NFS_SERVER 和 NFS_PATH 替换为自己的

apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner

---
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: ["list", "watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-runner
  apiGroup: rbac.authorization.k8s.io

StorageClass对象

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: course-nfs-storage
provisioner: fuseim.pri/ifs # 这里的内容跟nfs-client.yaml里面PROVISIONER_NAME值需要一致

创建资源

kubectl apply -f nfs-client.yaml
kubectl apply -f nfs-client-sa.yaml
kubectl apply -f nfs-client-class.yaml

kubectl get pods
# 查看存储类
kubectl get storageclass

# 设置默认资源
kubectl patch storageclass course-nfs-storage -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'

K8s 1.20x版本nfs动态存储报错persistentvolume-controller waiting for a volume to be created, either by ext

vi /etc/kubernetes/manifests/kube-apiserver.yaml

apiVersion: v1 ···

- --tls-private-key-file=/etc/kubernetes/pki/apiserver.key

- --feature-gates=RemoveSelfLink=false # 添加这个配置

重启apiserver即可

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐