安装NFS
在master安装NFS
systemctl start nfs-server
修改配置 /etc/exports
/data *(rw,no_root_squash,no_all_squash,sync)
目录为 /data 允许所有地址访问
验证下
[root@master nginx]# showmount -e 192.168.57.61
Export list for 192.168.57.61:
/data *
共享可以正常访问
K8S部署nginx
新建 nginx.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: data
mountPath: /usr/share/nginx/html
volumes:
- name: data
nfs:
path: /data
server: 192.168.57.61
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
ports:
- port: 9000
name: nginx
protocol: TCP
targetPort: 80
nodePort: 31090
selector:
app: nginx
type: NodePort
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv-nfs
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
nfs:
path: /data
server: 192.168.57.61
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: pvc-nfs
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
在deployment 配置中
volumeMounts:
- name: data
mountPath: /usr/share/nginx/html
volumes:
- name: data
nfs:
path: /data
server: 192.168.57.61
这部分代表 容器内的 /usr/share/nginx/html 目录
挂到NFS 的 /data目录
NFS服务器的地址为 192.168.57.61
在Service中
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
ports:
- port: 9000
name: nginx
protocol: TCP
targetPort: 80
nodePort: 31090
selector:
app: nginx
type: NodePort
ports:port:9000 代表pod之间通信的端口为9000
protocol:TCP 代表只允许TCP连接
targetPort:80 代表pod内的80端口
nodePort:31090 代表映射到集群外的31090端口,刚才提到的80端口
运行
kubectl apply -f nginx.yaml
查看pod状态(由于我没有设置namespace 就是默认的命名空间 不用指定 -n )
[root@master nginx]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-5d7bfd6588-6g5mn 1/1 Running 0 48m
nginx-deployment-5d7bfd6588-7sz4p 1/1 Running 0 48m
nginx-deployment-5d7bfd6588-vkbvq 1/1 Running 0 48m
测试
挂载NFS
mount -t nfs -o rw 192.168.57.61:/data /data1
echo "11111" >> /data1/index.html
访问:http://192.168.57.76:31090/
可以看到1111 已经显示在浏览器中了
index.html已经在容器内了