Example1: Mount ConfigMap as Volume #
Deployment, ConfigMap, Service #
vi example-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.16.1-alpine
volumeMounts:
- name: html-volume
mountPath: /usr/share/nginx/html
volumes:
- name: html-volume
configMap:
name: index-html
---
# ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: index-html
data:
index.html: |
Hello from Nginx Webserver
---
# NodePort Service
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 30080 # Service port (internal to cluster)
targetPort: 80 # Target port in the container
nodePort: 30200 # Manually specified NodePort (external port)
type: NodePort
kubectl create -f example-deployment.yaml
Verify the ConfigMap / Nginx #
# List services
kubectl get svc
# Shell output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-service NodePort 10.43.76.117 <none> 30080:30200/TCP 18s
Curl Nginx:
# Curl Nginx via the NodePort or ClusterIP service
curl 192.168.30.11:30200
curl 10.43.76.117:30080
# Shell output:
Hello from Nginx Webserver
Delete the Deployment #
# Delete the deployment, ConfigMap & service
kubectl delete -f example-deployment.yaml
Example: Mount ConfigMap as Environment Variable #
Deployment Version #
vi example-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: example-deployment
labels:
app: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: busybox
image: busybox:latest
command: ['sh', '-c', "echo $(USER) && echo $(PASSWORD) && sleep 3600"]
env:
- name: USER
valueFrom:
configMapKeyRef:
name: example-configmap
key: user
- name: PASSWORD
valueFrom:
configMapKeyRef:
name: example-configmap
key: password
---
# ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap
data:
user: some-user
password: some-password
kubectl create -f example-deployment.yaml
Pod Version #
vi example-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: busybox
image: busybox:latest
command: ['sh', '-c', "echo $(USER) && echo $(PASSWORD) && sleep 3600"]
env:
- name: USER
valueFrom:
configMapKeyRef:
name: example-configmap
key: user
- name: PASSWORD
valueFrom:
configMapKeyRef:
name: example-configmap
key: password
---
# ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap
data:
user: some-user
password: some-password
kubectl create -f example-pod.yaml
Verify the Environment Variable #
Pod Version #
# List the pod logs
kubectl logs example-pod
# Shell output:
some-user
some-password
# Print the variables via container terminal
kubectl exec example-pod -- sh -c 'echo $USER $PASSWORD'
# Shell output:
some-user some-password
Deployment Version #
# List pods
kubectl get pods
# Shell output:
NAME READY STATUS RESTARTS AGE
example-deployment-8469c89d56-vwqcs 1/1 Running 0 12s
# List the pod logs
kubectl logs example-deployment-8469c89d56-vwqcs
# Shell output:
some-user
some-password
# Print the variables via container terminal
kubectl exec example-deployment-8469c89d56-vwqcs -- sh -c 'echo $USER $PASSWORD'
# Shell output:
some-user some-password
Delete Resources #
# Delete the resources
kubectl delete -f example-pod.yaml
kubectl delete -f example-deployment.yaml