# ConfigMap
# env - valueFrom - configMapKeyRef
[19:36:29 root@master1 storage]#cat 12-storage-configmap-simple-env.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
port: "10086"
user: "www"
---
apiVersion: v1
kind: Pod
metadata:
name: configmap-env-test
spec:
containers:
- name: nginx1
image: 10.0.0.55:80/mykubernetes/nginx:1.21.3
env:
- name: NGINX_HOST
value: "10.0.0.55"
- name: NGINX_PORT
valueFrom:
configMapKeyRef:
name: nginx-config
key: port
optional: true
- name: NGINX_USER
valueFrom:
configMapKeyRef:
name: nginx-config
key: user
optional: false
[19:38:17 root@master1 storage]#kubectl apply -f 12-storage-configmap-simple-env.yaml
configmap/nginx-config created
pod/configmap-env-test created
[19:38:32 root@master1 storage]#kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
configmap-env-test 1/1 Running 0 6s 10.244.3.2 node1.noisedu.cn <none> <none>
[19:38:38 root@master1 storage]#kubectl get cm -o wide
NAME DATA AGE
kube-root-ca.crt 1 2d20h
nginx-config 2 15s
[19:38:48 root@master1 storage]#kubectl exec -it configmap-env-test -- bash
root@configmap-env-test:/# env
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=configmap-env-test
PWD=/
PKG_RELEASE=1~bullseye
HOME=/root
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
NGINX_HOST=10.0.0.55
NJS_VERSION=0.7.0
TERM=xterm
SHLVL=1
KUBERNETES_PORT_443_TCP_PROTO=tcp
NGINX_USER=www
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
NGINX_PORT=10086
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NGINX_VERSION=1.21.4
_=/usr/bin/env
root@configmap-env-test:/# exit
exit
# envFrom - configMapRef
[19:41:48 root@master1 storage]#cat 13-storage-configmap-simple-envfrom.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
NGINX_PORT: "10086"
NGINX_USER: "www"
---
apiVersion: v1
kind: Pod
metadata:
name: configmap-envfrom-test
spec:
containers:
- name: nginx1
image: 10.0.0.55:80/mykubernetes/nginx:1.21.3
envFrom:
- configMapRef:
name: nginx-config
[19:41:50 root@master1 storage]#kubectl apply -f 13-storage-configmap-simple-envfrom.yaml
configmap/nginx-config created
pod/configmap-envfrom-test created
[19:43:15 root@master1 storage]#kubectl get cm,pod -o wide
NAME DATA AGE
configmap/kube-root-ca.crt 1 2d20h
configmap/nginx-config 2 9s
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/configmap-envfrom-test 1/1 Running 0 9s 10.244.4.2 node2.noisedu.cn <none> <none>
[19:43:24 root@master1 storage]#kubectl exec -it configmap-envfrom-test -- bash
root@configmap-envfrom-test:/# env
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_SERVICE_PORT=443
HOSTNAME=configmap-envfrom-test
PWD=/
PKG_RELEASE=1~bullseye
HOME=/root
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
NJS_VERSION=0.7.0
TERM=xterm
SHLVL=1
KUBERNETES_PORT_443_TCP_PROTO=tcp
NGINX_USER=www
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
NGINX_PORT=10086
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP_PORT=443
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
NGINX_VERSION=1.21.4
_=/usr/bin/env
root@configmap-envfrom-test:/# exit
exit
# 运用configmap变量
[19:47:17 root@master1 storage]#cat 14-storage-configmap-simple-envargs.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: command-arg
data:
time: "3600"
---
apiVersion: v1
kind: Pod
metadata:
name: command-pod
spec:
containers:
- name: command-pod
image: 10.0.0.55:80/mykubernetes/busybox:v0.1
command: [ "/bin/sh", "-c", "sleep ${SPECIAL_TIME}" ]
env:
- name: SPECIAL_TIME
valueFrom:
configMapKeyRef:
name: command-arg
key: time
restartPolicy: Never
[19:47:22 root@master1 storage]#kubectl apply -f 14-storage-configmap-simple-envargs.yaml
configmap/command-arg created
pod/command-pod created
[19:47:25 root@master1 storage]#kubectl get cm,pod -o wide
NAME DATA AGE
configmap/command-arg 1 3s
configmap/kube-root-ca.crt 1 2d20h
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/command-pod 1/1 Running 0 3s 10.244.4.4 node2.noisedu.cn <none> <none>
[19:47:28 root@master1 storage]#kubectl describe pod command-pod
Name: command-pod
Namespace: default
Priority: 0
Node: node2.noisedu.cn/10.0.0.54
Start Time: Tue, 14 Dec 2021 19:47:25 +0800
Labels: <none>
Annotations: cni.projectcalico.org/containerID: 956c7f256e844be96d18e83c9b66a0aa10ae4672ee69f4dc3e66710035334941
cni.projectcalico.org/podIP: 10.244.4.4/32
cni.projectcalico.org/podIPs: 10.244.4.4/32
Status: Running
IP: 10.244.4.4
IPs:
IP: 10.244.4.4
Containers:
command-pod:
Container ID: docker://8db134f37c6cfb197b345dc2c52598424cfea480827da7b6b68ee0d06b98fc60
Image: 10.0.0.55:80/mykubernetes/busybox:v0.1
Image ID: docker-pullable://10.0.0.55:80/mykubernetes/busybox@sha256:d5c6e74e3115b55dce759c1b3a52b1d2db60dc6929681de2f76cdc5d6f4d7412
Port: <none>
Host Port: <none>
Command:
/bin/sh
-c
sleep ${SPECIAL_TIME}
State: Running
Started: Tue, 14 Dec 2021 19:47:26 +0800
Ready: True
Restart Count: 0
Environment:
SPECIAL_TIME: <set to the key 'time' of config map 'command-arg'> Optional: false
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-cznvv (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-cznvv:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 11s default-scheduler Successfully assigned default/command-pod to node2.noisedu.cn
Normal Pulled 10s kubelet Container image "10.0.0.55:80/mykubernetes/busybox:v0.1" already present on machine
Normal Created 10s kubelet Created container command-pod
Normal Started 10s kubelet Started container command-pod
# 通过configmap挂载文件 - spec.volumes.configMap
[19:51:18 root@master1 storage]#cat 15-storage-configmap-simple-volume.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: volume-config
data:
special.name: noise
file.conf: |
[daemon]
testing
---
apiVersion: v1
kind: Pod
metadata:
name: testvolume
spec:
volumes:
- name: config-volume
configMap:
name: volume-config
containers:
- name: nginx
image: 10.0.0.55:80/mykubernetes/nginx:1.21.3
volumeMounts:
- name: config-volume
mountPath: /cmap/
[19:51:20 root@master1 storage]#kubectl apply -f 15-storage-configmap-simple-volume.yaml
configmap/volume-config created
pod/testvolume created
[19:51:25 root@master1 storage]#kubectl get cm,pod -o wide
NAME DATA AGE
configmap/kube-root-ca.crt 1 2d20h
configmap/volume-config 2 5s
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/testvolume 1/1 Running 0 5s 10.244.3.3 node1.noisedu.cn <none> <none>
[19:51:30 root@master1 storage]#kubectl exec -it testvolume -- bash
root@testvolume:/# ll
bash: ll: command not found
root@testvolume:/# ls
bin boot cmap dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@testvolume:/# ls /cmap/
file.conf special.name
root@testvolume:/# cat /cmap/file.conf
[daemon]
testing
root@testvolume:/# cat /cmap/special.name
noiseroot@testvolume:/#
root@testvolume:/#
root@testvolume:/# exit
exit