Kubernetes Level 02 Day 11: Fix Issue with LAMP Environment¶
This document outlines the troubleshooting and resolution process for Kubernetes Level 02 Day 11. A WordPress application running on a LAMP stack inside a Kubernetes cluster stopped working after previously functioning correctly. Investigation revealed multiple configuration issues including incorrect service ports and application code referencing wrong environment variables.
Table of Contents¶
- Kubernetes Level 02 Day 11: Fix Issue with LAMP Environment
- Table of Contents
- Task Overview
- Troubleshooting Process
- 1. Inspect Cluster Resources
- 2. Analyze Service Configuration
- 3. Inspect Container Logs
- 4. Investigate Application Code
- Step-by-Step Solution
- 1. Fix NodePort Configuration
- 2. Identify Containers and Pod Name
- 3. Correct Environment Variable Usage in PHP
- 4. Restart PHP Service
- 5. Verification
- Deep Dive: Debugging Multi-Container Pods
- Service Port Misconfiguration
- Environment Variable Issues
- Why Application-Level Debugging Matters
Task Overview¶
Objective: Restore the functionality of the WordPress LAMP application deployed in Kubernetes.
The application was previously accessible but stopped working due to configuration and application-level issues.
Deployment Details
| Resource | Name |
|---|---|
| Deployment | lamp-wp |
| Service | lamp-service |
| Database Service | mysql-service |
| HTTP Port | 80 |
| Expected NodePort | 30008 |
Observed Problems
- The NodePort configured in the service was incorrect.
- The application could not connect to the MySQL database.
- The PHP application file referenced incorrect environment variable names.
Troubleshooting Process¶
1. Inspect Cluster Resources¶
First, verify the running resources in the cluster.
kubectl get pods,deployments,services
Example output:
NAME READY STATUS
pod/lamp-wp-5594699b75-67xrz 2/2 Running
NAME READY
deployment.apps/lamp-wp 1/1
NAME TYPE PORT(S)
lamp-service NodePort 80:30009/TCP
mysql-service ClusterIP 3306/TCP
Observation:
The NodePort was incorrectly set to 30009 instead of 30008.
2. Analyze Service Configuration¶
Export the service configuration to a YAML file.
kubectl get svc lamp-service -o yaml > lamp-svc.yml
Edit the file:
vi lamp-svc.yml
Fix the following values:
nodePort: 30008
port: 80
targetPort: 80
Apply the changes.
kubectl apply -f lamp-svc.yml
Verify:
kubectl get svc
Expected output:
lamp-service NodePort 80:30008/TCP
3. Inspect Container Logs¶
The pod contains two containers:
- HTTP container (Apache + PHP)
- MySQL container
Retrieve the container names.
HTTP=$(kubectl get pod -o=jsonpath='{.items[*].spec.containers[0].name}')
MYSQL=$(kubectl get pod -o=jsonpath='{.items[*].spec.containers[1].name}')
POD=$(kubectl get pods -o=jsonpath='{.items[*].metadata.name}')
Check the HTTP container logs.
kubectl logs -f $POD -c $HTTP
Logs indicated that the application was failing to connect to the database.
4. Investigate Application Code¶
Access the HTTP container shell.
kubectl exec -it $POD -c $HTTP -- sh
Navigate to the application directory.
cd /app
ls
The main application file is:
index.php
Display the file.
cat index.php
Incorrect variables were found:
$dbpass = $_ENV['MYSQL_PASSWORDS'];
$dbhost = $_ENV['HOST_MYQSL'];
These names do not match the environment variables provided by Kubernetes.
Step-by-Step Solution¶
1. Fix NodePort Configuration¶
Ensure the service exposes the correct port.
kubectl get svc lamp-service
Correct configuration:
80:30008/TCP
2. Identify Containers and Pod Name¶
HTTP=$(kubectl get pod -o=jsonpath='{.items[*].spec.containers[0].name}')
MYSQL=$(kubectl get pod -o=jsonpath='{.items[*].spec.containers[1].name}')
POD=$(kubectl get pods -o=jsonpath='{.items[*].metadata.name}')
3. Correct Environment Variable Usage in PHP¶
Edit the PHP file.
vi /app/index.php
Fix the incorrect variables.
Before
$dbpass = $_ENV['MYSQL_PASSWORDS'];
$dbhost = $_ENV['HOST_MYQSL'];
After
$dbpass = $_ENV['MYSQL_PASSWORD'];
$dbhost = $_ENV['MYSQL_HOST'];
These variables correspond to the Kubernetes secrets injected into the pod.
4. Restart PHP Service¶
Restart the PHP-FPM process inside the container.
service php-fpm restart
Verify status.
service php-fpm status
Expected output:
php-fpmd RUNNING
Exit the container.
exit
5. Verification¶
Confirm environment variables inside both containers.
HTTP container:
kubectl exec -it $POD -c $HTTP -- env | grep -i mysql
MySQL container:
kubectl exec -it $POD -c $MYSQL -- env | grep -i mysql
Expected variables:
MYSQL_DATABASE
MYSQL_USER
MYSQL_PASSWORD
MYSQL_HOST
MYSQL_ROOT_PASSWORD
When accessing the application URL through the NodePort, the page should display:
Connected successfully
Deep Dive: Debugging Multi-Container Pods¶
Service Port Misconfiguration¶
A Kubernetes NodePort service exposes an application externally. If the NodePort is incorrect, traffic will never reach the application container.
Correct mapping:
NodePort → Service Port → Container Port
30008 → 80 → 80
Environment Variable Issues¶
Applications often depend on environment variables injected by Kubernetes secrets or ConfigMaps.
Example:
MYSQL_HOST
MYSQL_USER
MYSQL_PASSWORD
MYSQL_DATABASE
If the application code references incorrect variable names, the connection to external services such as databases will fail even if the infrastructure is correct.
Why Application-Level Debugging Matters¶
Many Kubernetes troubleshooting scenarios involve not only infrastructure configuration but also application logic.
Typical debugging workflow:
- Verify Kubernetes resources.
- Inspect service configuration.
- Check container logs.
- Inspect environment variables.
- Debug application code inside the container.
This layered approach helps quickly isolate whether the issue is related to Kubernetes configuration or application behavior.