Fix Issue with VolumeMounts in Kubernetes

Ticker

6/recent/ticker-posts

Fix Issue with VolumeMounts in Kubernetes

Question :  We deployed a Nginx and PHPFPM based setup on Kubernetes cluster last week and it had been working fine. This morning one of the team members made a change somewhere which caused some issues, and it stopped working. Please look into the issue and fix it:

The pod name is nginx-phpfpm and configmap name is nginx-config. Figure out the issue and fix the same.

Once issue is fixed, copy /home/thor/index.php file from jump host into nginx-container under nginx document root and you should be able to access the website using Website button on top bar.

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster. 

Please Note :-  Perform the below commands based on your question server,  user name & other details that might differ. So please read the task carefully before executing it. 
All the Best 👍
Solution:  

1. Check existing running pods 

thor@jump_host ~$ kubectl get pods

NAME           READY   STATUS    RESTARTS   AGE

nginx-phpfpm   2/2     Running   0          3m4s

thor@jump_host ~$

 2. check the shared volume path in existing config map 

thor@jump_host ~$ kubectl get configmap

NAME               DATA   AGE

kube-root-ca.crt   1      42m

nginx-config       1      5m51s

thor@jump_host ~$

thor@jump_host ~$ kubectl describe configmap nginx-config

Name:         nginx-config

Namespace:    default

Labels:       <none>

Annotations:  <none>

 

Data

====

nginx.conf:

----

events {

}

http {

  server {

    listen 8099 default_server;

    listen [::]:8099 default_server;

 

    # Set nginx to serve files from the shared volume!

    root /var/www/html;

    index  index.html index.htm index.php;

    server_name _;

    location / {

      try_files $uri $uri/ =404;

    }

    location ~ \.php$ {

      include fastcgi_params;

      fastcgi_param REQUEST_METHOD $request_method;

      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

      fastcgi_pass 127.0.0.1:9000;

    }

  }

}

 Events:  <none>

thor@jump_host ~$

3. Get the configuration in the YAML file from the running pod 

thor@jump_host ~$  kubectl get pod nginx-phpfpm -o yaml  > /tmp/nginx.yaml

thor@jump_host ~$ ll /tmp/

total 12

-rwx------ 1 root root  836 Aug  1  2019 ks-script-rnBCJB

-rw-rw-r-- 1 thor thor 6866 Aug 13 15:21 nginx.yaml

-rw------- 1 root root    0 Aug  1  2019 yum.log

thor@jump_host ~$

thor@jump_host ~$ cat /tmp/nginx.yaml

apiVersion: v1

kind: Pod

