一,configMap
ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中。使用时, Pods 可以将其用作环境变量、命令行参数或者存储卷中的配置文件。
ConfigMap 将您的环境配置信息和 容器镜像 解耦,便于应用配置的修改。
注意:
ConfigMap 并不提供保密或者加密功能。 如果你想存储的数据是机密的,请使用 Secret, 或者使用其他第三方工具来保证你的数据的私密性,而不是用 ConfigMap。
ConfigMap 对象
ConfigMap 是一个 API 对象, 让你可以存储其他对象所需要使用的配置。 和其他 Kubernetes 对象都有一个 spec
不同的是,ConfigMap 使用 data
和 binaryData
字段。这些字段能够接收键-值对作为其取值。data
和 binaryData
字段都是可选的。data
字段设计用来保存 UTF-8 字节序列,而 binaryData
则 被设计用来保存二进制数据作为 base64 编码的字串。
ConfigMap 的名字必须是一个合法的 DNS 子域名。
data
或 binaryData
字段下面的每个键的名称都必须由字母数字字符或者 -
、_
或 .
组成。在 data
下保存的键名不可以与在 binaryData
下 出现的键名有重叠。
从 v1.19 开始,你可以添加一个 immutable
字段到 ConfigMap 定义中,创建 不可变更的 ConfigMap
创建一个configmap
使用命令行创建configmap
使用kubectl create configmap -h 查看
Examples:
# Create a new configmap named my-config based on folder bar
kubectl create configmap my-config --from-file=path/to/bar
# Create a new configmap named my-config with specified keys instead of file basenames on disk
kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt
--from-file=key2=/path/to/bar/file2.txt
# Create a new configmap named my-config with key1=config1 and key2=config2
kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
# Create a new configmap named my-config from the key=value pairs in the file
kubectl create configmap my-config --from-file=path/to/bar
# Create a new configmap named my-config from an env file
kubectl create configmap my-config --from-env-file=path/to/bar.env
直接给定键值
root@master:~# kubectl create configmap nginx-config --from-literal=nginx_port=8080 --from-literal=server_name=myapp.linux.com
configmap/nginx-config created
查看comfigmap
root@master:~# kubectl get cm
NAME DATA AGE
nginx-config 2 85s
root@master:~# kubectl describe cm nginx-config
Name: nginx-config
Namespace: default
Labels:
Annotations:
Data
====
nginx_port:
----
8080
server_name:
----
myapp.linux.com
Events:
使用文件创建
简单创建一个文件
root@master:~# cat www.conf
server {
server_name myapp.linux.com;
listen 80;
root /data/web/html/;
}
创建configmap
root@master:~# kubectl create configmap nginx-www --from-file=./www.conf
#注:
--from-file=./www.conf
这里也会被组织成键值数据可以是 --from-file=www=./www.conf 也这里键就是www值就是文件类容 可以是--from-file=./www.conf 这里不指定键文件名当键文件内容当值
root@master:~# kubectl get cm
NAME DATA AGE
nginx-config 2 17m
nginx-www 1 6m3s
查看cm详细内容
root@master:~# kubectl describe cm nginx-www
Name: nginx-www
Namespace: default
Labels:
Annotations:
Data
====
www.conf:
----
server {
server_name myapp.linux.com;
listen 80;
root /data/web/html/;
}
Events:
使用环境变量的方式引用configmap
vim config-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-config-1
namespace: default
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
env:
- name: NGINX_SERVER_PORT
valueFrom:
configMapKeyRef:
name: nginx-config
key: nginx_port
- name: NGINX_SERVER_NAME
valueFrom:
configMapKeyRef:
name: nginx-config
key: server_name
root@master:~# kubectl apply -f config-pod.yaml
root@master:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod-config-1 1/1 Running 0 101s
登录到pod中查看变量
root@master:~# kubectl exec -it pod-config-1 -- /bin/sh
/ # env
MYAPP_SVC_PORT_80_TCP_ADDR=10.98.57.156
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
MYAPP_SVC_PORT_80_TCP_PORT=80
HOSTNAME=pod-config-1
SHLVL=1
MYAPP_SVC_PORT_80_TCP_PROTO=tcp
HOME=/root
NGINX_SERVER_PORT=8080 #看这里已经传递进来了
NGINX_SERVER_NAME=myapp.linux.com
MYAPP_SVC_PORT_80_TCP=tcp://10.98.57.156:80
TERM=xterm
NGINX_VERSION=1.12.2
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
MYAPP_SVC_SERVICE_HOST=10.98.57.156
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT_HTTPS=443
PWD=/
KUBERNETES_SERVICE_HOST=10.96.0.1
MYAPP_SVC_SERVICE_PORT=80
MYAPP_SVC_PORT=tcp://10.98.57.156:80
如果使用edit修改了configmap Pod里的变量是不会改变的,因为pod是创建时引用的环境变量,使用存储卷的方式是可以实时更新的,
使用存储卷的方式传递
apiVersion: v1
kind: Pod
metadata:
name: pod-config-2
namespace: default
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
volumeMounts:
- name: nginxconf
mountPath: /etc/nginx/config.d
readOnly: true
volumes:
- name: nginxconf
configMap:
name: nginx-config
创建pod
kubectl apply -f config-pod-2.yaml
查看pod
root@master:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
pod-config-2 1/1 Running 0 2m55s
登录到pod中查看:
root@master:~# kubectl exec -it pod-config-2 -- /bin/sh
/ # cd /etc/nginx/config.d/
/etc/nginx/config.d # ls -l
total 0
lrwxrwxrwx 1 root root 17 Mar 29 07:30 nginx_port -> ..data/nginx_port
lrwxrwxrwx 1 root root 18 Mar 29 07:30 server_name -> ..data/server_name
/etc/nginx/config.d # cat nginx_port
8080/etc/nginx/config.d #
使用edit修改configmap
root@master:~# kubectl edit cm nginx-config
configmap/nginx-config edited
root@master:~# kubectl describe cm nginx-config
Name: nginx-config
Namespace: default
Labels:
Annotations:
Data
====
nginx_port:
----
8085
server_name:
----
myapp.linux.com
Events:
在登陆pod查看已经修改了
root@master:~# kubectl exec -it pod-config-2 -- /bin/sh
/ # cd /etc/nginx/config.d/
/etc/nginx/config.d # ls
nginx_port server_name
/etc/nginx/config.d # cat nginx_port
8085/etc/nginx/config.d #