我是为Kubernetes集群配置Ingress规则的新手。
我的Kubernetes集群部署在Bare Metal上。没有云。
我遵循此link在集群中使用RBAC设置了Nginx控制器。
这就是我部署的内容:
# 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.
对于我可能错过的任何事项,我们将不胜感激。
答案 0 :(得分:1)
据我所知,端口32093上只有一个NodePort服务。
您已经证明,您的NodePort将端口3000
发布到32093
到任何外部节点地址,但是您将Ingress配置为与3000
服务上的端口grafana
联系。
将targetPort
,port
和nodePort
添加到您的Grafana实例的服务中,并将targetPort
和port
指向3000,然后将nodePort保留为空/将其设置为32092。然后入口应按您发布的那样工作。片段:
nodePort: 32093
port: 3000
protocol: TCP
targetPort: 3000
或尝试将入口配置中的servicePort: 3000
设置为32093
。 警告:我从未测试过。我不知道Ingress是否支持。根据文档,它应该是NodePort是ClusterIP的超集:
NodePort:在静态端口(NodePort)上公开每个节点IP上的服务。将自动创建NodePort服务将路由到的ClusterIP服务。您可以通过请求:。
从集群外部联系NodePort服务。
修改 顺便说一句:http://grafana.test.mydomain.com:32093应该已经可以使用您的配置(NodePort)
答案 1 :(得分:1)
据我所见,您需要将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