Redirecting host to service path in kubernetes

+5 votes

How do I redirect host to a service path in kubernetes?

I'm trying to try to match a host address to a service path, as an example, let's think i have an nginx pod servying to sites: site1 and site2. Let's think about a service called my-nginx-service which services my two sites as paths:

- my-nginx-service (178.123.55.37) - /site1 - /site2

There exists a way to map it to something like:

- host: site-one.mydomain.com http: paths: - backend: serviceName: nginx-service servicePort: 80 servicePath: /site2 - host: site-two.mydomain.com http: paths: - backend: serviceName: nginx-service servicePort: 80 servicePath: /site2

Kindly help me out

Mar 27, 2018 in Kubernetes by DareDev
• 6,890 points
3,128 views

2 answers to this question.

+1 vote
Best answer

What you are trying to do is impossible, because services are unaware of host names. You might be able to reach what you want by modifying nginx configuration within the pod.

What you need to do is to add an ingress resource. Ingresses are aware of host names. They do load balancing between services. So, /site1 one would go to one service, and /site2 to the other one.

This is what the ingress should look like:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host: site-one.mydomain.com
    http:
      paths:
      - backend:
          serviceName: nginx-service1
          servicePort: 80
  - host: site-two.mydomain.com
    http:
      paths:
      - backend:
          serviceName: nginx-service2
          servicePort: 80

As you can see site1 and site2 would be running in different deployments, with different services targeting them. Ingress, which is L7 Load Balancer would be able to check the host name and forward the request to the right service, which in the end would hit the right pod.

You can also add paths to each host, so site-one.mydomain.com/path1, site-one.mydomain.com/path2 and site-two.mydomain.com/path1 , would be forwarded to different services.

The Ingress yaml file would look like this:

apiVersion: extensions/v1beta1
kind: Ingress
 metadata:
  name: ingress
 spec:
  rules:
  - host: site-one.mydomain.com
    http:
      paths:
      - path: /path1
        backend:
          serviceName: nginx-service1
          servicePort: 80
      - path: /path2
        backend:
          serviceName: nginx-service2
          servicePort: 8080
      - path: /
        backend:
          serviceName: nginx-service3
          servicePort: 80
  - host: site-two.mydomain.com
    http:
      paths:
      - path: /path1
        backend:
          serviceName: nginx-service1
          servicePort: 80

Try this out and let me know if it worked for you!

answered Mar 27, 2018 by DragonLord999
• 8,450 points

selected Oct 12, 2018 by Omkar
0 votes

Execute this command:

kubectl explain ingress.spec.rules.http.paths.backend:

...
FIELDS:
   serviceName  <string> -required-
     Specifies the name of the referenced service.

   servicePort  <string> -required-
     Specifies the port of the referenced service.
...

Specify your path in the following field: ingress.spec.rules.http.paths.path so that when that path is hit the request is routed using the Service specified at ingress.spec.rules.http.paths.backend.serviceName. If you're using nginx ingress the annotation nginx.ingress.kubernetes.io/rewrite-target: / can be used to redirect the request to /.

answered Oct 12, 2018 by Hannah
• 18,570 points

Related Questions In Kubernetes

0 votes
1 answer

unable to start Kubernetes due to so many open files in system

You can try the following steps: You can ...READ MORE

answered May 1, 2018 in Kubernetes by shubham
• 7,340 points
1,808 views
0 votes
1 answer

How to use gravitational teleport in a container/kubernetes environment?

You can use teleport to augment kubernetes ...READ MORE

answered Jun 28, 2018 in Kubernetes by ajs3033
• 7,300 points
2,217 views
0 votes
1 answer

Not able to access kubernetes api from a pod in azure

Follow these steps Add --bind-address=0.0.0.0 option to the line https://github.com/kubernetes/kubernetes/blob/v1.2.0/docs/getting-started-guides/coreos/azure/cloud_config_templates/kubernetes-cluster-main-nodes-template.yml#L218  Created ...READ MORE

answered Aug 30, 2018 in Kubernetes by Kalgi
• 52,360 points
804 views
0 votes
1 answer

How do I sign-in to kubernetes dashboard?

Since version 1.7 Dashboard uses more secure ...READ MORE

answered Sep 7, 2018 in Kubernetes by DareDev
• 6,890 points
2,017 views
0 votes
2 answers

Not able to expose port 80 on the host, kubernetes ingress

I was facing the same error. The nginix ...READ MORE

answered Sep 11, 2018 in Kubernetes by Kalgi
• 52,360 points
1,722 views
+1 vote
1 answer
0 votes
3 answers

Error while joining cluster with node

Hi Kalgi after following above steps it ...READ MORE

answered Jan 17, 2019 in Others by anonymous
14,526 views
0 votes
1 answer

Nginx routing for kubernetes services

Hey, backend is a service running on ...READ MORE

answered Feb 8, 2019 in Kubernetes by Kalgi
• 52,360 points
1,112 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP