Skip to main content

Lab 003: Kubernetes NGINX and PhpFPM Setup



Requirements

The Nautilus Application Development team is planning to deploy one of the php-based applications on Kubernetes cluster. As per the recent discussion with DevOps team, they have decided to use nginx and phpfpm. Additionally, they also shared some custom configuration requirements. Below you can find more details:

  1. Create a service to expose this app. The service type must be NodePort, nodePort should be 30012.

  2. Create a config map named nginx-config for nginx.conf with custom settings:

    • Change the default port 80 to 8092 in nginx.conf
    • Change the default document root /usr/share/nginx to /var/www/html in nginx.conf
    • Update the directory index to index index.html index.htm index.php in nginx.conf
  3. Create a pod named nginx-phpfpm with the following:

    • Create a shared volume named shared-files that will be used by both containers (nginx and phpfpm) as an emptyDir volume
    • Map the ConfigMap as a volume for nginx container. Name the volume nginx-config-volume, mount path /etc/nginx/nginx.conf with subPath nginx.conf
    • Nginx container: named nginx-container, using nginx:latest image
    • PhpFPM container: named php-fpm-container, using php:7.4-fpm-alpine image
    • Mount shared-files volume at /var/www/html location in both containers
    • Copy /opt/index.php from jump host to the nginx document root inside the nginx container

Note

This lab demonstrates multi-container pod setup with NGINX and PHP-FPM using ConfigMaps and shared volumes. The containers communicate through the shared volume for PHP file serving.

Prerequisites

  • Access to Kubernetes cluster via kubectl
  • kubectl configured to work with the target cluster
  • /opt/index.php available on jump host
  • Sufficient cluster resources for multi-container pod

Steps

Create the nginx configuration as a ConfigMap:

# Step 1: Create nginx configuration ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx-config
data:
nginx.conf: |
user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log warn;

events {
worker_connections 768;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;

server {
listen 8092 default_server;
listen [::]:8092 default_server;

server_name _;
root /var/www/html;

index index index.html index.htm index.php;

location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}

Create the Pod with NGINX and PHP-FPM containers:

# Step 2: Create nginx-phpfpm Pod
apiVersion: v1
kind: Pod
metadata:
name: nginx-phpfpm
labels:
app: nginx-phpfpm
spec:
containers:
- name: nginx-container
image: nginx:latest
ports:
- containerPort: 8092
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
- name: shared-files
mountPath: /var/www/html
- name: php-fpm-container
image: php:7.4-fpm-alpine
volumeMounts:
- name: shared-files
mountPath: /var/www/html
volumes:
- name: nginx-config-volume
configMap:
name: nginx-config
- name: shared-files
emptyDir: {}

Create the Service to expose the application:

# Step 3: Create NodePort Service
apiVersion: v1
kind: Service
metadata:
name: nginx-phpfpm-service
spec:
type: NodePort
selector:
app: nginx-phpfpm
ports:
- port: 8092
targetPort: 8092
nodePort: 30012

Apply all manifests:

# Apply ConfigMap, Pod, and Service
kubectl apply -f nginx-configmap.yaml
kubectl apply -f nginx-phpfpm-pod.yaml
kubectl apply -f nginx-service.yaml

# Copy index.php into the running pod
kubectl cp /opt/index.php nginx-phpfpm:/var/www/html/index.php -c nginx-container

Verification

Verify that all resources are created and running:

# Check ConfigMap
kubectl get configmap nginx-config

# Check Pod
kubectl get pod nginx-phpfpm
kubectl describe pod nginx-phpfpm

# Check Service
kubectl get service nginx-phpfpm-service

# Check container logs
kubectl logs nginx-phpfpm -c nginx-container
kubectl logs nginx-phpfpm -c php-fpm-container

# Test connectivity
kubectl port-forward pod/nginx-phpfpm 8092:8092

# Access the application
# Via NodePort: http://<node-ip>:30012
# Via port-forward: http://localhost:8092

Resources