问题描述
我正在使用kubernetes.我有Ingress服务,它在谈论我的容器服务.我们已经公开了一个可以正常工作的webapi.但是我们不断收到502错误的网关错误.我是kubernetes的新手,我不知道如何调试该问题.服务器是连接到数据库的nodejs服务器.配置有什么问题吗?
I am using kubernetes. I have Ingress service which talks my container service. We have exposed a webapi which works all fine. But we keep getting 502 bad gateway error. I am new to kubernetes and i have no clue how to go about debugging this issue. Server is a nodejs server connected to database. Is there anything wrong with configuration?
我的部署文件-
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: my-pod
spec:
replicas: 1
template:
metadata:
labels:
app: my-pod
spec:
containers:
- name: my-pod
image: my-image
ports:
- name: "http"
containerPort: 8086
resources:
limits:
memory: 2048Mi
cpu: 1020m
---
apiVersion: v1
kind: Service
metadata:
name: my-pod-serv
spec:
ports:
- port: 80
targetPort: "http"
selector:
app: my-pod
我的Ingress服务:
My Ingress Service:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: gateway
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: abc.test.com
http:
paths:
- path: /abc
backend:
serviceName: my-pod-serv
servicePort: 80
推荐答案
在您的情况下:
我认为您收到此502网关错误,因为您没有正确配置Ingress控制器.请尝试使用已安装的Ingress进行操作,如以下示例所示.它将自动完成所有操作.
I think that you get this 502 gateway error because you don't have Ingress controller configured correctly.Please try do do it with installed Ingress like in example below. It will do all automatically.
Nginx入口逐步:
1) 安装头盔
2)使用头盔安装Nginx控制器
2) Install nginx controller using helm
$ helm install stable/nginx-ingress --name nginx-ingress
它将创建2个服务.您可以通过
It will create 2 services. You can get their details via
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.39.240.1 <none> 443/TCP 29d
nginx-ingress-controller LoadBalancer 10.39.243.140 35.X.X.15 80:32324/TCP,443:31425/TCP 19m
nginx-ingress-default-backend ClusterIP 10.39.252.175 <none> 80/TCP 19m
nginx-ingress-controller -简而言之,它处理对Ingress的请求和定向
nginx-ingress-controller - in short, it's dealing with requests to Ingress and directing
nginx-ingress-default-backend -简而言之,默认后端是一种处理所有URL路径并托管Nginx控制器不理解的服务
nginx-ingress-default-backend - in short, default backend is a service which handles all URL paths and hosts the nginx controller doesn't understand
3)创建2个部署(或使用您的部署)
3) Create 2 deployments (or use yours)
$ kubectl run my-pod --image=nginx
deployment.apps/my-pod created
$ kubectl run nginx1 --image=nginx
deployment.apps/nginx1 created
4)连接到其中一个窗格
$ kubectl exec -ti my-pod-675799d7b-95gph bash
并在输出中添加其他行,以查看我们稍后将尝试连接的那一行.
And add additional line to the output to see which one we will try to connect later.
$ echo "HELLO THIS IS INGRESS TEST" >> /usr/share/nginx/html/index.html
$ exit
5).公开部署.
$ kubectl expose deploy nginx1 --port 80
service/nginx1 exposed
$ kubectl expose deploy my-pod --port 80
service/my-pod exposed
这将自动创建服务,并且看起来像
This will automatically create service and will looks like
apiVersion: v1
kind: Service
metadata:
labels:
app: my-pod
name: my-pod
selfLink: /api/v1/namespaces/default/services/my-pod
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: my-pod
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
6)现在该创建Ingress.yaml并进行部署了.需要指定每个进入规则.在这里,我有2个服务.每个服务规范都在规则参数下以-host开头.
6) Now its the time to create Ingress.yaml and deploy it. Each rule in ingress need to be specified. Here I have 2 services. Each service specification starts with -host under rule parameter.
Ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: two-svc-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: my.pod.svc
http:
paths:
- path: /pod
backend:
serviceName: my-pod
servicePort: 80
- host: nginx.test.svc
http:
paths:
- path: /abc
backend:
serviceName: nginx1
servicePort: 80
$ kubectl apply -f Ingress.yaml
ingress.extensions/two-svc-ingress created
7),您可以检查Ingress和主机
7) You can check Ingress and hosts
$ kubectl get ingress
NAME HOSTS ADDRESS PORTS AGE
two-svc-ingress my.pod.svc,nginx.test.svc 35.228.230.6 80 57m
8)概述为什么安装Ingress.
8) Eplanation why I installed Ingress.
连接到入口控制器舱
$ kubectl exec -ti nginx-ingress-controller-76bf4c745c-prp8h bash
www-data@nginx-ingress-controller-76bf4c745c-prp8h:/etc/nginx$ cat /etc/nginx/nginx.conf
因为我在安装Ingress.yaml之后较早地安装了nginx ingress,所以nginx-ingress-controller发现了更改并自动添加了必要的代码.在此文件中,您应该能够找到两个服务的整体配置.我将不复制配置,而仅复制标题.
Because I have installed nginx ingress earlier, after deploying Ingress.yaml, the nginx-ingress-controller found changes and automatically added necessary code.In this file you should be able to find whole configuration for two services. I will not copy configuration but only headers.
启动服务器nginx.test.svc
start server nginx.test.svc
www-data @ nginx-ingress-controller-76bf4c745c-prp8h:/etc/nginx $退出
www-data@nginx-ingress-controller-76bf4c745c-prp8h:/etc/nginx$ exit
9)测试
$ kubectl get svc to get your nginx-ingress-controller external IP
$ curl -H "HOST: my.pod.svc" http://35.X.X.15/
default backend - 404
$ curl -H "HOST: my.pod.svc" http://35.X.X.15/pod
<!DOCTYPE html>
...
</html>
HELLO THIS IS INGRESS TEST
请记住,Ingress必须位于与服务相同的名称空间中.如果您在多个名称空间中有一些服务,则需要为每个名称空间创建Ingress.
Please keep in mind Ingress needs to be in the same namespace like services. If you have a few services in many namespace you need to create Ingress for each namespace.
这篇关于查找为什么我在kubernetes上收到502 Bad Gateway错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!