Access other containers of a pod in Kubernetes

8.2k views Asked by At

When I define multiple containers in a pod/pod template like one container running nginx and another php-fpm, how can they access each other?

Do I have to define some links in the definition (I could not find docs explaining all available config options) or can they each other by default?

If yes what values do I have to put in the config files? I read the sharing a network namespace but I'm not aware of what that really means?

I also could not find any example for that.

2

There are 2 answers

0
larsks On BEST ANSWER

All the containers in a pod are bound to the same network namespace.

This means that (a) they all have the same ip address and (b) that localhost is the same across all the containers. In other words, if you have Apache running in one container in a pod and MysQL running in another, you can access MySQL at localhost:3306 from the Apache container (and you could access Apache at localhost:80 from the MySQL container).

While the containers share networking, they do not share filesystems. If you want to share files between containers you will need to make use of volumes. There is a simple volume example here.

0
shubham_asati On

Containers within a pod can talk to each other with localhost:port_no . .if nodejs server is accessing mongodb and both are running inside same pod then just write localhost:27017 in the connection string . An example of typical nodejs-mongodb connectivity inside a pod

 var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mydb";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  console.log("Database created!");
  db.close();
});