【云原生 | Kubernetes 系列】— Kubernetes存储方案

2024/1/5 18:02:30

本文主要是介绍【云原生 | Kubernetes 系列】— Kubernetes存储方案,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • 【云原生 | Kubernetes 系列】— Kubernetes存储方案
    • 🍇 一、基本存储
      • EmptyDir
      • HostPath
      • NFS
        • 搭建nfs服务器
    • 🍋 二、高级存储
      • PV和PVC
      • pv
      • pvc
      • 演示 -NFS
      • StorageClass(动态存储)
        • StorageClass准备
        • 创建deployment应用StorageClass
        • 创建StatefulSet应用StorageClass
    • 🥭三、配置存储
      • ConfigMap
        • ConfigMap 的创建
        • Pod 中使用 ConfigMap
      • Secret
        • Service Account
        • Opaque Secret
        • kubernetes.io/dockerconfigjson

【云原生 | Kubernetes 系列】— Kubernetes存储方案

版权 本文为云录原创文章,转载无需和我联系,但请注明来自云录 https://www.yunzhuan.site

🍇 一、基本存储

EmptyDir

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

用途:
1: 缓存空间,例如基于磁盘的归并排序。
2: 为耗时较长的计算任务提供检查点,以便任务能方便地从崩溃前状态恢复执行。
3: 在 Web 服务器容器服务数据时,保存内容管理器容器获取的文件。

apiVersion: v1
kind: Pod
metadata:
  name: volume-emptydir
spec:
  containers:
    - name: nginx
      image: nginx:latest
      ports:
        - name: nginx-port
          containerPort: 80
          protocol: TCP
      volumeMounts:    # 将logs-volume挂载到nginx容器的/var/log/nginx目录, nginx会将用户的访问日志写入到该目录的access.log文件中
        - name: logs-volume
          mountPath: /var/log/nginx
    - name: busybox
      image: busybox:latest
      command: ["/bin/sh", "-c", "tail -f /logs/access.log"]
      volumeMounts:    # 将logs-volume挂载到busybox容器中的/logs目录
        - name: logs-volume
          mountPath: /logs
  volumes:    # 声明volume
    - name: logs-volume
      emptyDir:
        sizeLimit: 500Mi
[root@k8s-master01 nfs]# kubectl   get po  -o wide
NAME              READY   STATUS    RESTARTS      AGE   IP           NODE        NOMINATED NODE   READINESS GATES
volume-emptydir   2/2     Running   2 (47s ago)   75s   10.244.1.7   k8s-node1   <none>           <none>

[root@k8s-master01 ~]# curl 10.244.1.7
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

