(十)Kubernetes ConfigMap和Secret

ConfigMap资源

介绍

ConfigMap是让配置文件从镜像中解耦,让镜像的可移植性和可复制性。许多应用程序会从配置文件、命令行参数或环境变量中读取配置信息。这些配置信息须要与docker image解耦,你总不能每修改一个配置就重作一个image吧?ConfigMap API给咱们提供了向容器中注入配置信息的机制,ConfigMap能够被用来保存单个属性,也能够用来保存整个配置文件或者JSON二进制大对象。ConfigMap保存的值为键值方式,value长度没有限制。html

建立ConfigMap

ConfigMap支持命令建立和使用清单建立。有如下四种方式建立。node

一、命令直接建立 --from-literal:mysql

“kubectl create configmap”命令使用“--from-literal”选项可在命令行直接给出键值来建立ConfigMap对象,重复使用此选项能够传递多个键值对。格式以下:linux

kubectl create configmap NAME --from-literal=key1=value1 --from-literal=key2=value2
[root@k8s-master ~]# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=www.ilinux.cn    #建立cm资源nginx-config,并指定两个键值
configmap/nginx-config created
[root@k8s-master ~]# kubectl get cm    #查看cm资源
NAME           DATA   AGE
nginx-config   2      4s
[root@k8s-master ~]# kubectl describe cm/nginx-config    #查看cm资源nginx-config的详细信息
Name:         nginx-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx_port:
----
80
server_name:
----
www.ilinux.cn
Events:  <none>

二、命令行基于文件建立 --from-file:nginx

“kubectl create configmap”命令使用“--from-file”选项便可基于文件内容来建立ConfigMap对象,一样能够重复屡次使用。格式以下:web

kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
[root@k8s-master ~]# mkdir configmap && cd configmap    #建立一个测试目录
[root@k8s-master configmap]# vim www.conf    #编辑文件内容用做cm的value
server {
    server_name www.ilinux.cn;
    listen 80;
    root /data/web/html/;
}
[root@k8s-master configmap]# kubectl create configmap nginx-www --from-file=www.conf=./www.conf    #使用上面建立的文件来建立cm资源对象
configmap/nginx-www created
[root@k8s-master configmap]# kubectl get cm    #查看cm资源对象
NAME           DATA   AGE
nginx-config   2      7m1s
nginx-www      1      5s
[root@k8s-master configmap]# kubectl get cm nginx-www -o yaml    #查看cm资源对象nginx-www的现象信息
apiVersion: v1
data:
  www.conf: |
    server {
        server_name www.ilinux.cn;
        listen 80;
        root /data/web/html/;
    }
kind: ConfigMap
......

三、命令行基于目录建立 --from-file:redis

若是配置文件数量较多且存储于有限的目录之中,kubectl还提供了基于目录直接将多个文件分别收纳为键值数据的ConfigMap资源建立方式。将“--from-file”选项后面所跟的路径指向一个目录路径就能将目录下的全部文件一同建立于同一ConfigMap资源中,命令格式以下:sql

kubectl create configmap <configmap_name> --from-file=<path-to-directory>
这里假设/data/configs/nginx/conf.d/这个目录下有许多的nginx的配置文件,下面这条命令则将这个目录下的全部配置文件在建立ConfigMap资源时,会分别存储为对应的键值数据。
# kubectl create configmap nginx-config-files --from-file=/data/configs/nginx/conf.d/

四、使用资源清单建立:docker

基于清单文件建立ConfigMap资源时,它所使用的字段包括一般的apiVersionkindmetadata字段,以及用于存储数据的关键字段“data”编程

[root@k8s-master ~]# kubectl explain cm
KIND:     ConfigMap
VERSION:  v1
FIELDS:
apiVersion    <string>
kind    <string>
metadata    <Object>
binaryData    <map[string]string>
data    <map[string]string>
[root@k8s-master configmap]# vim configmap-demo.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: configmap-demo
  namespace: default
data:
  log_level: INFO
  log_file: /var/log/test.log
