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