一、创建configmap

1、基于命令创建configmap

root@k8s-master01:~# kubectl create configmap demoapp-cfg --from-literal=listen.port=8080 --from-literal=listen.address='127.0.0.1'
configmap/demoapp-cfg created
root@k8s-master01:~# kubectl get cm
NAME DATA AGE
demoapp-cfg 2 7s
kube-root-ca.crt 1 35h root@k8s-master01:~# kubectl get cm demoapp-cfg -oyaml
apiVersion: v1
data:
listen.address: 127.0.0.1
listen.port: "8080"
kind: ConfigMap
metadata:
creationTimestamp: "2024-01-20T00:55:59Z"
name: demoapp-cfg
namespace: default
resourceVersion: "273698"
uid: 1df08044-46c7-4672-a328-7f81174e27d8

2、基于文件创建nginx-configmap

准备nginx配置文件

root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# pwd
/root/learning-k8s/examples/configmaps_and_secrets
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# ls nginx-conf.d/
myserver-gzip.cfg myserver-status.cfg myserver.conf
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets#
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# ls nginx-conf.d/
myserver-gzip.cfg myserver-status.cfg myserver.conf
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# cat nginx-conf.d/myserver-gzip.cfg
gzip on;
gzip_comp_level 5;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/xml text/javascript;
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# cat nginx-conf.d/myserver-status.cfg
location /nginx-status {
stub_status on;
access_log off;
}
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# cat nginx-conf.d/myserver.conf
server {
listen 8080;
server_name www.ik8s.io; include /etc/nginx/conf.d/myserver-*.cfg; location / {
root /usr/share/nginx/html;
}
}
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl create configmap nginx-cfg --from-file=./nginx-conf.d/myserver.conf  --from-file=./nginx-conf.d/myserver-status.cfg --from-file=./nginx-conf.d/myserver-gzip.cfg --dry-run=client -oyaml
apiVersion: v1
data:
myserver-gzip.cfg: |
gzip on;
gzip_comp_level 5;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/xml text/javascript;
myserver-status.cfg: |
location /nginx-status {
stub_status on;
access_log off;
}
myserver.conf: |
server {
listen 8080;
server_name www.ik8s.io; include /etc/nginx/conf.d/myserver-*.cfg; location / {
root /usr/share/nginx/html;
}
}
kind: ConfigMap
metadata:
creationTimestamp: null
name: nginx-cfg

以下命令同上命令效果相同

root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl create configmap nginx-cfg --from-file=./nginx-conf.d/ --dry-run=client -oyaml
apiVersion: v1
data:
myserver-gzip.cfg: |
gzip on;
gzip_comp_level 5;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/xml text/javascript;
myserver-status.cfg: |
location /nginx-status {
stub_status on;
access_log off;
}
myserver.conf: |
server {
listen 8080;
server_name www.ik8s.io; include /etc/nginx/conf.d/myserver-*.cfg; location / {
root /usr/share/nginx/html;
}
}
kind: ConfigMap
metadata:
creationTimestamp: null
name: nginx-cfg

二、在Pod上引用环境变量

  • 环境变量

    将configmap对象上的某key的值赋值给(valueFrom)指定的环境变量



  • 在Pod上基于configMap卷插件引用configmap对象

    在Container上挂载configMap卷

    每个kv会分别被映射为一个文件,文件名同key,value将成为文件内容

    注意:

    • 通过环境变量引用在Pod创建的时候完成赋值,configmap更改不生效。
    • 通过卷挂载,基于卷引用,configmap可以动态加载。

1、通过环境变量引用configmap

