How to route test traffic through kubernetes cluster (minikube)?

1/4/2021

I have a minikube cluster with two pods (with ubuntu containers). What I need to do is route test traffic from one port to another through this minikube cluster. This traffic should be sent through these two pods like in the picture. I am a beginner in this Kubernetes stuff so I really don't know how to do this and which way to go... Please, help me or give me some hints.

I am working on ubuntu server ver. 18.04.

enter image description here

-- Skyeee
docker
kubernetes
kubernetes-cluster
kubernetes-pod
minikube

2 Answers

1/4/2021

There are multiple options you can follow:

As you have two PODs you can expose one via service,

  1. so service-1 is exposed and sending traffic to POD-1
  2. POD-1 will send a request to service-2 of Kubernetes
  3. This way traffic will get forwarded to POD-2 and from there it will Go out of cluster

There is also a container to container communication possibility if you can run both applications in a single POD.

POD-1 to POD-2 communication you can use the service option or POD URI.

-- Harsh Manvar
Source: StackOverflow

1/11/2021

I agree with an answer provided by @Harsh Manvar and I would also like to expand a little bit on this topic.

There already is an answer with a similar setup. I encourage you to check it out:

There are different drivers that could be used to run your minikube. They will have differences when it comes to dealing with inbound traffic. I missed the part that was telling about the driver used in the setup (comment). If it's the Docker shown in the tags, you could follow below example.


Example

Steps:

  • Spawn nginx-one and nginx-two Deployments to imitate Pods from the image
  • Create a service that will be used to send traffic from nginx-one to nginx-two
  • Create a service that will allow you to connect to nginx-one from LAN
  • Test the setup

Spawn nginx-one and nginx-two Deployments to imitate Pods from the image

You can use following definitions to spawn two Deployments where each one will have a single Pod:

  • nginx-one.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-one
spec:
  selector:
    matchLabels:
      app: nginx-one
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-one
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
  • nginx-two.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-two
spec:
  selector:
    matchLabels:
      app: nginx-two
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-two
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

Create a service that will be used to send traffic from nginx-one to nginx-two

You will need to use a Service to send the traffic from nginx-one to nginx-two. Example of such Service could be following:

apiVersion: v1
kind: Service
metadata:
  name: nginx-two-service
spec:
  type: ClusterIP # could be changed to NodePort
  selector:
    app: nginx-two # IMPORTANT
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80

After applying this definition you will be able to send the traffic to nginx-two by using the service name (nginx-two-service)

A side note!

You can use the IP of the Pod without the Service but this is not a recommended way.

Create a service that will allow you to connect to nginx-one from LAN

Assuming that you want to expose your minikube instance to LAN with Docker driver you will need to create a service and expose it. Example of such setup could be the following:

apiVersion: v1
kind: Service
metadata:
  name: nginx-one-service
spec:
  type: ClusterIP # could be changed to NodePort
  selector:
    app: nginx-one # IMPORTANT
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80

You will also need to run:

  • $ kubectl port-forward --address 0.0.0.0 service/nginx-one-service 8000:80

Above command (ran on your minikube host!) will expose your nginx-one-service to be available on LAN. It will map port 8000 on the machine that ran this command to the port 80 of this service. You can check it by executing from another machine at LAN:

  • curl IP_ADDRESS_OF_MINIKUBE_HOST:8000

A side note!

You will need root access to have your inbound traffic enter on ports lesser than 1024.

Test the setup

You will need to check if there is a communication between the objects as shown in below "connection diagram".

PC -> nginx-one -> nginx-two -> example.com

The testing methodology could be following:

PC -> nginx-one:

  • Run on a machine in your LAN:
    • curl MINIKUBE_IP_ADDRESS:8000

nginx-one -> nginx-two:

  • Exec into your nginx-one Pod and run command:
    • $ kubectl exec -it NGINX_POD_ONE_NAME -- /bin/bash
    • $ curl nginx-two-service

nginx-two -> example.com:

  • Exec into your nginx-two Pod and run command:
    • $ kubectl exec -it NGINX_POD_TWO_NAME -- /bin/bash
    • $ curl example.com

If you completed above steps you can swap nginx Pods for your own software.


Additional notes and resources:

I encourage you to check kubeadm as it's the tool to create your own Kubernetes clusters:

As you said:

I am a beginner in this Kubernetes stuff so I really don't know how to do this and which way to go... Please, help me or give me some hints.

You could check following links for more resources:

-- Dawid Kruk
Source: StackOverflow