gpt4 book ai didi

azure - Kubernetes网络: my frontend cannot reach backend

转载 作者:行者123 更新时间:2023-12-02 12:15:44 26 4
gpt4 key购买 nike

我有以下可以正常工作的 docker-compose 文件:

version: '3'
services:
myfrontend:
image: myregistry.azurecr.io/im1:latest
container_name: myfrontend
ports:
- 80:80
- 443:443

mybackend:
image: myregistry.azurecr.io/im2:latest
container_name: mybackend
expose:
- 8080

后端仅将 8080 暴露给内部网络,前端有一个经过修改的 nginx 镜像,其配置如下(并且它的工作原理是 docker 使用容器名称解析 ip)

server {
listen 80 default_server;
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;

resolver 127.0.0.11 ipv6=off;

set $springboot "http://mybackend:8080";
proxy_pass $springboot;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

我将上述配置迁移到 kubernates 中,我从 nginx 收到 502 bad gateway 错误,我想是因为它无法解析后端地址。

这是 kubernatesconf,你能看一下并告诉我我做错了什么吗? :(

apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: mybackend
spec:
replicas: 1
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
minReadySeconds: 5
template:
metadata:
labels:
app: mybackend
spec:
nodeSelector:
"beta.kubernetes.io/os": linux
containers:
- name: mybackend
image: myregistry.azurecr.io/sgr-mybackend:latest
ports:
- containerPort: 8080
name: mybackend
resources:
requests:
cpu: 250m
limits:
cpu: 500m
---
apiVersion: v1
kind: Service
metadata:
name: mybackend
spec:
ports:
- port: 8080
selector:
app: mybackend
---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: myfrontend
spec:
replicas: 1
template:
metadata:
labels:
app: myfrontend
spec:
nodeSelector:
"beta.kubernetes.io/os": linux
containers:
- name: myfrontend
image: myregistry.azurecr.io/myfrontend:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: myfrontend
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: myfrontend

最佳答案

您需要将解析器设置为:

kube-dns.kube-system.svc.cluster.local

所以集群中的 kube-dns 名称\地址,因为本地主机上没有任何内容可以将 mybackend 解析为其 IP 地址。我不确定你是否需要这个,因为容器无论如何都会知道来自 kubernetes 的后端地址。我可能会放弃该设置

关于azure - Kubernetes网络: my frontend cannot reach backend,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56735450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com