Convert docker-compose to Kubernetes for nginx & php-fpm containers

12/19/2020

I have a dockerized symfony project and I'm trying to delpoy it in GPC on Kubernetes cluster. In development I use docker-compose and I have two sepparate containers for php-fpm and nginx.

When I run docker-compose up --build, it all works fine, but when I try to create a kubernetes cluster I get this error after I run kubectl apply -f nginx.deployment.yaml:

nginx: emerg host not found in upstream "php-fpm" in /etc/nginx/conf.d/default.conf:11

This is the nginx default.conf file:

server {
listen 80;
server_name localhost;
root /app/public;

location / {
    try_files $uri /index.php$is_args$args;
}

location ~ ^/index\.php(/|$) {
    fastcgi_pass php-fpm:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $document_root;
    internal;
}

location ~ \.php$ {
    return 404;
}
}

Please note

fastcgi_pass php-fpm:9000;

which references the php-fpm container.

Dockerfile for nginx:

ARG VERSION

# Dev image
FROM nginx:${VERSION}-alpine as dev

# Copy nginx config
COPY ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf

# Prod image
FROM dev as prod

# Copy assets
COPY ./assets /app/public

Here it is the nginx.deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert -f ../docker-compose.yml
    kompose.version: 1.22.0 (955b78124)
  creationTimestamp: null
  labels:
    io.kompose.service: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: nginx
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert -f ../docker-compose.yml
        kompose.version: 1.22.0 (955b78124)
      creationTimestamp: null
      labels:
        io.kompose.service: nginx
    spec:
      containers:
        - image: myDockerHubRegistry/symfony-nginx:0.2
          name: nginx
          ports:
            - containerPort: 80
          resources: {}
          volumeMounts:
            - mountPath: /app/public
              name: nginx-claim
      restartPolicy: Always
      volumes:
        - name: nginx-claim
          persistentVolumeClaim:
            claimName: nginx-claim
status: {}

I also tried to put nginx and php-fpm on the same deployment, but I still get the same error.

What am I missing?

-- George M.
docker
kubernetes
nginx
php
symfony

2 Answers

12/20/2020

There are 2 issues here:

  • The hostname php-fpm doesn't resolve to the service's IP via DNS in k8s

  • nginx/openresty refuses to start if a hostname used in a proxy pass can't be resolved

    nginx: emerg host not found in upstream "hostname"

SOLUTION:

  • Use a hostname that resolves correctly inside kubernetes.

    The DNS scheme used by Core DNS in GKE is <service>.<namespace>.svc.cluster.local

  • Store the correct hostname for the php-fpm service in a variable first.

    Then use this variable as the target of the the (fastcgi-)proxy pass.

    This will make nginx start regardless of being able to resolve the target hostname.

Example:

set $upstream php-fpm.your-namespace.svc.cluster.local:9000;

fastcgi_pass $upstream; 
-- Nicolai Fr&#246;hlich
Source: StackOverflow

12/20/2020

Since nginx refuses to start if any proxy_pass service is not already available, you need to start the php-fpm service first. You haven't shared the deployment yaml for php-fpm. Along with the kubernetes deployment for php-fpm, you need to create a kubernetes service object for php-fpm with the name php-fpm within the same namespace as nginx.

You could also use the variable hack suggested by Nicolai so that you can start nginx without any dependency on php-fpm. But either case you need to create the kubernetes deployment and service objects for php-fpm for your application to actually work.

-- Shashank V
Source: StackOverflow