Kubernetes Ingress Example: Step-by-Step Guide

Looking for a Kubernetes ingress example? Ingress is a crucial feature in Kubernetes that lets you manage how external users access services running in your cluster. Understanding ingress configuration and seeing real-world examples can help you implement ingress effectively and securely.

Kubernetes Ingress Example Explained

To illustrate how Kubernetes ingress works, let's walk through a basic example. Suppose you have two services, service-a and service-b, each exposed on different paths. Below is a simple ingress resource definition in YAML:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /service-a
        pathType: Prefix
        backend:
          service:
            name: service-a
            port:
              number: 80
      - path: /service-b
        pathType: Prefix
        backend:
          service:
            name: service-b
            port:
              number: 80

This ingress example directs traffic based on the request path so that traffic to /service-a goes to service-a, while /service-b requests are routed to service-b. You can learn more about setting up Kubernetes ingress resources through the Solo.io Kubernetes API Gateway documentation.

When to Use Ingress vs. LoadBalancer or NodePort?

  • Ingress: Best for handling complex HTTP(S) routing rules and TLS/SNI-based routing.
  • LoadBalancer: Good when you need direct, simple exposure of a service with a dedicated cloud load balancer.
  • NodePort: Useful mainly for dev/testing when exposing a service on a static port of each node.

Ingress allows centralized and automated management of routing logic, reducing manual configuration and improving scalability and security for your microservices architecture.

Frequently Asked Questions

What is a real-world Kubernetes ingress example?

A real-world example typically maps specific URLs or hostnames to different backend services; for instance, a company might route api.company.com/login to an authentication backend using an ingress resource.

How do I create a Kubernetes ingress resource?

Create a YAML file defining rules under spec.rules and apply it with kubectl apply -f <file>.yaml. Then ensure an ingress controller (such as Gloo Gateway or NGINX) is running in your cluster.

Are there security best practices for ingress?

Yes—always use TLS encryption, restrict ingress rules to only accepted paths/hosts, and regularly audit your configurations. Check the latest security recommendations at Solo.io's ingress guide.

A Practical Guide

Kubernetes Ingress Example: Step-by-Step Guide | Solo.io

What is Kubernetes Ingress?

A Kubernetes ingress is an API object used to manage external user access to services running in a Kubernetes cluster. It provides routing rules, defined within the ingress resource, which you can use to configure and manage external access to your clusters. The HTTPS/HTTP protocol is commonly used to facilitate routing.

An empty ingress point provides a single point of entry into a cluster or service, making it easier to manage applications and troubleshoot routing issues. Its primary functions are traffic load balancing, secure sockets layer (SSL) termination, and name-based virtual hosting.

An ingress is an alternative to creating a dedicated network load balancer in front of Kubernetes services, or manually exposing services within a node. It lets you flexibly configure routing rules, greatly simplifying your production environment.

This is part of a series of articles about Kubernetes API gateway.

Read more about API gateways in a cloud native world

What is a Kubernetes Ingress Controller?

An Ingress controller implements a Kubernetes Ingress and works as a a load balancing algorithm, balancer and reverse proxy entity. It abstracts traffic routing by directing traffic coming towards a Kubernetes platform to the pods inside and load balancing it. The controller's routing directions come from the Ingress resource configurations.

A Kubernetes cluster can have multiple associated Ingress controllers, and each one works similarly to a deployment controller. It is event-driven and gets triggered when, for example, a user creates, updates or deletes an Ingress. Once triggered, an Ingress controller tracks a call within the cluster's Ingress resources and reads the Ingress specifications. Then, it makes the configuration (in YAML or JSON) intelligible for the reverse proxy so it can provide the cluster with the resources it requires.

Learn more in our detailed guide to Kubernetes Ingress Controller

Types of Ingress

Single Service Ingress

A single service ingress exposes only one service to external users. To enable external access to a single service ingress, you must define a default backend-if the ingress object's host or path does not match information in the HTTP message, traffic is forwarded to this default backend. The default backend does not have any routing rules.

Image Source: Kubernetes

Simple Fanout Ingress

A simple fan-out ingress allows you to expose multiple services using a single IP address. This makes it easier to route traffic to destinations based on the type of request. This type of simple ingress controller simplifies traffic routing while reducing the total number of load balancers in the cluster.

