1. 程式人生 > >kubernetes1.4新特性:支援Docker新特性_Kubernetes中文社群

kubernetes1.4新特性:支援Docker新特性_Kubernetes中文社群

(一)背景資料

  • 在Kubernetes1.2中這個第三方元件就是go-dockerclient,這是一個GO語言寫的docker客戶端,支援Dockerremote API,這個專案在https://github.com/fsouza/go-dockerclient中。
  • 在Kubernetes1.3中直接使用docker公司提供的client來實現,通過這個client可以實現同DockerDeamon之間的通訊,這個客戶端專案在https://github.com/docker/engine-api/中,感興趣的話可以去看看。
  • 在Kubernetes1.4中延用了1.3中的方式,直接使用docker公司提供的client來實現。

(二)支援Docker版本

  • 對於Kubernetes1.4需要使用Docker版本至少是1.9.x,Docker1.9.x對應的API版本是1.21。
  • 下面是Docker版本同API版本對應關係,其中紅色字型的部分是Kubernetes1.4不支援的。

Docker版本

API版本

1.12x

1.24

1.11.x

1.23

1.10.x

1.22

1.9.x

1.21

1.8.x

1.20

1.7.x

1.19

1.6.x

1.18

1.5.x

1.17

1.4.x

1.16

1.3.x

1.15

1.2.x

1.14

(三)呼叫Docker API

下面表格展現了Docker最新版本所有的API列表,同時也展現了Kubernetes1.4版本和1.3版本都使用了哪些API。

  • 第一列是Docker 1.24版本API列表
  • 第二列是這些API使用方式
  • 第三列是Kubernetes1.4中使用到的API
  • 第四列是Kubernetes1.3中使用到的API
  • 紅色字型部分為1.4版本比1.3版本增加的呼叫API,也就是說1.4版本比1.3版本增加的操作Docker的功能

Docker API 1.24

使用方式

Kubernetes1.4

Kubernetes1.3

Get Container stats based on resource usage

GET /containers/(id)/stats

Update a container

POST /containers/(id)/update

Rename a container

POST /containers/(id)/rename

Retrieving information about files and folders in a container

HEAD /containers/(id)/archive

List containers

GET /containers/json

Inspect a container

GET /containers/(id)/json

Inspect changes on a container’s filesystem

GET /containers/(id)/changes

Create a container

POST /containers/create

Start a container

POST /containers/(id)/start

Stop a container

POST /containers/(id)/stop

Restart a container

POST /containers/(id)/restart

Pause a container

POST /containers/(id)/pause

Unpause a container

POST /containers/(id)/unpause

List processes running inside a container

GET /containers/(id)/top

Kill a container

POST /containers/(id)/kill

Remove a container

DELETE /containers/(id)

Get an archive of a filesystem resource in a container

GET /containers/(id)/archive

Extract an archive of files or folders to a directory in a container

PUT /containers/(id)/archive

Copy files or folders from a container

POST /containers/(id)/copy,以後會被刪除掉,使用archive代替

Wait a container

POST /containers/(id)/wait

Create a new image from a container’s changes

POST /commit

Attach to a container

POST /containers/(id)/attach

Attach to a container (websocket)

GET /containers/(id or name)/attach/ws

Get container logs

GET /containers/(id)/logs

Resize a container TTY

POST /containers/(id)/resize

Export a container

GET /containers/(id)/export

List Images

GET /images/json

Inspect an image

GET /images/(name)/json

Get the history of an image

GET /images/(name)/history

Push an image on the registry

POST /images/(name)/push

Build image from a Dockerfile

POST /build

Create an image

POST /images/create

Load a tarball with a set of images and tags into docker

POST /images/load

Get a tarball containing all images in a repository

GET /images/(name)/get

Get a tarball containing all images

GET /images/get

Tag an image into a repository

POST /images/(name)/tag

Remove an image

DELETE /images/(name)

Search images

GET /images/search

Monitor Docker’s events

GET /events

Show the docker version information

GET /version

Display system-wide information

GET /info

Ping the docker server

GET /_ping

List volumes

GET /volumes

Create a volume

POST /volumes/create

Inspect a volume

GET /volumes/(name)

Remove a volume

DELETE /volumes/(name)

List networks

GET /networks

Inspect network

GET /networks/<network-id>

Create a network

POST /networks/create

Remove a network

DELETE /networks/(id)

Connect a container to a network

POST /networks/(id)/connect

Disconnect a container from a network

POST /networks/(id)/disconnect

Check auth configuration