[root@k8s-master configmap]# kubectl apply -f configmap-demo.yaml 
configmap/configmap-demo created
[root@k8s-master configmap]# kubectl get cm
NAME             DATA   AGE
configmap-demo   2      6s
nginx-config     2      23m
nginx-www        1      16m
[root@k8s-master configmap]# kubectl get cm/configmap-demo -o yaml
apiVersion: v1
data:
  log_file: /var/log/test.log
  log_level: INFO
kind: ConfigMap
metadata:
....

使用ConfigMap

一、环境变量方式注入到Pod

[root@k8s-master configmap]# vim pod-configmap-1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-1
  namespace: default
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    env:
    - name: NGINX_SERVER_PORT
      valueFrom:    #下面这一段表示变量NGINX_SERVER_PORT的值来自于configmap资源nginx-config的key(nginx_port)的值
        configMapKeyRef:
          name: nginx-config    #configmap资源名称
          key: nginx_port      #configmap资源里面的key名
    - name: NGINX_SERVER_NAME
      valueFrom:
        configMapKeyRef:
          name: nginx-config
          key: server_name
[root@k8s-master configmap]# kubectl apply -f pod-configmap-1.yaml    #建立Pod
pod/pod-cm-1 created
[root@k8s-master configmap]# kubectl get pods     #查看pod
NAME       READY   STATUS    RESTARTS   AGE
pod-cm-1   1/1     Running   0          4s
[root@k8s-master configmap]# kubectl exec -it pod-cm-1 -- printenv |grep NGINX    #链接pod资源pod-cm-1并执行命令printenv打印环境变量。过滤是否有上面定义的两个环境变量
NGINX_SERVER_PORT=80
NGINX_SERVER_NAME=www.ilinux.cn

#测试,修改端口,能够发现使用环境变量的注入pod中的端口不会根据配置的变动而改变
[root@k8s-master configmap]# kubectl edit cm/nginx-config   #编辑cm资源nginx-config将nginx_port值改成8080
......
apiVersion: v1
data:
  nginx_port: "8080"
......
[root@k8s-master configmap]# kubectl exec -it pod-cm-1 -- printenv |grep NGINX
NGINX_SERVER_PORT=80
NGINX_SERVER_NAME=www.ilinux.cn

二、存储卷方式挂载ConfigMap

[root@k8s-master configmap]# vim pod-configmap-2.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-2
  namespace: default
  labels:
    app: myapp
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    volumeMounts:
    - name: nginxconf
      mountPath: /etc/nginx/config.d/
      readOnly: true
  volumes:    #建立一个存储卷
  - name: nginxconf    #存储卷名称
    configMap:    #使用configMap类型
      name: nginx-config    #指定configmap资源名称
[root@k8s-master configmap]# kubectl apply -f pod-configmap-2.yaml    #建立Pod
pod/pod-cm-2 created
[root@k8s-master configmap]# kubectl get pods     #查看pod
NAME       READY   STATUS    RESTARTS   AGE
pod-cm-1   1/1     Running   0          10m
pod-cm-2   1/1     Running   0          4s
[root@k8s-master configmap]# kubectl exec -it pod-cm-2 -- /bin/sh    #链接pod资源pod-cm-2,并进入到挂载目录查看。
/ # ls /etc/nginx/config.d/
nginx_port   server_name
/ # cd /etc/nginx/config.d/
/etc/nginx/config.d # cat nginx_port 
8080
/etc/nginx/config.d # cat server_name 
www.ilinux.cn 


#测试,修改端口,能够发现使用volume的方式挂载configmap到容器中,支持动态更新。
[root@k8s-master configmap]# kubectl edit cm/nginx-config    #编辑cm资源nginx-config将nginx_port值改成8088
apiVersion: v1
data:
  nginx_port: "8088"
[root@k8s-master configmap]# kubectl exec -it pod-cm-2 -- /bin/sh
/etc/nginx/config.d # cat nginx_port 
8088
/etc/nginx/config.d #

示例

这里使用上面建立的configmap资源nginx-www示例。

1)编辑资源清单文件

[root@k8s-master configmap]# vim pod-configmap-3.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-3
  namespace: default
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.12
    ports:
    - name: http
      containerPort: 80
    volumeMounts:
    - name: nginxwww
      mountPath: /etc/nginx/conf.d/
      readOnly: true
  volumes:
  - name: nginxwww
    configMap:
      name: nginx-www

2)建立Pod

[root@k8s-master configmap]# kubectl apply -f pod-configmap-3.yaml 
pod/pod-cm-3 created
[root@k8s-master configmap]# kubectl get pods -o wide   #查看pod
NAME       READY   STATUS    RESTARTS   AGE   IP            NODE        NOMINATED NODE   READINESS GATES
pod-cm-3   1/1     Running   0          5s    10.244.1.92   k8s-node1   <none>           <none>

3)进入到Pod中查看配置文件,并建立对应的数据目录进行测试

[root@k8s-master configmap]# kubectl exec -it pod-cm-3 -- /bin/sh    #进入到pod中
# ls /etc/nginx/conf.d
www.conf
# cat /etc/nginx/conf.d/www.conf    #查看生成的www.conf配置文件
server {
    server_name www.ilinux.cn;
    listen 80;
    root /data/web/html/;
}

# nginx -T    #查看当前nginx加载的配置文件
......
server {
    server_name www.ilinux.cn;
    listen 80;
    root /data/web/html/;
}
# mkdir -p /data/web/html    #建立数据目录
# echo "<h1>ConfigMap Pod Test</h1>" >> /data/web/html/index.html    #建立测试文件

#这里拿kubernetes集群节点测试
[root@k8s-master ~]# vim /etc/hosts    #编辑hosts文件将上面的pod和对应的域名进行解析
10.244.1.92    www.ilinux.cn
[root@k8s-master ~]# curl www.ilinux.cn    #访问测试
<h1>ConfigMap Pod Test</h1>

Secret资源

介绍

Secret对象存储数据的方式及使用方式相似于ConfigMap对象,以键值方式存储数据,在Pod资源中经过环境变量或存储卷进行数据访问,解决了密码、token、密钥等敏感数据的配置问题,而不须要将这些敏感数据暴露到镜像或者pod spec中。另外,Secret对象的数据存储和打印格式为Base64编码的字符串,所以用户在建立Secret对象时也要提供此种编码格式的数据。在容器中以环境变量或存储卷的方式访问时,它们会被自动解码为明文格式。须要注意的是,若是是在Master节点上,Secret对象以非加密的格式存储在etcd中,因此须要对etcd的管理和权限进行严格控制。

Secret的四种类型

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

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

  • kubernetes.io/dockerconfigjson:用来存储Docker镜像仓库的认证信息,类型标识符为docker-regiestry

  • kubernetes.io/tls:用于为SSL通讯模式存储证书和私钥文件,命令式建立时类型标识为tls

建立Secret

一、命令直接建立 --from-literal:

“kubectl create secret generic”命令使用“--from-literal”选项可在命令行直接给出键值来建立ConfigMap对象,重复使用此选项能够传递多个键值对。格式以下:

kubectl create secret generic NAME --from-literal=key1=value1 --from-literal=key2=value2
[root@k8s-master ~]# kubectl create secret generic mysql-auth --from-literal=username=root --from-literal=password=MyP@sswd     #建立secret资源mysql-auth,并指定两个键值
secret/mysql-auth created
[root@k8s-master ~]# kubectl get secret     #查看secret资源
NAME                  TYPE                                  DATA   AGE
default-token-blm9l   kubernetes.io/service-account-token   3      3d
mysql-auth            Opaque                                2      17s
[root@k8s-master ~]# kubectl describe secret/mysql-auth    #查看secret资源mysql-auth的详细信息
Name:         mysql-auth
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
password:  8 bytes
username:  4 bytes

二、命令行基于文件建立 --from-file:

“kubectl create secret generic”命令使用“--from-file”选项便可基于文件内容来建立ConfigMap对象,一样能够重复屡次使用。格式以下:

kubectl create secret generic my-secret --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
[root@k8s-master ~]# mkdir secret && cd secret
[root@k8s-master secret]# echo -n admin > ./username
[root@k8s-master secret]# echo -n 123456 > ./password
[root@k8s-master secret]# 
[root@k8s-master secret]# kubectl create secret generic mysecret --from-file=username=./username --from-file=password=./password
secret/mysecret created
[root@k8s-master secret]# kubectl get secret 
NAME                  TYPE                                  DATA   AGE
default-token-blm9l   kubernetes.io/service-account-token   3      3d
mysecret              Opaque                                2      6s
mysql-auth            Opaque                                2      5m23s
[root@k8s-master secret]# kubectl get secret/mysecret -o yaml
apiVersion: v1
data:
  password: MTIzNDU2    #这里能够看到secret存储的值都是base64编码格式
  username: YWRtaW4=
kind: Secret
metadata
......

三、使用资源清单建立:

Secret资源是标准的Kubernetes API对象,除了标准的apiVersionkindmetadata字段,它可用的其余字段以下:

data    <map[string]string>    #"key:value"格式的数据,一般是敏感信息,数据格式须要以Base64格式编码的字符串,所以须要事先完成编码
stringData    <map[string]string>    #以明文格式(非Base64编码)定义的“key:value"数据;无须事先对数据进行Base64编码,而是在建立为Secret对象时自动进行编码并保存于data字段中。
type    <string>    #仅是为了便于编程方式处理Secret数据而提供的类型标识。
[root@k8s-master secret]# vim secret-demo.yaml
apiVersion: v1
kind: Secret
metadata:
  name: secret-demo
  namespace: default
stringData:
  username: redis
  password: redisP@ss
type: Opaque
[root@k8s-master secret]# kubectl apply -f secret-demo.yaml 
secret/secret-demo created
[root@k8s-master secret]# kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-blm9l   kubernetes.io/service-account-token   3      3d1h
mysecret              Opaque                                2      28m
mysql-auth            Opaque                                2      33m
secret-demo           Opaque                                2      5s
[root@k8s-master secret]# kubectl get secret/secret-demo -o yaml
apiVersion: v1
data:
  password: cmVkaXNQQHNz
  username: cmVkaXM=
kind: Secret
metadata:
......

使用Secret

相似于Pod消费ConfigMap对象的方式,Secret对象能够注入为环境变量,也能够存储为存储卷形式挂载使用。由于Secret默认保存的是非明文格式,经过注入为环境变量实为不明智。

存储卷方式示例:

这里假设须要为Nginx测试建立SSL虚拟主机

1)首先建立私钥和自签证书

[root@k8s-master secret]# openssl genrsa -out nginx.key 2048
[root@k8s-master secret]# openssl req -new -x509 -key nginx.key -out nginx.crt -subj /C=CN/ST=ShenZhen/L=ShenZhen/O=DevOps/CN=www.ilinux.cn

2)建立secret

[root@k8s-master secret]# kubectl create secret tls nginx-ssl --key=./nginx.key --cert=./nginx.crt 
secret/nginx-ssl created
[root@k8s-master secret]# kubectl get secret nginx-ssl
NAME        TYPE                DATA   AGE
nginx-ssl   kubernetes.io/tls   2      14s

3)编辑资源清单

[root@k8s-master secret]# vim pod-secret-demo.yaml
apiVersion: v1
kind: Pod
metadata:
  name: secret-volume-demo
  namespace: default
spec:
  containers:
  - name: web-server
    image: nginx:1.12
    volumeMounts:
    - name: nginxcert
      mountPath: /etc/nginx/ssl/
      readOnly: true
  volumes:
  - name: nginxcert
    secret:
      secretName: nginx-ssl

4)建立pod并验证

[root@k8s-master secret]# kubectl apply -f pod-secret-demo.yaml 
pod/secret-volume-demo created
[root@k8s-master secret]# kubectl exec -it secret-volume-demo -- /bin/sh
# ls /etc/nginx/ssl
tls.crt  tls.key
相关文章
相关标签/搜索