ConfigMap

创建

Terminal
$kubeclt create configmap <config-name> --from-file=<dir># 将文件夹下的文件都变成配置文件
$kubeclt create configmap <config-name> --from-file=[key=]<path># 根据文件创建配置
$kubeclt create configmap <config-name> --from-literal=<key>=<value># 根据 key - value 创建
$kubeclt get configmap# 查看配置

使用

环境变量

Terminal
$kubeclt create configmap env-config --from-literal=JAVA_OPT_TEST='-X -B'
YAML
apiVersion: v1
kind: Pod
metadata:
  name: test-configmap-env
spec:
  containers:
  - name: env-test
    imagePullPolicy: IfNotPresent
    image: alpine
    command: ["/bin/sh", "-c", "env: sleep 30000"]
    env:
    - name: JAVA_OPT_VM_TEST
      valueFrom:                        # 从定义的 configMap 中获取配置
        configMapKeyRef: 
          name: env-config
          key: JAVA_OPT_TEST

配置文件

Terminal
$kubeclt create configmap file-config --from-file=test.ini=~/test.ini
YAML
apiVersion: v1
kind: Pod
metadata:
  name: test-configmap-file
spec:
  containers:
  - name: file-test
    imagePullPolicy: IfNotPresent
    image: alpine
    command: ["/bin/sh", "-c", "sleep 30000"]
    volumeMounts:                       # 加载数据卷
    - name: test-ini
      mountPath: "/user/local/config"   # 文件挂载的目录
  volumes:                              # 将配置文件转换为数据卷
  - name: test-ini
    configMap:
      name: file-config
      items:                            # 不定义,默认加载 file-config 中的所有配置
      - key: "test.ini"                 # file-config 中的 key
        path: "test_file.ini"           # 容器中的件夹名

SubPath

YAML
apiVersion: v1
kind: Pod
metadata:
  name: test-configmap-file
spec:
  containers:
  - name: file-test
    imagePullPolicy: IfNotPresent
    image: alpine
    command: ["/bin/sh", "-c", "sleep 30000"]
    volumeMounts:
    - name: test-ini
      mountPath: "/user/local/config/test_file.ini" # 绝对路径,要包含 subPath 与文件名
      subPath: config/                              # volumes 配置的相对文件夹
  volumes:
  - name: test-ini
    configMap:
      name: file-config
      items:
      - key: "test.ini"
        path: config/test_file.ini                  # 添加一个相对文件夹

热更新

通过 ConfigMap 生成配置,当配置被修改时,以下情况不会同步更新

  • subpath 挂载的配置文件
  • --from-literal 形式生成的配置
Terminal
$kubeclt edit configmap file-config# 直接修改配置文件
$kubeclt replace -f <file.yaml># 通过文件更新配置

immutable

YAML
apiVersion: v1
kind: ConfigMap
immutable: true                 # 当前配置不允许修改
metadata:
  name: configmap
  namespace: dev
data:                           # 实际加载的配置
  test_file.ini: |
    username=admin
    password=123456

secret

创建

Terminal
$kubeclt create secret generic <secret-name> --from-literal=<key>='value'# 根据 key - value 创建
$kubeclt create secret docker-registry <secret-name> --docker-username=<name> --docker-passward=<passward> --docker-email=<email># 配置 docker 仓库
$kubeclt get secret# 查看配置

私有仓库

Terminal
$kubeclt create secret docker-registry docker-secret --docker-username=admin --docker-passward=xxx --docker-email='xxx@email.com'
$kubeclt edit docker-secret# 查看生成的 base64 字符串
YAML
apiVersion: v1
kind: Pod
metadata:
  name: test-configmap-file
spec:
  imagePullSecrets:                    # 从私有仓库拉取镜像
  - name: docker-secret
  containers:
  - name: secret-test
    imagePullPolicy: IfNotPresent
    image: alpine
    command: ["/bin/sh", "-c", "sleep 30000"]

License

Author: 海拉鲁的三角

Link: http://localhost:1313/cpp_notes/posts/k8s/configure/

License: MIT

只要学不死,就往死里学