Kubernetes 之 ConfigMap
ConfigMap 定义
ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中。使用时, Pod 可以将其用作环境变量、命令行参数或者存储卷中的配置文件。通过使用 ConfigMap 可以将你的配置数据和应用程序代码分开。
ConfigMap 的创建
-
直接在命令行中指定 Config 参数创建,通过
--from-literal
指定参数shellkubectl create configmap mysql-config --from-literal=MYSQL_PORT=3306 --from-literal=MYSQL_ROOT_PASSWORD=passwd
root@k8s-master1:~# kubectl get cm/mysql-config NAME DATA AGE mysql-config 2 10s root@k8s-master1:~# kubectl describe cm/mysql-config Name: mysql-config Namespace: default Labels: <none> Annotations: <none> Data ==== MYSQL_PORT: ---- 3306 MYSQL_ROOT_PASSWORD: ---- passwd BinaryData ====
-
通过指定文件创建,先创建一个
nginx.conf
文件,然后通过--from-file
指令创建worker_processes 1; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } } }
shellkubectl create configmap nginx-config --from-file nginx/nginx.conf
root@k8s-master1:~# kubectl describe cm/nginx-config Name: nginx-config Namespace: default Labels: <none> Annotations: <none> Data ==== nginx.conf: ---- worker_processes 1;\r \r events {\r worker_connections 1024;\r }\r \r http {\r include /etc/nginx/mime.types;\r default_type application/octet-stream;\r \r server {\r listen 80;\r server_name localhost;\r \r location / {\r root /usr/share/nginx/html;\r index index.html index.htm;\r }\r }\r } BinaryData ==== Events: <none>
-
通过指定文件夹创建,先创建一个
nginx
文件夹,再创建一个index.html
文件,然后把index.html
文件和nginx.conf
文件移动到nginx
文件夹下,然后通过--from-file
指令创建html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Nginx Test</title> </head> <body> Hello World </body> </html>
shellkubectl create configmap nginx-config --from-file nginx/
root@k8s-master1:~# kubectl describe cm/nginx-config Name: nginx-config Namespace: default Labels: <none> Annotations: <none> Data ==== index.html: ---- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Nginx Test</title> </head> <body> Hello World </body> </html> nginx.conf: ---- worker_processes 1;\r \r events {\r worker_connections 1024;\r }\r \r http {\r include /etc/nginx/mime.types;\r default_type application/octet-stream;\r \r server {\r listen 80;\r server_name localhost;\r \r location / {\r root /usr/share/nginx/html;\r index index.html index.htm;\r }\r }\r } BinaryData ====
-
通过 yaml 文件的方式创建
shellkubectl create -f configmap-mysql-nginx.yaml
root@k8s-master1:~# kubectl describe cm/configmap-mysql-nginx Name: configmap-mysql-nginx Namespace: default Labels: app=configmap-mysql-nginx Annotations: <none> Data ==== MYSQL_PORT: ---- 3306 MYSQL_ROOT_PASSWORD: ---- passwd index.html: ---- <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Nginx Test</title> </head> <body> Hello World </body> </html> nginx.conf: ---- worker_processes 1; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } } } BinaryData ==== Events: <none>
ConfigMap 使用
yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-configmap
spec:
containers:
- name: configmap-test
image: busybox
command: [ "/bin/sh", "-c", "sleep 3600" ]
env:
- name: MYSQL_PORT
valueFrom:
configMapKeyRef:
name: configmap-mysql-nginx
key: MYSQL_PORT
- name: MYSQL_ROOT_PASSWORD
valueFrom:
configMapKeyRef:
name: configmap-mysql-nginx
key: MYSQL_ROOT_PASSWORD
envFrom:
- configMapRef:
name: service-config
volumeMounts:
- name: volume-configmap
mountPath: /etc/nginx
volumes:
- name: volume-configmap
configMap:
name: configmap-mysql-nginx
restartPolicy: Never
root@k8s-master1:~# kubectl exec -it pod-configmap -- /bin/sh
/ # env
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
HOSTNAME=pod-configmap
SHLVL=1
HOME=/root
MYSQL_ROOT_PASSWORD=passwd
SERVICE_NAME=configmap-test
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_PROTO=tcp
MYSQL_PORT=3306
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/
/ # ls -al /etc/nginx/
total 12
drwxrwxrwx 3 root root 4096 Jun 1 02:16 .
drwxr-xr-x 1 root root 4096 Jun 1 02:16 ..
drwxr-xr-x 2 root root 4096 Jun 1 02:16 ..2024_06_01_02_16_52.4048032045
lrwxrwxrwx 1 root root 32 Jun 1 02:16 ..data -> ..2024_06_01_02_16_52.4048032045
lrwxrwxrwx 1 root root 17 Jun 1 02:16 MYSQL_PORT -> ..data/MYSQL_PORT
lrwxrwxrwx 1 root root 26 Jun 1 02:16 MYSQL_ROOT_PASSWORD -> ..data/MYSQL_ROOT_PASSWORD
lrwxrwxrwx 1 root root 17 Jun 1 02:16 index.html -> ..data/index.html
lrwxrwxrwx 1 root root 17 Jun 1 02:16 nginx.conf -> ..data/nginx.conf
/ #