1、使用ConfigMap配置管理应用程序

       Kubernetes基于ConfigMap对象实现了将配置文件从容器中解耦出来,并将配置数据以键值对的形式进行存储,这些数据可以在Pod中使用或者为系统组件提供配置。

(1)创建ConfigMap

       ConfigMap的创建可以通过命令创建或者资源清单定义文件创建,通过命令创建时的命令语法格式如下:

Kubectl create configmap <map-name> <data-source>

       通过命令创建时,map-name为ConfigMap对象的名称;<data-source>为数据源,它可以通过值,文件或目录获取;无论它的值是什么,它都要转化为ConfigMap对象中的ke-value数据。

1)使用值直接创建

      使用”kubectl create configmap”命令创建时,可使用”—from-literal”选项在命令行直接给出键值对来创建ConfigMap对象。

# 创建一个名称为test-config的configmap 
[root@master01 ~]# kubectl create configmap test-config --from-literal=key-name=dayi123 --from-literal=key-passwd=dayi1234
configmap/test-config created
# 查看创建的configmap的配置清单文件
[root@master01 ~]# kubectl get configmaps test-config -o yaml
apiVersion: v1
data:
  key-name: dayi123
  key-passwd: dayi1234
kind: ConfigMap
metadata:
  creationTimestamp: "2019-01-28T06:23:22Z"
  name: test-config
  namespace: default
  resourceVersion: "2649395"
  selfLink: /api/v1/namespaces/default/configmaps/test-config
  uid: 365e5478-22c5-11e9-9f5c-000c298d15e0

       2)基于文件创建

       为“kubectl create configmap”命令使用”—from-file”选项可以基于文件内容来创建ConfigMap对象;”—from-file”选项可以使用多次以传递多个文件内容。命令使用格式如下:

Kubectl create configmap <configmap_name> --from-file=<path-to-file>

       如果需要自行指定键名则需要在”—from-file”选项中直接指定自定义的键,命令格式如下:

Kubectl create configmap <configmap_name> --from-file=<my-key-name>=<path-to-file>

基于文件使用configmap创建nginx的配置文件:

# 基于文件创建configmap,
[root@master01 ~]# kubectl create configmap test-nginxconfig --from-file=nginxconfig=/etc/nginx/nginx.conf
configmap/test-nginxconfig created
# 查看创建的nginx配置文件的configmap,值为nginx配置文件内容
[root@master01 ~]# kubectl get configmap test-nginxconfig -o yaml
apiVersion: v1
data:
  nginxconfig: |+
    # For more information on configuration, see:
    #   * Official English Documentation: http://nginx.org/en/docs/
    #   * Official Russian Documentation: http://nginx.org/ru/docs/

    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
. . . . . .

       3)基于目录创建

       若配置文件较多并且存在于同一个目录时,可以将该目录创建为configmap。“—from-file”选项后面所跟的路径指向一个目录路径就能将目录下的所有文件一同创建于同一ConfigMap资源中,命令格式如下:

Kubectl create configmap <configmap_name> --from-file=<path-to-directory>

       当该目录下有多个文件时,它们会分别被存储为多个不同的键值数据,如将nginx的配置文件目录”/etc/nginx”创建为configmap:

[root@master01 ~]# kubectl create configmap nginx-dir-config --from-file=/etc/nginx/
configmap/nginx-dir-config created

       4)使用资源配置清单创建configmap

       使用资源配置清单定义configmap配置文件时,需要定义的字段通常包括apiVersion、kind、metadata字段及用于存储数据的关键字段“data”。

# 定义configmap资源的配置清单
[root@master01 configmap]# cat test01-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata: 
  name: configmap-demo
data:
  log_level: INFO
  log_file: /var/log/nginx/access.log
  config_file: /etc/nginx/nginx.conf

(2)向Pod环境变量传递ConfigMap对象键值数据

       Pod资源获取环境变量时可以引用ConfigMap对象中的数据,具体做法是通过在env字段中为valueFrom内嵌configMapKeyRef对象实现。

# 定义一个configmap资源,在定义一个pod资源通过环境变量传递configmap键值数据
[root@master01 configmap]# cat test-env-configmap.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-env-configmap
data:
  httpd_port: "8080"
---
apiVersion: v1
kind: Pod
metadata: 
  name: test-envconfigmap-demo
spec:
  containers:
  - image: busybox
    name: busybox-httpd
    command: ["/bin/httpd"]
    args: ["-f","-p","$(HTTPD_PORT)"]
    env:
    - name: HTTPD_PORT
      valueFrom:
        configMapKeyRef:
          name: test-env-configmap
          key: httpd_port
