Kerbernetes使用ConfigMap资源配置非铭感信息

本文涉及的产品
实时计算 Flink 版,5000CU*H 3个月
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介: 文章介绍了在Kubernetes中使用ConfigMap资源来配置非敏感信息的方法,包括通过环境变量和配置文件两种方式,并提到了使用Secret资源来配置敏感信息。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.配置应用程序常用方法

  配置应用程序是很常见的应用程序,常用的配置途径是使用配置文件或命令行选项;但容器化应用是基于镜像文件启动,其配置方式有别于此两种途径。

  配置容器化应用程序常用的方法有以下几种:  
    (1)将设置好的配置文件硬编码进镜像中;  
    (2)基于环境变量:  
      Docker环境变量:Dockerfile中定义。  
      Kubernetes环境变量:资源配置文件中定义。  
    (3)使用Kubernetes的ConfigMap和Secret实现集中式配置(配置中心)。

二.基于环境变量引用ConfigMap实现配置容器案例(弊端:当ConfigMap文件的内容发生改变时已经创建的容器不会实时更新配置)

1>.使用命令行创建configmap对象

[root@master200.yinzhengjie.org.cn ~]# kubectl get namespace
NAME                 STATUS   AGE
default              Active   5d11h
ingress-nginx        Active   41h
kube-node-lease      Active   5d11h
kube-public          Active   5d11h
kube-system          Active   5d11h
myservice            Active   46h
testing              Active   2d15h
testing2             Active   2d5h
yinzhengjie-eshop    Active   33h
yinzhengjie-ns       Active   36h
yinzhengjie-volume   Active   24h
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl create namespace yinzhengjie-config
namespace/yinzhengjie-config created
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get namespace
NAME                 STATUS   AGE
default              Active   5d11h
ingress-nginx        Active   41h
kube-node-lease      Active   5d11h
kube-public          Active   5d11h
kube-system          Active   5d11h
myservice            Active   46h
testing              Active   2d15h
testing2             Active   2d5h
yinzhengjie-config   Active   2s
yinzhengjie-eshop    Active   33h
yinzhengjie-ns       Active   36h
yinzhengjie-volume   Active   24h
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl create namespace yinzhengjie-config

[root@master200.yinzhengjie.org.cn ~]# kubectl get namespace
NAME                 STATUS   AGE
default              Active   5d11h
ingress-nginx        Active   41h
kube-node-lease      Active   5d11h
kube-public          Active   5d11h
kube-system          Active   5d11h
myservice            Active   46h
testing              Active   2d15h
testing2             Active   2d5h
yinzhengjie-config   Active   50s
yinzhengjie-eshop    Active   33h
yinzhengjie-ns       Active   36h
yinzhengjie-volume   Active   24h
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl create configmap filebeat-cfg -n yinzhengjie-config --from-literal=redis_hostname="redis.default.service.cluster.local" --from-literal=log_level="Info"
configmap/filebeat-cfg created
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get configmap -n yinzhengjie-config
NAME           DATA   AGE
filebeat-cfg   2      28s
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl create configmap filebeat-cfg -n yinzhengjie-config --from-literal=redis_hostname="redis.default.service.cluster.local" --from-literal=log_level="Info"

[root@master200.yinzhengjie.org.cn ~]# kubectl get configmap -n yinzhengjie-config
NAME           DATA   AGE
filebeat-cfg   2      28s
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get configmap -n yinzhengjie-config -o yaml
apiVersion: v1
items:
- apiVersion: v1
  data:
    log_level: Info
    redis_hostname: redis.default.service.cluster.local
  kind: ConfigMap
  metadata:
    creationTimestamp: "2020-02-09T23:39:57Z"
    name: filebeat-cfg
    namespace: yinzhengjie-config
    resourceVersion: "501375"
    selfLink: /api/v1/namespaces/yinzhengjie-config/configmaps/filebeat-cfg
    uid: a90c2a63-53af-4e2a-bb65-c1c378ef017c
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl get configmap -n yinzhengjie-config -o yaml

2>.创建Pod并配置咱们自定义的configMap资源

[root@master200.yinzhengjie.org.cn ~]# vim /yinzhengjie/data/k8s/manifests/basic/configmap/pod-cfg.yaml
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/k8s/manifests/basic/configmap/pod-cfg.yaml
apiVersion: v1
kind: Pod
metadata:
  name: pod-cfg-dome
  namespace: yinzhengjie-config
spec:
  containers:
  - name: filebeat
    image: ikubernetes/filebeat:5.6.5-alpine
    env:
    - name: REDIS_HOST
      valueFrom:
        configMapKeyRef:
          name: filebeat-cfg
          key: redis_hostname
    - name: LOG_LEVEL
      valueFrom:
        configMapKeyRef:
          name: filebeat-cfg
          key: log_level
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# vim /yinzhengjie/data/k8s/manifests/basic/configmap/pod-cfg.yaml

[root@master200.yinzhengjie.org.cn ~]# kubectl apply -f /yinzhengjie/data/k8s/manifests/basic/configmap/pod-cfg.yaml
pod/pod-cfg-dome created
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get pods -n yinzhengjie-config
NAME           READY   STATUS    RESTARTS   AGE
pod-cfg-dome   1/1     Running   0          14s
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl apply -f /yinzhengjie/data/k8s/manifests/basic/configmap/pod-cfg.yaml

3>.验证传值是否成功

[root@master200.yinzhengjie.org.cn ~]# kubectl get pods -n yinzhengjie-config
NAME           READY   STATUS    RESTARTS   AGE
pod-cfg-dome   1/1     Running   0          6m43s
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl exec -it pod-cfg-dome -n yinzhengjie-config -- /bin/sh
/ # 
/ # printenv
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
LOG_LEVEL=Info
HOSTNAME=pod-cfg-dome
SHLVL=1
HOME=/root
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
REDIS_HOST=redis.default.service.cluster.local
FILEBEAT_VERSION=5.6.5
/ # 
/ # exit
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get configmap -n yinzhengjie-config -o yaml
apiVersion: v1
items:
- apiVersion: v1
  data:
    log_level: Info
    redis_hostname: redis.default.service.cluster.local
  kind: ConfigMap
  metadata:
    creationTimestamp: "2020-02-09T23:39:57Z"
    name: filebeat-cfg
    namespace: yinzhengjie-config
    resourceVersion: "501375"
    selfLink: /api/v1/namespaces/yinzhengjie-config/configmaps/filebeat-cfg
    uid: a90c2a63-53af-4e2a-bb65-c1c378ef017c
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl exec -it pod-cfg-dome -n yinzhengjie-config -- /bin/sh

4>.Pod运行之后再次修改configmap文件Pod中容器对应的变量是不会发生改变的哟

[root@master200.yinzhengjie.org.cn ~]# kubectl get cm -n yinzhengjie-config -o yaml
apiVersion: v1
items:
- apiVersion: v1
  data:
    log_level: Info
    redis_hostname: redis.default.service.cluster.local
  kind: ConfigMap
  metadata:
    creationTimestamp: "2020-02-09T23:39:57Z"
    name: filebeat-cfg
    namespace: yinzhengjie-config
    resourceVersion: "501375"
    selfLink: /api/v1/namespaces/yinzhengjie-config/configmaps/filebeat-cfg
    uid: a90c2a63-53af-4e2a-bb65-c1c378ef017c
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl edit cm filebeat-cfg -n yinzhengjie-config
configmap/filebeat-cfg edited
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get cm -n yinzhengjie-config -o yaml
apiVersion: v1
items:
- apiVersion: v1
  data:
    log_level: Notice
    redis_hostname: redis.default.service.cluster.local
  kind: ConfigMap
  metadata:
    creationTimestamp: "2020-02-09T23:39:57Z"
    name: filebeat-cfg
    namespace: yinzhengjie-config
    resourceVersion: "505489"
    selfLink: /api/v1/namespaces/yinzhengjie-config/configmaps/filebeat-cfg
    uid: a90c2a63-53af-4e2a-bb65-c1c378ef017c
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl edit cm filebeat-cfg -n yinzhengjie-config          #修改configmap的配置文件

[root@master200.yinzhengjie.org.cn ~]# kubectl get pods -n yinzhengjie-config
NAME           READY   STATUS    RESTARTS   AGE
pod-cfg-dome   1/1     Running   0          14m
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get cm -n yinzhengjie-config -o yaml
apiVersion: v1
items:
- apiVersion: v1
  data:
    log_level: Notice
    redis_hostname: redis.default.service.cluster.local
  kind: ConfigMap
  metadata:
    creationTimestamp: "2020-02-09T23:39:57Z"
    name: filebeat-cfg
    namespace: yinzhengjie-config
    resourceVersion: "505489"
    selfLink: /api/v1/namespaces/yinzhengjie-config/configmaps/filebeat-cfg
    uid: a90c2a63-53af-4e2a-bb65-c1c378ef017c
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl exec -it pod-cfg-dome -n yinzhengjie-config -- /bin/sh
/ # 
/ # printenv
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
LOG_LEVEL=Info
HOSTNAME=pod-cfg-dome
SHLVL=1
HOME=/root
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
REDIS_HOST=redis.default.service.cluster.local
FILEBEAT_VERSION=5.6.5
/ # 
/ # exit
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl exec -it pod-cfg-dome -n yinzhengjie-config -- /bin/sh

三.基于配置文件引用ConfigMap实现配置容器案例(此时ConfigMap类似充当了一个配置中心,即凡是使用该ConfigMap资源的Pod均会自动读取同一份配置文件)

1>.创建nginx的配置文件

[root@master200.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/k8s/manifests/basic/configmap/server01.conf 
server {
    server_name master.yinzhengjie.org.cn;
    listen 80;
    location / {
        root "/yinzhengjie/master/html/";
    }
}
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/k8s/manifests/basic/configmap/server01.conf

[root@master200.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/k8s/manifests/basic/configmap/server02.conf 
server {
    server_name master200.yinzhengjie.org.cn;
    listen 80;
    location / {
        root "/yinzhengjie/master200/html/";
    }
}
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/k8s/manifests/basic/configmap/server02.conf

2>.使用命令行方式创建ConfigMap资源

[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/k8s/manifests/basic/configmap/server01.conf 
server {
    server_name master.yinzhengjie.org.cn;
    listen 80;
    location / {
        root "/yinzhengjie/master/html/";
    }
}
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/k8s/manifests/basic/configmap/server02.conf 
server {
    server_name master200.yinzhengjie.org.cn;
    listen 80;
    location / {
        root "/yinzhengjie/master200/html/";
    }
}
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get cm -n yinzhengjie-config
NAME           DATA   AGE
filebeat-cfg   2      83m
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl create configmap nginx-cfg --from-file=/yinzhengjie/data/k8s/manifests/basic/configmap/server01.conf --from-file=server-second.conf=/yinzhengjie/data/k8s/manifests/basic/configmap/server02.conf -n yinzhengjie-config
configmap/nginx-cfg created
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get cm -n yinzhengjie-config
NAME           DATA   AGE
filebeat-cfg   2      83m
nginx-cfg      2      2s
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl create configmap nginx-cfg --from-file=/yinzhengjie/data/k8s/manifests/basic/configmap/server01.conf --from-file=server-second.conf=/yinzhengjie/data/k8s/manifests/basic/configmap/server02.conf -n yinzhengjie-config

[root@master200.yinzhengjie.org.cn ~]# kubectl get cm nginx-cfg -n yinzhengjie-config -o yaml
apiVersion: v1
data:
  server-second.conf: "server {ntserver_name master200.yinzhengjie.org.cn;ntlisten
    80;ntlocation / {nttroot "/yinzhengjie/master200/html/";nt}n}n"
  server01.conf: "server {ntserver_name master.yinzhengjie.org.cn;ntlisten 80;ntlocation
    / {nttroot "/yinzhengjie/master/html/";nt}n}n"
kind: ConfigMap
metadata:
  creationTimestamp: "2020-02-10T01:03:22Z"
  name: nginx-cfg
  namespace: yinzhengjie-config
  resourceVersion: "514708"
  selfLink: /api/v1/namespaces/yinzhengjie-config/configmaps/nginx-cfg
  uid: 3b5431ff-26c0-419d-9f26-f646f5b2ed06
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl get cm nginx-cfg -n yinzhengjie-config -o yaml

3>.创建Pod时应用ConfigMap存储卷

[root@master200.yinzhengjie.org.cn ~]# vim /yinzhengjie/data/k8s/manifests/basic/configmap/pod-mynginx.yaml 
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# cat /yinzhengjie/data/k8s/manifests/basic/configmap/pod-mynginx.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: mynginx
  namespace: yinzhengjie-config
spec:
  containers:
  - name: mynginx
    image: nginx:1.14-alpine
    volumeMounts:
    - name: config
      mountPath: /etc/nginx/conf.d/
  volumes:
  - name: config
    configMap:
      name: nginx-cfg
      items:
      - key: server01.conf
        path: server-first.conf
      - key: server-second.conf
        path: server-second.conf
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# vim /yinzhengjie/data/k8s/manifests/basic/configmap/pod-mynginx.yaml

[root@master200.yinzhengjie.org.cn ~]# kubectl get pods -n yinzhengjie-config -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP            NODE                         NOMINATED NODE   READINESS GATES
pod-cfg-dome   1/1     Running   0          62m   10.244.1.27   node201.yinzhengjie.org.cn   <none>           <none>
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl apply -f /yinzhengjie/data/k8s/manifests/basic/configmap/pod-mynginx.yaml 
pod/mynginx created
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get pods -n yinzhengjie-config -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP            NODE                         NOMINATED NODE   READINESS GATES
mynginx        1/1     Running   0          1s    10.244.1.30   node201.yinzhengjie.org.cn   <none>           <none>
pod-cfg-dome   1/1     Running   0          62m   10.244.1.27   node201.yinzhengjie.org.cn   <none>           <none>
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl apply -f /yinzhengjie/data/k8s/manifests/basic/configmap/pod-mynginx.yaml

[root@master200.yinzhengjie.org.cn ~]# kubectl get pods -n yinzhengjie-config -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP            NODE                         NOMINATED NODE   READINESS GATES
mynginx        1/1     Running   0          1s    10.244.1.30   node201.yinzhengjie.org.cn   <none>           <none>
pod-cfg-dome   1/1     Running   0          62m   10.244.1.27   node201.yinzhengjie.org.cn   <none>           <none>
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl exec mynginx -it -n yinzhengjie-config -- /bin/sh
/ # 
/ # cd /etc/nginx/conf.d/
/etc/nginx/conf.d # 
/etc/nginx/conf.d # ls
server-first.conf   server-second.conf
/etc/nginx/conf.d # 
/etc/nginx/conf.d # ls -l
total 0
lrwxrwxrwx    1 root     root            24 Feb 10 01:15 server-first.conf -> ..data/server-first.conf
lrwxrwxrwx    1 root     root            25 Feb 10 01:15 server-second.conf -> ..data/server-second.conf
/etc/nginx/conf.d # 
/etc/nginx/conf.d # cat server-first.conf 
server {
    server_name master.yinzhengjie.org.cn;
    listen 80;
    location / {
        root "/yinzhengjie/master/html/";
    }
}
/etc/nginx/conf.d # 
/etc/nginx/conf.d # cat server-second.conf 
server {
    server_name master200.yinzhengjie.org.cn;
    listen 80;
    location / {
        root "/yinzhengjie/master200/html/";
    }
}
/etc/nginx/conf.d # 
/etc/nginx/conf.d # netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      
/etc/nginx/conf.d # 
/etc/nginx/conf.d #

[root@master200.yinzhengjie.org.cn ~]# kubectl exec mynginx -it -n yinzhengjie-config -- /bin/sh

4>.修改ConfigMap的配置文件,验证容器中的配置是否发生改变

[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl get cm -n yinzhengjie-config 
NAME           DATA   AGE
filebeat-cfg   2      102m
nginx-cfg      2      19m
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl edit cm nginx-cfg -n yinzhengjie-config
configmap/nginx-cfg edited
[root@master200.yinzhengjie.org.cn ~]#

[root@master200.yinzhengjie.org.cn ~]# kubectl edit cm nginx-cfg -n yinzhengjie-config        #如下图所示,按需修改nginx的ConfigMap的配置文件

[root@master200.yinzhengjie.org.cn ~]# kubectl get pods -n yinzhengjie-config -o wide
NAME           READY   STATUS    RESTARTS   AGE   IP            NODE                         NOMINATED NODE   READINESS GATES
mynginx        1/1     Running   0          11m   10.244.1.30   node201.yinzhengjie.org.cn   <none>           <none>
pod-cfg-dome   1/1     Running   0          73m   10.244.1.27   node201.yinzhengjie.org.cn   <none>           <none>
[root@master200.yinzhengjie.org.cn ~]# 
[root@master200.yinzhengjie.org.cn ~]# kubectl exec mynginx -it -n yinzhengjie-config -- /bin/sh
/ # 
/ # cd /etc/nginx/conf.d/
/etc/nginx/conf.d # 
/etc/nginx/conf.d # ls
server-first.conf   server-second.conf
/etc/nginx/conf.d # 
/etc/nginx/conf.d # cat server-first.conf 
server {
    server_name master.yinzhengjie.org.cn;
    listen 8080;
    location / {
        root "/yinzhengjie/master/html/";
    }
}
/etc/nginx/conf.d # 
/etc/nginx/conf.d # netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      
/etc/nginx/conf.d # 
/etc/nginx/conf.d # nginx -s reload
2020/02/10 01:27:33 [notice] 25#25: signal process started
/etc/nginx/conf.d # 
/etc/nginx/conf.d # netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      
/etc/nginx/conf.d # 
/etc/nginx/conf.d #

[root@master200.yinzhengjie.org.cn ~]# kubectl exec mynginx -it -n yinzhengjie-config -- /bin/sh          #如下图所示,修改ConfigMap后,Pod中的配置文件也会跟着变化的

四.使用配置清单的方式创建ConfigMap资源

  尽管上面我们使用命令行的方式创建ConfigMap资源非常方便,但可复用性差且不利于追踪问题,生产环境建议大家以配置清单的方式创建ConfigMap资源哟。

  其实我们之前在部署flannel资源时,可能有小伙伴已经发现了一个yaml文件(https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml),该文件中的确有定义ConfigMap资源的案例,如下图所示。

五.Kerbernetes使用Secret资源配置铭感信息

  上面已经简单介绍了ConfigMap基于配置清单和命令行的定义和创建方式。如博客标题一样,ConfigMap适用于配置非铭感的配置信息。类似于用户密码这类的铭感信息不建议使用ConfigMap,而推荐使用Secret。  

  Sercret的使用逻辑和ConfigMap并没什么区别,唯一不同的是Secret是被base64编码后存储的。  

  博主推荐阅读:
    https://www.cnblogs.com/yinzhengjie/p/12297046.html
相关实践学习
通过Ingress进行灰度发布
本场景您将运行一个简单的应用,部署一个新的应用用于新的发布,并通过Ingress能力实现灰度发布。
容器应用与集群管理
欢迎来到《容器应用与集群管理》课程,本课程是“云原生容器Clouder认证“系列中的第二阶段。课程将向您介绍与容器集群相关的概念和技术,这些概念和技术可以帮助您了解阿里云容器服务ACK/ACK Serverless的使用。同时,本课程也会向您介绍可以采取的工具、方法和可操作步骤,以帮助您了解如何基于容器服务ACK Serverless构建和管理企业级应用。 学习完本课程后,您将能够: 掌握容器集群、容器编排的基本概念 掌握Kubernetes的基础概念及核心思想 掌握阿里云容器服务ACK/ACK Serverless概念及使用方法 基于容器服务ACK Serverless搭建和管理企业级网站应用
目录
相关文章
|
6月前
|
Prometheus 监控 前端开发
七步成诗 - 快速创建有效 SLO
七步成诗 - 快速创建有效 SLO
|
2月前
|
Kubernetes 关系型数据库 MySQL
Kerbernetes使用Secret资源配置铭感信息
文章介绍了如何在Kubernetes中使用Secret资源来配置敏感信息,包括基于环境变量引用Secret、创建tls类型Secret和创建镜像仓库类型的Secret的案例。
36 0
Kerbernetes使用Secret资源配置铭感信息
|
3月前
|
存储 Kubernetes Linux
Kubernetes 的配置资源 ConfigMap(01部分)
Kubernetes 的配置资源 ConfigMap(01部分)
|
3月前
|
存储 Kubernetes 数据格式
精通Kubernetes:利用YAML轻松管理资源
精通Kubernetes:利用YAML轻松管理资源
|
3月前
|
Kubernetes 监控 API
在K8S中,什么是静态Pod、自主式Pod、动态Pod?
在K8S中,什么是静态Pod、自主式Pod、动态Pod?
|
6月前
|
运维 Kubernetes Linux
Kubernetes详解(九)——资源配置清单创建Pod实战
Kubernetes详解(九)——资源配置清单创建Pod实战
119 2
|
6月前
|
JSON Kubernetes API
Kubernetes详解(八)——Kubernetes资源配置清单
Kubernetes详解(八)——Kubernetes资源配置清单
83 2
|
6月前
|
Kubernetes Go Perl
k8s 怎么精准获取deployment关联的pods?
该内容是关于Kubernetes中通过标签获取Deployment管理的Pod的流程和代码示例。首先,Deployment创建ReplicaSets,然后ReplicaSets创建Pod。获取Pod的步骤包括:1) 使用标签选择器获取ReplicaSets;2) 过滤出属于特定Deployment的ReplicaSets;3) 通过标签选择器获取Pod;4) 过滤出属于特定ReplicaSets的Pod。提供的Go代码展示了如何实现这一过程。
110 0
|
存储 JSON Kubernetes
k8s crd 自定义资源详解
k8s crd 自定义资源详解
|
Kubernetes 应用服务中间件 API
k8s 资源编排 yaml
作者主页:https://www.couragesteak.com/
k8s 资源编排 yaml