Ingress is a Kubernetes resource that is used to route external traffic. It routes this traffic to the services in our Kubernetes cluster depending on the rules defined by the user. An Ingress controller is responsible for implementing the Ingress resource.
There are various Ingress controllers available, including but not limited to the following:
Nginx Ingress controller
HAProxy Ingress controller
Traefik Ingress controller
# Create an Ingress apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: nginx-demo-ingress annotations: nginx.ingress.kubernetes.io/ssl-redirect: "false" spec: rules: - http: paths: - path: / pathType: Prefix backend: service: name: nginx-demo-service port: number: 8080 --- # Create a Service apiVersion: v1 kind: Service metadata: name: nginx-demo-service spec: selector: app: nginx ports: - port: 8080 targetPort: 80 --- # Create a Deployment apiVersion: apps/v1 kind: Deployment metadata: name: nginx-demo-deployment spec: selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - image: nginx name: nginx-demo-container
Click the "Run" button on the widget above to create a k3d
Kubernetes cluster consisting of a single node.
Note: The Kubernetes cluster creation will a couple minutes to complete.
Once done, it's time to create a Kubernetes Deployment, Service, and an Ingress object. The widget above contains a resources.yaml
manifest. This file contains the definitions of the Kubernetes Deployment, Service, and Ingress objects. A detailed explanation of this file is provided below:
Lines 3, 23, and 37: We define the Kubernetes API version that we’ll use to create the Deployment, Service, and Ingress objects, i.e., v1
, apps/v1
, and networking.k8s.io/v1
.
Lines 4, 24, and 38: We define the types of objects that will be created, i.e., Ingress, service, and a Deployment object.
Lines 6, 26, and 40: We give a name to the objects that will be created using their respective definitions, i.e., nginx-demo-ingress
, nginx-demo-service
and nginx-demo-deployment
.
Line 8: We specify the annotation for this Ingress object, i.e., nginx.ingress.kubernetes.io/ssl-redirect: "false"
. This annotation will prevent nginx from redirecting requests to https
.
Lines 19–29: We define the rules for this Ingress. This object contains a single path with the Prefix
type. This rule defines that every request we receive at /
path shall be redirected to the nginx-demo-service
service that is listening on the port 8080
. If we want to send a request received at another path, e.g., /educative
, we can do that by creating another path at line 20 with a different service name and port number.
Lines 29, 44, and 48: We define the labels for this Service and Deployment object, i.e., app: nginx
.
Lines 30–32: We define the port for this Service, i.e., 8080
, and the port of the nginx application, i.e., 80
.
Lines 50–52: We define the container that will be created in the Deployment object. This container will be created using the nginx
image available on DockerHub (the default container registry unless specified otherwise), and this container is named as nginx-demo-container
.
To create the Deployment, Service, and an Ingress object, execute the following command:
kubectl apply -f resources.yaml
We should get an output that these resources have been created successfully.
Let's get the status of our objects using the following command:
kubectl get deployment,service,ingress,pods# To run the Ingress on the platform, execute the following command.# If we're working locally, we don't need this commandkubectl port-forward svc/traefik -n kube-system 3000:80 --address 0.0.0.0
If the pods are in a running state, move ahead and run the following command:
curl localhost:3000
We should see the Nginx welcome page. We can also view this welcome page from the "Output" tab or by clicking the URL provided below the SPA widget.
Unlock your potential: Kubernetes Deployment and Advanced Operations series, all in one place!
To deepen your understanding of Kubernetes deployment and management, explore our series of Answers below:
How to create a Kubernetes cluster locally with Minikube
Learn how to set up a Kubernetes cluster on your local machine using Minikube for development and testing.
How to create deployment on a Kubernetes cluster via YAML files
Master the process of defining and managing deployments in Kubernetes using YAML configuration files.
What is an Ingress controller in Kubernetes?
Understand the role of an Ingress controller in managing external access and routing traffic within a Kubernetes cluster.
How to implement namespace quotas in Kubernetes
Discover how to apply resource limits to specific namespaces to optimize resource allocation and prevent overuse.
How to implement Kubernetes resource quotas
Learn how to enforce limits on CPU, memory, and storage consumption across your Kubernetes cluster.
Add users using certificates in a Kubernetes cluster
Explore the process of adding and authenticating users in Kubernetes by issuing and managing certificates.
What is service discovery in Kubernetes?
Understand how Kubernetes enables seamless service discovery, allowing applications to communicate efficiently.
How to put a database in Kubernetes
Learn the best practices for deploying and managing databases within a Kubernetes environment.
How to run WordPress on Kubernetes
Follow a step-by-step guide to deploying a scalable and resilient WordPress site on Kubernetes.
Free Resources