[root@k8s-master01 ~]# kubectl logs volume-emptydir -c busybox -f
10.244.0.0 - - [01/Jan/2024:05:20:38 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"


HostPath

警告:
HostPath 卷存在许多安全风险,最佳做法是尽可能避免使用 HostPath。 当必须使用 HostPath 卷时,它的范围应仅限于所需的文件或目录,并以只读方式挂载。
如果通过 AdmissionPolicy 限制 HostPath 对特定目录的访问,则必须要求 volumeMounts 使用 readOnly 挂载以使策略生效。
hostPath 卷能将主机节点文件系统上的文件或目录挂载到你的 Pod 中。 虽然这不是大多数 Pod 需要的,
但是它为一些应用程序提供了强大的逃生舱。

例如,hostPath 的一些用法有:

  • 运行一个需要访问 Docker 内部机制的容器;可使用 hostPath 挂载 /var/lib/docker 路径。

  • 在容器中运行 cAdvisor 时,以 hostPath 方式挂载 /sys。

  • 允许 Pod 指定给定的 hostPath 在运行 Pod 之前是否应该存在,是否应该创建以及应该以什么方式存在。
    除了必需的 path 属性之外,你可以选择性地为 hostPath 卷指定 type。

    取值 行为
    空字符串(默认)用于向后兼容,这意味着在安装 hostPath 卷之前不会执行任何检查。
    DirectoryOrCreate 如果在给定路径上什么都不存在,那么将根据需要创建空目录,权限设置为 0755,具有与 kubelet 相同的组和属主信息
    Directory 在给定路径上必须存在的目录。
    FileOrCreate 如果在给定路径上什么都不存在,那么将在那里根据需要创建空文件,权限设置为 0644,具有与 kubelet 相同的组和所有权。
    File 在给定路径上必须存在的文件。
    Socket 在给定路径上必须存在的 UNIX 套接字。
    CharDevice 在给定路径上必须存在的字符设备。
    BlockDevice 在给定路径上必须存在的块设备。
apiVersion: v1
kind: Pod
metadata:
  name: test-webserver
spec:
  containers:
    - name: test-webserver
      image: beicheng1892/test-webserver:latest
      volumeMounts:
        - mountPath: /var/local/aaa
          name: mydir
        - mountPath: /var/local/aaa/1.txt
          name: myfile
  volumes:
    - name: mydir
      hostPath:
        # 确保文件所在目录成功创建。
        path: /var/local/aaa
        type: DirectoryOrCreate
    - name: myfile
      hostPath:
        path: /var/local/aaa/1.txt
        type: FileOrCreate
-rw-r--r-- 1 root root 499 Dec 31 21:40 hostpath.yaml
[root@k8s-master01 nfs]# kubectl   apply   -f hostpath.yaml 
pod/test-webserver created
[root@k8s-master01 nfs]# kubectl  get po -o wide
NAME              READY   STATUS    RESTARTS   AGE    IP           NODE        NOMINATED NODE   READINESS GATES
test-webserver    1/1     Running   0          10s    10.244.1.8   k8s-node1   <none>           <none>
volume-emptydir   2/2     Running   0          3h6m   10.244.1.7   k8s-node1   <none>           <none>
[root@k8s-node1 ~]# cd /var/local/
[root@k8s-node1 local]# ll
total 0
drwxr-xr-x 2 root root 19 Dec 31 21:40 aaa
[root@k8s-node1 local]# cd aaa/
[root@k8s-node1 aaa]# ll
total 0
-rw-r--r-- 1 root root 0 Dec 31 21:40 1.txt

NFS

nfs 卷能将 NFS (网络文件系统) 挂载到你的 Pod 中。 不像 emptyDir 那样会在删除 Pod 的同时也会被删除,
nfs 卷的内容在删除 Pod 时会被保存,卷只是被卸载。 这意味着 nfs 卷可以被预先填充数据,并且这些数据可以在 Pod 之间共享。

搭建nfs服务器

1. 安装NFS服务
[root@k8s-node3 ~]# yum -y install rpcbind nfs-utils
2. 准备共享目录
[root@k8s-node3 ~]# mkdir -p /data/nfs
[root@k8s-node3 ~]# chmod 777 -R /data/nfs
3. 将共享目录以读写权限暴露给192.168.0.0/255网段中的所有主机
[root@k8s-node3 ~]# cat /etc/exports
[root@k8s-node3 ~]# vim /etc/exports
[root@k8s-node3 nfs]# cat  /etc/exports
/data/nfs/   *(rw,sync,all_squash)
# 启动服务
systemctl start rpcbind && systemctl start nfs
# 设置开机启动
systemctl enable rpcbind && systemctl enable nfs
[root@k8s-node3 ~]# systemctl start rpcbind && systemctl start nfs
[root@k8s-node3 ~]# systemctl enable rpcbind && systemctl enable nfs
其中no_root_squash表示:NFS客户端连接服务端时如果使用的是root的话,那么对服务端共享目录也拥有root权限
# 客户端安装步骤
yum -y install rpcbind
# 检测
showmount -e 192.168.0.105

apiVersion: v1
kind: Pod
metadata:
  name: test-pd
spec:
  containers:
    - image: beicheng1892/test-webserver:latest
      name: test-container
      volumeMounts:
        - mountPath: /my-nfs-data
          name: test-volume
  volumes:
    - name: test-volume
      nfs:
        server: 192.168.0.105
        path: /data/nfs
        readOnly: true
[root@k8s-master01 nfs]# kubectl   apply   -f  nfs.yaml 
pod/test-pd created

[root@k8s-master01 nfs]# kubectl   get po   -o wide
NAME              READY   STATUS    RESTARTS   AGE     IP           NODE        NOMINATED NODE   READINESS GATES
test-pd           1/1     Running   0          104s    10.244.2.6   k8s-node2   <none>           <none>
test-webserver    1/1     Running   0          26m     10.244.1.8   k8s-node1   <none>           <none>
volume-emptydir   2/2     Running   0          3h32m   10.244.1.7   k8s-node1   <none>           <none>

🍋 二、高级存储

PV和PVC

  • PersistentVolume (PV)
    是由管理员设置的存储,它是群集的一部分。就像节点是集群中的资源一样,PV 也是集群中的资源。 PV 是
Volume 之类的卷插件,但具有独立于使用 PV 的 Pod 的生命周期。此 API 对象包含存储实现的细节,即 NFS、
iSCSI 或特定于云供应商的存储系统
  • PersistentVolumeClaim (PVC)
    是用户存储的请求。它与 Pod 相似。Pod 消耗节点资源,PVC 消耗 PV 资源。Pod 可以请求特定级别的资源
(CPU 和内存)。声明可以请求特定的大小和访问模式(例如,可以以读/写一次或 只读多次模式挂载)
  • 静态 pv
集群管理员创建一些 PV。它们带有可供群集用户使用的实际存储的细节。它们存在于 Kubernetes API 中,可用
于消费
  • 动态
    当管理员创建的静态 PV 都不匹配用户的 PersistentVolumeClaim 时,集群可能会尝试动态地为 PVC 创建卷。此
配置基于 StorageClasses :PVC 必须请求 [存储类],并且管理员必须创建并配置该类才能进行动态创建。声明该
类为 "" 可以有效地禁用其动态配置
要启用基于存储级别的动态存储配置,集群管理员需要启用 API server 上的 DefaultStorageClass [准入控制器]
。例如,通过确保 DefaultStorageClass 位于 API server 组件的 --admission-control 标志,使用逗号分隔的
有序值列表中,可以完成此操作
  • 绑定
    master 中的控制环路监视新的 PVC,寻找匹配的 PV(如果可能),并将它们绑定在一起。如果为新的 PVC 动态
调配 PV,则该环路将始终将该 PV 绑定到 PVC。否则,用户总会得到他们所请求的存储,但是容量可能超出要求
的数量。一旦 PV 和 PVC 绑定后, PersistentVolumeClaim 绑定是排他性的,不管它们是如何绑定的。 PVC 跟
PV 绑定是一对一的映射
  • 持久化卷声明的保护
PVC 保护的目的是确保由 pod 正在使用的 PVC 不会从系统中移除,因为如果被移除的话可能会导致数据丢失
当启用PVC 保护 alpha 功能时,如果用户删除了一个 pod 正在使用的 PVC,则该 PVC 不会被立即删除。PVC 的
删除将被推迟,直到 PVC 不再被任何 pod 使用
  • 持久化卷类型
PersistentVolume 类型以插件形式实现。Kubernetes 目前支持以下插件类型:
GCEPersistentDisk AWSElasticBlockStore AzureFile AzureDisk FC (Fibre Channel)
FlexVolume Flocker NFS iSCSI RBD (Ceph Block Device) CephFS
Cinder (OpenStack block storage) Glusterfs VsphereVolume Quobyte Volumes
HostPath VMware Photon Portworx Volumes ScaleIO Volumes StorageOS

pv

apiVersion: v1  
kind: PersistentVolume
metadata:
  name: pv2
spec:
  nfs: # 存储类型,与底层真正存储对应
  capacity:  # 存储能力,目前只支持存储空间的设置
    storage: 2Gi
  accessModes:  # 访问模式
  storageClassName: # 存储类别
  persistentVolumeReclaimPolicy: # 回收策略

  • 回收策略(persistentVolumeReclaimPolicy)
    当PV不再被使用了之后,对其的处理方式。目前支持三种策略:

    • Retain (保留) 保留数据,需要管理员手工清理数据
    • Recycle(回收) 清除 PV 中的数据,效果相当于执行 rm -rf /thevolume/*(1.28 废弃了)
    • Delete (删除) 与 PV 相连的后端存储完成 volume 的删除操作,当然这常见于云服务商的存储服务
  • 访问模式(accessModes)
    用于描述用户应用对存储资源的访问权限,访问权限包括下面几种方式:

    • ReadWriteOnce(RWO):读写权限,但是只能被单个节点挂载
    • ReadOnlyMany(ROX): 只读权限,可以被多个节点挂载
    • ReadWriteMany(RWX):读写权限,可以被多个节点挂载
  • 状态
    卷可以处于以下的某种状态:

    • Available(可用)——一块空闲资源还没有被任何声明绑定
    • Bound(已绑定)——卷已经被声明绑定
    • Released(已释放)——声明被删除,但是资源还未被集群重新声明

pvc

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc
  namespace: dev
spec:
  accessModes: # 访问模式
  selector: # 采用标签对PV选择
  storageClassName: # 存储类别
  resources: # 请求空间
    requests:
      storage: 5Gi


PVC 的关键配置参数说明:

  • 访问模式(accessModes)
    ​ 用于描述用户应用对存储资源的访问权限

  • 选择条件(selector)
    通过Label Selector的设置,可使PVC对于系统中己存在的PV进行筛选

  • 存储类别(storageClassName) PVC在定义时可以设定需要的后端存储的类别,只有设置了该class的pv才能被系统选出

  • 资源请求(Resources ) 描述对存储资源的请求

演示 -NFS

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  nfs:
    path: /data/nfs
    server: 192.168.0.105
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
---
apiVersion: v1
kind: Pod
metadata:
  name: test-web
spec:
  containers:
    - name: nginx
      image: nginx:1.21.0
      imagePullPolicy: IfNotPresent
      ports:
        - containerPort: 80
      volumeMounts:
        - name: html
          mountPath: /usr/share/nginx/html
  volumes:
    - name: html
      persistentVolumeClaim:
        claimName: test-pvc

StorageClass(动态存储)

StorageClass准备

---
# sc安装
apiVersion: v1
kind: ServiceAccount
metadata:
  name: nfs-client-provisioner

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nfs-client-provisioner-runner
rules:
  - apiGroups: [""]
    resources: ["persistentvolumes"]
    verbs: ["get", "list", "watch", "create", "delete"]
  - apiGroups: [""]
    resources: ["persistentvolumeclaims"]
    verbs: ["get", "list", "watch", "update"]
  - apiGroups: ["storage.k8s.io"]
    resources: ["storageclasses"]
    verbs: ["get", "list", "watch"]
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["list", "watch", "create", "update", "patch"]
  - apiGroups: [""]
    resources: ["endpoints"]
    verbs: ["create", "delete", "get", "list", "watch", "patch", "update"]

---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: run-nfs-client-provisioner
subjects:
  - kind: ServiceAccount
    name: nfs-client-provisioner
    namespace: default
roleRef:
  kind: ClusterRole
  name: nfs-client-provisioner-runner
  apiGroup: rbac.authorization.k8s.io

---
# nfs client
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nfs-client-provisioner
spec:
  selector:
    matchLabels:
      app: nfs-client-provisioner
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nfs-client-provisioner
    spec:
      #serviceAccountName的 nfs-client-provisioner 名称是 ServiceAccount 名称
      serviceAccountName: nfs-client-provisioner
      containers:
        - name: nfs-client-provisioner
          #image: quay.io/external_storage/nfs-client-provisioner:latest
          #这里特别注意,在k8s-1.20以后版本中使用上面提供的包,并不好用,这里我折腾了好久,才解决,后来在官方的github上,别人提的问题中建议使用下面这个包才解决的,我这里是下载后,传到我自已的仓库里
          image: gmoney23/nfs-client-provisioner:latest
          volumeMounts:
            - name: nfs-client-root
              mountPath: /persistentvolumes
          env:
            - name: PROVISIONER_NAME
              value: fuseim.pri/ifs  # 指定分配器的名称,创建storageclass会用到
            - name: NFS_SERVER
              value: 192.168.0.105
            - name: NFS_PATH
              value: /data/nfs
      volumes:
        - name: nfs-client-root
          nfs:
            server: 192.168.0.105
            path: /data/nfs

---
# nfs 存储类
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nfs-storage
provisioner: fuseim.pri/ifs  #必须与provisioner.yaml中PROVISIONER_NAME的值一致



[root@k8s-master01 nfs]# kubectl get deploy nfs-client-provisioner
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
nfs-client-provisioner   1/1     1            1           102s
[root@k8s-master01 nfs]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-54b55f79fd-qspkr   1/1     Running   0          108s
[root@k8s-master01 nfs]# kubectl get sc
NAME          PROVISIONER      RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-storage   fuseim.pri/ifs   Delete          Immediate           false                  115s

创建deployment应用StorageClass

---
# 手动创建pvpvc
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: test-pvc
spec:
  storageClassName: nfs-storage
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
---
#创建deployment应用NFS
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-1
spec:
  selector:
    matchLabels:
      app: web-1
  replicas: 2
  template:
    metadata:
      labels:
        app: web-1
    spec:
      containers:
        - name: web-1
          image: nginx:latest
          ports:
            - containerPort: 80
          volumeMounts:
            - name: test-storage
              mountPath: /data/html
      volumes:
        - name: test-storage
          persistentVolumeClaim:
            #claimName 的 名称 是 PersistentVolumeClaim 的名字
            claimName: test-pvc
[root@k8s-master01 nfs]# kubectl get  po 
NAME                                    READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-876986df-j5c74   1/1     Running   0          118s
web-1-75685c5697-d2qsw                  1/1     Running   0          13m
web-1-75685c5697-xftfd                  1/1     Running   0          13m

[root@k8s-node3 data]# cd nfs/
[root@k8s-node3 nfs]# ll
total 4
drwxrwxrwx 2 nfsnobody nfsnobody  6 Jan  4 21:42 default-test-pvc-pvc-7c40ad07-b15f-42c6-a551-572b0bff8542
-rw-r--r-- 1 nfsnobody nfsnobody 17 Jan  4 01:39 test.txt
[root@k8s-node3 nfs]# ped
bash: ped: command not found...
[root@k8s-node3 nfs]# pwd
/data/nfs

创建StatefulSet应用StorageClass

--- #服务
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx
  serviceName: "nginx"
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
          - containerPort: 80
            name: web
          volumeMounts:
          - name: www
            mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "nfs-storage"
      resources:
        requests:
          storage: 1Gi




[root@k8s-master01 nfs]# kubectl get  sts 
NAME   READY   AGE
web    1/3     8s
[root@k8s-master01 nfs]# kubectl get  po 
NAME                                    READY   STATUS              RESTARTS   AGE
nfs-client-provisioner-876986df-j5c74   1/1     Running             0          9m29s
web-0                                   1/1     Running             0          13s
web-1                                   1/1     Running             0          9s
web-2                                   0/1     ContainerCreating   0          3s
[root@k8s-master01 nfs]# kubectl  get  pvc
NAME        STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
www-web-0   Bound    pvc-47b8b191-2eaa-4032-9a62-f96f586e5330   1Gi        RWO            nfs-storage    22s
www-web-1   Bound    pvc-a34e3185-8cd9-452b-9ec0-98ae65988b6a   1Gi        RWO            nfs-storage    18s
www-web-2   Bound    pvc-989aff50-0602-468f-a939-0c14df7104f0   1Gi        RWO            nfs-storage    12s
[root@k8s-master01 nfs]# kubectl  get  pv
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM               STORAGECLASS   REASON   AGE
pvc-47b8b191-2eaa-4032-9a62-f96f586e5330   1Gi        RWO            Delete           Bound    default/www-web-0   nfs-storage             23s
pvc-989aff50-0602-468f-a939-0c14df7104f0   1Gi        RWO            Delete           Bound    default/www-web-2   nfs-storage             13s
pvc-a34e3185-8cd9-452b-9ec0-98ae65988b6a   1Gi        RWO            Delete           Bound    default/www-web-1   nfs-storage             19s

🥭三、配置存储

ConfigMap

ConfigMap 功能在 Kubernetes1.2 版本中引入,许多应用程序会从配置文件、命令行参数或环境变量中读取配
置信息。ConfigMap API 给我们提供了向容器中注入配置信息的机制,ConfigMap 可以被用来保存单个属性,也
可以用来保存整个配置文件或者 JSON 二进制大对象

ConfigMap 的创建

Ⅰ、使用目录创建

$ ls docs/user-guide/configmap/kubectl/
game.properties
ui.properties
$ cat docs/user-guide/configmap/kubectl/game.properties
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
$ cat docs/user-guide/configmap/kubectl/ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
$ kubectl create configmap game-config --from-file=docs/user-guide/configmap/kubectl

Ⅱ、使用文件创建

$ kubectl create configmap game-config-2 --from-file=docs/user-
guide/configmap/kubectl/game.properties
$ kubectl get configmaps game-config-2 -o yaml

Ⅲ、使用字面值创建

$ kubectl create configmap special-config --from-literal=special.how=very --from-
literal=special.type=charm
$ kubectl get configmaps special-config -o yaml

Pod 中使用 ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO
---
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: hub.atguigu.com/library/myapp:v1
      command: [ "/bin/sh", "-c", "env" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.type
      envFrom:
       - configMapRef:
           name: env-config
  restartPolicy: Never

Secret

Secret 解决了密码、token、密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者 Pod Spec
中。Secret 可以以 Volume 或者环境变量的方式使用

Secret 有三种类型:

  • Service Account :用来访问 Kubernetes API,由 Kubernetes 自动创建,并且会自动挂载到 Pod 的 /run/secrets/kubernetes.io/serviceaccount 目录中

  • Opaque :base64编码格式的Secret,用来存储密码、密钥等

  • kubernetes.io/dockerconfigjson :用来存储私有 docker registry 的认证信息

Service Account

$ kubectl run nginx --image nginx
deployment "nginx" created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-3137573019-md1u2 1/1 Running 0 13s
$ kubectl exec nginx-3137573019-md1u2 ls /run/secrets/kubernetes.io/serviceaccount
ca.crt
namespace
token

Opaque Secret

$ echo -n "admin" | base64
YWRtaW4=
$ echo -n "1f2d1e2e67df" | base64
MWYyZDFlMmU2N2Rm
# secrets.yml
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  password: MWYyZDFlMmU2N2Rm
  username: YWRtaW4=

kubernetes.io/dockerconfigjson

使用 Kuberctl 创建 docker registry 认证的 secret

$ kubectl create secret docker-registry myregistrykey --docker-server=DOCKER_REGISTRY_SERVER --
docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL
secret "myregistrykey" created.

在创建 Pod 的时候,通过 imagePullSecrets 来引用刚创建的 myregistrykey

apiVersion: v1
kind: Pod
metadata:
  name: foo
spec:
  containers:
    - name: foo
      image: roc/awangyang:v1
  imagePullSecrets:
    - name: myregistrykey
如果有什么问题,请联系作者 beicheng1892@163.com
版权 本文为云录原创文章,转载无需和我联系,但请注明来自云录 https://www.yunzhuan.site


这篇关于【云原生 | Kubernetes 系列】— Kubernetes存储方案的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程