我正在尝试在AWS EKS的Kubernetes集群中部署简单的REST API应用程序。其他都是反向代理。
我使用以下配置进行部署。Pod是健康的,Pod每20分钟重新启动一次,因为AWS会断开与我的开发免费层db的连接,但我运行了几个副本,以防万一。
当我尝试连接到域时,我得到502坏网关错误。我真不知道发生了什么事。
apiVersion: apps/v1
kind: Deployment
metadata:
name: xyz-microservice
spec:
replicas: 4
template:
nodeSelector:
node.kubernetes.io/role: worker
tolerations:
- effect: NoSchedule
key: node.kubernetes.io/role
operator: Equal
value: worker
selector:
matchLabels:
app: xyz-microservice
template:
metadata:
labels:
app: xyz-microservice
spec:
hostname: xyz-microservice
containers:
- name: xyz-microservice
image: redacted
imagePullPolicy: Always
env:
- name: DB_USERNAME
value: redacted
- name: DB_PASSWORD
value: redacted
- name: DB_HOST
value: redacted
- name: DB_PORT
value: "5432"
- name: DB_NAME
value: redacted
resources:
requests:
memory: "32Mi"
cpu: "80m"
limits:
memory: "128Mi"
cpu: "100m"
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: xyz-microservice-service
spec:
type: ClusterIP
selector:
app: xyz-microservice
ports:
- port: 80
targetPort: 8080
protocol: TCP
入口配置:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "internal"
forecastle.stakater.com/expose: "true"
forecastle.stakater.com/appName: "XYZ Microservice"
cert-manager.io/cluster-issuer: "letsencrypt-aws"
name: xyz-microservice
spec:
rules:
- host: xyz.internal.staging.k8s.redacted
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: xyz-microservice-service
port:
number: 8080
tls:
- hosts:
- xyz.internal.staging.k8s.redacted
secretName: xyz-microservice-tls
如果有人能引导我,那就太好了。
请记住,几乎每次在nginx-ingress中遇到502错误网关时,它都与后端端口绑定问题/后端配置名称错误等有关。
这里你的nginx入口配置为重定向流量从"xyz.internal.staging.k8s.redacted"主机到您的"xyz-microservice-service"的8080端口;kubernetes服务。
然而8080是你的服务的targetPort。不,实际上是80.
因此,要解决这个问题,只需更改后端服务的端口,如下所示:apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "internal"
forecastle.stakater.com/expose: "true"
forecastle.stakater.com/appName: "XYZ Microservice"
cert-manager.io/cluster-issuer: "letsencrypt-aws"
name: xyz-microservice
spec:
rules:
- host: xyz.internal.staging.k8s.redacted
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: xyz-microservice-service
port:
number: 80
tls:
- hosts:
- xyz.internal.staging.k8s.redacted
secretName: xyz-microservice-tls
(或者重新配置服务的端口以匹配入口配置的端口)