kubernetes之confingmap的使用

常用创建configmap方式

  • 命令行模式
kubectl create configmap nginx-config --from-literal=nginx_port=888 --from-literal=server_name=myapp.test.com
[root@k8s0 ~]# kubectl describe cm nginx-config
Name:         nginx-config
Namespace:    dev
Labels:       
Annotations:  

Data
====
server_name:
----
myapp.test.com
nginx_port:
----
888
Events:  
  • 配置文件方式
vim nginx.conf
server {
        server_name myapp.test.com;
        listen 889;
        root /usr/share/nginx/html;
}
kubectl create configmap nginx-config-file --from-file=nginx.conf
[root@k8s0 nginx]# kubectl describe cm nginx-config-file
Name:         nginx-config-file
Namespace:    dev
Labels:       
Annotations:  

Data
====
nginx.conf:
----
server {
        server_name myapp.test.com;
        listen 889;
        root /usr/share/nginx/html;
}

Events:  

利用configmap注入环境变量

  • 创建nginx.yaml测试文件
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-configmap
spec:
  selector:
    matchLabels:
      app: nginx-configmap
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-configmap
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        env:
        - name: NGINX_SEVER_PORT
          valueFrom:
            configMapKeyRef:
              name: nginx-config
              key: nginx_port
        - name: NGINX_SERVER_NAME
          valueFrom:
            configMapKeyRef:
              name: nginx-config
              key: server_name
kubectl apply -f nginx.yaml

kubernetes之confingmap的使用_第1张图片kubernetes之confingmap的使用_第2张图片这种方式注入的环境变量无法动态修改

领用configmap挂载配置文件

  • 创建nginx-file.yaml测试文件
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-file-configmap
spec:
  selector:
    matchLabels:
      app: nginx-file-configmap
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-file-configmap
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 889
        volumeMounts:
        - name: nginxconf
          mountPath: /etc/nginx/conf.d
      volumes:
      - name: nginxconf
        configMap:
          name: nginx-config-file
kubectl apply -f nginx-file.yaml

kubernetes之confingmap的使用_第3张图片通过挂载的形式可以支持动态修改参数

  • 修改configmap的方式
kubectl edit configmap config-name

你可能感兴趣的:(Kubernetes)