网络存储卷:NFS

演示:

安装nfs服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@pool1 k8s_yaml]# yum -y install nfs-utils
[root@pool1 k8s_yaml]# vi /etc/exports
/data/k8s_data *(rw,no_root_squash)
[root@pool1 k8s_yaml]# mkdir /data/k8s_data
[root@pool1 k8s_yaml]# systemctl start nfs
[root@pool1 k8s_yaml]# systemctl enable nfs
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/
# 注:每个Node上都要部署nfs服务;

# 测试Node客户端挂载服务端目录
[root@pool2 ~]# mount -t nfs 172.16.1.20:/data/k8s_data /mnt
……
[root@pool2 ~]# df -hT | grep mnt
172.16.1.20:/data/k8s_data nfs4 50G 3.1G 47G 7% /mnt
[root@pool2 ~]# umount /mnt

部署deployment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
[root@pool1 k8s_yaml]# vi deployment-nfs.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
volumeMounts:
- name: nfs-volume
mountPath: /usr/share/nginx/html
volumes:
- name: nfs-volume
nfs:
server: 172.16.1.20
path: /data/k8s_data

[root@pool1 k8s_yaml]# kubectl apply -f deployment-nfs.yaml
deployment.apps/nginx-deployment created
[root@pool1 k8s_yaml]# kubectl get pods
NAME READY STATUS RESTARTS AGE
hostpath-pod 1/1 Running 0 27m
nginx-deployment-7454fb7b-bhcpn 1/1 Running 0 106s
nginx-deployment-7454fb7b-phhkk 1/1 Running 0 106s
nginx-deployment-7454fb7b-q9qh8 1/1 Running 0 106s
[root@pool1 k8s_yaml]# cat >> /data/k8s_data/index.html << EOF
my is nfs volume!
EOF
[root@pool1 k8s_yaml]# kubectl expose deployment nginx-deployment --port=80 --target-port=80
service/nginx-deployment exposed
[root@pool1 k8s_yaml]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 172.16.1.20:6443 5h44m
nginx-deployment 10.244.206.17:80,10.244.52.197:80,10.244.52.198:80 3s
[root@pool1 k8s_yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5h44m
nginx-deployment ClusterIP 10.108.89.236 <none> 80/TCP 12s
[root@pool1 k8s_yaml]# curl 10.108.89.236
my is nfs volume!