How to Route External Traffic to Kubernetes Pods

In Kubernetes, traffic is routed to Pods. This post will help explain Kubernetes Route rules and show how they can be used to route external traffic to pods. Route rules are Kubernetes’ routing mechanism for forwarding packets from one interface on a node to another. Routes control the set of destination IP addresses that can be reached by sending or receiving packets on an interface.

Kubernetes sets up routes for every pod interface, and each route has a next-hop. Kubernetes determines the best route to use by consulting the routing table, which is populated with information from multiple sources.

LoadBalancer

Kubernetes LoadBalancer is a Kubernetes resource that provides load balancing for Kubernetes services. A Kubernetes LoadBalancer object represents a load balancer that you have created in Kubernetes. It can be used to load balance traffic to Kubernetes services or pods. A Kubernetes service can be associated with one or more Kubernetes LoadBalancer objects.

To route traffic from external sources to a Pod running in the cluster, we need to create a service with a LoadBalancer type.

The below manifest file creates a deployment and a service that routes traffic from an external source to an internal pod (deployment).

To read more about routing, visit this post.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
        - name: web-app
          image: nginx
      restartPolicy: Always  
---
      
apiVersion: v1
kind: Service 
metadata:
  name: web-app
spec:
  type: LoadBalancer
  selector:
    app: web-app
  ports:
    - port: 80


Posted

in

by