# 创建定义的资源并查看pod中的进程
[root@master01 configmap]# kubectl apply -f test-env-configmap.yaml
[root@master01 configmap]# kubectl exec test-envconfigmap-demo ps aux
PID   USER     TIME  COMMAND
    1 root      0:00 /bin/httpd -f -p 8080

       向pod中传递configmap中定义的资源时,env字段中valueFrom字段内嵌的configMapKeyRef字段各内嵌字段作用如下:

           name:为要引用configmap对象的名称

           key:指定要引用ConfigMap对象中某键的名称

           optional:用于为当前Pod资源指名此引用是否可选

       configmap是名称空间级别的资源,它必须要与应用它的pod资源在同一名称空间中;当configmap中存在较多的键值数据时,pod资源支持在容器中使用envFrom字段直接将configmap资源中的所有键值一次性的完成导入。为了避免多个configmap应用键值数据时产生键名冲突,可以在每个应用中将被导入的键使用prefix字段指定一个特殊的前缀;同时,prefix字段可以省略,省略时,所有变量名同ConfigMap中的键名。

(3)configmap存储卷

       当configmap对象中的键值来源于较长的文件内容时,将其内容直接作为文件进行应用是较好的选择;实现方式是在定义pod资源时,将此类的configmap对象配置为onfigmap类型的存储卷,然后挂载至指定的目录进行访问。

       1)挂载整个存储卷

       当configmap对象关联为pod资源的存储卷时,configmap对象中的每个键都对应的表现为一个文件,键名会转为文件名,键值为文件内容;当pod资源使用该存储卷时,仅需指明存储卷名称及要引用的configmap对象的名称即可。

# 定义一个基于nginx的pod资源,配置文件使用前面创建的名称为nginx-dir-config的configmap存储卷进行挂载
[root@master01 configmap]# cat test-nginx-configmap-vol.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: configmap-nginx-voldemo
spec:
  containers:
  - image: nginx:1.12
    name: ngin-service
    volumeMounts:
    - name: ngxconfig
      mountPath: /etc/nginx/
      readOnly: true
  volumes:
  - name: ngxconfig
    configMap:
      name: nginx-dir-config

       2)挂载存储卷中的部分键值

       在某些时候,可能不期望在容器中挂载某configmap存储卷后于挂载点目录导出所有文件;此时,可以使用configMap字段嵌套的的items字段完成该需求,该items字段嵌套的字段主要有以下三个字段:

              key:必选字段,要应用的键名称

              path:对应的键于挂载点目录中生成的文件的相对路径

              mode:文件的权限模型

# 如果上面的配置文件中只使用nginx的主配置文件等部分配置,则volumes的定义如下
  volumes:
  - name: ngxconfig
    configMap:
      name: nginx-dir-config
      items:
      - key: nginx.conf
        path: nginx.conf
        mode: 0644
      - key: fastcgi.conf
        path: fastcgi.conf
      - key: mime.types
        path: mime.types

       3)独立挂载存储卷中的键值

       前面无论是挂载所有文件还是部分文件,挂载点目录下的所有文件都会被隐藏;如果希望将configmap对象提供的配置文件补充于挂载点目录,这种方式是最适合的,而这种方式是通过configmap字段来实现的。

# 只将nginx.conf配置文件补充挂载,其他的配置文件不动,当pod中的容器存在该配置文件时,该文件不会被
[root@master01 configmap]# cat test-nginx-configmap-vol03.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: configmap-nginx-voldemo03
spec:
  containers:
  - image: nginx:1.12
    name: ngin-service
    volumeMounts:
    - name: ngxconfig
      mountPath: /etc/nginx/nginx.conf
      subPath: nginx.conf
      readOnly: true
  volumes:
  - name: ngxconfig
    configMap:
      name: nginx-dir-config

2、Secret资源的使用

      Secret资源的使用同configmap资源的使用方法基本相同,secret资源用于存放敏感数据。

(1)secret资源

      Secret资源也是通过键值得方式存储数据,在pod资源中通过环境变量或存储卷进行数据访问,但secret对象仅会被分发至调用了此对象的pod资源所在的工作节点且只能由节点将其存储于内存中,而在master节点上,secret对象以非加密的格式存储于etcd中。

      Secret对象主要由两种用途,一是作为存储卷注入到pod上由容器应用程序所使用,二是用于kubelet为pod里的容器拉取镜像时向私有仓库提供认证信息。Secret资源主要是由以下四种类型组成的:

          Opaque:自定义数据内容,base64编码,用来定义存储密码、秘钥、证书等数据,类型的标识符为generic

          Kubernetes.io/service-account-token:service Account的认证信息,可在创建   service account时由kubernetes自动创建

          Kubernetes.io/dockerconfigjson:用来存储docker镜像仓库的认证信息,类型表示   为docker-registry

          Kuberbetes.io/tls:用于为SSL通信模式存储证书和私钥文件,命令式创建时类型   标识为tls。

