Title: kubernetes host not found in upstream "php"
Tags: php;nginx;kubernetes
Question: 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
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 151.154.11.224: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
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
Comment: (a) you have not posted a single kubernetes descriptor in your question (b) why in the world would you expect a hostname of `php` to magically appear? If you want a hostname of `php` inside your cluster, then create ` Service` with `name: php` pointing to those Pod+port combinations

Comment: They may very well be closely related, but tightly coupling them in prevents scaling them independently, or having different failure modes, and that list goes on. But, it's your app, so do as you wish. Anyway, as you correctly noticed, you can address your `php` container on the address `151.154.11.224:9002` so what's your question?

Comment: The deployment yaml was quite long, but I posted it now. I have a container called `php`. All the containers are closely related so I was hoping for them to go in the same container instead of separating them into different pods.


Here is another answer: 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 &quot;Cluster Networking&quot; (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 &quot;Pods&quot;, section &quot;Pods networking&quot; (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.
Within a Pod, containers share an IP address and port space, and can find
each other via localhost.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:10/15/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<php><nginx><kubernetes>', 'Title': 'kubernetes host not found in upstream "php"', 'ViewCount': 3402, 'id': 52808264, 'user_AboutMe': '', 'user_AccountId': 2726743.0, 'user_DisplayName': 'nealous3', 'user_Reputation': 664.0, 'user_Views': 62.0, 'user_id': 2351577.0}}