目錄
一、字面值創(chuàng)建
二、通過(guò)文件創(chuàng)建
三、通過(guò)目錄創(chuàng)建
四、通過(guò)yaml文件創(chuàng)建
五、使用configmap設(shè)置環(huán)境變量
六、使用conigmap設(shè)置命令行參數(shù)
七、通過(guò)數(shù)據(jù)卷使用configmap
八、configmap熱更新
? ? ? 在Kubernetes中,ConfigMap是一種存儲(chǔ)配置數(shù)據(jù)的對(duì)象。它允許將配置數(shù)據(jù)分離出來(lái),以便在不改變應(yīng)用程序容器鏡像的情況下進(jìn)行修改。ConfigMap可以存儲(chǔ)以鍵值對(duì)形式表示的配置數(shù)據(jù),比如環(huán)境變量、命令行參數(shù)、配置文件等。
一、字面值創(chuàng)建
kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
kubectl get cm
kubectl describe cm my-config
二、通過(guò)文件創(chuàng)建
kubectl create configmap my-config-2 --from-file=/etc/resolv.conf
kubectl describe cm my-config-2
三、通過(guò)目錄創(chuàng)建
mkdir test
cp /etc/passwd test/
cp /etc/fstab test/
ls test/
kubectl create configmap my-config-3 --from-file=test
kubectl describe cm my-config-3
四、通過(guò)yaml文件創(chuàng)建
vim cm1.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: cm1-config
data:
db_host: "172.25.0.250"
db_port: "3306"
kubectl apply -f cm1.yaml
kubectl describe cm cm1-config
五、使用configmap設(shè)置環(huán)境變量
vim pod1.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod1
spec:
containers:
- name: pod1
image: busybox
command: ["/bin/sh", "-c", "env"]
env:
- name: key1
valueFrom:
configMapKeyRef:
name: cm1-config
key: db_host
- name: key2
valueFrom:
configMapKeyRef:
name: cm1-config
key: db_port
restartPolicy: Never
kubectl apply -f pod1.yaml
kubectl logs pod1
kubectl delete pod pod1
vim pod2.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod2
spec:
containers:
- name: pod2
image: busybox
command: ["/bin/sh", "-c", "env"]
envFrom:
- configMapRef:
name: cm1-config
restartPolicy: Never
kubectl apply -f pod2.yaml
kubectl logs pod2
kubectl delete pod pod2
六、使用conigmap設(shè)置命令行參數(shù)
vim pod3.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod3
spec:
containers:
- name: pod3
image: busybox
command: ["/bin/sh", "-c", "echo $(db_host) $(db_port)"]
envFrom:
- configMapRef:
name: cm1-config
restartPolicy: Never
kubectl apply -f pod3.yaml
kubectl logs pod3
kubectl delete pod pod3
?
七、通過(guò)數(shù)據(jù)卷使用configmap
vim pod4.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod4
spec:
containers:
- name: pod4
image: busybox
command: ["/bin/sh", "-c", "cat /config/db_host"]
volumeMounts:
- name: config-volume
mountPath: /config
volumes:
- name: config-volume
configMap:
name: cm1-config
restartPolicy: Never
kubectl apply -f pod4.yaml
kubectl logs pod4
kubectl delete pod pod4
八、configmap熱更新
vim nginx.conf
server {
listen 8000;
server_name _;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
kubectl create configmap nginxconf --from-file=nginx.conf
kubectl describe cm nginxconf
vim my-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/conf.d
volumes:
- name: config-volume
configMap:
name: nginxconf
kubectl apply -f my-nginx.yaml
kubectl get pod -o wide
kubectl exec my-nginx-85fb986977-mqtl9 -- cat /etc/nginx/conf.d/nginx.conf
curl 10.244.109.75:8000
編輯cm,修改端口
kubectl edit cm nginxconf
方式一:(推薦)
kubectl delete pod my-nginx-85fb986977-mqtl9
kubectl get pod -o wide
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-737936.html
方式二:(手動(dòng)觸發(fā)版本更新,會(huì)新建一個(gè)replicaset)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-737936.html
kubectl patch deployments.apps my-nginx --patch '{"spec": {"template": {"metadata": {"annotations": {"version/config": "20230312"}}}}}'
到了這里,關(guān)于kubernetes存儲(chǔ)-configmap的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!