Learn how to configure Ingress resources in Kubernetes to route traffic to applications deployed across multiple namespaces, enabling flexible and secure access to your services.
In Kubernetes, Ingress Controllers play a crucial role in routing external traffic to services within your cluster. While they can reside in any namespace, a common practice is to place them in a dedicated namespace for better organization. This article will demonstrate how to route traffic to services residing in different namespaces using a single Ingress resource.
While an Ingress Controller can reside in any namespace, it's typically placed in a separate one from your applications, like ingress
or kube-system
. This separation enhances organization.
Let's say you want to route traffic to services in different namespaces using a single Ingress resource. You can achieve this by referencing the services directly in your Ingress definition.
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /service1
pathType: Prefix
backend:
service:
name: service1
namespace: namespace1
port:
number: 80
- path: /service2
pathType: Prefix
backend:
service:
name: service2
namespace: namespace2
port:
number: 80
In this example, requests to example.com/service1
are routed to service1
in namespace1
, while requests to example.com/service2
go to service2
in namespace2
.
This approach allows you to manage traffic for services in different namespaces from a central Ingress resource.
This code deploys two simple web servers, one running Apache (httpd) and the other Nginx, each in a separate Kubernetes namespace. It then creates an Ingress resource that routes traffic to the appropriate service based on the URL path. Accessing http://example.com/service1
will reach the Apache server in namespace1
, while http://example.com/service2
will reach the Nginx server in namespace2
. This demonstrates how Ingress can manage traffic for services across different namespaces.
This code example demonstrates the article's concept by deploying two simple services, each in a different namespace, and then routing traffic to them using a single Ingress resource.
Prerequisites:
Steps:
Create the Namespaces:
kubectl create namespace namespace1
kubectl create namespace namespace2
Deploy the Services:
namespace1/service1.yaml:
apiVersion: v1
kind: Service
metadata:
name: service1
namespace: namespace1
spec:
selector:
app: service1
ports:
- protocol: TCP
port: 80
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: service1
namespace: namespace1
spec:
replicas: 1
selector:
matchLabels:
app: service1
template:
metadata:
labels:
app: service1
spec:
containers:
- name: service1
image: httpd
ports:
- containerPort: 8080
namespace2/service2.yaml:
apiVersion: v1
kind: Service
metadata:
name: service2
namespace: namespace2
spec:
selector:
app: service2
ports:
- protocol: TCP
port: 80
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: service2
namespace: namespace2
spec:
replicas: 1
selector:
matchLabels:
app: service2
template:
metadata:
labels:
app: service2
spec:
containers:
- name: service2
image: nginx
ports:
- containerPort: 8080
Deploy both services:
kubectl apply -f namespace1/service1.yaml
kubectl apply -f namespace2/service2.yaml
Create the Ingress Resource:
ingress.yaml:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /service1
pathType: Prefix
backend:
service:
name: service1
namespace: namespace1
port:
number: 80
- path: /service2
pathType: Prefix
backend:
service:
name: service2
namespace: namespace2
port:
number: 80
Deploy the Ingress:
kubectl apply -f ingress.yaml
Access the Services:
Note: You might need to configure your DNS or use /etc/hosts
to map example.com
to your Ingress controller's IP address.
http://example.com/service1
should route you to service1
in namespace1
.http://example.com/service2
should route you to service2
in namespace2
.This example demonstrates how a single Ingress resource can manage traffic for services residing in different namespaces, providing a centralized approach to traffic management in your Kubernetes cluster.
Benefits of Namespace Separation for Ingress Controllers:
Important Considerations:
example.com
in the example) to the IP address of your Ingress Controller.Alternative Approaches:
Troubleshooting:
selector
labels in your Service definitions match the labels
in your Deployment Pod templates.kubectl describe ingress
, kubectl describe service
, and kubectl describe deployment
to get detailed information about your resources and their current status.This article explains how Kubernetes Ingress resources can route traffic to services residing in different namespaces.
Key takeaways:
ingress
or kube-system
for better organization.example.com/service1
to service1
in namespace1
and example.com/service2
to service2
in namespace2
.By leveraging Ingress resources and understanding cross-namespace communication, you can effectively manage external access to your services, ensuring a robust and scalable architecture for your applications. Remember to consider security best practices, such as TLS/SSL termination, and explore alternative approaches like service meshes for more complex traffic management needs. Properly configuring Ingress is fundamental to building well-structured and manageable Kubernetes deployments.