使用 NFS 作为 Kuberenetes 持久化存储
安装 NFS Server
yum install -y nfs-utils nfs-utils-lib # centos
---- or ----
apt install -y nfs-common nfs-kernel-server # ubuntu or debain
创建一个目录作为 NFS 共享
sudo mkdir -p /mnt/nfs4/share
cat > /etc/exports
/mnt/nfs4/share clientIP(rw,sync,no_subtree_check)
or
/mnt/nfs4/share subnetIP/24(rw,sync,no_subtree_check)
exportfs -a
安装 Kuberenetes 客户端
需要在每个节点上都安装 nfs 客户端
yum install -y nfs-utils # centos
--- or ----
apt install -u nfs-common # ubuntu or debain
需要三个 yaml 文件
- 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: ["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
- class.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: managed-nfs-storage
provisioner: fuseim.pri/ifs # or choose another name, must match deployment's env PROVISIONER_NAME'
parameters:
archiveOnDelete: "false"
- 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: 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: <nfs server address> # 这里输入 NFS 服务器地址
- name: NFS_PATH
value: /ifs/kubernetes # nfs share path
volumes:
- name: nfs-client-root
nfs:
server: <nfs server address> # 这里输入 NFS 服务器地址
path: /ifs/kubernetes # nfs share path
执行 kubectl apply
kubectl create -f deploy/rbac.yaml
kubectl create -f deploy/class.yaml
kubectl create -f deploy/deployment.yaml
创建一个测试 NFS 服务的 POD
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: test-pv-claim
annotations:
volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Mi
---
kind: Pod
apiVersion: v1
metadata:
name: test-pod
spec:
containers:
- name: test-pod
image: redis:5.0.9-buster
volumeMounts:
- name: nfs-pvc
mountPath: "/data"
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: test-pv-claim