k8s集群安装nfs-server服务
- 下载并修改yaml
mkdir -p /root/yaml/addons/nfs
cd /root/yaml/addons/nfs
wget https://raw.githubusercontent.com/kubernetes-csi/csi-driver-nfs/master/deploy/example/nfs-provisioner/nfs-server.yaml
- 修改yaml内容,添加namespace
[root@kube-master nfs]# cat nfs-server.yaml
---
apiVersion: v1
kind: Namespace
metadata:
name: nfs
---
kind: Service
apiVersion: v1
metadata:
name: nfs-server
namespace: nfs
labels:
app: nfs-server
spec:
type: ClusterIP # use "LoadBalancer" to get a public ip
selector:
app: nfs-server
ports:
- name: tcp-2049
port: 2049
protocol: TCP
- name: udp-111
port: 111
protocol: UDP
---
kind: Deployment
apiVersion: apps/v1
metadata:
name: nfs-server
namespace: nfs
spec:
replicas: 1
selector:
matchLabels:
app: nfs-server
template:
metadata:
name: nfs-server
labels:
app: nfs-server
spec:
nodeSelector:
"kubernetes.io/os": linux
containers:
- name: nfs-server
image: itsthenetwork/nfs-server-alpine:latest
env:
- name: SHARED_DIRECTORY
value: "/exports"
volumeMounts:
- mountPath: /exports
name: nfs-vol
securityContext:
privileged: true
ports:
- name: tcp-2049
containerPort: 2049
protocol: TCP
- name: udp-111
containerPort: 111
protocol: UDP
volumes:
- name: nfs-vol
hostPath:
path: /nfs-vol # modify this to specify another path to store nfs share data
type: DirectoryOrCreate
[root@kube-master nfs]#
- 部署nfs-server
[root@kube-master nfs]# kubectl apply -f nfs-server.yaml
namespace/nfs created
service/nfs-server created
deployment.apps/nfs-server created
- 检查nfs-server资源状态
[root@kube-master nfs]# ls -l /nfs-vol/
total 0
[root@kube-master nfs]# kubectl get all -n nfs
NAME READY STATUS RESTARTS AGE
pod/nfs-server-7cc5bcdcd5-rnwgr 1/1 Running 0 56s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nfs-server ClusterIP 10.97.192.219 <none> 2049/TCP,111/UDP 56s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nfs-server 1/1 1 1 56s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nfs-server-7cc5bcdcd5 1 1 1 56s
k8s集群安装csi-driver-nfs
- 下载
git clone https://github.com/kubernetes-csi/csi-driver-nfs.git
cd csi-driver-nfs/
- 修改镜像仓库为阿里云
[root@kube-master csi-driver-nfs]# ls -ld deploy/v*
drwxr-xr-x 2 root root 129 Mar 7 09:35 deploy/v3.0.0
drwxr-xr-x 2 root root 129 Mar 7 09:35 deploy/v3.1.0
drwxr-xr-x 2 root root 129 Mar 7 09:35 deploy/v4.0.0
drwxr-xr-x 2 root root 118 Mar 7 09:35 deploy/v4.1.0
drwxr-xr-x 2 root root 118 Mar 7 09:35 deploy/v4.2.0
drwxr-xr-x 2 root root 220 Mar 7 09:35 deploy/v4.3.0
drwxr-xr-x 2 root root 220 Mar 7 09:35 deploy/v4.4.0
drwxr-xr-x 2 root root 220 Mar 7 09:35 deploy/v4.5.0
drwxr-xr-x 2 root root 220 Mar 7 09:35 deploy/v4.6.0
[root@kube-master csi-driver-nfs]# cp -a deploy/v4.6.0/ deploy/v4.6.0-bak
sed -i '[email protected]/[email protected]/google_containers@g' deploy/v4.6.0/*.yaml
- 安装nfs csi driver
[root@kube-master csi-driver-nfs]# ./deploy/install-driver.sh v4.6.0 local
use local deploy
Installing NFS CSI driver, version: master ...
serviceaccount/csi-nfs-controller-sa created
serviceaccount/csi-nfs-node-sa created
clusterrole.rbac.authorization.k8s.io/nfs-external-provisioner-role created
clusterrolebinding.rbac.authorization.k8s.io/nfs-csi-provisioner-binding created
csidriver.storage.k8s.io/nfs.csi.k8s.io created
deployment.apps/csi-nfs-controller created
daemonset.apps/csi-nfs-node created
NFS CSI driver installed successfully.
[root@kube-master csi-driver-nfs]#
- 查看nfs csi资源的状态
[root@kube-master csi-driver-nfs]# kubectl -n kube-system get pod -o wide |grep csi-nfs
csi-nfs-controller-6f5f88ff49-8rsrz 4/4 Running 0 4m41s 192.168.76.135 kube-master <none> <none>
csi-nfs-node-xmjgh 3/3 Running 0 4m41s 192.168.76.135 kube-master <none> <none>
- 【可选】卸载nfs csi
./deploy/uninstall-driver.sh v4.6.0 local
快速测试nfs-server及nfs csi可用性
- test-nfs.yaml
---
apiVersion: v1
kind: PersistentVolume
metadata:
annotations:
pv.kubernetes.io/provisioned-by: nfs.csi.k8s.io
name: pv-nginx
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
mountOptions:
- nfsvers=4.1
csi:
driver: nfs.csi.k8s.io
# volumeHandle format: {nfs-server-address}#{sub-dir-name}#{share-name}
# make sure this value is unique for every share in the cluster
volumeHandle: nfs-server.nfs.svc.cluster.local/share##
volumeAttributes:
server: nfs-server.nfs.svc.cluster.local
share: /nfs-vol
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-nginx
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
volumeName: pv-nginx
storageClassName: ""
---
apiVersion: v1
kind: Pod
metadata:
name: nginx-nfs-example
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- mountPath: /var/www
name: pvc-nginx
readOnly: false
volumes:
- name: pvc-nginx
persistentVolumeClaim:
claimName: pvc-nginx
- 创建pv,pvc并测试pod
[root@kube-master nfs]# kubectl apply -f test-nfs.yaml
persistentvolume/pv-nginx created
persistentvolumeclaim/pvc-nginx created
pod/nginx-nfs-example created
[root@kube-master nfs]# kubectl exec -it nginx-nfs-example -- touch /var/www/a.log
[root@kube-master nfs]# ls -l /nfs-vol/
total 0
-rw-r--r-- 1 root root 0 Mar 7 11:09 a.log
[root@kube-master nfs]# rm -f /nfs-vol/a.log
[root@kube-master nfs]# kubectl exec -it nginx-nfs-example -- ls -l /var/www/
total 0
[root@kube-master nfs]#
- 查看nfs挂载信息
[root@kube-master nfs]# kubectl exec nginx-nfs-example -- bash -c "findmnt /var/www -o TARGET,SOURCE,FSTYPE"
TARGET SOURCE FSTYPE
/var/www nfs-server.nfs.svc.cluster.local:/ nfs4
- 删除测试资源
[root@kube-master nfs]# kubectl delete -f test-nfs.yaml
persistentvolume "pv-nginx" deleted
persistentvolumeclaim "pvc-nginx" deleted
pod "nginx-nfs-example" deleted
测试storage class
- 创建storageclass-nfs.yaml
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-sc
provisioner: nfs.csi.k8s.io
parameters:
server: nfs-server.nfs.svc.cluster.local
share: /
reclaimPolicy: Retain
volumeBindingMode: Immediate
mountOptions:
- nfsvers=4.1
- 查看sc状态
[root@kube-master nfs]# kubectl apply -f nfs-sc.yaml
storageclass.storage.k8s.io/nfs-sc created
[root@kube-master nfs]# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-sc nfs.csi.k8s.io Retain Immediate false 10s
- 创建pvc
[root@kube-master nfs]# cat pvc-nginx-log.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-nginx-log
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
storageClassName: nfs-sc
[root@kube-master nfs]# kubectl apply -f pvc-nginx-log.yaml
persistentvolumeclaim/pvc-nginx-log created
- 创建test-nfs-sc.yaml
[root@kube-master nfs]# cat test-nfs-sc.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: nginx-nfs-example
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- mountPath: /var/log/nginx/
name: nginx-log
readOnly: false
volumes:
- name: nginx-log
persistentVolumeClaim:
claimName: pvc-nginx-log
[root@kube-master nfs]# kubectl apply -f test-nfs-sc.yaml
pod/nginx-nfs-example created
[root@kube-master nfs]#
[root@kube-master nfs]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS VOLUMEATTRIBUTESCLASS AGE
pvc-nginx-log Bound pvc-cead6357-bea8-421d-a31b-78a1c715a427 1Gi RWX nfs-sc <unset> 4m24s
[root@kube-master nfs]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-nfs-example 1/1 Running 0 19s 10.244.0.30 kube-master <none> <none>
- 查看nginx日志是否保存到nfs-server
[root@kube-master nfs]# curl 10.244.0.30
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@kube-master nfs]# ls -l /nfs-vol/
total 0
drwxr-xr-x 2 root root 41 Mar 7 11:44 pvc-cead6357-bea8-421d-a31b-78a1c715a427
[root@kube-master nfs]# ls -l /nfs-vol/pvc-cead6357-bea8-421d-a31b-78a1c715a427/
total 4
-rw-r--r-- 1 root root 0 Mar 7 11:44 access.log
-rw-r--r-- 1 root root 515 Mar 7 11:45 error.log
- 删除pod,并新建pod,看nfs-server文件夹的状态
[root@kube-master nfs]# kubectl delete -f test-nfs-sc.yaml
persistentvolumeclaim "pvc-nginx-log" deleted
pod "nginx-nfs-example" deleted
[root@kube-master nfs]# ls -l /nfs-vol/
total 0
drwxr-xr-x 2 root root 41 Mar 7 11:27 pvc-cfa95fab-9cf0-4acd-8699-1a123d218741
[root@kube-master nfs]# ls -l /nfs-vol/pvc-cfa95fab-9cf0-4acd-8699-1a123d218741/
total 8
-rw-r--r-- 1 root root 273 Mar 7 11:31 access.log
-rw-r--r-- 1 root root 1268 Mar 7 11:32 error.log
[root@kube-master nfs]#
[root@kube-master nfs]# kubectl apply -f test-nfs-sc.yaml
pod/nginx-nfs-example created
[root@kube-master nfs]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp1 2/2 Running 4 (3h10m ago) 6d1h 10.244.0.21 kube-master <none> <none>
nginx-nfs-example 1/1 Running 0 117s 10.244.0.31 kube-master <none> <none>
[root@kube-master nfs]#
[root@kube-master nfs]#
[root@kube-master nfs]# curl 10.244.0.31
[root@kube-master nfs]# cat /nfs-vol/pvc-cead6357-bea8-421d-a31b-78a1c715a427/access.log
10.244.0.1 - - [07/Mar/2024:03:45:22 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
10.244.0.1 - - [07/Mar/2024:03:50:00 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
10.244.0.1 - - [07/Mar/2024:03:50:03 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
10.244.0.1 - - [07/Mar/2024:03:50:03 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
10.244.0.1 - - [07/Mar/2024:03:50:04 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
10.244.0.1 - - [07/Mar/2024:03:50:04 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
10.244.0.1 - - [07/Mar/2024:03:50:05 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
10.244.0.1 - - [07/Mar/2024:03:50:06 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"
参考文档
- https://www.cnblogs.com/wangguishe/p/17865625.html
- https://cloud.tencent.com/developer/article/2365976