k8s三种存储卷

一、存储卷

1.1、emptyDir存储卷

当pod被分配给节点时,首先创建emptyDir卷,并且只要该Pod在该节点上运行,该卷就会存在。正如卷的名字所述,它最初是空的。Pod中的容器可以读取和写入emptyDir卷中的相同文件,尽管该卷可以挂载到每个容器中的相同或不同路径上。当出于任何原因从节点中删除 pod时,emptyDir中的数据将被永久删除

vim demo1.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  containers:
  - name: nginx1
    image: nginx
    ports:
    - name: http
      containerPort: 80
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html/
  - name: nginx2
    image: nginx
    volumeMounts:
    - name: html
      mountPath: /data/
    command: ['/bin/bash','-c','while true;do echo $(date) >> /data/index.html;sleep 10;done']
volumes:
- name: html
  emptyDir: {}

kubectl apply -f demo3.yaml

kubectl get pods -o wide

kubectl exec -it nginx -c nginx2 sh  #进去nginx2容器
cat /data/index.html

kubectl exec -it nginx -c nginx1 sh  #进去nginx1容器
cat /usr/share/nginx/html/index.html

1.2 hostPath存储卷

hostpath卷将 node节点的文件系统中的文件或目录挂载到集群中。hostPath可以实现持久存储,但是在node节点故障时,也会导致数据的丢失

vim demo4.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx3
    labels:
      app: nginx3
spec:
  containers:
  - name: nginx1
    image: nginx
    ports:
    - name: http
      containerPort: 80
    volumeMounts:        
    - name: html    ##使用的存储卷名称,如果跟下面volume字段name值相同,则表示使用volume的这个存储卷
      mountPath: /usr/share/nginx/html/   ##挂载至容器中哪个目录
      readOnly: false     #读写挂载方式,默认为读写模式false
  volumes: 
  - name: html     #存储卷名称
    hostPath:      
    path: /data/pod/volume   #在宿主机上目录的路径
    type: DirectoryOrCreate  #定义类型,这表示如果宿主机没有此目录则会自动创建

kubectl apply -f demo2.yaml

#查看pod在哪个node节点创建,可以用ip访问测试
kubectl get pods -o wide 

#在node节点上自动创建的目录中写入内容
echo '111111' > /data/pod/volume/index.html

#访问测试
curl http://IP

#主节点写入内容测试共享
kubectl exec -it nginx3 -c nginx1 sh #进入容器
echo '222222' >> /usr/share/nginx/html/index.html
#再退出容器测试node02节点:curl http://IP

1.3、nfs共享存储卷

1.3.1、开启nfs服务器

#设置主机名
hostnamectl set-hostname nfs

#关闭防火墙及SElinux
systemctl stop firewalld
setenforce 0

#创建共享目录并给权限
mkdir -p /data/volume
chmod 777 /data/volume

#安装nfs,并配置nfs服务
yum install -y rpcbind nfs-utils

vim /etc/exports
/data/volume 192.168.80.0/24(rw,sync,no_root_squash)

#启动nfs服务并查看本机共享目录
systemctl start rpcbind
systemctl start nfs

exportfs -rv
showmount -e

#在共享目录写入网页文件
echo '111111' > /data/volume/index.html

1.3.2、节点配置nfs映射

echo '192.1268.80.14 nfs' > /etc/hosts

1.3.3、编写yaml文件

vim demo5.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx3
  labels:
    app: nginx3
spec:
  containers:
  - name: nginx3
    image: nginx
    ports:
    - name: http
      containerPort: 80
    volumeMounts:        
    - name: html    ##使用的存储卷名称,如果跟下面volume字段name值相同,则表示使用volume的这个存储卷
      mountPath: /usr/share/nginx/html/   ##挂载至容器中哪个目录
      readOnly: false     #读写挂载方式,默认为读写模式false
volumes: 
- name: html     #存储卷名称
  nfs:      
    path: /data/volume   #在宿主机上目录的路径
    server: nfs

kubectl apply -f demo5.yaml
kubectl get pods -o wide
curl http://IP  #调度的node节点的ip

posted @   wuwukai  阅读(651)  评论(0)    收藏  举报
点击右上角即可分享
微信分享提示