常用創建configmap方式
- 命令行模式
kubectl create configmap nginx-config --from-literal=nginx_port=888 --from-literal=server_name=myapp.test.com
[root@k8s0 ~]# kubectl describe cm nginx-config
Name: nginx-config
Namespace: dev
Labels: <none>
Annotations: <none>
Data
====
server_name:
----
myapp.test.com
nginx_port:
----
888
Events: <none>
- 配置文件方式
vim nginx.conf
server {
server_name myapp.test.com;
listen 889;
root /usr/share/nginx/html;
}
kubectl create configmap nginx-config-file --from-file=nginx.conf
[root@k8s0 nginx]# kubectl describe cm nginx-config-file
Name: nginx-config-file
Namespace: dev
Labels: <none>
Annotations: <none>
Data
====
nginx.conf:
----
server {
server_name myapp.test.com;
listen 889;
root /usr/share/nginx/html;
}
Events: <none>
利用configmap注入環境變量
- 創建nginx.yaml測試文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-configmap
spec:
selector:
matchLabels:
app: nginx-configmap
replicas: 1
template:
metadata:
labels:
app: nginx-configmap
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
env:
- name: NGINX_SEVER_PORT
valueFrom:
configMapKeyRef:
name: nginx-config
key: nginx_port
- name: NGINX_SERVER_NAME
valueFrom:
configMapKeyRef:
name: nginx-config
key: server_name
kubectl apply -f nginx.yaml
這種方式注入的環境變量無法動態修改
領用configmap掛載配置文件
- 創建nginx-file.yaml測試文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-file-configmap
spec:
selector:
matchLabels:
app: nginx-file-configmap
replicas: 1
template:
metadata:
labels:
app: nginx-file-configmap
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 889
volumeMounts:
- name: nginxconf
mountPath: /etc/nginx/conf.d
volumes:
- name: nginxconf
configMap:
name: nginx-config-file
kubectl apply -f nginx-file.yaml
通過掛載的形式可以支持動態修改參數
- 修改configmap的方式
kubectl edit configmap config-name