配置资源管理

1 Secret

Secret 是用来保存密码、token、密钥等敏感数据的 k8s 资源,这类数据虽然也可以存放在 Pod 或者镜像中,但是放在 Secret 中可以更方便的控制数据的使用,并减少暴露的风险。

1.1 Secret 的类型

  • kubernetes.io/service-account-token:由 Kubernetes 自动创建,用来访问 APIServer 的 Secret,Pod 会默认使用这个 Secret 与 APIServer 通信, 并且会自动挂载到 Pod 的 /run/secrets/kubernetes.io/serviceaccount 目录中
  • Opaque:base64 编码格式的 Secret,用来存储用户自定义的密码、密钥等,默认的 Secret 类型;
  • kubernetes.io/dockerconfigjson:用来存储私有 docker registry 的认证信息。

1.2 Secret 使用方式

Pod 需要先引用才能使用某个 secret,Pod 有 3 种方式来使用 secret:

  • 作为挂载到一个或多个容器上的卷中的文件。
  • 作为容器的环境变量。
  • 由 kubelet 在为 Pod 拉取镜像时使用。

1.3 示例

1.3.1 创建 Secret 示例

1.用kubectl create secret命令创建Secret
echo -n ‘zhangsan’ > username.txt
echo -n ‘abc1234’ > password.txt
命令解释:在 Kubernetes 中创建一个名为 “mysecret” 的通用密钥(Generic)。该密钥将从 "username.txt"和 "password.txt"两个文件中读取数据。将这两个文件中的内容作为密钥的值存储在 Kubernetes 的 Secret 中,以便在容器中安全地使用这些敏感信息。
kubectl create secret generic mysecret --from-file=username.txt --from-file=password.txt
查看
kubectl get secrets
在这里插入图片描述
kubectl describe secret mysecret
在这里插入图片描述
get 或 describe 指令都不会展示 secret 的实际内容,这是出于对数据的保护的考虑
2.用 base64 编码,创建Secret
echo -n zhangsan | base64
在这里插入图片描述
echo -n abc1234 | base64
在这里插入图片描述
vim secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: mysecret1
type: Opaque
data:
  username: emhhbmdzYM4=
  password: YWJjMTIzNA==

kubectl create -f secret.yaml

kubectl get secrets
在这里插入图片描述
kubectl get secret mysecret1 -o yaml
在这里插入图片描述

1.3.2 使用 Secret 示例

1.将 Secret 挂载到 Volume 中,以 Volume 的形式挂载到 Pod 的某个目录下
vim secret-test.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: secrets
      mountPath: "/etc/secrets"
      readOnly: true
  volumes:
  - name: secrets
    secret:
      secretName: mysecret

kubectl create -f secret-test.yaml

kubectl get pods
在这里插入图片描述
kubectl exec -it seret-test bash
cd /etc/secrets/
ls
在这里插入图片描述
cat password.txt
在这里插入图片描述
cat username.txt
在这里插入图片描述
2.将 Secret 导出到环境变量中
vim secret-test1.yaml

apiVersion: v1
kind: Pod
metadata:
  name: mypod1
spec:
  containers:
  - name: nginx
    image: nginx
    env:
      - name: TEST_USER
        valueFrom:
          secretKeyRef:
            name: mysecret1
            key: username
      - name: TEST_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret1
            key: password

kubectl apply -f secret-test1.yaml

kubectl get pods
在这里插入图片描述
kubectl exec -it mypod bash
echo $TEST_USER
在这里插入图片描述
echo $TEST_PASSWORD
在这里插入图片描述

2 ConfigMap

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

2.1 示例

2.1.1 创建 ConfigMap 示例

1.使用目录创建
mkdir /opt/configmap/

vim /opt/configmap/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

vim /opt/configmap/ui.properties

color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice

解释:–from-file 指定在目录下的所有文件都会被用在 ConfigMap 里面创建一个键值对,键的名字就是文件名,值就是文件的内容
kubectl create configmap game-config --from-file=/opt/configmap/
kubectl get cm
在这里插入图片描述
kubectl get cm game-config -o yaml
在这里插入图片描述
2.使用文件创建
只要指定为一个文件就可以从单个文件中创建 ConfigMap
–from-file 这个参数可以使用多次,即可以使用两次分别指定上个实例中的那两个配置文件,效果就跟指定整个目录是一样的

kubectl create configmap game-config-2 --from-file=/opt/configmap/game.properties --from-file=/opt/configmap/ui.properties

kubectl describe cm game-config-2
在这里插入图片描述
3.使用字面值创建
使用文字值创建,利用 --from-literal 参数传递配置信息,该参数可以使用多次,格式如下:
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=good

kubectl get configmaps special-config -o yaml
在这里插入图片描述
删除
kubectl delete cm --all
kubectl delete pod --all

2.1.2 Pod 中使用 ConfigMap

1.使用 ConfigMap 来替代环境变量
vim env.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: good
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO

kubectl create -f env.yaml

kubectl get cm
在这里插入图片描述
创建 Pod
vim test-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: busybox
    image: busybox:1.28.4
    command: [ "/bin/sh", "-c", "env" ]
    env:
      - name: SPECIAL_HOW_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

kubectl create -f test-pod.yaml

kubectl get pods
在这里插入图片描述
kubectl logs pod-test
在这里插入图片描述
2.用 ConfigMap 设置命令行参数
vim test-pod2.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-pod2
spec:
  containers:
  - name: busybox
    image: busybox:1.28.4
    command: 
	- /bin/sh
	- -c
	- echo "$(SPECIAL_HOW_KEY) $(SPECIAL_TYPE_KEY)"
    env:
      - name: SPECIAL_HOW_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

kubectl create -f test-pod2.yaml

kubectl get pods
在这里插入图片描述
kubectl logs test-pod2
在这里插入图片描述
3.通过数据卷插件使用ConfigMap
在数据卷里面使用 ConfigMap,就是将文件填入数据卷,在这个文件中,键就是文件名,键值就是文件内容
vim test-pod3.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-pod3
spec:
  containers:
  - name: busybox
    image: busybox:1.28.4
    command: [ "/bin/sh", "-c", "sleep 36000" ]
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
  restartPolicy: Never

kubectl create -f test-pod3.yaml

kubectl get pods
在这里插入图片描述
kubectl exec -it test-pod3 sh
cd /etc/config/
ls
在这里插入图片描述
cat special.how
在这里插入图片描述
cat special.type
在这里插入图片描述

2.1.3 ConfigMap 的热更新

vim test-pod4.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: log-config
  namespace: default
data:
  log_level: INFO
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      run: my-nginx
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: config-volume
          mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: log-config

kubectl apply -f test-pod4.yaml

kubectl get pods
在这里插入图片描述
kubectl edit configmap log-config
在这里插入图片描述
等大概10秒左右,使用该 ConfigMap 挂载的 Volume 中的数据同步更新
kubectl exec -it my-nginx-7b8755d996-ndf8n – cat /etc/config/log_level
在这里插入图片描述

2.1.4 ConfigMap 更新后滚动更新 Pod

更新 ConfigMap 可以触发相关 Pod 的滚动更新,可以通过在 .spec.template.metadata.annotations 中添加 version/config ,每次通过修改 version/config 来触发滚动更新
kubectl patch deployment my-nginx --patch ‘{“spec”: {“template”: {“metadata”: {“annotations”: {“version/config”: “20210525” }}}}}’

kubectl get pods
在这里插入图片描述

  • 16
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值