Ingress
What is a ingress?
In Kubernetes, an Ingress is an object that allows access to Kubernetes services from outside the Kubernetes cluster. The external traffic could be via HTTP or HTTPS to a service running within your Kubernetes cluster.
Ingress in Kubernetes offers more advanced features for managing and routing traffic to APIs compared to NodePort and LoadBalancer.
- It allows you to direct requests to different services based on domain names, paths, or headers
- Ingress also provides built-in support for SSL/TLS encryption and load balancing
- It simplifies the configuration and centralizes the management of traffic routing rules
How Does Kubernetes Ingress work
There are 2 concepts in k8s ingress:
- Kubernetes Ingress Resource: Kubernetes Ingress Resource is responsible for storing DNS routing rules in the cluster. It specifies how incoming requests should be handled, such as domain-based routing or SSL/TLS termination.
- Kubernetes Ingress Controller: Kubernetes ingress controllers (Nginx/HAProxy etc.) are responsible for implementing the actual traffic routing based on the rules defined in the Ingress Resource. It configures the underlying load balancers or reverse proxies to direct the traffic accordingly.
Kubernetes Ingress Resource
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
The above declaration means, that all calls to test.apps.example.com
should hit the service named hello-service
residing in the dev
namespace.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Annotations
Ingress frequently uses annotations to configure some options depending on the Ingress controller, an example of which is the rewrite-target
annotation. Different Ingress controllers support different annotations.
Explain rewrite-target
annotation. For example we have 2 services:
watch
app athttp://<watch-service>:<port>/
wear
app athttp://<wear-service>:<port>/
We must configure Ingress to achieve the below:
- http://
: /watch –> http:// : / - http://
: /wear –> http:// : /
Without the rewrite-target
option, this is what would happen:
- http://
: /watch –> http:// : /watch - http://
: /wear –> http:// : /wear
Notice watch
and wear
at the end of the target URLs. The target applications are not configured with /watch
or /wear
paths. They are different applications built specifically for their purpose, so they don’t expect /watch
or /wear
in the URLs
To fix that we want to ReWrite the URL when the request is passed on to the watch
or wear
applications. We don’t want to pass in the same path that user typed in. So we specify the rewrite-target
option. This rewrites the URL by replacing whatever is under rules->http->paths->path
with the value in rewrite-target
. This works just like a search and replace function.
For example: replace(path, rewrite-target)
In our case: replace("/testpath","/")
Ingress rules
Each HTTP each contains the following information:
- An optional host. In the above example, no host is specified, so the rule applies to all inbound HTTP traffic throught the IP adress specified. If a host is provided (eg: dinhhuy258.com) the rules apply to that host.
- A list of paths (Eg:
/testpath
), each of which has an associated backend defined with aservice.name
andservice.port.name
orservice.port.number
. Both the host and path must match the content of an incomming request before the load balancer directs traffic to the referenced Service.
Ingress path types
Each path in Ingress is required to have a corresponding path type. There are three supported path types:
ImplementationSpecific
: With this path type, matching is up to the IngressClass. Implementation can treat this as a separatepathType
or treat it identically toPrefix
orExact
path types.Exact
: Match the URL path exactly and with case sensitive.Prefix
: Match based on a URL path prefix split by/
Note: If the last element of the path is a substring of the last element in the request path, it is not match (Eg: /foo/bar
matches /foo/bar/baz
but does not match /foo/barbaz
)
TLS
You can secure an Ingress by specifying a Secret that contains a TLS private key an certificate.
Eg:
1 2 3 4 5 6 7 8 9 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
Kubernetes Ingress Controller
An ingress controller is typically a reverse web proxy server implementation in the cluster. In kubernetes terms, it is a reverse proxy server deployed as kubernetes deployment exposed to a service type Loadbalancer. (Nginx is one of the widely used ingress controllers)
Key things to understand about ingress objects.
- An ingress object requires an ingress controller for routing traffic.
- And most importantly, the external traffic does not hit the ingress API, instead, it will hit the ingress controller service endpoint configured directly with a load balancer.
Follow this tutorial to learn how to setup an ingress controller in k8s.