DEV Community

Wycliffe A. Onyango
Wycliffe A. Onyango

Posted on

100 Days of DevOps: Day 53

Resolving Volume Mounts Issue in Kubernetes

Problem Statement

The Nginx and PHP-FPM setup deployed on a Kubernetes cluster experienced a functionality halt that prevented website access through the standard interface. The deployment consisted of a single pod (nginx-phpfpm) with two containers and an associated ConfigMap (nginx-config).

Initial Assessment

Investigation Commands:

kubectl get pods -o wide
kubectl describe pod nginx-phpfpm
kubectl logs nginx-phpfpm -c nginx-container
kubectl logs nginx-phpfpm -c php-fpm-container
kubectl get configmap nginx-config -o yaml
Enter fullscreen mode Exit fullscreen mode

Key Findings:

  • Pod status: Running (2/2 containers ready)
  • Nginx listening on port 8099 instead of expected port 80
  • Service configured for port 8099:30008 mapping
  • Volume mount path inconsistencies between containers

Root Cause Analysis

The investigation revealed multiple configuration misalignments:

  1. Port Configuration Issue: Nginx was configured to listen on port 8099, incompatible with the website access button which expects port 80

  2. Volume Mount Path Mismatch: Critical discrepancy in shared volume mounting:

    • nginx-container: /usr/share/nginx/html
    • php-fpm-container: /var/www/html
    • nginx.conf document root: /var/www/html
  3. Service Port Mapping: The nginx-service was forwarding traffic to port 8099 instead of port 80

Resolution Steps

Step 1: ConfigMap Correction

Updated the nginx configuration to resolve port and path issues:

kubectl edit configmap nginx-config
Enter fullscreen mode Exit fullscreen mode

Changes Applied:

  • Modified listen 8099 to listen 80
  • Changed document root to /var/www/html
  • Added try_files $uri =404; directive for proper file validation

Step 2: Service Configuration Update

kubectl edit service nginx-service
Enter fullscreen mode Exit fullscreen mode

Port mapping corrected:

  • From: port: 8099, targetPort: 8099
  • To: port: 80, targetPort: 80

Step 3: Pod Manifest Reconstruction

Created a new pod manifest to align volume mount paths:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-phpfpm
  labels:
    app: php-app
spec:
  containers:
  - name: php-fpm-container
    image: php:7.2-fpm-alpine
    volumeMounts:
    - name: shared-files
      mountPath: /var/www/html
  - name: nginx-container
    image: nginx:latest
    volumeMounts:
    - name: shared-files
      mountPath: /var/www/html  # Aligned with php-fpm path
    - name: nginx-config-volume
      mountPath: /etc/nginx/nginx.conf
      subPath: nginx.conf
Enter fullscreen mode Exit fullscreen mode

Step 4: Pod Recreation and File Deployment

kubectl delete pod nginx-phpfpm
kubectl apply -f nginx-phpfpm-pod-fixed.yaml
kubectl wait --for=condition=Ready pod/nginx-phpfpm --timeout=120s
kubectl cp /home/thor/index.php nginx-phpfpm:/var/www/html/index.php -c nginx-container
Enter fullscreen mode Exit fullscreen mode

Verification Results

Final Test Output:

kubectl exec nginx-phpfpm -c nginx-container -- curl -s localhost:80/index.php
Enter fullscreen mode Exit fullscreen mode

The PHP application now responds correctly, displaying the PHP information page as expected.

Service Status:

  • Pod: Running (2/2 Ready)
  • Nginx: Listening on port 80
  • Service: Properly forwarding port 80 traffic
  • PHP-FPM: Successfully processing PHP requests

Technical Insights

The primary technical challenge was identifying that nginx was passing file paths (/usr/share/nginx/html/index.php) to PHP-FPM that PHP-FPM couldn't access because it only had visibility to /var/www/html/. This path mismatch between containers sharing the same volume but mounting it at different locations caused the persistent "File not found" errors even when files were physically present.

The resolution required not just configuration changes but also architectural alignment ensuring both containers accessed the shared volume through identical mount paths.

Outcome

The website is now fully functional and accessible through the standard interface. All components operate on the correct ports with proper file sharing between the nginx and PHP-FPM containers. The deployment demonstrates proper container orchestration with aligned volume mounts and service configurations.

Top comments (0)