libnetwork容器網(wǎng)絡(luò)管理
Libnetwork 提供一個(gè)原生 Go 實(shí)現(xiàn)的容器連接,是容器的網(wǎng)絡(luò)。libnetwork 的目標(biāo)是定義一個(gè)健壯的容器網(wǎng)絡(luò)模型(Container Network Model),提供一個(gè)一致的編程接口和應(yīng)用程序的網(wǎng)絡(luò)抽象。
Libnetwork一開始的代碼只是 libcontainer 和 Docker Engine 中網(wǎng)絡(luò)部分代碼的合并,Docker 官方的愿景是希望 libnetwork 能像 libcontainer 一樣,成為一個(gè)多平臺(tái)的容器網(wǎng)絡(luò)基礎(chǔ)包。
受之前的一個(gè) GitHub issue 啟 發(fā),libnetwork 引入了容器網(wǎng)絡(luò)模型(CNM)的概念,CNM 定義了三個(gè)新的術(shù)語,分別是網(wǎng)絡(luò)沙箱、Endpoint、Network。網(wǎng)絡(luò)沙箱 指的是在每一個(gè)容器中,將會(huì)有一個(gè)隔離的用于網(wǎng)絡(luò)配置的環(huán)境。Endpoint 是一個(gè)網(wǎng)絡(luò)接口,可用于某一網(wǎng)絡(luò)上的交流。Network 是一個(gè)唯一的且可識別的 Endpoint組。
接下來,Docker 公司將會(huì)把 libnetwork 集成到 Docker Engine,并在 Docker CLI 中使用新的網(wǎng)絡(luò)命令。具體的項(xiàng)目路線圖讀者可以參考 GitHub。
注意:libnetwork 項(xiàng)目正在大力開發(fā)中,還不適合日常使用!
使用示例:
// Create a new controller instance
controller := libnetwork.New()
// Select and configure the network driver
networkType := "bridge"
driverOptions := options.Generic{}
genericOption := make(map[string]interface{})
genericOption[options.GenericData] = driverOptions
err := controller.ConfigureNetworkDriver(networkType, genericOption)
if err != nil {
return
}
// Create a network for containers to join.
// NewNetwork accepts Variadic optional arguments that libnetwork and Drivers can make of
network, err := controller.NewNetwork(networkType, "network1")
if err != nil {
return
}
// For each new container: allocate IP and interfaces. The returned network
// settings will be used for container infos (inspect and such), as well as
// iptables rules for port publishing. This info is contained or accessible
// from the returned endpoint.
ep, err := network.CreateEndpoint("Endpoint1")
if err != nil {
return
}
// A container can join the endpoint by providing the container ID to the join
// api which returns the sandbox key which can be used to access the sandbox
// created for the container during join.
// Join acceps Variadic arguments which will be made use of by libnetwork and Drivers
_, err = ep.Join("container1",
libnetwork.JoinOptionHostname("test"),
libnetwork.JoinOptionDomainname("docker.io"))
if err != nil {
return
}