编辑Yaml文件
[root@k8smaster4 mysql]# cat mysql.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-configmap
labels:
environment: test
app: mysql
data:
up: "1"
lower: "3"
查看编辑结果
[root@k8smaster4 mysql]# kubectl get configmap mysql-configmap
NAME DATA AGE
mysql-configmap 2 20s
[root@k8smaster4 mysql]# kubectl describe cm mysql-configmap
Name: mysql-configmap
Namespace: default
Labels: app=mysql
environment=test
Annotations: <none>
Data
====
lower:
----
3
up:
----
1
BinaryData
====
Events: <none>
创建模拟Pod
[root@k8smaster4 mysql]# cat mysql-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: mysqlcm-test
labels:
environment: test
app: mysql
spec:
containers:
- name: mysql-config
image: docker.io/library/busybox
command: ["/bin/sh", "-c", "sleep 3600"]
imagePullPolicy: IfNotPresent
env:
- name: login_bin
valueFrom:
configMapKeyRef:
name: mysql-configmap
key: up
- name: off_bin
valueFrom:
configMapKeyRef:
name: mysql-configmap
key: lower
restartPolicy: Never
查看创建结果
[root@k8smaster4 mysql]# kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox 0/1 Completed 0 16d
mysqlcm-test 1/1 Running 0 5s
nfs-provisioner-6f77f76894-v96h6 1/1 Running 17 (3d13h ago) 37d
test-storageclass 1/1 Running 5 (3d13h ago) 37d
web-0 1/1 Running 3 (3d13h ago) 16d
web-1 1/1 Running 3 (3d13h ago) 16d
查看env参数
[root@k8smaster4 mysql]# kubectl exec -it mysqlcm-test -c mysql-config -- /bin/sh
/ # printenv
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_SERVICE_PORT=443
off_bin=3
HOSTNAME=mysqlcm-test
SHLVL=1
HOME=/root
NGINX_TEST_SERVICE_HOST=10.105.242.121
NGINX_TEST_SERVICE_PORT=80
NGINX_TEST_PORT=tcp://10.105.242.121:80
TERM=xterm
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
NGINX_TEST_SERVICE_PORT_WEB=80
NGINX_TEST_PORT_80_TCP_ADDR=10.105.242.121
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
login_bin=1
KUBERNETES_PORT_443_TCP_PORT=443
NGINX_TEST_PORT_80_TCP_PORT=80
KUBERNETES_PORT_443_TCP_PROTO=tcp
NGINX_TEST_PORT_80_TCP_PROTO=tcp
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
NGINX_TEST_PORT_80_TCP=tcp://10.105.242.121:80
KUBERNETES_SERVICE_HOST=10.96.0.1
PWD=/