美文网首页
kubernetes configmap和secret 使用

kubernetes configmap和secret 使用

作者: shadow123 | 来源:发表于2022-08-11 16:50 被阅读0次

ConfigMap

ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中。使用时, Pods 可以将其用作环境变量、命令行参数或者存储卷中的配置文件。

configMap 使用

使用 kubectl 管理 ConfigMap

$ kubectl create configmap configmapname --from-literal=key=value
# 获取整个configmap 数据
$ kubectl get configmap configmapname -o go-template='{{.data}}'
# 查看详情
$ kubectl describe configmap configmapname
# 获取具体某个key值
$ kubectl get configmap configmapname -o go-template='{{.data.key}}'
# 删除
$ kubectl delete configmap configmapname
# 再查看
$ kubectl get configmap configmapname

# 通过文件创建ConfigMap
$ echo hello > test1.txt
$ ehco world > test2.txt
$ kubectl create configmap my-config --from-file=key1=test1.txt  --from-file=key2=test2.txt
$ kubectl describe configmap my-config

使用配置文件管理 ConfigMap

configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: game-demo
data:
  # 类属性键;每一个键都映射到一个简单的值
  player_initial_lives: "3"
  ui_properties_file_name: "user-interface.properties"

  # 类文件键
  game.properties: |
    enemy.types=aliens,monsters
    player.maximum-lives=5    
  user-interface.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true    

pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: configmap-demo-pod
spec:
  containers:
    - name: demo
      image: alpine
      command: ["sleep", "3600"]
      env:
        # 定义环境变量
        - name: PLAYER_INITIAL_LIVES # 请注意这里和 ConfigMap 中的键名是不一样的
          valueFrom:
            configMapKeyRef:
              name: game-demo           # 这个值来自 ConfigMap
              key: player_initial_lives # 需要取值的键
        - name: UI_PROPERTIES_FILE_NAME
          valueFrom:
            configMapKeyRef:
              name: game-demo
              key: ui_properties_file_name
      volumeMounts:
      - name: config
        mountPath: "/config"
        readOnly: true
  volumes:
    # 你可以在 Pod 级别设置卷,然后将其挂载到 Pod 内的容器中
    - name: config
      configMap:
        # 提供你想要挂载的 ConfigMap 的名字
        name: game-demo
        # 来自 ConfigMap 的一组键,将被创建为文件
        items:
        - key: "game.properties"
          path: "game.properties"
        - key: "user-interface.properties"
          path: "user-interface.properties"

上面的例子定义了一个卷并将它作为 /config 文件夹挂载到 demo 容器内, 创建两个文件,/config/game.properties/config/user-interface.properties.

secret

Secret 是一种包含少量敏感信息例如密码、令牌或密钥的对象。

Secret 类似于 ConfigMap,专门用于保存机密数据。

Secret 的使用

使用 kubectl 管理 Secret

echo -n 'admin' > ./username.txt
echo -n '1f2d1e2e67df' > ./password.txt

使用 kubectl create secret 创建

kubectl create secret generic db-user-pass \
  --from-file=./username.txt \
  --from-file=./password.txt

默认密钥名称是文件名。也可以使用 --from-file=[key=]source 来设置密钥名称。

kubectl create secret generic db-user-pass \
  --from-file=username=./username.txt \
  --from-file=password=./password.txt

还可以使用 --from-literal=<key>=<value> 标签提供 Secret 数据。
特殊字符(,\,*,= 和 !)需要转义,例如 `\\`。 最简便的方法使用单引号括起来

查看 Secret

$ kubectl get secrets
NAME                  TYPE                       DATA      AGE
db-user-pass          Opaque                      2        51s

查看 Secret 的描述:

$ kubectl describe secrets/db-user-pass

解码 Secret

$ kubectl get secret db-user-pass -o jsonpath='{.data}'
{"password":"MWYyZDFlMmU2N2Rm","username":"YWRtaW4="}
# 解码 password 的数据
$ echo 'MWYyZDFlMmU2N2Rm' | base64 --decode
1f2d1e2e67df
# 避免在 shell 历史记录中存储 Secret 的编码值,可以执行如下命令:
$ kubectl get secret db-user-pass -o jsonpath='{.data.password}' | base64 --decode
1f2d1e2e67df

清理 Secret

$ kubectl delete secret db-user-pass

使用配置文件管理 Secret

1.将字符串转换为 base64

$ echo -n 'admin' | base64
YWRtaW4=
$ echo -n '1f2d1e2e67df' | base64
MWYyZDFlMmU2N2Rm

2.创建 secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  username: YWRtaW4=
  password: MWYyZDFlMmU2N2Rm

3.使用 kubectl apply 创建 Secret

$ kubectl apply -f secret.yaml

imagePullSecret:Pod 拉取私有镜像仓库时使用的账户信息,会传递给 kubelet,然后 kubelet 就可以拉取仓库里的镜像。

创建一个docker registry 的 secret

$ kubectl create secret docker-registry <name> --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAI

subPath

解决目录覆盖问题

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-nginx
spec:
  selector:
    matchLabels:
      app: demo-nginx
  template:
    metadata:
      labels:
        app: demo-nginx
    spec:
      containers:
        - name: demo-nginx
          image: nginx
          resources:
            limits:
              memory: "128Mi"
              cpu: "500m"
          ports:
            - containerPort: 80
          volumeMounts:
            - name: config-volume
              mountPath: /etc/nginx/nginx.conf
              subPath: etc/nginx/nginx.conf
      volumes:
        - name: config-volume
          configMap:
            name: nginx-conf
            items:
              - key: nginx-conf
                path: etc/nginx/nginx.conf
$ kubectl create cm nginx-conf --from-file=nginx.conf

热更新

更新方式:edit、create

edit 更新

$ kubectl edit cm nginx-conf

ConfigMap 和 Secret 如果是以subPath 的形式挂载的,那么 Pod 是不会感知到 ConfigMap 和 Secret 更新的。

如果 Pod 的变量来自于 ConfigMap 和 Secret 中定义的内容,那么 ConfigMap 和 Secret 更新后,也不会更新 Pod 的变量。

create 更新

$ kubectl create cm nginx-conf --from-file=nginx.conf --dry-run -oyaml | kubectl replace -f-

不可变

安全配置

apiVersion: v1
kind: ConfigMap
metadata:
  name: myapp
data:
  key: value
immutable:true

相关文章

网友评论

      本文标题:kubernetes configmap和secret 使用

      本文链接:https://www.haomeiwen.com/subject/mektgrtx.html