configmap资源类型与用法

confimap是用来存放k8s当中的配置文件或变量的,比如nginx的配置文件,或pod中的环境变量

步骤
一、创建configmap
命令行方式
申明方式
二、把创建的configmap关联到pod中

一、命令行创建configmap
1、以键值对的方式创建configmap 【–from-literal=key1=value1】

(1) 以键值对的方式创建configmap 【--from-literal=key1=value1】
# kubectl create configmap wenqiang-configmap --from-literal=name="wenqiang" --from-literal=name2="alix"
# kubectl get configmap
NAME                 DATA   AGE
wenqinag-configmap   1      3s   # 查看创建的configmap

(2) 查看configmap格式 
# kubectl get configmaps wenqinag-configmap -o yaml
apiVersion: v1
kind: ConfigMap
data:              # 主要内容都在data下面
  name: wenqiang   # 格式是key:value
  name2: alix
metadata:          # 元数据
  creationTimestamp: "2023-12-22T03:52:10Z"  # 创建时间
  name: wenqinag-configmap                   # configmap名称
  namespace: default                         # 名称空间
  resourceVersion: "113445"                  # 资源版本号
  uid: 35070bfc-06d1-47e7-91a7-385157993d3d  # 资源uid号

2、以普通文件的方式创建configmap

(1) 以普通文件的方式创建configmap
[root@master1 tmp]# cat nginx.conf 
upstream backend {
  server 192.168.1.111;
  server 192.168.1.112;
}
server {
   sername: www.qiang.com;
   listen: 80;
   location / {
     proxy_pass http://backend;
   }
}
(2) 查看configmap
[root@master1 tmp]# kubectl create configmap nginx-cm --from-file=./nginx.conf 
configmap/nginx-cm created
[root@master1 tmp]# kubectl get configmap nginx-cm -o yaml
apiVersion: v1
data:
  nginx.conf: |          # 文件名|用于把文件名和内容隔开
    upstream backend {
      server 192.168.1.111;
      server 192.168.1.112;
    }
    server {
       sername: www.qiang.com;
       listen: 80;
       location / {
         proxy_pass http://backend;
       }
    }
kind: ConfigMap
metadata:
  creationTimestamp: "2023-12-22T06:37:02Z"
  name: nginx-cm
  namespace: default
  resourceVersion: "130970"
  uid: 03ac2855-c4d5-441b-85da-b55904a35f5f

3、以环境变量方式创建configmap

(1) 先创建一个文件并存放好环境变量内容
# cat env.txt 
version=3.0
name=wenqiang
(2) 去创建configmap
# kubectl create configmap name-cm --from-env-file=./env.txt 
configmap/name-cm created
# kubectl get configmap 
NAME               DATA   AGE
kube-root-ca.crt   1      2d
name-cm            2      41s
nginx-cm           2      24m
[root@master1 tmp]# kubectl get configmap name-cm -o yaml
apiVersion: v1
data:
  name: wenqiang
  version: "3.0"
kind: ConfigMap
metadata:
  creationTimestamp: "2023-12-22T07:07:28Z"
  name: name-cm
  namespace: default
  resourceVersion: "134209"
  uid: 579c1985-ea02-41c5-8a1f-5ef31edc5076

(3) 还可以以目录方式创建configmap
# kubectl create configmap -h
--from-file=./directory   # 可以把目录中的所有文件加载进configmap中,比如目录中有多个env.txt

二、申明式创建configmap

(1) 复制现有的configmap文件并导出,然后修改内容再创建configmap
# kubectl get configmap nginx-cm -o yaml > test.txt
# vim test.txt 修改内容
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-dp-cm
  namespace: default
data:
  version: 1.20.1
  name: wenqinag
  nginx.conf: |
    upstream backend {
      server 192.168.1.1;
      server 192.168.1.2;
    }

    server {
       sername: www.weqiang.com;
       listen: 80;
       location / {
         proxy_pass http://backend;
       }
    }

(2) 创建configmap
# kubectl create configmap newname --from-file=./test.txt  # 创建configmap
# kubectl apply -f test.txt                                # 或直接通过这种方式创建 

