19,k8s 之 Volume

Posted k8s-pod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了19,k8s 之 Volume相关的知识,希望对你有一定的参考价值。

本地数据卷的使用:hostPath, emptyDir
emptyDir(容器数据共享)
hostPath(访问宿主机数据)

Kubernetes中的Volume提供了在容器中挂载外部存储的能力
Pod需要设置卷来源(spec.volume)和挂载点(spec.containers.volumeMounts)两个信息后才可以使用相应的Volume

emptyDir(容器数据共享):
	创建一个空卷,挂载到Pod中的容器。Pod删除该卷也会被删除.
	应用场景:Pod中容器之间数据共享

hostPath(访问宿主机数据):
	挂载Node文件系统上文件或者目录到Pod中的容器。
	应用场景:Pod中容器需要访问宿主机文件
	
[root@centos7 ssl]# kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
web-5c987b8447-kptld   1/1     Running   0          6d22h
web-5c987b8447-nwhcd   1/1     Running   0          4d16h
web-5c987b8447-qjpz4   1/1     Running   0          4d16h
[root@centos7 ssl]# 
[root@centos7 ssl]# 
[root@centos7 ssl]# 
[root@centos7 ssl]# cat emptydir.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: my-pod2
spec:
  containers:
  - name: write
    image: centos
    command: ["bash","-c","for i in {1..100};do echo $i >> /data/hello;sleep 1;done"]
    volumeMounts:
      - name: data
        mountPath: /data

  - name: read
    image: centos
    command: ["bash","-c","tail -f /data/hello"]
    volumeMounts:
      - name: data
        mountPath: /data
  
  volumes:
  - name: data
    emptyDir: {}

[root@centos7 ssl]# 
[root@centos7 ssl]# kubectl apply -f emptydir.yaml 
pod/my-pod2 created
[root@centos7 ssl]# kubectl get pods
NAME                   READY   STATUS    RESTARTS   AGE
my-pod2                2/2     Running   0          107s
web-5c987b8447-kptld   1/1     Running   0          6d22h
web-5c987b8447-nwhcd   1/1     Running   0          4d16h
web-5c987b8447-qjpz4   1/1     Running   0          4d16h
[root@centos7 ssl]# kubectl logs my-pod2 -c write
[root@centos7 ssl]# kubectl logs my-pod2 -c read
1
2
3
4
5
6
7
8
9
10
11
12
13
.......



[root@centos7 ssl]# cat hostpath.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: busybox
    image: busybox
    args:
    - /bin/sh
    - -c
    - sleep 36000
    volumeMounts:
    - name: data
      mountPath: /data
  volumes:
  - name: data
    hostPath:
      path: /tmp
      type: Directory
[root@centos7 ssl]# 
[root@centos7 ssl]# 
[root@centos7 ssl]# 
[root@centos7 ssl]# kubectl apply -f hostpath.yaml 
pod/my-pod created
[root@centos7 ssl]# 
[root@centos7 tmp]# kubectl exec -it my-pod sh
/ # 




NFS(网络存储):
NFS服务端: yum install -y nfs-utils 并且启动 systemctl start nfs
Node节点想设置为客户端的话需要如下设置:node节点都安装如下包:yum install -y nfs-utils

[root@centos7 demo]# cat nfs.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        volumeMounts:
        - name: wwwroot
          mountPath: /usr/share/nginx/html
        ports:
        - containerPort: 80
      volumes:
      - name: wwwroot
        nfs:
          server: 192.168.0.11 
          path: /data/nfs
[root@centos7 demo]# 
[root@centos7 demo]# kubectl apply -f nfs.yaml 
deployment.apps/nginx-deployment created
[root@centos7 demo]# 
[root@centos7 demo]# kubectl exec -it nginx-deployment-5bb8c9db86-98vns bash
root@nginx-deployment-5bb8c9db86-98vns:/# ls
bin  boot  dev	docker-entrypoint.d  docker-entrypoint.sh  etc	home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@nginx-deployment-5bb8c9db86-98vns:/# cd /usr/share/nginx/html/
root@nginx-deployment-5bb8c9db86-98vns:/usr/share/nginx/html# ls
root@nginx-deployment-5bb8c9db86-98vns:/usr/share/nginx/html# echo ‘hello world‘ >index.html
root@nginx-deployment-5bb8c9db86-98vns:/usr/share/nginx/html#

  

以上是关于19,k8s 之 Volume的主要内容,如果未能解决你的问题,请参考以下文章

k8s 之volume

Kubernetes(k8s)之Volume(卷)

Kubernetes K8S之存储Volume详解

k8s1.5.4挂载volume之glusterfs

Kubernetes K8S之存储Volume详解

k8s之volumes持久化存储