环境信息
三台机器,操作系统CentOS 7.4:
hanyu-210 10.20.0.210
hanyu-211 10.20.0.211
hanyu-212 10.20.0.212
前提条件:
已搭建K8S集群(1个master 2个node节点)
1、搭建nfs(hanyu211节点上搭建)
执行
[root@hanyu-211 ~]# yum -y install nfs-utils rpcbind
NFS 配置
[root@hanyu-211 ~]# mkdir -p /home/nfs
[root@hanyu-211 ~]# cat /etc/exports
/home/nfs *(rw, async)
启动NFS
[root@hanyu-211 ~]# systemctl start rpcbind.service
[root@hanyu-211 ~]# systemctl status rpcbind.service
[root@hanyu-211 ~]# systemctl enable rpcbind.service
[root@hanyu-211 ~]# systemctl start nfs.service
[root@hanyu-211 ~]# systemctl enable nfs.service
[root@hanyu-211 ~]# systemctl status nfs.service
验证nfs可用(hanyu-210 hanyu-212执行)
[root@hanyu-211 ~]# yum -y intall nfs-utils (客户端上不需要启动nfs服务,只是为了使用showmount工具)
检测rpc是否启动
[root@hanyu-211 ~]# showmount -e 10.20.0.211
[root@hanyu-211 ~]# mount -t nfs 10.20.0.211:/home/nfs /mnt (挂载至本地/mnt目录)
[root@hanyu-211 ~]# df -h
[root@hanyu-211 ~]# umount /mnt
2、K8S中使用nfs作为存储卷
直接将nfs作为存储卷使用:kubectl appply -f redis-deployment.yaml
[root@hanyu-210 k8s_nfs]# cat redis-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis
spec:
selector:
matchLabels:
app: redis
revisionHistoryLimit: 2
template:
metadata:
labels:
app: redis
spec:
containers:
- image: redis
name: redis
imagePullPolicy: IfNotPresent
ports:
- containerPort: 6379
name: redis6379
env:
- name: ALLOW_EMPTY_PASSWORD
value: "yes"
- name: REDIS_PASSWORD
value: "redis"
volumeMounts:
- name: redis-persistent-storage
mountPath: /data
volumes:
- name: redis-persistent-storage
nfs:
path: /home/nfs
server: 10.20.0.211
image.png
3、K8S中使用nfs结合pv使用
4、K8S中使用nfs结合storageClass动态供给使用
[root@hanyu-210 k8s_nfs]# cat nfs-provisioner-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-provisioner
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-provisioner
template:
metadata:
labels:
app: nfs-provisioner
spec:
serviceAccount: nfs-provisioner
containers:
- name: nfs-provisioner
image: quay.io/kubernetes_incubator/nfs-provisioner:v1.0.8
ports:
- name: nfs
containerPort: 2049
- name: mountd
containerPort: 20048
- name: rpcbind
containerPort: 111
- name: rpcbind-udp
containerPort: 111
protocol: UDP
securityContext:
capabilities:
add:
- DAC_READ_SEARCH
- SYS_RESOURCE
args:
# 定义提供者的名称,存储类通过此名称指定提供者
- "-provisioner=nfs-provisioner"
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: SERVICE_NAME
value: nfs-provisioner
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
imagePullPolicy: "IfNotPresent"
volumeMounts:
- name: export-volume
mountPath: /export
volumes:
- name: export-volume
hostPath:
path: /srv
[root@hanyu-210 k8s_nfs]# cat nfs-provisioner-service.yaml
kind: Service
apiVersion: v1
metadata:
name: nfs-provisioner
labels:
app: nfs-provisioner
spec:
ports:
- name: nfs
port: 2049
- name: mountd
port: 20048
- name: rpcbind
port: 111
- name: rpcbind-udp
port: 111
protocol: UDP
selector:
app: nfs-provisioner
创建存储类:kubectl apply -f nfs-storageclass.yaml
[root@hanyu-210 k8s_nfs]# cat nfs-storageclass.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-storageclass
provisioner: nfs-provisioner
创建pvc:kubectl apply -f nfs-pvc.yaml
[root@hanyu-210 k8s_nfs]# cat nfs-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany
storageClassName: nfs-storageclass
resources:
requests:
storage: 1Mi
创建应用使用nfs-pvc:kubectl apply -f busybox-deployment.yaml
[root@hanyu-210 k8s_nfs]# cat busybox-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: busybox-deployment
spec:
replicas: 2
selector:
matchLabels:
name: busybox-deployment
template:
metadata:
labels:
name: busybox-deployment
spec:
serviceAccount: nfs-provisioner
containers:
- image: busybox
command:
- sh
- -c
- 'while true; do date > /mnt/index.html; hostname >> /mnt/index.html; sleep $(($RANDOM % 5 + 5)); done'
imagePullPolicy: IfNotPresent
name: busybox
volumeMounts:
- name: nfs
mountPath: /mnt
volumes:
- name: nfs
persistentVolumeClaim:
claimName: nfs-pvc
网友评论