kubernetes-配置管理ConfigMap

kubernetes-配置管理ConfigMap

ConfigMap介绍

为什么要用configMap?

将配置文件和POD解耦

ConfigMap如何存储配置?

键值对:key:value
文件名:文件内容

ConfigMap支持的配置类型

直接定义的键值对
基于文件创建的键值对

ConfigMap创建方法

命令行
资源配置清单

ConfigMap的配置文件如何传递到POD里

变量传递
数据卷挂载

使用ConifgMap的限制条件

1.ConfigMap必须在pod之前创建,pod才能引用
2.ConfigMap受限于命名空间限制,只有处于同一个命名空间中的pod才可以被引用

命令行创建ConfigMap

键值对配置

# 创建ConfigMap
[root@master-1 ~]# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=www.ljy.com
configmap/nginx-config created

# 查找ConfigMap
[root@master-1 ~]# kubectl get configmap
NAME           DATA   AGE
mysql          2      13h
nginx-config   2      9s

# 也可以缩写为cm
[root@master-1 ~]# kubectl get cm
NAME           DATA   AGE
mysql          2      13h
nginx-config   2      13s

# 查看nginx-config的详细信息
[root@master-1 ~]# kubectl describe cm nginx-config 
Name:         nginx-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx_port:
----
80
server_name:
----
www.ljy.com
Events:  <none>

pod环境变量形式引用ConfigMap

变量传递

# 命令行传递变量
kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=www.ljy.com

# 编写变量传递的ConfigMap
[root@master-1 ~]# vim nginx-cm.conf
apiVersion: v1
kind: Pod
metadata:
  name: nginx-cm
spec:
  containers:
  - name: nginx-pod
    image: nginx:alpine
    env:
    - name: NGINX_PORT
      valueFrom:
        configMapKeyRef:
          name: nginx-config
          key: nginx_port
    - name: SERVER_NAME
      valueFrom:
        configMapKeyRef:
          name: nginx-config
          key: server_name

image-20230927155736377

[root@master-1 ~]# kubectl exec -it nginx-cm sh
/ # echo ${NGINX_PORT}
80
/ # echo ${SERVER_NAME}
www.ljy.com
/ # printenv |egrep "NGINX_PORT|SERVER_NAME"
NGINX_PORT=80
SERVER_NAME=www.ljy.com

命令行创建文件形式的ConfigMap

# 编写配置文件
[root@master-1 ~]# vim www.conf
server {
        listen  80;
        server_name www.ljy.com;
        location / {
                root /usr/share/nginx/html/www;
                index index.html index.htm;
        }
}

# 创建configmap
[root@master-1 ~]# kubectl create configmap nginx-www --from-file=www.conf=./www.conf 
configmap/nginx-www created

# 查看创建的configmap
[root@master-1 ~]# kubectl describe cm nginx-www 
Name:         nginx-www
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
www.conf:
----
server {
  listen  80;
  server_name www.ljy.com;
  location / {
    root /usr/share/nginx/html/www;
    index index.html index.htm;
  }
}

Events:  <none>

挂载ConfigMap

# 编写configmap挂载资源清单
[root@master-1 ~]# vim nginx-www.yaml
apiVersion: v1
kind: Pod
metadata: 
  name: nginx-www
spec:
  volumes:
  - name: nginx-www
    configMap:
     name: nginx-www
     items: 
     - key: www.conf
       path: www.conf
  containers:
  - name: nginx-pod
    image: nginx:alpine
    volumeMounts:
    - name: nginx-www
      mountPath: /etc/nginx/conf.d/

# 连接pod
[root@master-1 ~]# kubectl exec -it nginx-www sh
## 进入nginx配置文件目录      
/ # cd /etc/nginx/conf.d/
/etc/nginx/conf.d # ls
www.conf
## 查看挂载的nginx配置文件
/etc/nginx/conf.d # cat www.conf 
server {
        listen  80;
        server_name www.ljy.com;
        location / {
                root /usr/share/nginx/html/www;
                index index.html index.htm;
        }
}

image-20230927163253371

ConfigMap资源清单(常用)

编辑configmap资源清单

# 编写包含configmap的资源清单
[root@master-1 ~]# vim nginx-configMap.yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: web-config
data:
  www.conf: |
    server {
            listen 80;
            server_name www.ljy.com;
            location / {
                root /usr/share/nginx/html/www;
                index index.html index.htm;
            }
        }
  blog.conf: |
    server {
            listen 80;
            server_name blog.ljy.com;
            location / {
                root /usr/share/nginx/html/blog;
                index index.html index.htm;
            }
        }

# 查看configmap的详细信息
[root@master-1 ~]# kubectl describe cm web-config 
Name:         web-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
blog.conf:
----
server {
        listen 80;
        server_name blog.ljy.com;
        location / {
            root /usr/share/nginx/html/blog;
            index index.html index.htm;
        }
    }

www.conf:
----
server {
        listen 80;
        server_name www.ljy.com;
        location / {
            root /usr/share/nginx/html/www;
            index index.html index.htm;
        }
    }

Events:  <none>

挂载configmap的资源清单

# 编写挂载资源清单
[root@master-1 ~]# vim nginx-web.yml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-web
spec:
  volumes:
    - name: nginx-web
      configMap:
        name: web-config
        items:
          - key: www.conf
            path: www.ljy.com.conf
          - key: blog.conf
            path: blog.ljy.com.conf
  containers:
    - name: nginx-pod
      image: nginx:alpine
      volumeMounts:
        - name: nginx-web
          mountPath: /etc/nginx/conf.d/

# 连接pod
[root@master-1 ~]# kubectl exec -it nginx-web sh
## 进入配置文件目录
/ # cd /etc/nginx/conf.d/
## 查看配置文件目录(每次更新配置文件,都会更新日期文件再软链接到配置文件中)
/etc/nginx/conf.d # ls -la
total 0
drwxrwxrwx    3 root     root           108 Sep 27 08:54 .
drwxr-xr-x    3 root     root           152 Dec 29  2021 ..
drwxr-xr-x    2 root     root            55 Sep 27 08:54 ..2023_09_27_08_54_26.412989534
lrwxrwxrwx    1 root     root            31 Sep 27 08:54 ..data -> ..2023_09_27_08_54_26.412989534
lrwxrwxrwx    1 root     root            24 Sep 27 08:54 blog.ljy.com.conf -> ..data/blog.ljy.com.conf
lrwxrwxrwx    1 root     root            23 Sep 27 08:54 www.ljy.com.conf -> ..data/www.ljy.com.conf

修改configMap方法

## 直接修改cm
[root@master-1 nginx]# kubectl edit cm web-config

## 修改资源清单
[root@master-1 nginx]# vim nginx-cm-www.yaml
[root@master-1 nginx]# kubectl apply -f nginx-cm-www.yaml
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