Nginx Ingress 在 microk8s 上提供 404 并提供工作服务

发布于 2025-01-10 15:44:21 字数 2163 浏览 0 评论 0原文

在 Ubuntu 20.04.4 LTS 上运行 microk8s v1.23.3。我已经设置了一个最小的 pod+service:

kubectl create deployment whoami --image=containous/whoami --namespace=default

这按预期工作,curl 10.1.76.4:80 给出了 whoami 的正确回复。 我配置了一个服务,请参阅 service-whoami.yaml 的内容:

apiVersion: v1
kind: Service
metadata:
  name: whoami
  namespace: default
spec:
  selector:
    app: whoami
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

这也按预期工作,可以通过 curl 10.152.183.220:80 上的 clusterIP 访问 pod 。 现在我想使用 ingress-whoami.yaml 公开服务:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: whoami-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  defaultBackend:
    service:
      name: whoami
      port:
        number: 80
  rules:
  - http:
      paths:
      - path: /whoami
        pathType: Prefix
        backend:
          service:
            name: whoami
            port:
              number: 80

启用了 ingress 插件。

microk8s is running
high-availability: no
  datastore master nodes: 127.0.0.1:19001
  datastore standby nodes: none
addons:
  enabled:
    ha-cluster           # Configure high availability on the current node
    ingress              # Ingress controller for external access

入口似乎指向正确的 Pod 和端口。 kubectl描述入口给出

Name:             whoami-ingress
Labels:           <none>
Namespace:        default
Address:
Default backend:  whoami:80 (10.1.76.12:80)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *
              /whoami   whoami:80 (10.1.76.12:80)
Annotations:  <none>
Events:       <none>

尝试使用curl 127.0.0.1/whoami从外部到达pod会给出404

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

我哪里出错了?这个设置在几周前就起作用了。

Running microk8s v1.23.3 on Ubuntu 20.04.4 LTS. I have set up a minimal pod+service:

kubectl create deployment whoami --image=containous/whoami --namespace=default

This works as expected, curl 10.1.76.4:80 gives the proper reply from whoami.
I have a service configured, see content of service-whoami.yaml:

apiVersion: v1
kind: Service
metadata:
  name: whoami
  namespace: default
spec:
  selector:
    app: whoami
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

This also works as expected, the pod can be reached through the clusterIP on curl 10.152.183.220:80.
Now I want to expose the service using the ingress-whoami.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: whoami-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  defaultBackend:
    service:
      name: whoami
      port:
        number: 80
  rules:
  - http:
      paths:
      - path: /whoami
        pathType: Prefix
        backend:
          service:
            name: whoami
            port:
              number: 80

ingress addon is enabled.

microk8s is running
high-availability: no
  datastore master nodes: 127.0.0.1:19001
  datastore standby nodes: none
addons:
  enabled:
    ha-cluster           # Configure high availability on the current node
    ingress              # Ingress controller for external access

ingress seems to point to the correct pod and port. kubectl describe ingress gives

Name:             whoami-ingress
Labels:           <none>
Namespace:        default
Address:
Default backend:  whoami:80 (10.1.76.12:80)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *
              /whoami   whoami:80 (10.1.76.12:80)
Annotations:  <none>
Events:       <none>

Trying to reach the pod from outside with curl 127.0.0.1/whoami gives a 404:

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>

Where did I go wrong? This setup worked a few weeks ago.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兮颜 2025-01-17 15:44:21

好吧,想通了。
我忘记在注释块中指定ingress.class
我更新了 ingress-whoami.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: whoami-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: public
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /whoami
        pathType: Prefix
        backend:
          service:
            name: whoami
            port:
              number: 80

现在一切正常。

Ok, figured it out.
I had forgotten to specify the ingress.class in the annotations-block.
I updated ingress-whoami.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: whoami-ingress
  namespace: default
  annotations:
    kubernetes.io/ingress.class: public
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /whoami
        pathType: Prefix
        backend:
          service:
            name: whoami
            port:
              number: 80

Now everything is working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文