POST /auth

Exec Create

POST /containers/(id)/exec

Exec Start

POST /exec/(id)/start

Exec Resize

POST /exec/(id)/resize

Exec Inspect

GET /exec/(id)/json

List plugins

GET /plugins

Install a plugin

POST /plugins/pull?name=<plugin name>

Inspect a plugin

GET /plugins/(plugin name)

Enable a plugin

POST /plugins/(plugin name)/enable

Disable a plugin

POST /plugins/(plugin name)/disable

Remove a plugin

DELETE /plugins/(plugin name)

List nodes

GET /nodes

Inspect a node

GET /nodes/<id>

Remove a node

DELETE /nodes/<id>

Update a node

POST /nodes/<id>/update

Inspect swarm

GET /swarm

Initialize a new swarm

POST /swarm/init

Join an existing swarm

POST /swarm/join

Leave a swarm

POST /swarm/leave

Update a swarm

POST /swarm/update

List services

GET /services

Create a service

POST /services/create

Remove a service

DELETE /services/(id or name)

Inspect one or more services

GET /services/(id or name)

Update a service

POST /services/(id or name)/update

List tasks

GET /tasks

Inspect a task

GET /tasks/(task id)

1)       從表格中可以看到,Kubernetes1.4中呼叫了Docker的Resize a container TTY介面,用來配置Docker容器的虛擬終端(TTY),重新設定Docker容器的虛擬終端之後,需要重新啟動容器才能生效。

HTTP請求例子:

POST/containers/4fa6e0f0c678/resize?h=40&w=80 HTTP/1.1

返回響應例子:

HTTP/1.1 200 OK  

Content-Length: 0  

Content-Type: text/plain; charset=utf-8

請求引數:

h – 虛擬終端高度

w – 虛擬終端寬度

HTTP返回響應狀態值:

200 – 設定成功

404 – 沒有找到指定Docker容器

500 – 不能夠重新設定虛擬終端引數

2)       從表格中還可以看到,Kubernetes1.4中呼叫了Docker的Exec Resize介面,如果在Docker容器中執行exec命令時指定了虛擬終端(tty),那麼通過這個API介面就可以重新設定虛擬終端(tty)。

HTTP請求例子:

POST/exec/e90e34656806/resize?h=40&w=80 HTTP/1.1  

Content-Type: text/plain

返回響應例子:

HTTP/1.1201 Created  

Content-Type: text/plain

請求引數:

h –虛擬終端高度

w –虛擬終端寬度

HTTP返回響應狀態值:

201 –設定成功

404 –沒有找到指定exec例項

3)       Kubernetes1.4新增加了上面兩個介面呼叫,可以看看這兩個介面呼叫在原始碼中的位置:

func AttachContainer(client DockerInterface,containerID kubecontainer.ContainerID, stdin io.Reader, stdout, stderrio.WriteCloser, tty bool, resize <-chan term.Size) error {   

<span style="color:#FF0000;"> kubecontainer.HandleResizing(resize, func(size term.Size) {  

 client.ResizeContainerTTY(containerID.ID,int(size.Height), int(size.Width))    

       })</span>  

       opts:= dockertypes.ContainerAttachOptions{  

              Stream:true,  

              Stdin:  stdin != nil,  

              Stdout:stdout != nil,  

              Stderr:stderr != nil,  

       }  

       ……  

}
func (*NativeExecHandler)ExecInContainer(client DockerInterface, container *dockertypes.ContainerJSON,cmd []string, stdin io.Reader, stdout, stderr io.WriteCloser, tty bool, resize<-chan term.Size) error {  

         ……  

        <span style="color:#FF0000;"> kubecontainer.HandleResizing(resize, func(size term.Size) {  

                client.ResizeExecTTY(execObj.ID, int(size.Height),int(size.Width))  

         })</span>  

         startOpts:= dockertypes.ExecStartCheck{Detach: false, Tty: tty}  

         streamOpts:= StreamOptions{  

                InputStream:  stdin,  

                OutputStream:stdout,  

                ErrorStream:  stderr,  

                RawTerminal:  tty,  

         }  

         err= client.StartExec(execObj.ID, startOpts, streamOpts)  

         iferr != nil {  

                returnerr  

         }  

  ……  

  }

這兩處開發開發人員的註釋如下:

Have to start this before the call toclient.AttachToContainer because client.AttachToContainer is a blocking call:-( Otherwise, resize events don’t get processed and the terminal neverresizes.

Have to start this before the call toclient.StartExec because client.StartExec is a blocking call