Skip to content

Forwarding Requests Directly to an External IP Address with Ingress Nginx Controller

Kubernetes is a powerful orchestration tool that allows you to manage containerized applications efficiently. One common requirement is to route traffic from an Ingress controller to an external IP address directly. This can be particularly useful when you need to integrate an external service with your Kubernetes cluster. In this blog post, we’ll walk through the steps to achieve this using the Ingress Nginx controller.

Step 1: Define the Service

First, we need to create a Service of type ClusterIP. This Service will not use selectors but will rely on the Endpoints object for the actual IP addresses.

Create a file named service.yaml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: backend-app
  namespace: your-namespace
spec:
  ports:
  - port: 3000
    protocol: TCP
    targetPort: 3000

This defines a Service named backend-app in the specified namespace, exposing port 3000.

Step 2: Define the Endpoints

Next, we need to create an Endpoints object to specify the actual IP address of your backend service.

Create a file named endpoints.yaml with the following content:

apiVersion: v1
kind: Endpoints
metadata:
  name: backend-app
  namespace: your-namespace
subsets:
- addresses:
  - ip: 192.168.0.1
  ports:
  - port: 3000
    protocol: TCP

This Endpoints object specifies that the backend service is located at IP address 192.168.0.1 on port 3000.

Step 3: Define the Ingress Resource

Now, we need to update our Ingress resource to reference the backend-app service. This will route incoming traffic to the external IP address specified in the Endpoints object.

Create a file named ingress.yaml with the following content:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  namespace: your-namespace
spec:
  rules:
  - host: your.domain.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: backend-app
            port:
              number: 3000

This Ingress resource routes traffic from your.domain.com to the backend-app service on port 3000.

Step 4: Apply the Configurations

Finally, apply these YAML configurations to your Kubernetes cluster using the kubectl command-line tool.

kubectl apply -f service.yaml
kubectl apply -f endpoints.yaml
kubectl apply -f ingress.yaml

These commands will create the Service, Endpoints, and Ingress resources in your Kubernetes cluster.

By following these steps, you can forward requests directly to an external IP address using the Ingress Nginx controller in Kubernetes. This setup allows you to seamlessly integrate external services with your Kubernetes-managed applications, enhancing your architecture’s flexibility and scalability.

Make sure to replace placeholders like your-namespace and your.domain.com with actual values relevant to your environment. With this configuration, you can leverage the power of Kubernetes while maintaining access to essential external services.

Published inKubernetesLinuxNginx