一、创建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. Scriban语言手册中文版

    Scriban是一个快速.强大.安全且轻量级的模板引擎,同时兼容liquid语法规则. 项目地址:https://github.com/scriban/scriban 这个文档是语言语法的中文翻译 原 ...

  2. Abp.Zero 手机号免密登录验证与号码绑定功能的实现(二):改造Abp默认实现

    接下来我们重写原Abp的部分实现,来驳接手机号相关业务. 改造User类 重写PhoneNumber使得电话号码为必填项,和中国大陆手机号11位长度 public new const int MaxP ...

  3. hire 聘用 受雇 租金 单词记忆

    hire 基本解释 vt. 聘用:录用:雇用:租用 vi. 受雇:得到工作 n. 租金:酬金,工钱:[非正式用语] 被雇佣的人:销售部的两个新雇员 来自Proto-Germanic*hurjan,租, ...

  4. 已安装docker-compose,安装harbor时还是提示docker-compose未安装或者Permission denied的解决方案

    安装Harbor时,下载安装了docker-compose并赋予权限 sudo curl -L "https://github.com/docker/compose/releases/dow ...

  5. "高绩效"指南

    前言 最近被问到一个问题,在工作中,如何拿高绩效.或者换一种表达方式,如何成为老板的"嫡系". 在这里我想简单谈一谈我的想法,可能不准确,各位看官,当饭后茶语罢了. 为了更加有说服 ...

  6. Ubuntu 14.04 升级到Gnome3.12z的折腾之旅(警示后来者)+推荐Extensions.-------(一)

    PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明   本文发布于 2014-12-19 22:40:20 ...

  7. awk第一天

    awk第一天 1.用awk 打印整个test.txt (以下操作都是用awk工具实现,针对test.txt) awk '{print}' test.txt [root@master ~]# awk ' ...

  8. Linux文件查找、三剑客、正则表达式

    Linux文件查找 1.find查找概述 为什么要有文件查找,因为很多时候我们可能会忘了某个文件所在的位置,此时就需要通过find来查找. find命令可以根据不同的条件来进行查找文件,例如:文件名称 ...

  9. 07.Android之多媒体问题

    目录介绍 7.0.0.1 加载bitmap图片的时候需要注意什么?为何bitmap容易造成OOM?如何计算Bitmap占用内存? 7.0.0.2 如何理解recycle释放内存问题?图片加载到内存其实 ...

  10. Java原生序列化与反序列化

    序列化与反序列化 Java序列化是指把Java对象转换为字节序列的过程;而Java反序列化是指把字节序列恢复为Java对象的过程. 为什么需要序列化? 序列化分为两大部分:序列化和反序列化.序列化是这 ...