metadata:

  annotations:

    kubectl.kubernetes.io/last-applied-configuration: |

      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"app":"php-app"},"name":"nginx-phpfpm","namespace":"default"},"spec":{"containers":[{"image":"php:7.2-fpm","name":"php-fpm-container","volumeMounts":[{"mountPath":"/var/www/html","name":"shared-files"}]},{"image":"nginx:latest","name":"nginx-container","volumeMounts":[{"mountPath":"/usr/share/nginx/html","name":"shared-files"},{"mountPath":"/etc/nginx/nginx.conf","name":"nginx-config-volume","subPath":"nginx.conf"}]}],"volumes":[{"emptyDir":{},"name":"shared-files"},{"configMap":{"name":"nginx-config"},"name":"nginx-config-volume"}]}}

  creationTimestamp: "2021-08-13T15:17:18Z"

  labels:

    app: php-app

  managedFields:

  - apiVersion: v1

    fieldsType: FieldsV1

    fieldsV1:

      f:metadata:

        f:annotations:

          .: {}

          f:kubectl.kubernetes.io/last-applied-configuration: {}

        f:labels:

          .: {}

          f:app: {}

      f:spec:

        f:containers:

          k:{"name":"nginx-container"}:

            .: {}

            f:image: {}

            f:imagePullPolicy: {}

            f:name: {}

            f:resources: {}

            f:terminationMessagePath: {}

            f:terminationMessagePolicy: {}

            f:volumeMounts:

              .: {}

              k:{"mountPath":"/etc/nginx/nginx.conf"}:

                .: {}

                f:mountPath: {}

                f:name: {}

                f:subPath: {}

              k:{"mountPath":"/usr/share/nginx/html"}:

                .: {}

                f:mountPath: {}

                f:name: {}

          k:{"name":"php-fpm-container"}:

            .: {}

            f:image: {}

            f:imagePullPolicy: {}

            f:name: {}

            f:resources: {}

            f:terminationMessagePath: {}

            f:terminationMessagePolicy: {}

            f:volumeMounts:

              .: {}

              k:{"mountPath":"/var/www/html"}:

                .: {}

                f:mountPath: {}

                f:name: {}

        f:dnsPolicy: {}

        f:enableServiceLinks: {}

        f:restartPolicy: {}

        f:schedulerName: {}

        f:securityContext: {}

        f:terminationGracePeriodSeconds: {}

        f:volumes:

          .: {}

          k:{"name":"nginx-config-volume"}:

            .: {}

            f:configMap:

              .: {}

              f:defaultMode: {}

              f:name: {}

            f:name: {}

          k:{"name":"shared-files"}:

            .: {}

            f:emptyDir: {}

            f:name: {}

    manager: kubectl-client-side-apply

    operation: Update

    time: "2021-08-13T15:17:18Z"

  - apiVersion: v1

    fieldsType: FieldsV1

    fieldsV1:

      f:status:

        f:conditions:

          k:{"type":"ContainersReady"}:

            .: {}

            f:lastProbeTime: {}

            f:lastTransitionTime: {}

            f:status: {}

            f:type: {}

          k:{"type":"Initialized"}:

            .: {}

            f:lastProbeTime: {}

            f:lastTransitionTime: {}

            f:status: {}

            f:type: {}

          k:{"type":"Ready"}:

            .: {}

            f:lastProbeTime: {}

            f:lastTransitionTime: {}

            f:status: {}

            f:type: {}

        f:containerStatuses: {}

        f:hostIP: {}

        f:phase: {}

        f:podIP: {}

        f:podIPs:

          .: {}

          k:{"ip":"10.244.0.5"}:

            .: {}

            f:ip: {}

        f:startTime: {}

    manager: kubelet

    operation: Update

    time: "2021-08-13T15:18:27Z"

  name: nginx-phpfpm

  namespace: default

  resourceVersion: "4384"

  uid: 8e516726-a04d-42f5-b4f2-431595e0046d

spec:

  containers:

  - image: php:7.2-fpm

    imagePullPolicy: IfNotPresent

    name: php-fpm-container

    resources: {}

    terminationMessagePath: /dev/termination-log

    terminationMessagePolicy: File

    volumeMounts:

    - mountPath: /var/www/html

      name: shared-files

    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount

      name: default-token-zxqtx

      readOnly: true

  - image: nginx:latest

    imagePullPolicy: Always

    name: nginx-container

    resources: {}

    terminationMessagePath: /dev/termination-log

    terminationMessagePolicy: File

    volumeMounts:

    - mountPath: /usr/share/nginx/html

      name: shared-files

    - mountPath: /etc/nginx/nginx.conf

      name: nginx-config-volume

      subPath: nginx.conf

    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount

      name: default-token-zxqtx

      readOnly: true

  dnsPolicy: ClusterFirst

  enableServiceLinks: true

  nodeName: kodekloud-control-plane

  preemptionPolicy: PreemptLowerPriority

  priority: 0

  restartPolicy: Always

  schedulerName: default-scheduler

  securityContext: {}

  serviceAccount: default

  serviceAccountName: default

  terminationGracePeriodSeconds: 30

  tolerations:

  - effect: NoExecute

    key: node.kubernetes.io/not-ready

    operator: Exists

    tolerationSeconds: 300

  - effect: NoExecute

    key: node.kubernetes.io/unreachable

    operator: Exists

    tolerationSeconds: 300

  volumes:

  - emptyDir: {}

    name: shared-files

  - configMap:

      defaultMode: 420

      name: nginx-config

    name: nginx-config-volume

  - name: default-token-zxqtx

    secret:

      defaultMode: 420

      secretName: default-token-zxqtx

status:

  conditions:

  - lastProbeTime: null

    lastTransitionTime: "2021-08-13T15:17:18Z"

    status: "True"

    type: Initialized

  - lastProbeTime: null

    lastTransitionTime: "2021-08-13T15:18:27Z"

    status: "True"

    type: Ready

  - lastProbeTime: null

    lastTransitionTime: "2021-08-13T15:18:27Z"

    status: "True"

    type: ContainersReady

  - lastProbeTime: null

    lastTransitionTime: "2021-08-13T15:17:18Z"

    status: "True"

    type: PodScheduled

  containerStatuses:

  - containerID: containerd://4a9f5b2152f51eb7f8f8a12734bbe982ec9c79f9472d034537986a542af425fe

    image: docker.io/library/nginx:latest

    imageID: docker.io/library/nginx@sha256:8f335768880da6baf72b70c701002b45f4932acae8d574dedfddaf967fc3ac90

    lastState: {}

    name: nginx-container

    ready: true

    restartCount: 0

    started: true

    state:

      running:

        startedAt: "2021-08-13T15:18:27Z"

  - containerID: containerd://1423c8170b38029767027ccaff9f66b882f0d50ebbe55ce187bcb0094f5e8c03

    image: docker.io/library/php:7.2-fpm

    imageID: docker.io/library/php@sha256:9c84ae47fddb97b94d1d2e289635b7306142a5336bc4ece0a393458c5e0d2cef

    lastState: {}

    name: php-fpm-container

    ready: true

    restartCount: 0

    started: true

    state:

      running:

        startedAt: "2021-08-13T15:18:09Z"

  hostIP: 172.17.0.2

  phase: Running

  podIP: 10.244.0.5

  podIPs:

  - ip: 10.244.0.5

  qosClass: BestEffort

  startTime: "2021-08-13T15:17:18Z"

thor@jump_host ~$

4. Edit the nginx.yaml file and changed ‘/usr/share/nginx/html’ to ‘/var/www/html’ in 3 places.  ( Refer Below Video for Clarity )

thor@jump_host ~$ cat /tmp/nginx.yaml  |grep /usr/share/nginx/html

      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"labels":{"app":"php-app"},"name":"nginx-phpfpm","namespace":"default"},"spec":{"containers":[{"image":"php:7.2-fpm","name":"php-fpm-container","volumeMounts":[{"mountPath":"/var/www/html","name":"shared-files"}]},{"image":"nginx:latest","name":"nginx-container","volumeMounts":[{"mountPath":"/usr/share/nginx/html","name":"shared-files"},{"mountPath":"/etc/nginx/nginx.conf","name":"nginx-config-volume","subPath":"nginx.conf"}]}],"volumes":[{"emptyDir":{},"name":"shared-files"},{"configMap":{"name":"nginx-config"},"name":"nginx-config-volume"}]}}

              k:{"mountPath":"/usr/share/nginx/html"}:

    - mountPath: /usr/share/nginx/html

thor@jump_host ~$

5. Post changes the mount path run below command to replace the running pods   

thor@jump_host ~$ kubectl replace -f /tmp/nginx.yaml --force

pod "nginx-phpfpm" deleted

pod/nginx-phpfpm replaced

thor@jump_host ~$

6.  Wait for pods to get running status.

thor@jump_host ~$ kubectl get pods

NAME           READY   STATUS    RESTARTS   AGE

nginx-phpfpm   2/2     Running   0          7s

thor@jump_host ~$

7. Now copy the index.php file as per the task.  

thor@jump_host ~$ ll /home/thor/

total 4

-rw-r--r-- 1 root root 19 Aug 13 15:17 index.php

thor@jump_host ~$ kubectl cp  /home/thor/index.php  nginx-phpfpm:/var/www/html -c nginx-container

thor@jump_host ~$


8. validate the task by curl the Nginx port 

thor@jump_host ~$ kubectl exec -it nginx-phpfpm -c nginx-container  -- curl -I  http://localhost:8099

HTTP/1.1 200 OK

Server: nginx/1.21.1

Date: Fri, 13 Aug 2021 16:01:17 GMT

Content-Type: text/html; charset=UTF-8

Connection: keep-alive

X-Powered-By: PHP/7.2.34

 thor@jump_host ~$


9.  Click on Finish & Confirm to complete the task successfully

Happy Learning!!!!

Apart from this if you need more clarity,  I have made a  tutorial video on this , please go through and share your comments. Like and share the knowledge 




Post a Comment

0 Comments

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user