容器網絡管理:libnetwork
Libnetwork 提供一個原生 Go 實現的容器連接,是容器的網絡。libnetwork 的目標是定義一個健壯的容器網絡模型(Container Network Model),提供一個一致的編程接口和應用程序的網絡抽象。
Libnetwork一開始的代碼只是 libcontainer 和 Docker Engine 中網絡部分代碼的合并,Docker 官方的愿景是希望 libnetwork 能像 libcontainer 一樣,成為一個多平臺的容器網絡基礎包。
受之前的一個 GitHub issue 啟發,libnetwork 引入了容器網絡模型(CNM)的概念,CNM 定義了三個新的術語,分別是網絡沙箱、Endpoint、Network。網絡沙箱指的是在每一個容器中,將會有一個隔離的用于網絡配置的環境。Endpoint 是一個網絡接口,可用于某一網絡上的交流。Network 是一個唯一的且可識別的 Endpoint組。
接下來,Docker 公司將會把 libnetwork 集成到 Docker Engine,并在 Docker CLI 中使用新的網絡命令。具體的項目路線圖讀者可以參考 GitHub。
注意:libnetwork 項目正在大力開發中,還不適合日常使用!
使用示例:
// 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
} 本文由用戶 ec3y 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!