Configuration YAMLs
Environment Definition
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: thomaspk/fortune:args
imagePullPolicy: Always
env:
- name: INTERVAL
value: 15
The above yaml file example shows how environment variables can be introduced directly through the yaml file definition.
Pod using ConfigMap
apiVersion: v1
kind: Pod
metadata:
name: fortune-env
spec:
containers:
- image: thomaspk/fortune:args
imagePullPolicy: Always
name: html-generator
env:
- name: INTERVAL
valueFrom:
configMapKeyRef:
name: fortune-config
key: sleep-interval
The above example shows how the pod/fortune-env gets its environment variable INTERVAL
from the fortune-config config map’s key value sleep-interval
. The value can be set from the command line with $ kubectl create configmap fortune-config --from-literal=sleep-interval=25
.
Mounted ConfigMap Volume
The two connected yaml files provided below provides another example of using a config map for an nginx configuration file and mounting it as a volume.
ConfigMap file
1 apiVersion: v1
2 kind: ConfigMap
3 metadata:
4 name: fortune-config
5 namespace: default
6 data:
7 my-nginx-config.conf: |
8 server {
9 listen 80;
10 server_name www.bigtom.local;
11 gzip on;
12 gzip_types text/plain application/xml;
13
14 location / {
15 root /usr/share/nginx/html;
16 index index.html index.html;
17 }
18 }
19 sleep-interval: "25"
The yaml file above creates a fortune-config ConfigMap which contains the nginx configuration settings and sleep-interval configs. The |
character used on line 7 above denotes a contiguous string block.
ConfigMap Mounted Volume
apiVersion: v1
kind: Pod
metadata:
name: nginx-cm-volume
labels:
app: nginx-cm-app
spec:
containers:
- image: nginx:alpine
name: web-server
ports:
- containerPort: 80
volumeMounts:
- name: config-volume
mountPath: /etc/nginx/conf.d
readOnly: true
volumes:
- name: config-volume
configMap:
name: fortune-config
The yaml file above creates a pod based on the nginx:alpine image, using the configuration from the ConfigMap:fortune-config and mounting it as a volume in the container at /etc/nginx/config.d
.