1:环境准备:NFS
NFS是Network File System的简写,即网络文件系统。NFS基于RPC(Remote Procedure Call)远程过程调用实现。由操作系统的内核,将 NFS 文件系统的调用请求通过 TCP/IP 发送至服务端的 NFS 服务。NFS服务器执行相关的操作,并将操作结果返回给客户端
- no_root_squash:客户端连接服务端时使用的是root,拥有服务端分享的目录的root权限
- sync:同步写操作,数据写入存储设备后返回成功信息(默认)
- async:异步写操作,数据在未完全写入存储设备前就返回成功信息,实际还在内存
#所有机器安装nfs
[root@k8s-master ~]# yum install -y nfs-utils
# 在主节点执行,即暴露/nfs/data目录,所有机器都弄访问
[root@k8s-master ~]# echo "/nfs/data/ *(insecure,rw,sync,no_root_squash)" > /etc/exports
[root@k8s-master ~]# mkdir -p /nfs/data/
# 在主节点执行,绑定并开启nfs服务
[root@k8s-master ~]# systemctl enable rpcbind --now
[root@k8s-master ~]# systemctl enable nfs-server --now
Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.
# 在主节点执行,配置生效,检查/nfs/data目录是否暴露
[root@k8s-master ~]# exportfs -r
[root@k8s-master ~]# exportfs
/nfs/data <world>
# 所有从节点,检查nfs服务暴露那些文件
[root@k8s-node1 ~]# showmount -e 192.168.0.136
Export list for 192.168.0.136:
/nfs/data *
[root@k8s-node1 ~]# mkdir -p /nfs/data
#执行以下命令挂载 nfs 服务器上的共享目录到本机路径 (nfs第一个位服务端目录,第二个位本机目录)
[root@k8s-node1 ~]# mount -t nfs 192.168.0.136:/nfs/data /nfs/data
#测试,主节点创建文件
[root@k8s-master ~]# cd /nfs/data/
[root@k8s-master data]# ls
[root@k8s-master data]# echo 1111 > a
[root@k8s-master data]# ll
total 4
-rw-r--r-- 1 root root 5 Oct 17 18:02 a
[root@k8s-master data]#
#从节点检查是否同步
[root@k8s-node1 ~]# ls /nfs/data/
a
# 任意一台机器改变都能同步
2:原生挂载方式
[root@k8s-master ~]# vim deploy.yaml
[root@k8s-master ~]# mkdir /nfs/data/nginx-pv
[root@k8s-master ~]# kubectl apply -f deploy.yaml
deployment.apps/nginx-pv-demo created
# deploy.yaml内容
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-pv-demo
name: nginx-pv-demo
spec:
replicas: 2
selector:
matchLabels:
app: nginx-pv-demo
template:
metadata:
labels:
app: nginx-pv-demo
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html #pod里面目录
volumes:
- name: html
nfs:
server: 172.31.0.4 #nfs服务器ip
path: /nfs/data/nginx-pv #nfs服务器挂载路径
缺点:1:删除pod,挂载目录还存在
2:需要自己创建目录
3:使用资源没有现在
3:PV&PVC
PV:持久卷(Persistent Volume),将应用需要持久化的数据保存到指定位置
PVC:持久卷申明(Persistent Volume Claim),申明需要使用的持久卷规格
3.1:创建pv池
事先创建好多个不同大小挂载空间,然后客户端申请即可用
#nfs主节点
[root@k8s-master data]# mkdir -p /nfs/data/01
[root@k8s-master data]# mkdir -p /nfs/data/02
[root@k8s-master data]# mkdir -p /nfs/data/03
[root@k8s-master ~]# vim pv.yaml
[root@k8s-master ~]# kubectl apply -f pv.yaml
persistentvolume/pv01-10m created
persistentvolume/pv02-1gi created
persistentvolume/pv03-3gi created
[root@k8s-master ~]# kubectl get persistentvolume
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv01-10m 10M RWX Retain Available nfs 71s
pv02-1gi 1Gi RWX Retain Available nfs 71s
pv03-3gi 3Gi RWX Retain Available nfs 71s
#配置文件
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv01-10m
spec:
capacity:
storage: 10M
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data/01
server: 172.31.0.4
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv02-1gi
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data/02
server: 172.31.0.4
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv03-3gi
spec:
capacity:
storage: 3Gi
accessModes:
- ReadWriteMany
storageClassName: nfs
nfs:
path: /nfs/data/03
server: 172.31.0.4
3.2:创建pvc
[root@k8s-master ~]# vim pvc.yaml
[root@k8s-master ~]# kubectl apply -f pvc.yaml
persistentvolumeclaim/nginx-pvc created
[root@k8s-master ~]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv01-10m 10M RWX Retain Available nfs 8m33s
pv02-1gi 1Gi RWX Retain Bound default/nginx-pvc nfs 8m33s
pv03-3gi 3Gi RWX Retain Available nfs 8m33s
# pvc.yaml内容
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
storageClassName: nfs
部署时创建pvc挂载
[root@k8s-master ~]# vim dep_pvc.yaml
[root@k8s-master ~]# kubectl apply -f dep_pvc.yaml
deployment.apps/nginx-deploy-pvc created
[root@k8s-master ~]# kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/nginx-pvc Bound pv02-1gi 1Gi RWX nfs 6m47s
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pv01-10m 10M RWX Retain Available nfs 15m
persistentvolume/pv02-1gi 1Gi RWX Retain Bound default/nginx-pvc nfs 15m
persistentvolume/pv03-3gi 3Gi RWX Retain Available nfs 15m
#dep_pvc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx-deploy-pvc
name: nginx-deploy-pvc
spec:
replicas: 2
selector:
matchLabels:
app: nginx-deploy-pvc
template:
metadata:
labels:
app: nginx-deploy-pvc
spec:
containers:
- image: nginx
name: nginx
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
volumes:
- name: html
persistentVolumeClaim:
claimName: nginx-pvc
3:ConfigMap
抽取应用配置,并且可以自动更新
[root@k8s-master ~]# vim redis.conf
[root@k8s-master ~]# kubectl create cm redis.conf --from-file /root/redis.conf
configmap/redis.conf created
[root@k8s-master ~]# kubectl get cm
NAME DATA AGE
kube-root-ca.crt 1 7d6h
redis.conf 1 10s
[root@k8s-master ~]# rm -rf redis.conf
# redis.yaml内容
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis
command:
- redis-server
- "/redis-master/redis.conf" #指的是redis容器内部的位置
ports:
- containerPort: 6379
volumeMounts:
- mountPath: /data
name: data
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: redis-conf
items:
- key: redis.conf
path: redis.conf