1. EmptyDir

理解:创建是一个临时的目录,无须指定寄主机上对应的目录文件,k8s会自动去分配目录,但是当pod删除后,里面的数据也会被永久删除。

用途:

  • 作为临时目录
  • 可以作为共享目录,如容器A从容器B中获取数据,须注意容器所在节点,创建时可以通过nodeName: k8s-node-02 指定节点

容器共享的例子:

在一个pod中准备两个容器nginx和busybox,然后声明Volume分别挂载两个容器中,nginx负责将日志写入volume中,busybox负责读取。

vim volume-emptydir.yaml

apiVersion: v1
kind: Pod
metadata:
  name: volume-emptydir
  namespace: default
spec:
  nodeName: longxi-02
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 80
    volumeMounts: #讲logs-volume挂载在nginx容器,对应的目录为/var/log/nginx
    - name: logs-volume
      mountPath: /var/log/nginx
  - name: busybox
    image: busybox:1.30
    command: ["/bin/sh","-c","tail -f /logs/access.log"]
    volumeMounts: #将logs-volune 挂在busybox容器中,对应目录为logs
    - name: logs-volume
      mountPath: /logs
  volumes: #声明volume类型
    - name: logs-volume
      emptyDir: {}

创建pod

[root@longxi-01 ~]# kubectl get pods -o wide

验证

