Networking in Kubernetes

Networking in Kubernetes

In this post, I will be discussing about the networking in Kubernetes. This is going to be a short post.

Pod internal

If multiple containers are running inside a pod, they can communicate with each other using localhost.

Cluster Internal

There are 2 ways:

  • Suppose, we have an app that we only want to be accessible from inside the cluster and not from the outside world.

For that, we create a separate deployment and a separate service. We give the service, a type = ClusterIP instead of LoadBalancer.

Now, to access that service from other apps in the cluster, we get an IP address of that service as an environment variable. The format for the name of the environment variable is going to be:

  • Suppose the name of the service is auth-service-rv.
  • The name of the environment variable will be AUTH_SERVICE_RV_SERVICE_HOST.

We can use that to make requests to the auth app from different apps that may or may not be public-facing.
eg.

const domain = process.env.AUTH_SERVICE_RV_SERVICE_HOST;
axios.post(`http://${domain}:${PORT}/login`)
  .then(() => {
    ...
  });
  • Second way, is to use this format for the domain <SERVICE-NAME>.<NAMESPACE> for eg. auth-service.default. default is the default namespace.

Public Access

For external communication, we just need to set the service type to LoadBalancer. This will make sure that the IP is accessible outside of the cluster.


This is it for basics of networking in Kubernetes. Feel free to share your thoughts in the comments section below.

Comments