Fix Database issue in K8s Application

Ticker

6/recent/ticker-posts

Fix Database issue in K8s Application

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

FYI, we are seeing database connection error, please identify the root cause and fix the same. Please do not try to delete/modify any other existing components like deployment name, service name, labels etc, also do not hard code any DB details in the application configuration. Once fixed, you can access the WordPress website using the WP Website option, on the 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

lamp-wp-9877654b8-fx8j5   2/2     Running   0          6m4s

thor@jump_host ~$

thor@jump_host ~$ kubectl describe pods

Name:         lamp-wp-9877654b8-fx8j5

Namespace:    default

Priority:     0

Node:         kodekloud-control-plane/172.18.0.2

Start Time:   Sat, 14 Aug 2021 14:00:06 +0000

Labels:       app=lamp

              pod-template-hash=9877654b8

              tier=frontend

Annotations:  <none>

Status:       Running

IP:           10.244.0.5

IPs:

  IP:           10.244.0.5

Controlled By:  ReplicaSet/lamp-wp-9877654b8

Containers:

  httpd-php-wp-container:

    Container ID:   containerd://660d060af99c5413c857e48c00e56fe55dbaf5863f1bc77f849b662d7f6fc6c0

    Image:          wordpress:php7.4-apache

    Image ID:       docker.io/library/wordpress@sha256:b590a25a358650cb91233ef9e058b2785a121f3a9e622d9dfc03a2749004013e

    Port:           80/TCP

    Host Port:      0/TCP

    State:          Running

      Started:      Sat, 14 Aug 2021 14:01:17 +0000

    Ready:          True

    Restart Count:  0

    Environment:

      MYSQL_ROOT_PASSWORD:  <set to the key 'password' in secret 'mysql-root-pass'>  Optional: false

      MYSQL_DATABASE:       <set to the key 'database' in secret 'mysql-db-url'>     Optional: false

      MYSQL_USER:           <set to the key 'username' in secret 'mysql-user-pass'>  Optional: false

      MYSQL_PASSWORD:       <set to the key 'password' in secret 'mysql-user-pass'>  Optional: false

      MYSQL_HOST:           <set to the key 'host' in secret 'mysql-host'>           Optional: false

    Mounts:

      /var/run/secrets/kubernetes.io/serviceaccount from default-token-jppp7 (ro)

      /var/www/html from lamp-persistent-storage (rw)

  mysql-container:

    Container ID:   containerd://52b12c204466d9fcba1757dab9cb56e2020f2ccff717dae161dee6dd35a9945e

    Image:          mysql:5.6

    Image ID:       docker.io/library/mysql@sha256:391f655177931dc2905b6fbf6b21d769060f8797ce1b515e8579a157afcce459

    Port:           3306/TCP

    Host Port:      0/TCP

    State:          Running

      Started:      Sat, 14 Aug 2021 14:01:53 +0000

    Ready:          True

    Restart Count:  0

    Environment:

      MYSQL_ROOT_PASSWORD:  <set to the key 'password' in secret 'mysql-root-pass'>  Optional: false

      MYSQL_DATABASE:       <set to the key 'database' in secret 'mysql-db-url'>     Optional: false

      MYSQL_USER:           <set to the key 'username' in secret 'mysql-user-pass'>  Optional: false

      MYSQL_PASSWORD:       <set to the key 'password' in secret 'mysql-user-pass'>  Optional: false

      MYSQL_HOST:           <set to the key 'host' in secret 'mysql-host'>           Optional: false

    Mounts:

      /var/run/secrets/kubernetes.io/serviceaccount from default-token-jppp7 (ro)

      /var/www/html from lamp-persistent-storage (rw)

Conditions:

  Type              Status

  Initialized       True

  Ready             True

  ContainersReady   True

  PodScheduled      True

Volumes:

  lamp-persistent-storage:

    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)

    ClaimName:  lamp-pv-claim

    ReadOnly:   false

  default-token-jppp7:

    Type:        Secret (a volume populated by a Secret)

    SecretName:  default-token-jppp7

    Optional:    false

QoS Class:       BestEffort

Node-Selectors:  <none>

Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s

                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s

Events:

  Type    Reason     Age    From               Message

  ----    ------     ----   ----               -------

  Normal  Scheduled  6m10s  default-scheduler  Successfully assigned default/lamp-wp-9877654b8-fx8j5 to kodekloud-control-plane

  Normal  Pulling    6m9s   kubelet            Pulling image "wordpress:php7.4-apache"

  Normal  Pulled     5m     kubelet            Successfully pulled image "wordpress:php7.4-apache" in 1m8.618937068s

  Normal  Created    5m     kubelet            Created container httpd-php-wp-container

  Normal  Started    4m59s  kubelet            Started container httpd-php-wp-container

  Normal  Pulling    4m59s  kubelet            Pulling image "mysql:5.6"

  Normal  Pulled     4m24s  kubelet            Successfully pulled image "mysql:5.6" in 35.200179785s

  Normal  Created    4m24s  kubelet            Created container mysql-container

  Normal  Started    4m23s  kubelet            Started container mysql-container

thor@jump_host ~$

2. Go through the logs of the container to find the issue

thor@jump_host ~$ kubectl logs lamp-wp-9877654b8-fx8j5 -c httpd-php-wp-container

WordPress not found in /var/www/html - copying now...

Complete! WordPress has been successfully copied to /var/www/html

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.244.0.5. Set the 'ServerName' directive globally to suppress this message

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 10.244.0.5. Set the 'ServerName' directive globally to suppress this message

[Sat Aug 14 14:01:25.609638 2021] [mpm_prefork:notice] [pid 1] AH00163: Apache/2.4.38 (Debian) PHP/7.4.22 configured -- resuming normal operations

[Sat Aug 14 14:01:25.690373 2021] [core:notice] [pid 1] AH00094: Command line: 'apache2 -D FOREGROUND'

[Sat Aug 14 14:03:34.250010 2021] [php7:warn] [pid 90] [client 10.244.0.1:16476] PHP Warning:  Use of undefined constant MYSQL_PASSWORD - assumed 'MYSQL_PASSWORD' (this will throw an Error in a future version of PHP) in /var/www/html/wp-config.php on line 29, referer: https://4109bb179a8b429e.labs.kodekloud.com/

10.244.0.1 - - [14/Aug/2021:14:03:34 +0000] "GET / HTTP/1.1" 500 2834 "https://4109bb179a8b429e.labs.kodekloud.com/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"

[Sat Aug 14 14:05:34.713626 2021] [php7:warn] [pid 91] [client 10.244.0.1:61597] PHP Warning:  Use of undefined constant MYSQL_PASSWORD - assumed 'MYSQL_PASSWORD' (this will throw an Error in a future version of PHP) in /var/www/html/wp-config.php on line 29, referer: https://4109bb179a8b429e.labs.kodekloud.com/

10.244.0.1 - - [14/Aug/2021:14:05:34 +0000] "GET / HTTP/1.1" 500 2834 "https://4109bb179a8b429e.labs.kodekloud.com/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36"

thor@jump_host ~$

3. Kindly refer to the below video for the Configuration part


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