Image Source: Kubernetes

Name-based Virtual Hosting

Name-based virtual hosting makes it possible to route HTTP traffic to multiple hostnames with the same IP address. This ingress type usually directs incoming traffic to a specific host before evaluating routing rules.

Image Source: Kubernetes

Ingress vs. ClusterIP vs. NodePort vs. LoadBalancer

There are several different ways to enable traffic to flow into a Kubernetes cluster:

  • Ingress-allows you to consolidate traffic routing rules into a single resource that runs natively within the Kubernetes cluster.
  • ClusterIP-this is the recommended option for accessing internal services using internal IP addresses. You can use it for debugging services, internal traffic, or for the Kubernetes dashboard during development stages.
  • NodePort-this is a virtual machine (VM) used to expose services using a static port number. It is not recommended for production environments, but can be used to expose services in development environments. It does not provide load balancing or multi-service routing capabilities.
  • LoadBalancer-this object exposes services to the Internet using an external LoadBalancer. It is acceptable for use in production Kubernetes clusters.

When should you use Kubernetes ingress?

The ingress resource is recommended when exposing services in production Kubernetes clusters. It is useful when you need to define complex traffic routing, and helps you reduce the cost of external traffic load balancers, leveraging the resources of your Kubernetes cluster nodes. Another motivation for using an ingress is when you want to manage traffic routing within Kubernetes and avoid having one more system to manage.

Kubernetes Ingress Examples

Basic Example: Default Ingress Resource

A default Kubernetes Ingress resource example is below:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-ingress-example
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: demo-nginx-example
rules:
- http:
paths:
- path: /demofilepath
pathType: Prefix
backend:
service:
name: demoservice
port:
number: 99

An Ingress requires the following fields:

  • apiVersion
  • kind
  • metadata
  • spec

The Ingress object's name has to be a valid DNS subdomain title. The specification has everything required for configuring load balancers and proxy servers. It also specifies rules to match incoming requests against. However, an Ingress resource can only have rules to direct HTTP(S) traffic.

An Ingress Controller needs to be deployed for using Kubernetes Ingress. Some different choices of Ingress Controller that Kubernetes supports are:

  • AKS Application Gateway Ingress Controller
  • Istio Ingress
  • Kusk Gateway
  • NGINX Ingress Controller For Kubernetes

Exposing Services as Externally Usable URLs via Ingress

Configuration inside an ingress Controller gets updated through Ingress Resources. Taking Nginx as an example, a set of pods are present inside the Nginx instance which monitors the Ingress Resources for any changes. Then, it reloads the configuration after updating it if there are changes.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-Ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
ingressClassName: nginx
rules:
- host: demo.web.com
http:
paths:
- path: /demo-Ingress
pathType: Prefix
backend:
service:
name: demo-Ingress
port: 1919

The specification tells the Nginx Ingress controller that it has to monitor and update by specifying ingress-nginx as the ingress class. In case there are multiple Controllers, the ingress.class specifies which Controller monitors the resources. The spec part contains the following information:

  • the rules
  • the hostname which the rules apply to
  • if the traffic is HTTP or HTTPs or neither
  • the path it is watching
  • the port the traffic is going to get sent to

The traffic received at demo.web.com gets directed to the demo-Ingress service on the 1919 port. Such a specification provides a path-based load balancing strategy and allows using a single DNS entry for a host(s).

Load Balancing Traffic with Kubernetes Ingress

The following specification allows load balancing through splitting up the traffic and routing it towards different backends and deployments in the application based on the path. It is favorable for endpoints that receive more traffic than others and a single deployment can be scaled for /demo-Ingress.

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: demo-Ingress-split
annotations:
nginx.ingress.kubernetes.io/rewrite-target:
spec:
ingressClassName: nginx
rules:
- host: demo.web.com
http:
paths:
- path: /demo-Ingress
pathType: Prefix
backend:
service:
name: demo-Ingress
port:
number: 1919
- path: /demo-Ingress-split
pathType: Prefix
backend:
service:
name: demo-Ingress-split
port:
number: 2020

Here, the specification is the same as before. However, the additional part features another deployment and service.