nealous3
nealous3

Reputation: 742

kubernetes host not found in upstream "php"

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

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

The default.conf looks like this:

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;
}

When I deploy to kubernetes, I get the following error from nginx container:

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

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

upstream php {
  server php;
}

But now I get the error:

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

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

Update I got nginx to not crash by changing the fastcgi_pass php:9000; to fastcgi_pass 127.0.0.1:9002. 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.

Update 2: Reply to Matthews comments This is the deployment yaml:

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

Upvotes: 3

Views: 4239

Answers (1)

znpy
znpy

Reputation: 504

I wanted to chime in since I faced the same problem while helping a colleague troubleshooting this issue.

I looked up the kubernetes documentation and container-to-container communications within a pod must pass through localhost, there is no name aliasing like it happens with docker-compose on the desktop.

Here is links and extracts from the kubernetes documentation:

From the page "Cluster Networking" (https://kubernetes.io/docs/concepts/cluster-administration/networking/)

    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.

From the page "Pods", section "Pods networking" (https://kubernetes.io/docs/concepts/workloads/pods/#pod-networking):

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. 

Upvotes: 4

Related Questions