Microk8s dashboard using nginx-ingress via http not working (Error: `no matches for kind "Ingress" in version "extensions/v1beta1"`)

Microk8s dashboard using nginx-ingress via http not working (Error: `no matches for kind "Ingress" in version "extensions/v1beta1"`)

我在 Ubuntu 20.04.3 LTS 上安装了 microk8s v1.22.2 运行。

来自 /etc/hosts 的输出:

127.0.0.1 localhost
127.0.1.1 main

摘自microk8s status

addons:
  enabled:
    dashboard            # The Kubernetes dashboard
    ha-cluster           # Configure high availability on the current node
    ingress              # Ingress controller for external access
    metrics-server       # K8s Metrics Server for API access to service metrics

我检查了 运行 仪表板 (kubectl get all --all-namespaces):

NAMESPACE     NAME                                             READY   STATUS    RESTARTS   AGE
kube-system   pod/calico-node-2jltr                            1/1     Running   0          23m
kube-system   pod/calico-kube-controllers-f744bf684-d77hv      1/1     Running   0          23m
kube-system   pod/metrics-server-85df567dd8-jd6gj              1/1     Running   0          22m
kube-system   pod/kubernetes-dashboard-59699458b-pb5jb         1/1     Running   0          21m
kube-system   pod/dashboard-metrics-scraper-58d4977855-94nsp   1/1     Running   0          21m
ingress       pod/nginx-ingress-microk8s-controller-qf5pm      1/1     Running   0          21m

NAMESPACE     NAME                                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
default       service/kubernetes                  ClusterIP   10.152.183.1     <none>        443/TCP    23m
kube-system   service/metrics-server              ClusterIP   10.152.183.81    <none>        443/TCP    22m
kube-system   service/kubernetes-dashboard        ClusterIP   10.152.183.103   <none>        443/TCP    22m
kube-system   service/dashboard-metrics-scraper   ClusterIP   10.152.183.197   <none>        8000/TCP   22m

NAMESPACE     NAME                                               DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR            AGE
kube-system   daemonset.apps/calico-node                         1         1         1       1            1           kubernetes.io/os=linux   23m
ingress       daemonset.apps/nginx-ingress-microk8s-controller   1         1         1       1            1           <none>                   22m

NAMESPACE     NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
kube-system   deployment.apps/calico-kube-controllers     1/1     1            1           23m
kube-system   deployment.apps/metrics-server              1/1     1            1           22m
kube-system   deployment.apps/kubernetes-dashboard        1/1     1            1           22m
kube-system   deployment.apps/dashboard-metrics-scraper   1/1     1            1           22m

NAMESPACE     NAME                                                   DESIRED   CURRENT   READY   AGE
kube-system   replicaset.apps/calico-kube-controllers-69d7f794d9     0         0         0       23m
kube-system   replicaset.apps/calico-kube-controllers-f744bf684      1         1         1       23m
kube-system   replicaset.apps/metrics-server-85df567dd8              1         1         1       22m
kube-system   replicaset.apps/kubernetes-dashboard-59699458b         1         1         1       21m
kube-system   replicaset.apps/dashboard-metrics-scraper-58d4977855   1         1         1       21m

我想在本地网络中公开 microk8s 仪表板,以便通过 http://main/dashboard/

访问它

为此,我执行了以下操作 nano ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: public
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
  name: dashboard
  namespace: kube-system
spec:
  rules:
  - host: main
    http:
      paths:
      - backend:
          serviceName: kubernetes-dashboard
          servicePort: 443
        path: /

通过 kubectl apply -f ingress.yaml 启用入口配置出现以下错误:

error: unable to recognize "ingress.yaml": no matches for kind "Ingress" in version "extensions/v1beta1"

不胜感激,谢谢!

更新: @harsh-manvar 指出配置版本不匹配。我已经将 ingress.yaml 重写为一个非常精简的版本:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: dashboard
  namespace: kube-system
spec:
  rules:
  - http:
      paths:
      - path: /dashboard
        pathType: Prefix
        backend:
          service:
            name: kubernetes-dashboard
            port:
              number: 443

应用这个作品。此外,还创建了入口规则。

NAMESPACE     NAME        CLASS    HOSTS   ADDRESS     PORTS   AGE
kube-system   dashboard   public   *       127.0.0.1   80      11m

但是,当我通过 http://<ip-of-kubernetes-master>/dashboard 访问仪表板时,出现 400 错误。

来自入口控制器的日志:

192.168.0.123 - - [10/Oct/2021:21:38:47 +0000] "GET /dashboard HTTP/1.1" 400 54 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36" 466 0.002 [kube-system-kubernetes-dashboard-443] [] 10.1.76.3:8443 48 0.000 400 ca0946230759edfbaaf9d94f3d5c959a

仪表板是否也需要使用 microk8s proxy 公开?我以为入口控制器会处理这个,还是我误解了这个?

error: unable to recognize "ingress.yaml": no matches for kind "Ingress" in version "extensions/v1beta1"

这是因为入口 API 版本不匹配。

你是 运行 v1.22.2 而 YAML 中的 API 版本是旧的。

很好的例子:https://kubernetes.io/docs/tasks/access-application-cluster/ingress-minikube/

您在 YAML 中使用较旧的入口 API 版本,即 extensions/v1beta1

您需要根据您使用的入口版本和 K8s 版本更改此项 运行。

这适用于 K8s 中的 1.19 版本,也适用于 1.22

示例:

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

参考:https://kubernetes.io/docs/concepts/services-networking/ingress/

要修复错误 error: unable to recognize "ingress.yaml": no matches for kind "Ingress" in version "extensions/v1beta1,您需要将 apiVersion 设置为 networking.k8s.io/v1。来自 Kubernetes v1.16 article about deprecated APIs:

  • NetworkPolicy in the extensions/v1beta1 API version is no longer served - Migrate to use the networking.k8s.io/v1 API version, available since v1.8. Existing persisted data can be retrieved/updated via the new version.

现在进入第二期。您需要添加一些注释并在 Ingress 定义中进行一些更改,以使仪表板正确暴露在 microk8s 集群上:

我们需要他们将请求正确转发到后端 pods - good explanation in this article:

Note: The "nginx.ingress.kubernetes.io/rewrite-target" annotation rewrites the URL before forwarding the request to the backend pods. In /dashboard(/|$)(.*) for path, (.*) stores the dynamic URL that's generated while accessing the Kubernetes Dashboard. The "nginx.ingress.kubernetes.io/rewrite-target" annotation replaces the captured data in the URL before forwarding the request to the kubernetes-dashboard service. The "nginx.ingress.kubernetes.io/configuration-snippet" annotation rewrites the URL to add a trailing slash ("/") only if ALB-URL/dashboard is accessed.

我们还需要另外两个改变:

完成上述所有操作后,最终的 YAML 文件如下所示:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/configuration-snippet: |
      rewrite ^(/dashboard)$ / redirect;
    nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
    kubernetes.io/ingress.class: public
  name: dashboard
  namespace: kube-system
spec:
  rules:
  - http:
      paths:
      - path: /dashboard(/|$)(.*)
        pathType: Prefix
        backend:
          service:
            name: kubernetes-dashboard
            port:
              number: 443

它应该可以正常工作。无需 运行 microk8s proxy 命令。