首页 > 其他分享 >k8s之configmap应用

k8s之configmap应用

时间:2024-01-20 10:12:17浏览次数:31  
标签:configmap configmaps cfg nginx 应用 myserver k8s root

一、创建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>

标签:configmap,configmaps,cfg,nginx,应用,myserver,k8s,root
From: https://www.cnblogs.com/OpenSourceSite/p/17976085

相关文章

  • 应用CRM管理系统的这些好处,您都清楚吗?
    作为大量企业都在使用的管理软件,CRM管理系统的好处不言而喻,通常包括以下几点: 提高销售效率:CRM系统可以帮助销售人员自动化销售流程,提高工作效率。例如,CRM系统可以自动跟踪客户联系记录、创建报价单、发送电子邮件等,从而节省销售人员的时间和精力。提高销售数据的可视化:CRM系统......
  • 销售人员可以从CRM的应用中获得哪些好处?为何要应用CRM?
    客户关系管理不再只是简单的客户关系管理,它可以管理销售流程,并大幅增加销售量。现如今,我们将CRM解决方案视为提高效率、促进销售和将线索转化为客户的有效工具。当CRM被物尽其用时,它已被证明可以大幅提高销售额,提高生产力,从而提高盈利能力。销售可以从CRM获得什么好处?CRM销售管......
  • k8s之构建Mysql和Wordpress集群
    一、实验目的基于Kubernetes集群实现多负载的WordPress应用。将WordPress数据存储在后端Mysql,Mysql实现主从复制读写分离功能。1、准备Kubernetes集群环境root@k8s-master01:~#kubectlgetnodesNAMESTATUSROLESAGEVERSIONk8s-master01Re......
  • k8s之存储卷OpenEBS
    一、OpenEBS简介OpenEBS是一种开源云原生存储解决方案,托管于CNCF基金会,目前该项目处于沙箱阶段。OpenEBS能够将Kubernetes工作节点上可用的住何存储转换为术卷或分布式复制卷。OpenEBS支持两大类卷——本地卷和复制卷。本地卷本地卷,即节点级卷,仅支持在卷所在的节点本地......
  • C# 运算符详解:包含算术、赋值、比较、逻辑运算符及 Math 类应用
    运算符用于对变量和值执行操作。在C#中,有多种运算符可用,包括算术运算符、关系运算符、逻辑运算符等。算术运算符算术运算符用于执行常见的数学运算:intx=100+50;//加法,结果为150inty=x-30;//减法,结果为120intz=x*2;//乘法,结果为300intw=......
  • CAN总线协议简介及其常见的应用领域
    CAN总线协议是一种串行通讯协议,主要用于汽车和工业自动化领域,实现了实时应用的需求。首先,CAN总线协议的基本概念包括报文、信息路由和位速率。在CAN系统中,总线上传输的信息以不同格式的报文发送,但长度有限。CAN总线的位速率根据系统的不同而不同。其次,CAN总线协议具有多主控制的特......
  • 新能源汽车智慧充电桩方案:智能高效的充电桩管理模式及应用场景
    一、行业背景随着全球对环境保护的日益重视,新能源汽车成为了未来的发展趋势。而充电桩作为新能源汽车的核心基础设施,其智慧化的解决方案对于推动新能源汽车的普及和发展至关重要。通过智能化、高效化的充电服务,提高用户体验,满足快速增长的电动汽车充电需求已经成为当前行业发......
  • Docker、K8S
    .Netcore微服务基础1.Docker2.K8S 参考资料1.docker官网https://www.docker.com/products/docker-desktop/2.docker学习教程 https://blog.csdn.net/javaboyweng/article/details/1326220753..netcore微服务之ASP.NETCoreOnDocker https://www.cnblogs.com/edis......
  • 鸿蒙原生应用/元服务实战-AGC中几个菜单栏的关系
    大家是否清楚AGC这几个菜单栏的相互关系?我的元服务:点击后跳转到“我的应用”列表中的“HarmonyOS”页签,并且过滤出元服务。开发者可以在此模块中管理和运营元服务,例如创建元服务、发布元服务等。我的应用:开发者可以在此模块中管理和运营应用,例如创建应用、配置应用信息、发布应用、......
  • 元编程在 Python 中有哪些应用场景
    元编程是一种强大而灵活的程序设计技术,允许我们在运行时动态地创建、检查、操作和扩展代码。在Python中,元编程可以发挥出其优势,并在许多应用场景中提供解决方案。本文将介绍一些常见的元编程应用场景,以帮助您更好地理解和利用这一技术。一、框架和库开发1.类装饰器:通过定义类装饰器......