简体   繁体   English

访问Kubernetes的其他容器容器

[英]Access other containers of a pod in Kubernetes

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? 当我在pod / pod模板中定义多个容器,比如一个运行nginx和另一个php-fpm的容器时,它们如何相互访问?

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. 我也找不到任何例子。

All the containers in a pod are bound to the same network namespace. pod中的所有容器都绑定到同一网络命名空间。

This means that (a) they all have the same ip address and (b) that localhost is the same across all the containers. 这意味着(a)它们都具有相同的IP地址,(b)所有容器中的localhost都相同。 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). 换句话说,如果您在一个容器中运行Apache并在另一个容器中运行MysQL,您可以从Apache容器访问localhost:3306 MySQL(并且您可以从localhost:80访问Apache localhost:80来自MySQL容器)。

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 . 有一个简单的例子体积这里

Containers within a pod can talk to each other with localhost:port_no . pod中的容器可以使用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 . .if nodejs服务器正在访问mongodb,两者都在同一个pod中运行,然后在连接字符串中写入localhost:27017。 An example of typical nodejs-mongodb connectivity inside a pod pod中典型的nodejs-mongodb连接示例

 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();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM