TKE中configMap的使用

本篇文章主要介绍了如何在tke集群中挂载configMap到pod中。

一般我们使用configMap主要用途分为2种,一种是挂载configmap中的配置文件进容器里,一种是引用configMap中的键值对作为容器的环境变量。

我们这里测试之前创建了3个测试文件,测试镜像为nginx最新镜像

测试文件

nginx.conf

在配置文件中加入了一行测试注释 #test line,方便我们后续进行验证

apiVersion: v1
data:
  nginx.conf: |2-
    user  nginx;
    worker_processes  1;
    # test line
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    events {
        worker_connections  1024;
    }
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        include /etc/nginx/conf.d/*.conf;
    }
kind: ConfigMap
metadata:
  creationTimestamp: "2020-06-02T09:19:45Z"
  name: nginx-conf
  namespace: test
  resourceVersion: "10479370942"
  selfLink: /api/v1/namespaces/test/configmaps/nginx-conf
  uid: 32ca67ac-a4b2-11ea-9c35-e28957d7d0b3

nginx-env

nginx-env用来测试环境变量的引用

test.txt

如何挂载配置文件到容器内

我们一般挂载文件到容器内分为多种情况:

  • 替换容器内已经存在的某个配置文件

  • 替换容器内不存在的配置文件

  • 替换容器内的某个目录下所有文件

upload-image

对应的yaml文件如下

如何引用configMap的值作为环境变量

upload-image

yaml文件如下

最后更新于

这有帮助吗?