CNCF configmap及Secrets 学习笔记

应用配置管理

背景问题
1 可变配置:configmap
2 敏感信息:Secret
3 身份认证:ServiceAccount
4 资源配置:spec.containers[].Resources.limits/requests
5 安全管控:Spec.Containers[].SecurityContext
6 前置效验:Spec.initContainers

configmap

创建configmap 模式
kubectl create configmap -from
directories:每个文件作为一个data item
files:每个文件作为一个data item
增加key 输入参数,将替换文件名称:
kubectl create configmap game-config-from-files-with-key --from-file=game-key=configmap/example-dirs/game.properties
envfile:
属性文件中的每一个属性作为一个 data item,输入多个文件,只有最后一个文件生效
literal values
直接输入
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm

或者使用configMapGenerator+Properties 文件
kustomization.yaml模式
复杂,不推荐
kubectl apply -k dir 模式创建
可以基于文件或者literal
cat <./kustomization.yaml
configMapGenerator:

  • name: game-config-4
    files:
    • configure-pod-container/configmap/game.properties
      EOF

cat <./kustomization.yaml
configMapGenerator:

  • name: special-config-2
    literals:
    • special.how=very
    • special.type=charm
      EOF
      yaml-spec
      data
      key:values
      kind:ConfigMap
      matadata:name必须有

使用Configmap模式
env :单个env
envFrom:整个env
volume
1 指定在volume的path
2 Mounted ConfigMaps are updated automatically-加上内部定时刷新可以达到动态更新配置目的
3 sync period (1 minute by default) + ttl of ConfigMaps cache (1 minute by default)

Restrictions
1 You must create a ConfigMap before referencing it in a Pod specification,unless you mark the ConfigMap as “optional”
2 use envFrom to define environment variables from ConfigMaps, keys that are considered invalid will be skipped
3 ConfigMaps reside in a specific Namespace
4 You can’t use ConfigMaps for static pods,
5 使用volume 指定path:模式要确保key存在
6 同一config只能被一个volume mapping
7 size 不能大于etcd最大值,1M

Secret

创建和configmap一样
kubectl create secret generic–需要选择创建模式
1 docker-registry Create a secret for use with a Docker registry
2 generic Create a secret from a local file, directory or literal value
3 tls Create a TLS secret

注意点
1 kubectl get和kubectl describe避免显示密码的内容
2 使用get -o 才可以看到内容
3 stringData 字段
3.1 field is provided for convenience, and allows you to provide secret data as unencoded strings.
3.2 自动转换为BASE64,但在annotation可以看到明文

Using Secrets
1 和configmap类似,env/evnFrom/volume/volume指定路径 4种模式
2 volume 可以以10进制模式指定文件权限
volumes:

  • name: foo
    secret:
    secretName: mysecret
    defaultMode: 256
    这里256 对应 0400
    3 volume更新与configmap一致
    4 尽量避免示意list、watch获取secrets

Using imagePullSecrets
特殊的secret,创建方式如下:
kubectl create secret docker-registry --docker-server=DOCKER_REGISTRY_SERVER --docker-username=DOCKER_USER --docker-password=DOCKER_PASSWORD --docker-email=DOCKER_EMAIL

pod spec 指定secrets

spec:
containers:
- name: foo
image: janedoe/awesomeapp:v1
imagePullSecrets:
- name: myregistrykey

Restrictions
1 Individual secrets are limited to 1MiB in size
2 其他和configmap一致

安全属性
1 The kubelet stores the secret into a tmpfs so that the secret is not written to disk storage
2 one Pod does not have access to the secrets of another Pod

Risks
3 secret data is stored in etcd
4 Administrators should enable encryption at rest for cluster data (requires v1.13 or later).
kube-apiserver 的参数 --experimental-encryption-provider-config
5 Administrators should limit access to etcd to admin users.
6 Administrators may want to wipe/shred disks used by etcd when no longer in use.
7 If running etcd in a cluster, administrators should make sure to use SSL/TLS for etcd peer-to-peer communication.
8 base64本质还是明文
9 应用程序在从卷中读取 secret 后仍然需要保护 secret 的值,例如不会意外记录或发送给不信任方
types
1 Opaque:使用base64编码存储信息,可以通过base64 --decode解码获得原始数据,因此安全性弱。
2 kubernetes.io/dockerconfigjson:用于存储docker registry的认证信息。
3 kubernetes.io/service-account-token:用于被 serviceaccount 引用。serviceaccout 创建时 Kubernetes 会默认创建对应的 secret。

你可能感兴趣的:(CNCF,docker)