环境:
NFS服务器: 192.168.0.252 /data/nfs

Kubernetes Master: 192.168.0.210
Kubernetes Node: 192.168.0.211
Kubernetes Node: 192.168.0.212

本地我们使用NFS作为Nginx的网站目录,使所有的pod共享一套代码

1.NFS服务器搭建

yum -y install nfs-utils

创建共享目录
mkdir -p /data/nfs

修改配置文件
vim /etc/exports
/data/nfs 192.168.0.0/24(rw,no_root_squash,no_all_squash,sync)

配置选项说明:
/data/nfs 需要共享出去的目录
192.168.0.0/24 只有192.168.0.0网段的机器可以访问这个NFS服务
rw 所有客户机可写
sync 说明所有写入会同时写入硬盘和内存,async就说明会先暂存于内存中,然后写入硬盘
no_root_squash 如果登录的用户是root的话,那么他就具有root的权限
no_all_squash 与no_root_squash相识,如果登录的是user用户,那么也是user权限

systemctl start nfs
systemctl enable nfs

在192.168.0.210挂载测试
mkdir -p /tmp/nfs
mount -t nfs 192.168.0.252:/data/nfs /mnt/nfs

mount: wrong fs type, bad option, bad superblock on 192.168.0.252:/data/nfs,
missing codepage or helper program, or other error
(for several filesystems (e.g. nfs, cifs) you might
need a /sbin/mount.<type> helper program)

   In some cases useful info is found in syslog - try

如果提示这个说明机器没有安装yum -y install nfs-utils
注意:所有kubernetes机器都需要安装nfs-utils

2.kubernetes创建PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
  labels:
    pv: nfs-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  nfs:
    path: /data/nfs
    server: 192.168.0.252

kubernetes创建PVC

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  selector:
    matchLabels:
      pv: nfs-pv

kubernetes创建deployment

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-test
spec:
  replicas: 3
  selector:
    matchLabels:
      name: nginx-test
  template:
    metadata:
      labels:
        name: nginx-test
    spec:
      containers:
      - name: nginx-test
        image: nginx
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-data
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nfs-pvc

创建kubernetes的service

apiVersion: v1
kind: Service
metadata:
  name: nginx-test
  labels:
    name: nginx-test
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: http
    nodePort: 30008
  selector:
    name: nginx-test

因为上面我们已经在192.168.0.210上将nfs挂载到/mnt/nfs目录了,进入目录
echo "hello,nfs for kubernetes">/tmp/nfs/index.html

打开http://192.168.0.210:30008/就显示
hello,nfs for kubernetes