k8s之configmap应用
一、创建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应用的更多相关文章
- spring-cloud-kubernetes与k8s的configmap
本文是<spring-cloud-kubernetes实战系列>的第六篇,主要内容是在kubernetes上部署一个java web应用,该应用使用了spring-cloud-kubern ...
- k8s之Configmap与Secret
ConfigMap:k8s标准资源,将配置文件做成k8s资源,使其它资源可加载其中配置 Secret:实现加密功能的安全配置文件.由多个key:val中组成 创建configmap资源,可直接使用ku ...
- k8s中configmap的使用方法
ConfigMaps允许您将配置构件与映像内容解耦,以保持容器化应用程序的可移植性.本文展示如何创建configmap,修改configmap以及如何把configmap应用于pod. 创建con ...
- k8s之configmap和secret
1.configmap configmap和secret是两种特殊的存储卷,它们不是给pod提供存储空间用的,而是给管理员或者用户提供了从外部向pod内部注入信息的方式. configmap:把配置文 ...
- 容器编排系统K8s之ConfigMap、Secret资源
前文我们了解了k8s上的pv/pvc/sc资源的使用和相关说明,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14188621.html:今天我们主要来聊一下 ...
- k8s通过configmap管理应用配置信息
Secret 可以为 Pod 提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap. ConfigMap 的创建和使用方式与 Secret 非常类 ...
- k8s的configMap基本概念及案例
pod中两种特殊类型的存储卷:secret,configMap pod.spec.volumes.secret pod.spec.volumes.configMap多数情况下,这两个存储卷不是给p ...
- k8s用 ConfigMap 管理配置(13)
一.ConfigMap介绍 Secret 可以为 Pod 提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap ConfigMap 的创建和使用方 ...
- Kubernetes(K8S) 配置管理-ConfigMap 介绍
作用:存储不加密数据到 etcd,让 Pod 以变量或者 Volume 挂载到容器中 场景:配置文件 创建配置文件 redis.properties redis.host=127.0.0.1 redi ...
- k8s之configmap配置中心
记录在石墨笔记中,懒得再粘贴了,大家直接移步下面地址 https://shimo.im/docs/ktNM72QPweEEkcWg/
随机推荐
- [QT] 记录一些使用技巧
目录 概述 打开窗口 弹出消息框 判断文件存在 获取时间 获取子控件 TableWidget设置不可编辑 QT QString判断纯数字 Qt 保存文件选择器 读写ini 概述 最近花了好几天的时间编 ...
- FeignClient 报错: A bean with that name has already been defined and overriding is disabled.
1. 错误信息 *************************** APPLICATION FAILED TO START *************************** Descript ...
- 2023 Gartner RPA魔力象限报告解读:国产厂商“破纪录”跃升意味着什么?
2023 Gartner RPA魔力象限报告解读:象限跃升彰显国产RPA厂商实力 2023 Gartner RPA魔力象限报告四大行业趋势,国产RPA厂商已在践行 文/王吉伟 8月3日,全球著名咨询调 ...
- slice 切片数组测试记录【GO 基础】
〇.测试前准备 本文是在 GO 环境下测试记录系列之一,GO 基本环境部署步骤将略过,直接上代码. 下面是常用命令:[初始化 + 运行 + 编译] // {GOPATH} 环境变量值, example ...
- Prometheus技术分享——prometheus的函数与计算公式详解
Prometheus与zabbix相比,它的强大之处就在于可以它可以使用的很多计算公式去获取自己需要的数据.当然,这里所涉及到的计算公式,也是我们普遍认为的难点所在.比如,我们要获取CPU使用率,使用 ...
- Failed to execute goal on project WebBackend: Could not resolve dependencies for project com.lang.yi:WebBackend:jar:1.0.0
一.问题由来 自己在搭建项目的时候报一个错误,如标题所示,具体错误信息如下: Failed to execute goal on project WebBackend: Could not resol ...
- snipaste 替换 微信截图快捷键 F3贴图功能实在是太帅了 - 软件推荐
snipaste 替换 微信截图快捷键 这个软件很久之前就知道,一直也没觉得可以替换微信的截图功能,毕竟能懒就懒. 今天同事又推荐 用了下,觉得确实ok. https://zh.snipaste.co ...
- iview viewdesign 里面的Select 输入添加 query不能从新为空 @on-create 解决方案 v-if 从新刷一遍
iview viewdesign 里面的Select 输入添加 query不能从新为空 @on-create 解决方案 v-if 从新刷一遍
- day18--Java集合01
Java集合01 1.什么是集合? 前面我们保存数据使用的是数组,数组有不足的地方,我们来分析一下: 长度开始时必须指定,而且一但指定不能更改 保存的必须是同一类型的元素 使用数组进行增加/删除元素的 ...
- yum总纲
yum总纲 yum源搭建 首先本文档讲解两种搭建方式,第一有网络环境和第二种无网络环境. 第一种:有网络环境 首先 进入系统执行:以下命令,获取网络源 wget -O /etc/yum.repos.d ...