Introduction

In this new article we discuss about Ingresses.

What is an Ingress ?

An Ingress is basically the URL which will be used to reach your app. It exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource.

    internet
        |
   [ Ingress ]
   --|-----|--
   [ Services ]

An Ingress may be configured to give Services externally-reachable URLs, load balance traffic, terminate SSL / TLS, and offer name based virtual hosting.

How to create an Ingress

Using a configuration YAML file

As usual, we create the following YAML File with the following mandatory parts :

apiVersion: 
kind: 
metadata:


spec:

In our example, we want to create an Ingress which will redirect the URL http://demo.lionel.com/service1 or http://demo.lionel.com/service2 to the specified service.

We want the Ingress to remove the “/service1” and “/service2” automatically.

So let’s begin with these 4 sections :

  • The api version in our case is “networking.k8s.io/v1beta1”.
  • The kind will be Ingress, for obvious reasons
  • Metadata will contain the name of our ingress. It can obviously contain the namespace or labels. We will discuss about the annotations parameter later.
  • Finally, the “spec” is the most important part of our YAML file. It will contain information needed to define an particular ingress in the given namespace.

Here is our final YAML file called ingress.yaml :

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-example
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: demo.lionel.com
    http:
      paths:
      - path: /service1
        backend:
          serviceName: service1
          servicePort: 4200
      - path: /service2
        backend:
          serviceName: service2
          servicePort: 8080

Notice the annotations part with the rewrite-target option.

This rewrites the URL by replacing whatever is under rules->http->paths->path which happens to be /service1 or /service2 in this case with the value in rewrite-target. This works just like a search and replace function.

For example: replace(service1, rewrite-target)

In our case: replace(“/service2″,”/”)

We can now deploy our network policy running the following command :

$ kubectl create -f ingress.yaml

And that’s all !

Sources

kubernetes.io

Leave a Comment

Your email address will not be published. Required fields are marked *