(2)创建secret资源

      1)通过命令创建

      使用命令创建secret资源的方法同使用命令创建configmap资源的方法基本一致,通过命令创建secret对象的命令格式如下:

Kubectl create secret generic <SECRET_NAME> --from-literal=key=value

      创建完成一个secret对象,其数据会以base64的编码格式进行加密。

# 创建一个用户名和密码的secret对象
[root@master01 configmap]# kubectl create secret generic auth --from-literal=username=admin --from-literal=password=dayi123
secret/auth created
# 查看创建的secret对象时,其值是加密的
[root@master01 configmap]# kubectl get secrets auth -o yaml
apiVersion: v1
data:
  password: ZGF5aTEyMw==
  username: YWRtaW4=
kind: Secret
metadata:
  creationTimestamp: "2019-01-29T09:24:28Z"
  name: auth
  namespace: default
  resourceVersion: "2799717"
  selfLink: /api/v1/namespaces/default/secrets/auth
  uid: ad5b869d-23a7-11e9-9f5c-000c298d15e0
type: Opaque

      使用命令创建时也可以使用”—from-file”命令从文件中直接加载;如果要基于证书文件创建用于SSL/TLS通信的Secret对象,则需要使用”kubectl create secret tls <SECRET_NAME> --cert—key=”命令来创建。

# 生成用于测试的私钥和自签证书
[root@master01 volumes]# openssl genrsa -out tls.key 2048
[root@master01 volumes]# openssl req -new -x509 -key tls.key -out tls.crt -subj /C=CN/ST=Shanghai/L=Shanghai/O/dev/CN=www.dayi123.com -days 736
# 将生成的证书创建为secret资源对象
[root@master01 volumes]# kubectl create secret tls test-ssl --key=tls.key --cert=tls.crt 
secret/test-ssl created

      2)通过定义资源清单的方式创建

      通过资源清单的方式定义secret资源对象时,除了定义标准的apiVersion、kind、metadata字段外,还需要定义如下的字段:

         data:”kev:value”格式的数据,通常是敏感的数据,数据格式是以base64编码的数   据,需要用户提前编码。

         stringData:以名文格式定义的”key:value”数据,在创建Secret对象时会自动编码   并保存于data字段中,如果使用”kubectl apply”命令创建,注解信息中可能还是会输出这些信息。

         type:为了便于变成方式处理Secret数据而提供的类型标识

# 定义一个secret资源清单,password使用加密后的密码
[root@master01 volumes]# cat test-secret.yaml 
apiVersion: v1
kind: Secret
metadata:
  name: test-secret
data:
  password: ZGF5aTEyMwo=
stringData:
  username: admin
type: Opaque

      3)secret的使用

      Secret资源对象使用方法同configmap资源使用方法基本一致,可以注入为环境变量,也可以以存储卷的形式挂载使用,但是不建议以环境变量的方式使用。

      在pod中将secret挂载为存储卷使用时,除了类型及标识需要替换为Secret及secretName外,其他的用法同configmap存储卷的用法基本类似。

# 定义一个基于nginx镜像的pod资源,将前面创建的nginx-test挂载到nginx中
[root@master01 volumes]# cat test-nginx-secret-vol.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: test-nginx-secret
spec:
  containers:
  - name: secret-nginx
    image: nginx:1.12
    volumeMounts:
    - name: nginxcert
      mountPath: /etc/nginx/certs
      readOnly: true
  volumes:
  - name: nginxcert
    secret:
      secretName: test-ssl

      4)imagePullSecret资源对象的使用

      imagePullSecret资源可以辅助kubelet从需要认证的私有仓库完成认证并获取镜像。使用imagePullSecret的方式有两种,都需要创建docker-registry类型的Secret对象,一种是通过定义pod资源时明确通过”imagePullSecret”字段给出;另一种是将其添加到特定的ServiceAccount对象中。

      在创建docker-registry类型的Secret对象时,使用如下的命令格式创建

kubectl create secret docker-registry <SECRET_NAME> --docker-user=<USERNAME> --docker-password=<PASSWORD> --docker-email=<DOCKER_USER_EMAIL>

       如下,创建一个阿里云认证的docker-registry,并创建pod资源去阿里云拉取镜像。

# 创建docker-registry
[root@master01 volumes]# kubectl create secret docker-registry aliyun-registry --docker-username=dayi123 --docker-password=dayi123 --docker-email=dayi123@126.com
# 定义一个pod资源,使用上面创建的docker-registry做认证去阿里镜像仓库拉取镜像
[root@master01 volumes]# cat docker-pull-ali.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: secret-imagepull-ail
spec:
  imagePullSecrets:
  - name: aliyun-registry
  containers:
  - name: test-busybox
image: registry.cn-hangzhou.aliyuncs.com/dayi123/busybox:v1.1

 

Logo

开源、云原生的融合云平台

更多推荐