[root@longxi-01 ~]# curl 10.244.1.57
[root@longxi-01 ~]# kubectl logs -f volume-emptydir -c busybox
10.244.0.0 - - [18/Dec/2023:09:26:23 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.61.1" "-"

2. HostPath

不建议生产使用,这里是指定的节点启动了一个,如果没指定节点就会出现问题

满足数据持久化将node主机中的实例目录挂载到Pod中,完成持久化Pod被销毁也不会删除数据,会保留至node节点挂载目录中,但是这种不合适生产环境。

示例

#因为我指定了node节点的所以先在logxi-02节点创建准备映射的目录,也可以不创建由参数type决定
[root@longxi-02 home]# mkdir test

type参数说明:

DirectoryOrCreate:目录存在就使用,不存在就先创建后使用
Directory:目录必须存在
FileOrCreate:文件存在就使用,不存在就先创建后使用
File:文件必须存在
Socket:unix套接字必须存在
CharDevice:字符设备必须存在
BlockDevice:块设备必须存在

yaml文件

apiVersion: v1
kind: Pod
metadata:
  name: volume-hostpath
  namespace: default
spec:
  nodeName: longxi-03
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 80
    volumeMounts:
    - name: logs-volume
      mountPath: /var/log/nginx
  - name: busybox
    image: busybox:1.30
    command: ["/bin/sh","-c","tail -f /logs/access.log"]
    volumeMounts: #将logs-volune 挂在busybox容器中,对应目录为logs
    - name: logs-volume
      mountPath: /logs
  volumes:
    - name: logs-volume
      hostPath:
        path: /home/test
        type: DirectoryOrCreate

创建并测试

[root@longxi-01 ~]# kubectl apply -f  volume-hostpath.yaml
[root@longxi-01 ~]# kubectl get pods -o wide 
NAME                                      READY   STATUS    RESTARTS       AGE     IP             NODE        NOMINATED NODE   READINESS GATES
nfs-client-provisioner-855774bfd4-rp8nh   1/1     Running   10 (15m ago)   5d18h   10.244.0.99    longxi-01   <none>           <none>
volume-hostpath                           2/2     Running   0              113s    10.244.5.195   longxi-03   <none>           <none>

访问一下nginx,查看/home/test/下是否有新增的access.log文件

[root@longxi-01 ~]# curl 10.244.5.195 
[root@longxi-03 test]# ll /home/test/
总用量 4
-rw-r--r-- 1 root root 91 12月 19 14:16 access.log
-rw-r--r-- 1 root root  0 12月 19 14:01 error.log

3. NFS

由于我环境安装了nfs的这里不做演示直接上yaml,具体参考:部署kubesphere管理平台-CSDN博客

默认存储这一块

apiVersion: v1
kind: Pod
metadata:
  name: volume-hostpath
  namespace: default
spec:
  nodeName: longxi-03
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 80
    volumeMounts:
    - name: logs-volume
      mountPath: /var/log/nginx
  - name: busybox
    image: busybox:1.30
    command: ["/bin/sh","-c","tail -f /logs/access.log"]
    volumeMounts: #将logs-volune 挂在busybox容器中,对应目录为logs
    - name: logs-volume
      mountPath: /logs
  volumes:
    - name: logs-volume
      nfs:
       server: 10.211.55.5 #nfs服务器地址
       path: /home/k8s/data #共享的文件路径 

3. PV和PVC

pv 和 pvc这一块实操也不过多解释了,详细使用,请参考部署kubesphere管理平台-CSDN博客

4. 配置存储

4.1. ConfigMap

ConfigMap是一种比较特殊的存储卷,它主要是用来存储配置信息的

例子:

# vim configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap   #configmap名称
data:
#info是配置文件的名称|下面是配置文件的值
 info: |
   username:admin
   password:123456
#创建configmap
[root@longxi-01 ~]# kubectl apply -f configmap.yaml 
configmap/configmap created
[root@longxi-01 ~]# 
[root@longxi-01 ~]# 
#查看是否成功
[root@longxi-01 ~]# kubectl get cm
NAME               DATA   AGE
configmap          1      6s
kube-root-ca.crt   1      32d
[root@longxi-01 ~]# 
[root@longxi-01 ~]# 
#查看configmap详情
[root@longxi-01 ~]# kubectl describe cm configmap
Name:         configmap
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
info:
----
username:admin
password:123456


BinaryData
====

Events:  <none>

创建pod

[root@longxi-01 ~]# vim pod-configmap.yml
apiVersion: v1
kind: Pod
metadata:
  name: pod-configmap
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
    volumeMounts: # 将configmap挂载到目录
    - name: config
      mountPath: /configmap/config
  volumes: #引用configmap
  - name: config
    configMap:
      name: configmap
#创建pod
[root@longxi-01 ~]# kubectl apply  -f pod-configmap.yml 
pod/pod-configmap created
#查看pod
[root@longxi-01 ~]# kubectl get pods -o wide 
NAME                                      READY   STATUS    RESTARTS       AGE     IP             NODE        NOMINATED NODE   READINESS GATES
nfs-client-provisioner-855774bfd4-rp8nh   1/1     Running   10 (87m ago)   5d19h   10.244.0.99    longxi-01   <none>           <none>
pod-configmap                             1/1     Running   0              69s     10.244.5.198   longxi-03   <none>     
#进入容器查看
[root@longxi-01 ~]# kubectl exec -it pod-configmap -- bash
root@pod-configmap:/# 
root@pod-configmap:/# 
root@pod-configmap:/# 
root@pod-configmap:/# cd /configmap/config/
root@pod-configmap:/configmap/config# ls
info
root@pod-configmap:/configmap/config# cat info 
username:admin
password:123456

4.2. Secret

对敏感信息加密用base64对数据转码

[root@longxi-01 ~]# echo -n 'admin' | base64
YWRtaW4=
[root@longxi-01 ~]# echo -n '123456' | base64
MTIzNDU2

创建secret

[root@longxi-01 ~]# vim secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: secret
type: Opaque
data:
  username: YWRtaW4=
  password: MTIzNDU2
  
[root@longxi-01 ~]# kubectl apply -f secret.yaml 
secret/secret created
[root@longxi-01 ~]# kubectl get secrets 
NAME                                 TYPE                                  DATA   AGE
default-token-zhlqr                  kubernetes.io/service-account-token   3      32d
nfs-client-provisioner-token-z46h8   kubernetes.io/service-account-token   3      6d
secret                               Opaque                                2      30s
[root@longxi-01 ~]# 
[root@longxi-01 ~]# kubectl describe secrets secret 
Name:         secret
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
password:  6 bytes
username:  5 bytes

创建pod

[root@longxi-01 ~]# vim pod-secret.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: pod-secret
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
    volumeMounts: # 将secret挂载到目录
    - name: config
      mountPath: /secret/config
  volumes: #引用secret
  - name: config
    secret:
      secretName: secret

#创建pod      
[root@longxi-01 ~]# kubectl apply -f pod-secret.yaml 
pod/pod-secret created
[root@longxi-01 ~]# 
[root@longxi-01 ~]# 
[root@longxi-01 ~]# 
#查看pod
[root@longxi-01 ~]# kubectl get pod pod-secret 
NAME         READY   STATUS              RESTARTS   AGE
pod-secret   1/1     Running               0          11s
#进入容器,查看secret信息,发现已经自动解码了
[root@longxi-01 ~]# kubectl exec -it pod-secret -- bash
root@pod-secret:/# cd secret/config/
root@pod-secret:/secret/config# ls
password  username
root@pod-secret:/secret/config# more password 
123456
root@pod-secret:/secret/config# more username 
admin

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