305-998-7702 | 415-800-2922 info@rx-m.com

Practice Drill

Learn how to put the latest open source technology into practice with hands-on training, delivered by industry experts, aligned to your desired business outcomes

Create a pod that runs centos/httpd and stores its log directory at /var/log/httpd under /tmp/httpd/ on the host.

Practice Drill: Answer

First, create a pod spec imperatively using kubectl run:

$ kubectl run --restart Never --image centos/httpd --dry-run=client -o yaml mod4drillpod  > mod4drillpod.yaml

$ 

Then, add a hostPath entry under the volumes array of the pod spec:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: mod4drillpod
  name: mod4drillpod
spec:
  containers:
  - image: centos/httpd
    name: mod4drillpod
  volumes:
  - name: apache-logs
    hostPath:
      path: /tmp/httpd/

Now add a volumeMounts array to the container spec that mounts the hostPath volume to /var/log/httpd:

apiVersion: v1
kind: Pod
metadata:
  labels:
    run: mod4drillpod
  name: mod4drillpod
spec:
  containers:
  - image: centos/httpd
    name: mod4drillpod
    volumeMounts:
    - name: apache-logs
      mountPath: /var/log/httpd
  volumes:
  - name: apache-logs
    hostPath:
      path: /tmp/httpd/

Now create the pod:

$ kubectl apply -f mod4drillpod.yaml

pod/mod4drillpod created

$

And use ls to confirm that the volume mounted successfully:

$ ls -l /tmp/httpd/

total 4
-rw-r--r-- 1 root root   0 Feb 27 10:44 access_log
-rw-r--r-- 1 root root 767 Feb 27 10:44 error_log

$

As an additional exercise, create a storage class that uses the local storage class plugin and a PVC that uses that storage class.