三、configmap关联到pod中

  • configmap有两种引用方式,第一种是通过变量引用到pod中,第二种是通过volume方式挂载到pod中

1、通过变量方式引用到pod中,然后再pod中就可以通过env查看环境变量了

  • 这种方式如果configmap有多个变量,pod中就要引用多次,会很麻烦
(1) 把configmap和pod写在一个文件中
# cat nginx-dp.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-dp-cm         # cm名称  
  namespace: default
data:
  host: 1.1.1.1             # 定义变量
  port: "8888"
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dp
  namespace: default
spec:
  selector:
    matchLabels:
      pord: mynginx
  replicas: 3
  template:
    metadata:
      labels:
        pord: mynginx
    spec:
      containers:
      - name: my-nginx
        ports:
        - containerPort: 80
          protocol: TCP
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        securityContext:
          privileged: true
        env:                 # 创建环境变量
        - name: HOST         # pod中创建一个环境变量名字叫HOST
          valueFrom:         # 表示要通过环境变量的方式调用configmap
            configMapKeyRef: # 定义pod启动时要引用哪个configmap
              name: nginx-dp-cm  # 要引用的configmap名称
              key: host          # 要引用configmap中的哪个key,pod启动时会把这个key的值赋值给HOST
              optional: true
        - name: PORT             # 创建第二个环境变量
          valueFrom:
            configMapKeyRef:
              name: nginx-dp-cm
              key: port
              optional: true


(2) 启动并查看pod中的环境变量
# kubectl apply -f nginx-dp.yaml
# kubectl get configmap
nginx-dp-cm        2      4m41s
# kubectl exec -it  nginx-dp-9857cbdd8-lblnx -- env
HOST=1.1.1.1     # pod中的程序就可以调用这个环境变量
PORT=8888

2、通过volume方式挂载到pod中,这种方式会把configmap中的内容引入到pod中的文件里面
• 如果通过edit修改了configmap内容并执行kubectl apply -f后,需要查看内部文件一次才会发送变化。
• 比如nginx通过volume挂载的方式有两种:一种是只挂载虚拟主机,一种是挂载nginx.conf住配置文件
• configmap如果怕写不好格式可以先把nginx.conf创建成测试configmap,然后再复制里面内容
2.1 第一种只替换nginx.conf主配置文件

# cat nginx-dp.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config       # configmap名称 通过kubectl get cm查看
  namespace: default
data:
  index.html: welcome to nginx v1.21!    # 定义html页面的key:value
  nginx.conf: |                          # 定义nginx主配置文件key:value,内容要来源于nginx镜像内容
    user  nginx;
    worker_processes  auto;

    error_log  /var/log/nginx/error.log notice;
    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;

    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dp
  namespace: default
spec:
  selector:
    matchLabels:
      pord: mynginx
  replicas: 3
  template:
    metadata:
      labels:
        pord: mynginx
    spec:
      volumes:                   # 宿主机创建卷组
        - name: volume-config    # 宿主机第一个卷组名称
          configMap:             # 宿主机引用confimap
            name: nginx-config   # 引用哪个configmap
            items:
            - key: nginx.conf   # 引用configmap中哪个key
              path: nginx.conf  # 和key的名字保持一致
              mode: 0644
        - name: index-config
          configMap:
            name: nginx-config
            items:
            - key: index.html
              path: index.html
              mode: 0644
      containers:
      - name: my-nginx
        ports:
        - containerPort: 80
          protocol: TCP
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        securityContext:
          privileged: true
        volumeMounts:             # 创建pod卷组
        - name: volume-config     # pod中第一个卷组名称
          mountPath: /etc/nginx/nginx.conf    # 注意这里必须这样写,路径要写到配置文件
          subPath: nginx.conf     # 这里必须这样写要和上面path对应
        - name: index-config      # pod中第二个卷组名称
          mountPath: /usr/share/nginx/html/index.html   # 这里最好也写全
          subPath: index.html
          
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-dp-svc
  namespace: default
spec:
  #externalTrafficPolicy: Local
  type: NodePort
  ports:
    - port: 81
      targetPort: 80
      nodePort: 30379
  selector:
    pord: mynginx

2.2 第二种增加虚拟主机文件
• 这里配置了2个虚拟主机和两个index页面

[root@master1 tmp]# cat nginx-dp.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config    # configmap名称 通过kubectl get cm查看
  namespace: default
data:
  index.html: welcome to nginx www.qiang.com!    # 定义2个页面
  index2.html: welcome to nginx www.wen.com!
  www_qiang_com.conf: |                          # 定义第一个虚拟主机
    server {
      listen 80;
      server_name www.qiang.com;
      location / {
        index index.html;
        root /usr/share/nginx/html80;
      }
    }
  www_wen_com.conf: |                          # 定义第二个虚拟主机
    server {
      listen 83;
      server_name www.wen.com;
      location / {
        index index.html;
        root /usr/share/nginx/html83;
      }
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dp
  namespace: default
spec:
  selector:
    matchLabels:
      pord: mynginx
  replicas: 3
  template:
    metadata:
      labels:
        pord: mynginx
    spec:
      volumes:                     # 在宿主机创建卷组
        - name: volume-config      # 宿主机第一个卷组名称
          configMap:               # 宿主机调用configmap
            name: nginx-config     # 调用哪个configmap
            items:
            - key: www_qiang_com.conf  # 调用configmap中的哪个key
              path: www_qiang_com.conf # 和key的名字保持一致
              mode: 0644
        - name: index-config       # 宿主机第二个卷组名称
          configMap:
            name: nginx-config
            items:
            - key: index.html
              path: index.html
              mode: 0644

        - name: volume83-config   # 宿主机第三个卷组名称
          configMap:
            name: nginx-config
            items:
            - key: www_wen_com.conf
              path: www_wen_com.conf
        - name: index83-config     # 宿主机第四个卷组名称
          configMap:
            name: nginx-config
            items:
            - key: index2.html
              path: index2.html    # 和key的名字保持一致
      containers:
      - name: my-nginx
        ports:
        - name: nginx-80
          containerPort: 80
          protocol: TCP
        - name: nginx-83
          containerPort: 83
          protocol: TCP
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        securityContext:
          privileged: true
        volumeMounts:          # 在pod中创建卷组
        - name: volume-config  # pod中第一个卷组名称要对应容器中的卷组名称(意为引用容器中卷组对应的configmap)
          mountPath: /etc/nginx/conf.d/www_qiang_com.conf   # 表示把cm中的key挂载到这里路径要写全,如果这里的名字改成default.conf会替换掉pod中默认的default.conf内容,这个名字也可以改成其他名字相当于修改名称
          subPath: www_qiang_com.conf                       # 对应宿主机卷组中的path
         
        - name: index-config
          mountPath: /usr/share/nginx/html80/index.html   # 这里的html80目录不存在会创建
          subPath: index.html

        - name: volume83-config
          mountPath: /etc/nginx/conf.d/www_wen_com.conf
          subPath: www_wen_com.conf

        - name: index83-config
          mountPath: /usr/share/nginx/html83/index.html #这里把cm中的key:index2.html改名为index.html
          subPath: index2.html
          
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-dp-svc
  namespace: default
spec:
  type: NodePort
  ports:
    - name: nginx-80
      port: 80
      targetPort: 80
      protocol: TCP
      nodePort: 30380
    - name: nginx-83
      port: 83
      targetPort: 83
      protocol: TCP
      nodePort: 30383
  selector:
    pord: mynginx

# 启动并查看
# kubectl apply -f nginx-dp.yaml
# kubectl exec -it nginx-dp-8c8cf6dbb-pc7jc -- ls /etc/nginx/conf.d/
default.conf www_qiang_com.conf www_wen_com.conf #如果想把default.conf改名成www_qiang_com.conf见2.3
# kubectl exec -it nginx-dp-9c987d45c-bj9bj -- ls /usr/share/nginx/
html  html80  html83
# kubectl exec -it nginx-dp-9c987d45c-bj9bj -- ls /usr/share/nginx/html80
index.html
# kubectl exec -it nginx-dp-9c987d45c-bj9bj -- ls /usr/share/nginx/html83
index.html
# kubectl describe svc nginx-dp-svc |tail -10
TargetPort:               80/TCP
NodePort:                 nginx-80  30380/TCP
Endpoints:                172.20.136.44:80,172.20.137.101:80,172.20.180.30:80
Port:                     nginx-83  83/TCP
TargetPort:               83/TCP
NodePort:                 nginx-83  30383/TCP
Endpoints:                172.20.136.44:83,172.20.137.101:83,172.20.180.30:83
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
# 访问页面,如果直接访问ip是default内容。
# cat /etc/hosts
192.168.1.111 www.qiang.com
192.168.1.111 www.wen.com
# curl http://www.qiang.com:30380
welcome to nginx www.qiang.com!
# curl http://www.qiang.com:30383
welcome to nginx www.wen.com!

2.3 如果想把default.conf这个文件直接改名为www_qiang_com.conf可以这样做
• 但是这种方式不支持虚拟主机配置文件通过指定key和subPath方式调用

[root@master1 tmp]# cat nginx-dp.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config     # 创建第一个configmap
  namespace: default
data:
  www_qiang_com.conf: |  # 第一个虚拟主机
    server {
      listen 80;
      server_name www.qiang.com;
      location / {
        index index.html;
        root /usr/share/nginx/html;
      }
    }
  www_wen_com.conf: |      # 第二个虚拟主机
    server {
      listen 83;
      server_name www.wen.com;
      location / {
        index index.html;
        root /usr/share/nginx/html83;
      }
    }
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-index-cm    # 第二个configmap
  namespace: default
data:
  index.html: welcome to nginx www.qiang.com!  # 存放两个页面文件
  index2.html: welcome to nginx www.wen.com!
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dp
  namespace: default
spec:
  selector:
    matchLabels:
      pord: mynginx
  replicas: 3
  template:
    metadata:
      labels:
        pord: mynginx
    spec:
      volumes:                           # 宿主机创建卷组
        - name: volume-config            # 创建第一个卷组名称
          configMap:
            name: nginx-config           # 引用哪个configmap,要实现直接改名default.conf下面不能下items
        - name: index-config             # 创建第二个卷组名称
          configMap:
            name: nginx-index-cm         # 引用存放页面的configmap
            items:
            - key: index.html            # 引用key
              path: index.html
        - name: index2-config           # 创建第三个卷组名称
          configMap:
            name: nginx-index-cm
            items:
            - key: index2.html
              path: index2.html
      containers:
      - name: my-nginx
        ports:
        - name: nginx-80
          containerPort: 80
          protocol: TCP
        - name: nginx-83
          containerPort: 83
          protocol: TCP
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        securityContext:
          privileged: true
        volumeMounts:               # pod创建卷组
        - name: volume-config       # 引用宿主机卷组
          mountPath: /etc/nginx/conf.d/      # 把configmap中所有key(wen.conf qiang.conf)挂载到这个目录,不要写全部路径
        - name: index-config        # 引用宿主机第二个卷组
          mountPath: /usr/share/nginx/html/index.html
          subPath: index.html
        - name: index2-config
          mountPath: /usr/share/nginx/html83/index.html    # 这里把卷组中的index2.html改为index.html
          subPath: index2.html
                
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-dp-svc
  namespace: default
spec:
  type: NodePort
  ports:
    - name: nginx-80
      port: 80
      targetPort: 80
      protocol: TCP
      nodePort: 30380
    - name: nginx-83
      port: 83
      targetPort: 83
      protocol: TCP
      nodePort: 30383
  selector:
    pord: mynginx


# 启动并查看
# kubectl apply -f nginx-dp.yaml
# kubectl exec -it nginx-dp-6575b48d45-dcpqg -- ls /etc/nginx/conf.d/
www_qiang_com.conf  www_wen_com.conf     # 这里已经没有默认的.defaut.conf
# kubectl exec -it nginx-dp-6575b48d45-dcpqg -- ls /usr/share/nginx/html/
50x.html  index.html
# kubectl exec -it nginx-dp-6575b48d45-dcpqg -- ls /usr/share/nginx/html83/
index.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的爱迪生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值