"host not found in upstream ..." 使用 'kubectl apply -f' 但在 'docker-compose up' 中有效
"host not found in upstream ..." when using 'kubectl apply -f' but works in 'docker-compose up'
我在 docker 中有四个图像,它们是两个服务,一个前端和一个反向代理。
当我使用 docker-compose up
时,它工作正常并且服务 运行ning。
但是当我想在 pod 中使用 kubectl apply -f
到 运行 时。日志中/etc/nginx/nginx.conf:11中有[emerg] 1#1: host not found in upstream "backend-user:8080"。我对此一无所知。
这是我的图片:
bmjlearntocode/reverseproxy latest
bmjlearntocode/udacity-frontend local
bmjlearntocode/udacity-restapi-user latest
bmjlearntocode/udacity-restapi-feed latest
这里是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"
这里是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;
}
}
}
当我使用 docker-compose up
时,它有效。
这里是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
我不确定为什么 nginx 在 pod 启动时找不到 'back-end',但可以在 docker-compose up
中找到 运行
我认为这是因为后端用户只对 docker 网络有意义。当您在 docker-compose 文件中定义服务时,您可以通过其名称访问它。显然你不能在 kubernetes 集群中访问 backend-user:8080 因为它是未定义的。
在 kubernetes 中,您需要服务资源才能进行此类访问。此外,您需要入口资源或服务,其类型为节点端口,用于从 kubernetes 集群外部(例如从您的浏览器)访问应用程序。
您可以查看此资源。他们写得很好。
https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/
https://kubernetes.io/docs/tasks/access-application-cluster/ingress-minikube/
您也可以将 nginx 和后端服务放在同一个 pod 中,并使用 localhost 而不是 hostname/servicename。这样 nginx 就可以找到上游服务器。
我在 docker 中有四个图像,它们是两个服务,一个前端和一个反向代理。
当我使用 docker-compose up
时,它工作正常并且服务 运行ning。
但是当我想在 pod 中使用 kubectl apply -f
到 运行 时。日志中/etc/nginx/nginx.conf:11中有[emerg] 1#1: host not found in upstream "backend-user:8080"。我对此一无所知。
这是我的图片:
bmjlearntocode/reverseproxy latest
bmjlearntocode/udacity-frontend local
bmjlearntocode/udacity-restapi-user latest
bmjlearntocode/udacity-restapi-feed latest
这里是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"
这里是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;
}
}
}
当我使用 docker-compose up
时,它有效。
这里是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
我不确定为什么 nginx 在 pod 启动时找不到 'back-end',但可以在 docker-compose up
我认为这是因为后端用户只对 docker 网络有意义。当您在 docker-compose 文件中定义服务时,您可以通过其名称访问它。显然你不能在 kubernetes 集群中访问 backend-user:8080 因为它是未定义的。
在 kubernetes 中,您需要服务资源才能进行此类访问。此外,您需要入口资源或服务,其类型为节点端口,用于从 kubernetes 集群外部(例如从您的浏览器)访问应用程序。
您可以查看此资源。他们写得很好。
https://kubernetes.io/docs/tasks/access-application-cluster/service-access-application-cluster/ https://kubernetes.io/docs/tasks/access-application-cluster/ingress-minikube/
您也可以将 nginx 和后端服务放在同一个 pod 中,并使用 localhost 而不是 hostname/servicename。这样 nginx 就可以找到上游服务器。