无法在Kubernetes Ingress中配置基于路径的路由

时间:2018-04-16 22:40:46

标签: nginx kubernetes kubernetes-ingress

我在版本1.9.5上运行3节点裸机群集。

3个节点的IP是:

[root@node1 new]# kubectl get nodes
NAME      STATUS    ROLES         AGE       VERSION    IP
node1     Ready     master,node   1d        v1.9.5     172.16.16.1
node2     Ready     node          1d        v1.9.5     172.16.16.2
node3     Ready     node          1d        v1.9.5     172.16.16.3

我在下面解释的所有内容都是在1个名称空间中完成的,即ingress-nginx

我已部署了2个应用。

[root@node1 new]# kubectl get po -n ingress-nginx
NAME                                     READY     STATUS    RESTARTS   AGE
app1-5d4d466cc7-595lc                    1/1       Running   0          25m
app2-55cf97d86d-9v8gl                    1/1       Running   0          25m

他们的服务是:

[root@node1 new]# kubectl get svc -n ingress-nginx
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                        AGE
appsvc1                NodePort    10.233.60.145   <none>        80:32601/TCP                   25m
appsvc2                NodePort    10.233.46.230   <none>        80:30616/TCP                   25m

因此,当我通过NodePort访问它们时,我得到了我想要的结果。

curl http://172.16.16.2:32601
<h1>Hello app1!</h1>

curl http://172.16.16.2:30616
<h1>Hello app2!</h1>

现在我的目标是使用nginx入口控制器配置基于路径的路由,以便在最后,我可以使用

路由到app1
curl http://172.16.16.2/app1
<h1>Hello app1!</h1>

&

curl http://172.16.16.2/app2
<h1>Hello app2!</h1>

所以现在我使用ingress-nginx设置了入口控制器。

nginx控制器也部署在应用程序的同一名称空间中,即ingress-nginx

我的入口控制器已成功部署,因为日志显示:

[root@node1 new]# kubectl logs nginx-ingress-controller-9c7b694-bjn6h -n ingress-nginx
-------------------------------------------------------------------------------
NGINX Ingress controller
  Release:    0.12.0
  Build:      git-1df421a
  Repository: https://github.com/kubernetes/ingress-nginx
-------------------------------------------------------------------------------

I0415 16:08:18.736790       5 main.go:225] Running in Kubernetes Cluster version v1.9 (v1.9.5) - git (clean) commit f01a2bf98249a4db383560443a59bed0c13575df - platform linux/amd64
I0415 16:08:18.743855       5 main.go:84] validated ingress-nginx/default-http-backend as the default backend
I0415 16:08:19.182913       5 stat_collector.go:77] starting new nginx stats collector for Ingress controller running in namespace  (class nginx)
I0415 16:08:19.182944       5 stat_collector.go:78] collector extracting information from port 18080
I0415 16:08:19.211749       5 nginx.go:281] starting Ingress controller
I0415 16:08:20.325839       5 event.go:218] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"default", Name:"app-ingress", UID:"918405ac-410e-11e8-a473-080027917402", APIVersion:"extensions", ResourceVersion:"75539", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress default/app-ingress
I0415 16:08:20.326503       5 event.go:218] Event(v1.ObjectReference{Kind:"Ingress", Namespace:"ingress-nginx", Name:"nginx-ingress", UID:"8dc22500-410e-11e8-a473-080027917402", APIVersion:"extensions", ResourceVersion:"75538", FieldPath:""}): type: 'Normal' reason: 'CREATE' Ingress ingress-nginx/nginx-ingress
I0415 16:08:20.413514       5 store.go:614] running initial sync of secrets
I0415 16:08:20.413591       5 nginx.go:302] starting NGINX process...
I0415 16:08:20.418356       5 leaderelection.go:174] attempting to acquire leader lease...
W0415 16:08:20.422596       5 controller.go:777] service ingress-nginx/nginx-ingress does not have any active endpoints
I0415 16:08:20.422686       5 controller.go:183] backend reload required
I0415 16:08:20.422694       5 stat_collector.go:34] changing prometheus collector from  to default
I0415 16:08:20.439620       5 status.go:196] new leader elected: nginx-ingress-controller-9c7b694-h2n4b
I0415 16:08:20.534277       5 controller.go:192] ingress backend successfully reloaded...
W0415 16:08:28.768140       5 controller.go:777] service ingress-nginx/nginx-ingress does not have any active endpoints
I0415 16:09:00.478068       5 leaderelection.go:184] successfully acquired lease ingress-nginx/ingress-controller-leader-nginx
I0415 16:09:00.478207       5 status.go:196] new leader elected: nginx-ingress-controller-9c7b694-bjn6h

然后我使用:

配置我的入口
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: app-ingress
spec:
  rules:
  - host: my-test.com
    http:
      paths:
      - backend:
          serviceName: appsvc1
          servicePort: 80
        path: /app1
      - backend:
          serviceName: appsvc2
          servicePort: 80
        path: /app2

我使用以下方法创建了这个:

kubectl create -f app-ingress.yaml -n ingress-nginx

然后我使用以下方式公开:

apiVersion: v1
kind: Service
metadata:
  name: nginx-ingress
spec:
  type: NodePort
  externalIps:
  - 172.16.16.1
  - 172.16.16.2
  - 172.16.16.3
  ports:
    - port: 80
      nodePort: 30000
      name: http
  selector:
    app: nginx-ingress

app: nginx-ingress指向我在入口控制器窗格中的标签标记。

我使用以下方式部署它:

