第十章 Secret & Configmap (中)

10.3 在Pod中使用Secret

  10.3.1 Volume方式  

apiVersion: v1
kind: Pod
metaata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: busybox
    args:
      - /bin/sh
      - -c
      - sleep 10; touch /tmp/healthy; sleep 30000
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"    # 在容器内部的该路径下
      readOnly: true
  volumes:
  - name: foo
    secret:
      secretName: mysecret     # 指定有前面创建的mysecret
 kubectl apply -f mypod.yml
 pod "mypod" created

kubeusr@GalaxyKubernetesMaster:~$ kubectl get pods
NAME                READY     STATUS              RESTARTS   AGE
mypod               0/1       ContainerCreating   0          7s
producer-consumer   2/2       Running             4          18h
kubeusr@GalaxyKubernetesMaster:~$ kubectl get pods
NAME                READY     STATUS    RESTARTS   AGE
mypod               1/1       Running   0          14s
producer-consumer   2/2       Running   4          18h
kubeusr@GalaxyKubernetesMaster:~$ kubectl exec -it mypod  sh     # 进入容器
/ # cd /etc/foo              # 进入
/etc/foo # ls
password  username
/etc/foo #

cat /etc/foo/password # 可以直接查看内容,是名文。
123456

K8s会在指定的路径下为每条敏感数据创建一个文件,文件名是数据条目的Key, /etc/foo/username和 etc/foo/password, value是以明文的形式存放在文件中。

我们也可以自定义存放数据的文件名,配置文件如下改动:这时,数据将存放在/etc/foo/my-group/myt-username中。

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: busybox
    args:
      - /bin/sh
      - -c
      - sleep 10; touch /tmp/healthy; sleep 30
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"
      readOnly: true
  volumes:
  - name: foo
    secret:
      secretName: mysecret
      items:
      - key: username
        path: my-group/my-username
      - key: password
        path: my-group/my-password

     以Voluime方式使用secret支持动态更新:Secret更新后,容器中的数据也会更新。

 10.3.2 环境变量方式

   通过volume方式使用secret,容器必须从文件读取数据,稍显麻烦。

   K8s支持通过环境变量使用secret。

   

10.4 ConfigMap

猜你喜欢

转载自www.cnblogs.com/liufei1983/p/10206991.html