k8s之nfs
2018-11-12 本文已影响0人
xiao_b4b1
NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TCP/IP网络共享资源。
环境
角色 | 节点 |
---|---|
k8s集群 | master-192,node-193,node-194(已部署) |
NFS服务器 | master-192 |
nfs server安装
[root@master-192 nfs]#yum install -y nfs-utils
[root@master-192 nfs]# cat /etc/exports
/data/nfs *(rw,all_squash,anonuid=65534,anongid=65534)
[root@master-192 nfs]# systemctl restart nfs-server
[root@master-192 nfs]# systemctl enable nfs-server
nfs client安装
[root@node-193 ~]# yum install -y nfs-utils
验证
[root@master-192 nfs]# mount -t nfs 172.30.81.192:/data/nfs /mnt
k8s使用nfs
1.创建pv,pv是没有namspace概念的
[root@master-192 nfs]# kubectl create -f pv.yaml
[root@master-192 nfs]# cat pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
namespace: storage
spec:
capacity:
storage: 100Mi
accessModes:
- ReadWriteMany
nfs:
# FIXME: use the right IP
server: 172.30.81.192
path: "/data/nfs"
2.创建pvc
[root@master-192 nfs]# kubectl create -f pvc.yaml
[root@master-192 nfs]# cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
namespace: default
spec:
accessModes:
- ReadWriteMany
storageClassName: ""
resources:
requests:
storage: 90Mi
3.验证
[root@master-192 nfs]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
nfs-pv 100Mi RWX Retain Bound default/nfs-pvc 11s
[root@master-192 nfs]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nfs-pvc Bound nfs-pv 100Mi RWX 6s
4.使用pvc
[root@master-192 nfs]# kubectl create -f deploy.yaml
[root@master-192 nfs]# cat deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test1
spec:
replicas: 1
template:
metadata:
labels:
app: test1
spec:
containers:
- name: test1
image: nginx
volumeMounts:
- name: nfs
mountPath: /usr/share/nginx/html
volumes:
- name: nfs
persistentVolumeClaim:
claimName: nfs-pvc
nodeSelector:
kubernetes.io/hostname: node-193
---
apiVersion: v1
kind: Service
metadata:
labels:
app: test1
name: test1
namespace: default
spec:
selector:
app: test1
ports:
- port: 80
type: NodePort
clusterIP: 10.96.76.104