root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# cat configmaps-env-demo.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
name: demoapp-config
namespace: default
data:
demoapp.port: "8080"
demoapp.host: 127.0.0.1
---
apiVersion: v1
kind: Pod
metadata:
name: configmaps-env-demo
namespace: default
spec:
containers:
- image: ikubernetes/demoapp:v1.0
name: demoapp
env:
- name: PORT
valueFrom:
configMapKeyRef:
name: demoapp-config
key: demoapp.port
optional: false
- name: HOST
valueFrom:
configMapKeyRef:
name: demoapp-config
key: demoapp.host
optional: true
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl apply -f configmaps-env-demo.yaml
configmap/demoapp-config created
pod/configmaps-env-demo created
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl get cm
NAME DATA AGE
demoapp-cfg 2 24m
demoapp-config 2 6s
kube-root-ca.crt 1 35h
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl get pod
NAME READY STATUS RESTARTS AGE
configmaps-env-demo 1/1 Running 0 7m27s

2、通过卷挂载configmap

  • 创建configmap
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl create configmap nginx-config-files --from-file=./nginx-conf.d/
configmap/nginx-config-files created
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl get cm
NAME DATA AGE
demoapp-cfg 2 38m
demoapp-config 2 13m
kube-root-ca.crt 1 36h
nginx-config-files 3 6s
  • 创建nginx pod将configmap作为配置文件挂载到指定目录下
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# cat configmaps-volume-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: configmaps-volume-demo
namespace: default
spec:
containers:
- image: nginx:1.22
name: nginx-server
volumeMounts:
- name: ngxconfs
mountPath: /etc/nginx/conf.d/
readOnly: true
volumes:
- name: ngxconfs
configMap:
name: nginx-config-files
optional: false
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl apply -f configmaps-volume-demo.yaml
pod/configmaps-volume-demo created
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl get pod
NAME READY STATUS RESTARTS AGE
configmaps-env-demo 1/1 Running 0 23m
configmaps-volume-demo 1/1 Running 0 5m16s
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl exec -it configmaps-volume-demo bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@configmaps-volume-demo:/# ls /etc/nginx/conf.d/
myserver-gzip.cfg myserver-status.cfg myserver.conf
root@configmaps-volume-demo:/# nginx -T
...
# configuration file /etc/nginx/conf.d/myserver.conf:
server {
listen 8080;
server_name www.ik8s.io; include /etc/nginx/conf.d/myserver-*.cfg; location / {
root /usr/share/nginx/html;
}
} # configuration file /etc/nginx/conf.d/myserver-gzip.cfg:
gzip on;
gzip_comp_level 5;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/xml text/javascript; # configuration file /etc/nginx/conf.d/myserver-status.cfg:
location /nginx-status {
stub_status on;
access_log off;
}
...
  • 访问nginx服务
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
configmaps-env-demo 1/1 Running 0 28m 10.244.2.24 k8s-node01 <none> <none>
configmaps-volume-demo 1/1 Running 0 10m 10.244.2.25 k8s-node01 <none> <none>
root@k8s-master01:~/learning-k8s/examples/configmaps_and_secrets# curl 10.244.2.25:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p> <p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p>
</body>
</html>

k8s之configmap应用的更多相关文章

  1. spring-cloud-kubernetes与k8s的configmap

    本文是<spring-cloud-kubernetes实战系列>的第六篇,主要内容是在kubernetes上部署一个java web应用,该应用使用了spring-cloud-kubern ...

  2. k8s之Configmap与Secret

    ConfigMap:k8s标准资源,将配置文件做成k8s资源,使其它资源可加载其中配置 Secret:实现加密功能的安全配置文件.由多个key:val中组成 创建configmap资源,可直接使用ku ...

  3. k8s中configmap的使用方法

      ConfigMaps允许您将配置构件与映像内容解耦,以保持容器化应用程序的可移植性.本文展示如何创建configmap,修改configmap以及如何把configmap应用于pod. 创建con ...

  4. k8s之configmap和secret

    1.configmap configmap和secret是两种特殊的存储卷,它们不是给pod提供存储空间用的,而是给管理员或者用户提供了从外部向pod内部注入信息的方式. configmap:把配置文 ...

  5. 容器编排系统K8s之ConfigMap、Secret资源

    前文我们了解了k8s上的pv/pvc/sc资源的使用和相关说明,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14188621.html:今天我们主要来聊一下 ...

  6. k8s通过configmap管理应用配置信息

    Secret 可以为 Pod 提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap. ConfigMap 的创建和使用方式与 Secret 非常类 ...

  7. k8s的configMap基本概念及案例

    pod中两种特殊类型的存储卷:secret,configMap  pod.spec.volumes.secret  pod.spec.volumes.configMap多数情况下,这两个存储卷不是给p ...

  8. k8s用 ConfigMap 管理配置(13)

    一.ConfigMap介绍 Secret 可以为 Pod 提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap ConfigMap 的创建和使用方 ...

  9. Kubernetes(K8S) 配置管理-ConfigMap 介绍

    作用:存储不加密数据到 etcd,让 Pod 以变量或者 Volume 挂载到容器中 场景:配置文件 创建配置文件 redis.properties redis.host=127.0.0.1 redi ...

  10. k8s之configmap配置中心

    记录在石墨笔记中,懒得再粘贴了,大家直接移步下面地址 https://shimo.im/docs/ktNM72QPweEEkcWg/

