Kubernetes nginx Ingress 配置不适用于 Grafana

Kubernetes nginx Ingress configuration not working for Grafana

我不熟悉为我的 Kubernetes 集群配置 Ingress 规则。

我的 Kubernetes 集群部署在 Bare Metal 上。没有云。

我按照这个 link 在我的集群中使用 RBAC 设置了我的 nginx-controller。

这是我部署的:

# kubectl get all -n ingress-nginx
NAME                                           READY     STATUS    RESTARTS   AGE
pod/default-http-backend-7c5bc89cc9-ks6kd      1/1       Running   0          2h
pod/nginx-ingress-controller-5b6864749-8xbhf   1/1       Running   0          2h

NAME                           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
service/default-http-backend   ClusterIP   10.233.15.56   <none>        80/TCP                       2h
service/ingress-nginx          NodePort    10.233.38.84   <none>        80:31118/TCP,443:32003/TCP   2h

NAME                                       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/default-http-backend       1         1         1            1           2h
deployment.apps/nginx-ingress-controller   1         1         1            1           2h

NAME                                                 DESIRED   CURRENT   READY     AGE
replicaset.apps/default-http-backend-7c5bc89cc9      1         1         1         2h
replicaset.apps/nginx-ingress-controller-5b6864749   1         1         1         2h

鉴于我的设置,我想使用 URL.

访问我的 grafana 仪表板

我的 grafana 设置工作得很好。

# kubectl get all -n default
NAME                           READY     STATUS    RESTARTS   AGE
pod/grafana-67c6585fbd-4jl7p   1/1       Running   0          2h

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
service/grafana      NodePort    10.233.5.111   <none>        3000:32093/TCP   2h


NAME                      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/grafana   1         1         1            1           2h

NAME                                 DESIRED   CURRENT   READY     AGE
replicaset.apps/grafana-67c6585fbd   1         1         1         2h

我可以使用 http://10.27.239.145:32093 访问仪表板,这是我的一个 K8S 工作节点的 IP。

现在我不想通过 IP:NodePort 访问,而是想通过 URL 访问,例如grafana.test.mydomain.com

所以我在默认命名空间中配置的入口规则是:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  creationTimestamp: 2018-09-25T20:32:24Z
  generation: 5
  name: grafana
  namespace: default
  resourceVersion: "28485"
  selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/jenkins-tls
  uid: 1c51cece-c102-11e8-bf0f-02000a1bef39
spec:
  rules:
  - host: grafana.test.mydomain.com
    http:
      paths:
      - backend:
          serviceName: grafana
          servicePort: 3000
        path: /

在我正在测试的本地笔记本电脑上,我已将以下条目添加到我的 /etc/hosts 中:

10.27.239.145 grafana.test.mydomain.com

在我的浏览器中,我尝试访问 http://grafana.test.mydomain.com 但我只得到 This site can’t be reached grafana.test.mydomain.com refused to connect.

我有一种强烈的感觉,我错过了什么,但又想不通。

我将 NodePort 更改为 ClusterIP,但没有成功。

我知道我的入口控制器正在工作,因为每次我更改入口规则时,我都会从入口控制器获取日志。

I0925 21:00:19.041440       9 event.go:221] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"grafana", UID:"1c51cece-c102-11e8-bf0f-02000a1bef39", APIVersion:"extensions/v1beta1", ResourceVersion:"28485", FieldPath:""}): type: 'Normal' reason: 'UPDATE' Ingress default/grafana
I0925 21:00:19.041732       9 controller.go:171] Configuration changes detected, backend reload required.
I0925 21:00:19.216044       9 controller.go:187] Backend successfully reloaded.
I0925 21:00:19.217645       9 controller.go:204] Dynamic reconfiguration succeeded.

对于我可能遗漏的内容,我们将不胜感激。

据我所知,您在端口 32093 上只有一个 NodePort 服务。

您的 NodePort 将端口 3000 发布到 32093 到您已经证明的任何外部节点地址,但是您配置 Ingress 以联系 grafana 上的端口 3000服务。

