Certified Kubernetes Application Developer (CKAD) Study Guide
apiVersion: v1
kind: Pod
metadata:
name: business-app
spec:
initContainers:
- name: configurer
image: busybox:1.32.0
command: ['sh', '-c', 'echo Configuring application... && \\
mkdir -p /usr/shared/app && echo -e "{\\"dbConfig\\": \\
{\\"host\\":\\"localhost\\",\\"port\\":5432,\\"dbName\\":\\"customers\\"}}" \\
> /usr/shared/app/config.json']
volumeMounts:
- name: configdir
mountPath: "/usr/shared/app"
containers:
- image: bmuschko/nodejs-read-config:1.0.0
name: web
ports:
- containerPort: 8080
volumeMounts:
- name: configdir
mountPath: "/usr/shared/app"
volumes:
- name: configdir
emptyDir: {}
kubectl create -f init.yaml
pod/business-app created
$ kubectl get pod business-app
NAME READY STATUS RESTARTS AGE
business-app 0/1 Init:0/1 0 2s
$ kubectl get pod business-app
NAME READY STATUS RESTARTS AGE
business-app 1/1 Running 0 8s
<aside>
👍🏼 While init
containers are running, status shows Init:m/n
.
</aside>
apiVersion: v1
kind: Pod
metadata:
name: webserver
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: logs-vol
mountPath: /var/log/nginx
- name: sidecar
image: busybox
command: ["sh","-c","while true; do if [ \\"$(cat /var/log/nginx/error.log \\
| grep 'error')\\" != \\"\\" ]; then echo 'Error discovered!'; fi; \\
sleep 10; done"]
volumeMounts:
- name: logs-vol
mountPath: /var/log/nginx
volumes:
- name: logs-vol
emptyDir: {}
$ kubectl create -f sidecar.yaml
pod/webserver created
$ kubectl get pods webserver
NAME READY STATUS RESTARTS AGE
webserver 0/2 ContainerCreating 0 4s
$ kubectl get pods webserver
NAME READY STATUS RESTARTS AGE
webserver 2/2 Running 0 5s
apiVersion: v1
kind: Pod
metadata:
name: adapter
spec:
containers:
- args:
- /bin/sh
- -c
- 'while true; do echo "$(date) | $(du -sh ~)" >> /var/logs/diskspace.txt; \\
sleep 5; done;'
image: busybox
name: app
volumeMounts:
- name: config-volume
mountPath: /var/logs
- image: busybox
name: transformer
args:
- /bin/sh
- -c
- 'sleep 20; while true; do while read LINE; do echo "$LINE" | cut -f2 -d"|" \\
>> $(date +%Y-%m-%d-%H-%M-%S)-transformed.txt; done < \\
/var/logs/diskspace.txt; sleep 20; done;'
volumeMounts:
- name: config-volume
mountPath: /var/logs
volumes:
- name: config-volume
emptyDir: {}
$ kubectl create -f adapter.yaml
pod/adapter created
$ kubectl get pods adapter
NAME READY STATUS RESTARTS AGE
adapter 2/2 Running 0 10s
$ kubectl exec adapter --container=transformer -it -- /bin/sh
/ # cat /var/logs/diskspace.txt
Sun Jul 19 20:28:07 UTC 2020 | 4.0K /root
Sun Jul 19 20:28:12 UTC 2020 | 4.0K /root
/ # ls -l
total 40
-rw-r--r-- 1 root root 60 Jul 19 20:28 2020-07-19-20-28-28-transformed.txt
...
/ # cat 2020-07-19-20-28-28-transformed.txt
4.0K /root
4.0K /root
apiVersion: v1
kind: Pod
metadata:
name: rate-limiter
spec:
containers:
- name: business-app
image: bmuschko/nodejs-business-app:1.0.0
ports:
- containerPort: 8080
- name: ambassador
image: bmuschko/nodejs-ambassador:1.0.0
ports:
- containerPort: 8081