<p>How do I configure default.conf in kubernetes. This docker-compose works locally:</p>

<pre><code>version: '3'

services:
  php:
    container_name: my_php
    build:
        context: .
        dockerfile: php/Dockerfile
    ports:
        - 9002:9000
    volumes:
        - ../:/var/www/some-service
        - ./logs/application:/var/www/some-service/var/logs:cached
    networks:
        - some-service
  nginx:
    container_name: my_nginx
    build:
        context: .
        dockerfile: nginx/Dockerfile
    ports:
        - 8080:80
    volumes:
        - ../:/var/www/some-service
        - ./logs/nginx/:/var/log/nginx:cached
    networks:
        - some-service
  db:
    image: mysql:5.7
    container_name: my_db
    environment:
        - MYSQL_ROOT_PASSWORD=root
    volumes:
        - some-service-db:/var/lib/mysql:cached
    ports:
        - 3311:3306
    networks:
        - some-service

volumes:
  some-service-db:

networks:
  some-service:
    driver: bridge
</code></pre>

<p>The default.conf looks like this:</p>

<pre><code>server {
  index index.php
  server_name  localhost;
  root /var/www/some-service/public;

  location / {
    try_files $uri @rewriteapp;
  }

  location @rewriteapp {
    rewrite ^(.*)$ /index.php/$1 last;
  }

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

  error_log /var/log/nginx/some-service-error.log;
  access_log /var/log/nginx/some-service-access.log;
}
</code></pre>

<p>When I deploy to kubernetes, I get the following error from nginx container:</p>

<pre><code>2018/10/14 22:51:14 [emerg] 1#1: host not found in upstream "php" in /etc/nginx/conf.d/default.conf:15
nginx: [emerg] host not found in upstream "php" in /etc/nginx/conf.d/default.conf:15
</code></pre>

<p>I then got rid of the port in <code>fastcgi_pass php:9000;</code> but then got <code>no port in upstream "php"</code>. So I added the below to the default.conf:</p>

<pre><code>upstream php {
  server php;
}
</code></pre>

<p>But now I get the error:</p>

<pre><code>2018/10/14 23:32:23 [emerg] 1#1: upstream "php" may not have port 9000 in /etc/nginx/conf.d/default.conf:15
nginx: [emerg] upstream "php" may not have port 9000 in /etc/nginx/conf.d/default.conf:15
</code></pre>

<p>I also changed the port to <code>9002</code> but get the same error but for <code>9002</code>. In my kubernetes deployment yaml, I used the container ports from the docker-compose as the <code>containerPort</code> value. The php and db containers start fine. How do I configure nginx to work correctly in kubernetes with the php container?</p>

<p><strong>Update</strong>
I got nginx to not crash by changing the <code>fastcgi_pass php:9000;</code> to <code>fastcgi_pass 127.0.0.1:9002</code>. However, it still seems to not crash if you just have any IP or port. As long as I have both it will not crash.</p>

<p><strong>Update 2: Reply to Matthews comments</strong>
This is the deployment yaml:</p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: some-service-deployment
  labels:
    app: some-service
spec:
  replicas: 1
  selector:
    matchLabels:
      app: some-service
  template:
    metadata:
      labels:
        app: some-service
    spec:
      containers:
      - name: php
        image: 1111111.dkr.ecr.us-east-1.amazonaws.com/some-service/php:latest
        ports:
        - containerPort: 9002
      - name: nginx
        image: 1111111.dkr.ecr.us-east-1.amazonaws.com/some-service/nginx:latest
        ports:
        - containerPort: 8089
        volumeMounts:
        - name: config-volume
          mountPath: /etc/nginx/conf.d/
      - name: db
        image: mysql:5.7
        ports:
        - containerPort: 3311
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: somepassword
      volumes:
      - name: config-volume
        configMap:
          name: nginx-conf
</code></pre>

<p>I wanted to chime in since I faced the same problem while helping a colleague troubleshooting this issue.</p>
<p>I looked up the kubernetes documentation and container-to-container communications within a pod <em>must</em> pass through localhost, there is no name aliasing like it happens with docker-compose on the desktop.</p>
<p>Here is links and extracts from the kubernetes documentation:</p>
<p>From the page &quot;Cluster Networking&quot; (<a href="https://kubernetes.io/docs/concepts/cluster-administration/networking/" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/cluster-administration/networking/</a>)</p>
<pre><code>    Networking is a central part of Kubernetes, but it can be challenging 
    to understand exactly how it is expected to work. 
    There are 4 distinct networking problems to address:

    1. Highly-coupled container-to-container communications: 
       this is solved by Pods and localhost communications.
    2. Pod-to-Pod communications: this is the primary focus of this document.
    3. Pod-to-Service communications: this is covered by services.
    4. External-to-Service communications: this is covered by services.
</code></pre>
<p>From the page &quot;Pods&quot;, section &quot;Pods networking&quot; (<a href="https://kubernetes.io/docs/concepts/workloads/pods/#pod-networking" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/workloads/pods/#pod-networking</a>):</p>
<pre><code>Inside a Pod (and only then), the containers that belong to the Pod can
communicate with one another using localhost. 
[cut]
Within a Pod, containers share an IP address and port space, and can find
each other via localhost. 
</code></pre>
