Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Mastering Kubernetes Ingress: A Step-by-Step Guide#
Kubernetes Ingress is a powerful resource that manages external access to services within a cluster. In this guide, we’ll walk through the process of deploying a simple application, creating a service for it, and exposing it to the external world using Ingress. Let’s dive in!
Prerequisites#
Before we begin, ensure you have:
- A running Kubernetes cluster
kubectlconfigured to communicate with your cluster- Docker installed on your local machine
- An Ingress Controller installed in your cluster (e.g., NGINX Ingress Controller)
Step 1: Building and Pushing the Docker Image#
First, let’s build our application image:
1docker build -t hello-world .Tag and push the image to Docker Hub:
1docker tag hello-world <dockerhub-username>/hello-world2docker push <dockerhub-username>/hello-worldReplace <dockerhub-username> with your Docker Hub username.
Step 2: Creating the Deployment#
Create a file named deployment.yaml:
1apiVersion: apps/v12kind: Deployment3metadata:4 name: web5spec:6 replicas: 17 selector:8 matchLabels:9 app: web10 template:11 metadata:12 labels:13 app: web14 spec:15 containers:16 - name: web17 image: <dockerhub-username>/hello-world:latest18 ports:19 - containerPort: 3000Apply the deployment:
1kubectl apply -f deployment.yamlStep 3: Creating the Service#
Create a file named service.yaml:
1apiVersion: v12kind: Service3metadata:4 name: web5spec:6 selector:7 app: web8 ports:9 - protocol: TCP10 port: 8011 targetPort: 3000Apply the service:
1kubectl apply -f service.yamlStep 4: Creating the Ingress#
Create a file named ingress.yaml:
1apiVersion: networking.k8s.io/v12kind: Ingress3metadata:4 name: hello-world-ingress5 annotations:6 nginx.ingress.kubernetes.io/rewrite-target: /$17spec:8 rules:9 - host: hello-world.exposed10 http:11 paths:12 - path: /13 pathType: Prefix14 backend:15 service:16 name: web17 port:18 number: 80Apply the Ingress:
1kubectl apply -f ingress.yamlStep 5: Verifying the Setup#
-
List the Ingress object:
Terminal window 1kubectl get ingress -
Add an entry to your
/etc/hostsfile:1<ingress-controller-ip> hello-world.exposedReplace
<ingress-controller-ip>with the external IP of your Ingress controller. -
Test the application:
Terminal window 1curl http://hello-world.exposedYou should see the “Hello World” message.
Key Takeaways#
-
Ingress Controller: Ensure an Ingress Controller is running in your cluster. It’s crucial for Ingress rules to take effect.
-
Host-based Routing: Ingress allows you to route traffic based on hostnames, enabling multiple services to share a single IP address.
-
Path-based Routing: You can route to different services based on URL paths, allowing for flexible application architectures.
-
Annotations: Ingress annotations provide a way to customize the behavior of your Ingress controller.
-
SSL/TLS: While not covered in this basic example, Ingress can be configured to manage SSL/TLS termination for secure communication.
Conclusion#
Kubernetes Ingress provides a powerful way to expose your services to the outside world. By following this guide, you’ve learned how to deploy an application, create a service, and expose it using Ingress. As you continue your Kubernetes journey, explore more advanced Ingress features like SSL/TLS termination, authentication, and rate limiting to build robust, production-ready applications.
Remember, Ingress is just one piece of the Kubernetes networking puzzle. Combine it with other resources like Services, NetworkPolicies, and external load balancers to create comprehensive networking solutions for your applications.