随机推荐

  1. 手把手教你用 NebulaGraph AI 全家桶跑图算法

    前段时间 NebulaGraph 3.5.0 发布,@whitewum 吴老师建议我把前段时间 NebulaGraph 社区里开启的新项目 ng_ai 公开给大家. 所以,就有了这个系列文章,本文是该 ...

  2. 用 NetworkX + Gephi + Nebula Graph 分析<权力的游戏>人物关系(下篇)

    在上一篇[1]中,我们通过 NetworkX 和 Gephi 展示了<权力的游戏>中的人物关系.在本篇中,我们将展示如何通过 NetworkX 访问图数据库 Nebula Graph. N ...

  3. 使用grafana+Prometheus监控时PromQL内置函数详解

    1.Prometheus简介 Prometheus(中文名:普罗米修斯)是由SoundCloud开发的开源监控报警系统和时序列数据库(TSDB). Prometheus使用Go语言开发, 是Googl ...

  4. dnsmasq 本地局域网DNS服务器搭建

    项目背景 因为本地环境需要使用域名进行调试,需要DNS服务器 DNS 机器IP:192.168.5.249   dnsmasq 服务端部署 #01 关闭防火墙 systemctl stop firew ...

  5. kubernetes 1.20版本 二进制部署

    kubernetes 1.20版本 二进制部署 目录 kubernetes 1.20版本 二进制部署 1. 前言 2. 环境准备 2.1 机器规划 2.2 软件版本 3. 搭建集群 3.1 机器基本配 ...

  6. 我为什么使用Linux做开发

    系统选择 目前市面上主流的桌面操作系统在大多数人眼里只有Windows和MacOS,那为什么我没选择它们两呢? 首先,不选MacOS的原因,就是太贵.当然这是我的原因不是苹果的原因,我最早使用Linu ...

  7. Zabbix“专家坐诊”第198期问答汇总

    问题一 Q:请问一下,自带的思科SNMP交换机模板,怎么不监控down的接口? A1:这种一般在自动发现规则里加个过滤器,过滤出IFSTATUS匹配(1|3)的就能实现只发现up的端口了. A2: 1 ...

  8. ncc - koa 后台源码加密打包工具 @vercel/ncc - webpack node打包更正规

    加个重点 webpack 打包更正规 安装 npm i -g @vercel/ncc 又发现一个 https://github.com/zeit/ncc npm i -g @zeit/ncc 卸载之前 ...

  9. Android自定义View学习(1)——基础知识介绍

    原文:Android自定义View学习(1)--基础知识介绍 - Stars-One的杂货小窝 准备学习自定义View,介绍一下先了解了下相关的前置基础知识,特此总结 本系列集合文章链接可访问Andr ...

  10. CC++ 如何确定一个变量的类型(恶心的指针)

    如何确定一个变量的类型 目录 如何确定一个变量的类型 1.如果一个变量声明中没有括号 2.变量声明中有括号 3.检测一下(逐渐变态··· 4.总结 1.如果一个变量声明中没有括号 如果一个变量声明中没 ...