码迷,mamicode.com
首页 > 其他好文 > 详细

19,k8s 之 Volume

时间:2020-07-04 15:25:09      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:网络   done   客户   class   status   directory   index   entry   tail   

本地数据卷的使用: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

标签:网络   done   客户   class   status   directory   index   entry   tail   

原文地址:https://www.cnblogs.com/k8s-pod/p/13234873.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!