I have four images in docker which are two services, a frontend and a reverseproxy.
When I was using docker-compose up
, it works and the services were running.
But when I want to use kubectl apply -f
to run in pod. There are [emerg] 1#1: host not found in upstream "backend-user:8080" in /etc/nginx/nginx.conf:11 in logs. I have no idea about this.
Here is my images:
bmjlearntocode/reverseproxy latest
bmjlearntocode/udacity-frontend local
bmjlearntocode/udacity-restapi-user latest
bmjlearntocode/udacity-restapi-feed latest
Here is the docker-compose.yaml
version: "3"
services:
reverseproxy:
image: bmjlearntocode/reverseproxy
ports:
- 8080:8080
restart: always
depends_on:
- backend-user
- backend-feed
backend-user:
image: bmjlearntocode/udacity-restapi-user
volumes:
- $HOME/.aws:/root/.aws
environment:
POSTGRESS_USERNAME: $POSTGRESS_USERNAME
POSTGRESS_PASSWORD: $POSTGRESS_PASSWORD
POSTGRESS_DB: $POSTGRESS_DB
POSTGRESS_HOST: $POSTGRESS_HOST
AWS_REGION: $AWS_REGION
AWS_PROFILE: $AWS_PROFILE
AWS_BUCKET: $AWS_BUCKET
JWT_SECRET: $JWT_SECRET
URL: "http://localhost:8100"
backend-feed:
image: bmjlearntocode/udacity-restapi-feed
volumes:
- $HOME/.aws:/root/.aws
environment:
POSTGRESS_USERNAME: $POSTGRESS_USERNAME
POSTGRESS_PASSWORD: $POSTGRESS_PASSWORD
POSTGRESS_DB: $POSTGRESS_DB
POSTGRESS_HOST: $POSTGRESS_HOST
AWS_REGION: $AWS_REGION
AWS_PROFILE: $AWS_PROFILE
AWS_BUCKET: $AWS_BUCKET
JWT_SECRET: $JWT_SECRET
URL: "http://localhost:8100"
frontend:
image: bmjlearntocode/udacity-frontend:local
ports:
- "8100:80"
Here is the nginx.conf
worker_processes 1;
events { worker_connections 1024; }
error_log /dev/stdout debug;
http {
sendfile on;
upstream user {
server backend-user:8080;
}
upstream feed {
server backend-feed:8080;
}
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
server {
listen 8080;
location /api/v0/feed {
proxy_pass http://feed;
}
location /api/v0/users {
proxy_pass http://user;
}
}
}
When I use docker-compose up
, it works.
Here is the reverseproxy-deployment.ymal
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
service: reverseproxy
name: reverseproxy
spec:
replicas: 2
template:
metadata:
labels:
service: reverseproxy
spec:
containers:
- image: bmjlearntocode/reverseproxy
name: reverseproxy
imagePullPolicy: Always
resources:
requests:
memory: "64Mi"
cpu: "250m"
limits:
memory: "1024Mi"
cpu: "500m"
ports:
- containerPort: 8080
restartPolicy: Always
I am not sure about why the nginx can not find the 'back-end' when the pod start, but can run in docker-compose up
I think its because backend-user only meaningful to docker network. When you define service in docker-compose file, you can access it with by its name. Obviously you cant access backend-user:8080 within kubernetes cluster because its undefined.
In kubernetes, you need Service resource for that kind of access. Also, you need Ingress resource or Service which its type nodeport for accesing apps from outside of kubernetes cluster(e.g from your browser).
you can check this resources. they are well written.
https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/ https://kubernetes.io/docs/tasks/access-application-cluster/ingress-minikube/
also you can put your nginx and back-end service in same pod and use localhost instead of hostname/servicename. so nginx can find upstream server.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With