kubectl create -f nginx-ingress-controller-service.yaml -n=ingress

但是当我尝试使用网址访问应用时,我得到:

curl http://172.16.16.2/app1
default backend - 404

&

curl http://172.16.16.2/app2
default backend - 404

即使在做

curl http://my-test.com/app1
default backend - 404

&

curl http://my-test.com/app2
default backend - 404

不起作用。

我的/ etc / hosts文件是:

172.16.16.1 my-test.com

我有什么遗漏或做错了吗?

[root@node1 new]# kubectl get all -n ingress-nginx
NAME                              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deploy/app1                       2         2         2            2           48m
deploy/app2                       2         2         2            2           48m
deploy/default-http-backend       1         1         1            1           3h
deploy/nginx-ingress-controller   1         1         1            1           3h

NAME                                  DESIRED   CURRENT   READY     AGE
rs/app1-5d4d466cc7                    2         2         2         48m
rs/app2-55cf97d86d                    2         2         2         48m
rs/default-http-backend-55c6c69b88    1         1         1         3h
rs/nginx-ingress-controller-9c7b694   1         1         1         3h

NAME                              DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
deploy/app1                       2         2         2            2           48m
deploy/app2                       2         2         2            2           48m
deploy/default-http-backend       1         1         1            1           3h
deploy/nginx-ingress-controller   1         1         1            1           3h

NAME                                  DESIRED   CURRENT   READY     AGE
rs/app1-5d4d466cc7                    2         2         2         48m
rs/app2-55cf97d86d                    2         2         2         48m
rs/default-http-backend-55c6c69b88    1         1         1         3h
rs/nginx-ingress-controller-9c7b694   1         1         1         3h

NAME                                        READY     STATUS    RESTARTS   AGE
po/app1-5d4d466cc7-595lc                    1/1       Running   0          48m
po/app1-5d4d466cc7-5dn72                    1/1       Running   0          48m
po/app2-55cf97d86d-9v8gl                    1/1       Running   0          48m
po/app2-55cf97d86d-lckpn                    1/1       Running   0          48m
po/default-http-backend-55c6c69b88-8shkt    1/1       Running   0          3h
po/nginx-ingress-controller-9c7b694-bjn6h   1/1       Running   0          52m

NAME                       TYPE        CLUSTER-IP      EXTERNAL-IP                           PORT(S)                      AGE
svc/appsvc1                NodePort    10.233.60.145   <none>                                80:32601/TCP                 48m
svc/appsvc2                NodePort    10.233.46.230   <none>                                80:30616/TCP                 48m
svc/default-http-backend   ClusterIP   10.233.5.30     <none>                                80/TCP                       3h
svc/ingress-nginx          NodePort    10.233.6.186    <none>                                80:31301/TCP,443:32103/TCP   3h
svc/nginx-ingress          NodePort    10.233.9.163    172.16.16.1,172.16.16.2,172.16.16.3   80:30000/TCP                 2h

我甚至更改了我的入口配置以删除主机并为所有主机开放:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
  name: app-ingress
spec:
  rules:
  - http:
      paths:
      - path: /app1
        backend:
          serviceName: appsvc1
          servicePort: 80
      - path: /app2
        backend:
          serviceName: appsvc2
          servicePort: 80

现在我被重定向了。

[root@node1 ~]# curl http://172.16.16.1/app1
<html>
<head><title>308 Permanent Redirect</title></head>
<body bgcolor="white">
<center><h1>308 Permanent Redirect</h1></center>
<hr><center>nginx/1.13.9</center>
</body>
</html>
[root@node1 ~]# curl http://172.16.16.1/app2
<html>
<head><title>308 Permanent Redirect</title></head>
<body bgcolor="white">
<center><h1>308 Permanent Redirect</h1></center>
<hr><center>nginx/1.13.9</center>
</body>
</html>

Ingress控制器日志说:

10.233.102.128 - [10.233.102.128] - - [15/Apr/2018:17:35:52 +0000] "GET /app1 HTTP/1.1" 308 187 "-" "curl/7.29.0" 80 0.000 [ingress-nginx-appsvc1-80] - - - -
10.233.102.128 - [10.233.102.128] - - [15/Apr/2018:17:36:09 +0000] "GET /app1 HTTP/1.1" 308 187 "-" "curl/7.29.0" 79 0.000 [ingress-nginx-appsvc1-80] - - - -
10.233.102.128 - [10.233.102.128] - - [15/Apr/2018:17:36:11 +0000] "GET /app2 HTTP/1.1" 308 187 "-" "curl/7.29.0" 79 0.000 [ingress-nginx-appsvc2-80] - - - -
10.233.102.128 - [10.233.102.128] - - [15/Apr/2018:17:36:25 +0000] "GET /app2 HTTP/1.1" 308 187 "-" "curl/7.29.0" 85 0.000 [ingress-nginx-appsvc2-80] - - - -
10.233.102.128 - [10.233.102.128] - - [15/Apr/2018:17:36:51 +0000] "GET /app2 HTTP/1.1" 308 187 "-" "curl/7.29.0" 80 0.000 [ingress-nginx-appsvc2-80] - - - -

2 个答案:

答案 0 :(得分:1)

你的所有配置都很好,除了与注释有关的一件事:

  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /

您不需要此注释,因为它的处理时间早于路由,您始终会收到/的请求。您没有此路径的路径,因此只需删除此注释即可。

答案 1 :(得分:0)

您将必须在nginx控制器configmap中设置use-forwarded-headers: "true",并在入口中使用kubernetes.io/ingress.class: "nginx"批注。那应该使它工作