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