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

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


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; 


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.