入口 - 连接被拒绝

Ingress - Connection refused

我知道它们有很多类似的问题,但是在我阅读了所有这些之后我无法弄清楚...:/

首先我没有使用minikube,我使用的是kubernetes docker桌面。

我有简单的 express.js 服务器,例如:

app.get('/test', async (_, res) => {
  console.log("Sth happened")
  res.json({ status: 200 });
});

我正在尝试部署它并使用 ingress 将其公开到我的本地主机。

我做了什么: 在 etc/hosts 我添加了:

127.0.0.1 123test.mydomain.com

入口看起来像:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mytestservice
  namespace: mytestservice
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
    - host: 123test.mydomain.com
      http:
        paths:
          - pathType: ImplementationSpecific
            path: /
            backend:
              service:
                name: mytestservice
                port:
                  number: 80

服务:

apiVersion: v1
kind: Service
metadata:
  name: mytestservice
spec:
  ports:
  - name: http
    port: 80
    targetPort: 3000
  selector:
    name: mytestservice

部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mytestservice
spec:
  replicas: 1
  selector:
    matchLabels:
      name: mytestservice
  template:
    metadata:
      labels:
        name: mytestservice
    spec:
      containers:
        - name: mytestservice
          image: myimagename
          imagePullPolicy: IfNotPresent 
          ports:
            - containerPort: 3000
          env:
              ...my envs, doesnt matter

当我部署它时,pod 启动并且 运行 因为我能够看到启动日志 入口描述

kubectl get ingress mytestservice
NAME        CLASS    HOSTS                     ADDRESS   PORTS     AGE
mytestservice   <none>   123test.mydomain.com             80, 443   53s

kubectl describe ingress mytestservice
Name:             mytestservice
Namespace:        mytestservice
Address:
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host                     Path  Backends
  ----                     ----  --------
  123test.mydomain.com
                           /   mytestservice:80 (10.1.0.16:3000)
Annotations:               kubernetes.io/ingress.class: nginx
                           meta.helm.sh/release-name: mytestservice
                           meta.helm.sh/release-namespace: mytestservice
Events:                    <none>

当我卷曲它时,我收到:

curl 123test.mydomain.com/test
curl: (7) Failed to connect to 123test.mydomain.com port 80: Connection refused 

您是否在同一个命名空间中创建了所有资源?我看到您的入口定义为:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: mytestservice
  namespace: mytestservice

因此它将在 mytestservice 命名空间中创建。而您的部署和服务在元数据中没有 namespace: mytestservice。因此它们将在 default 命名空间中创建。

检查您的所有资源是否都在同一个命名空间中:

kubectl get deployments --namespace mytestservice
kubectl get service --namespace mytestservice
kubectl get ingress --namespace mytestservice

入口描述中有错误提示:

Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)

问题出在 DNS 上。在本地 /etc/hosts 中,您指向本地主机而不是 Kubernetes 主机的 IP。