targetPortportnodePort 添加到您的 Grafana 实例的服务中,并将 targetPortport 指向 3000 并保留 nodePort empty/set 它到 32092。然后入口应该像你发布的那样工作。片段:

nodePort: 32093
port: 3000
protocol: TCP
targetPort: 3000

或者尝试将入口配置中的 servicePort: 3000 设置为 32093警告:我从未测试过这个。我不知道 Ingress 是否支持。根据文档,NodePort 应该是 ClusterIP 的超集:

NodePort: Exposes the service on each Node’s IP at a static port (the NodePort). A ClusterIP service, to which the NodePort service will route, is automatically created. You’ll be able to contact the NodePort service, from outside the cluster, by requesting :.

编辑 顺便说一句:http://grafana.test.mydomain.com:32093 应该已经可以使用您的配置 (NodePort)

据我所知,您需要将 grafana.test.mydomain.com 设置为指向 10.233.38.84

基本上,您的 nginx 控制器服务将流量定向到您的入口,然后您的入口将其转发到 nodePort 上的后端(这在入口中是隐含的)。它适用于我,但我使用的是 AWS ELB,我基本上将 grafana.test.mydomain.com 设置为指向 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-0000000000.us-west-2.elb.amazonaws.com

$ kubectl get all -n ingress-nginx
NAME                                            READY     STATUS    RESTARTS   AGE
pod/default-http-backend-6586bc58b6-snxbv       1/1       Running   0          1h
pod/grafana-5b969bb7f9-tsv5k                    1/1       Running   0          52m
pod/nginx-ingress-controller-6bd7c597cb-lfwcf   1/1       Running   0          1h
pod/prometheus-server-5dbf9f4fc9-mnwn4          1/1       Running   0          53m

NAME                           TYPE           CLUSTER-IP       EXTERNAL-IP                                                               PORT(S)                      AGE
service/default-http-backend   ClusterIP      10.x.x.x         <none>                                                                    80/TCP                       1h
service/grafana                NodePort       10.x.x.x         <none>                                                                    3000:30073/TCP               52m
service/ingress-nginx          LoadBalancer   10.x.x.x         aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-0000000000.us-west-2.elb.amazonaws.com   80:30276/TCP,443:32011/TCP   1h
service/prometheus-server      NodePort       10.x.x.x         <none>                                                                    9090:32419/TCP               53m

NAME                                       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/default-http-backend       1         1         1            1           1h
deployment.apps/grafana                    1         1         1            1           52m
deployment.apps/nginx-ingress-controller   1         1         1            1           1h
deployment.apps/prometheus-server          1         1         1            1           53m

NAME                                                  DESIRED   CURRENT   READY     AGE
replicaset.apps/default-http-backend-6586bc58b6       1         1         1         1h
replicaset.apps/grafana-5b969bb7f9                    1         1         1         52m
replicaset.apps/nginx-ingress-controller-6bd7c597cb   1         1         1         1h
replicaset.apps/prometheus-server-5dbf9f4fc9          1         1         1         53m

$ kubectl describe ingress grafana-ingress -n ingress-nginx
Name:             grafana-ingress
Namespace:        ingress-nginx
Address:          aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-0000000000.us-west-2.elb.amazonaws.com
Default backend:  default-http-backend:80 (<none>)
Rules:
  Host                       Path  Backends
  ----                       ----  --------
  grafana.test.mydomain.com
                             /   grafana:3000 (<none>)
Annotations:
  kubectl.kubernetes.io/last-applied-configuration:  {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"nginx.ingress.kubernetes.io/rewrite-target":"/"},"name":"grafana-ingress","namespace":"ingress-nginx"},"spec":{"rules":[{"http":{"paths":[{"backend":{"serviceName":"grafana","servicePort":3000},"path":"/"}]}}]}}

  nginx.ingress.kubernetes.io/rewrite-target:  /
Events:
  Type    Reason  Age                From                      Message
  ----    ------  ----               ----                      -------
  Normal  CREATE  40m                nginx-ingress-controller  Ingress ingress-nginx/grafana-ingress
  Normal  UPDATE  22m (x2 over 40m)  nginx-ingress-controller  Ingress ingress-nginx/grafana-ingress