Path-based routing ingress does not seem to work

2,425 views
Skip to first unread message

jamelse...@gmail.com

unread,
Mar 8, 2017, 8:42:05 PM3/8/17
to Kubernetes user discussion and Q&A
I am trying to use an Ingress with path-based routing, similar to the following example:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: path-based-ingress
spec:
rules:
- http:
paths:
- path: /foo
backend:
serviceName: http-svc
servicePort: 80

But when I make a request to the host ip address with a path (i.e. curl -i http://1.2.3.4/foo), I get a "404 - Not Found" response. If I change the path in the ingress config file to just "/" and run "curl -i http://1.2.3.4/", everything works fine; I get a valid response.

Thanks in advance for any help!

Guangya Liu

unread,
Mar 8, 2017, 9:04:07 PM3/8/17
to kubernet...@googlegroups.com
Please make sure adding ` ingress.kubernetes.io/rewrite-target: /` annotation to your service profile, otherwise, nginx proxy will not work.

```
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: mynginx
  annotations:
spec:
  rules:
    http:
      paths:
      - path: /v1
        backend:
          serviceName: my-nginx
          servicePort: 80
      - path: /v2
        backend:
          serviceName: my-nginx-1
          servicePort: 80
```



--
You received this message because you are subscribed to the Google Groups "Kubernetes user discussion and Q&A" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kubernetes-users+unsubscribe@googlegroups.com.
To post to this group, send email to kubernetes-users@googlegroups.com.
Visit this group at https://groups.google.com/group/kubernetes-users.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

jamelse...@gmail.com

unread,
Mar 8, 2017, 9:31:39 PM3/8/17
to Kubernetes user discussion and Q&A
I appreciate the suggestion! I have tried the "rewrite" annotation, and the result is the same. Also I am not using nginx.

Is my url request format (as I put in my previous post, just attaching the path to the end of the ip address) correct? I am at a loss. Ingress would be really useful if I could actually get this to work.
> To unsubscribe from this group and stop receiving emails from it, send an email to kubernetes-use...@googlegroups.com.
>
> To post to this group, send email to kubernet...@googlegroups.com.

Guangya Liu

unread,
Mar 8, 2017, 9:51:15 PM3/8/17
to kubernet...@googlegroups.com
Your template is right, are you using ingress controller?

Here want to show you an example as what I did:

1) start the ingress controller.

2) Create an deployment, service and ingress service.

```
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: mynginx
  annotations:
spec:
  rules:
  - host: mynginx.com
    http:
      paths:
      - path: /v1
        backend:
          serviceName: my-nginx
          servicePort: 80
---
apiVersion: v1
kind: Service
metadata:
  labels:
    run: my-nginx
  name: my-nginx
  namespace: default
spec:
  ports:
  - name: nginx
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: my-nginx
  type: NodePort
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    run: my-nginx
  name: my-nginx
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      run: my-nginx
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - image: gyliu/nginxv1:1.0
        imagePullPolicy: IfNotPresent
        name: my-nginx
        ports:
        - containerPort: 80
          protocol: TCP
```

After the objects create finished, then check ingress service.

```
[root@bd002 kq]# kubectl get ing -o yaml
apiVersion: v1
items:
- apiVersion: extensions/v1beta1
  kind: Ingress
  metadata:
    annotations:
    creationTimestamp: 2017-03-09T02:41:23Z
    generation: 1
    name: mynginx
    namespace: default
    resourceVersion: "625497"
    selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/mynginx
    uid: e22b0f6f-0471-11e7-8035-f2ecb40a350b
  spec:
    rules:
    - host: mynginx.com
      http:
        paths:
        - backend:
            serviceName: my-nginx
            servicePort: 80
          path: /v1
  status:
    loadBalancer:
      ingress:
      - ip: 9.111.253.118
kind: List
metadata: {}
resourceVersion: ""
selfLink: ""
```

On the host where you want to access the nginx service, add `9.111.253.118 mynginx.com` to your /etc/hosts.

Then `curl`.

This is my output:

```
[root@bd002 kq]# curl mynginx.com/v1
my-nginx-1427292677-mzcdz-v1
```

If you still have question, please check the nginx.conf in your nginx controller to check the proxy policy there, there should be some configuration as this:

```
upstream default-my-nginx-80 {
        least_conn;
        server 10.1.37.206:80 max_fails=0 fail_timeout=0;

    }

location ~* /v1 {

...
rewrite /v1/(.*) /$1 break;
rewrite /v1 / break;

        }

```

To unsubscribe from this group and stop receiving emails from it, send an email to kubernetes-users+unsubscribe@googlegroups.com.
To post to this group, send email to kubernetes-users@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages