<p>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.</p>
<p>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:</p>
<blockquote>
<p>nginx: [emerg] host not found in upstream &quot;php-fpm&quot; in /etc/nginx/conf.d/default.conf:11</p>
</blockquote>
<p>This is the nginx default.conf file:</p>
<pre><code>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;
}
}
</code></pre>
<p>Please note</p>
<blockquote>
<p>fastcgi_pass php-fpm:9000;</p>
</blockquote>
<p>which references the php-fpm container.</p>
<p>Dockerfile for nginx:</p>
<pre><code>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
</code></pre>
<p>Here it is the nginx.deployment.yaml file:</p>
<pre><code>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: {}
</code></pre>
<p>I also tried to put nginx and php-fpm on the same deployment, but I still get the same error.</p>
<p>What am I missing?</p>

<p>There are <strong>2 issues</strong> here:</p>
<ul>
<li><p>The hostname <code>php-fpm</code> doesn't resolve to the service's IP via DNS in k8s</p>
</li>
<li><p>nginx/openresty refuses to start if a hostname used in a proxy pass can't be resolved</p>
<blockquote>
<p>nginx: [emerg] host not found in upstream &quot;hostname&quot;</p>
</blockquote>
</li>
</ul>
<p><strong>SOLUTION</strong>:</p>
<ul>
<li><p>Use a hostname that resolves correctly inside kubernetes.</p>
<p>The DNS scheme used by Core DNS in GKE is <code>&lt;service&gt;.&lt;namespace&gt;.svc.cluster.local</code></p>
</li>
<li><p>Store the correct hostname for the php-fpm service in a variable first.</p>
<p>Then use this variable as the target of the the (fastcgi-)proxy pass.</p>
<p>This will make nginx start regardless of being able to resolve the target hostname.</p>
</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code>set $upstream php-fpm.your-namespace.svc.cluster.local:9000;

fastcgi_pass $upstream; 
</code></pre>
