Docker Context基本原理

介绍

本指南介绍了上下文如何使单个Docker CLI轻松管理多个Swarm集群、多个Kubernetes集群和多个单独的Docker节点。

单个Docker CLI可以有多个上下文。每个上下文包含管理不同集群或节点所需的所有端点和安全信息。docker context命令使配置这些上下文和在它们之间切换变得很容易。

例如,您公司笔记本电脑上的单个Docker客户端可能配置有两个上下文:dev-k8s和prod swarm。dev-k8s包含用于在开发环境中配置和管理Kubernetes集群的端点数据和安全凭证。prod swarm包含在生产环境中管理集群所需的一切。一旦配置好这些上下文,就可以使用顶级docker context use<context name>在它们之间轻松切换。

有关使用Docker Context将应用部署到云端的信息,请参阅在Azure上部署Docker容器和在ECS上部署Docker容器。

Prerequisites

To follow the examples in this guide, you’ll need:

  • A Docker client that supports the top-level context command

Run docker context to verify that your Docker client supports contexts.

You will also need one of the following:

  • Docker Swarm cluster
  • Single-engine Docker node
  • Kubernetes cluster

The anatomy of a context

A context is a combination of several properties. These include:

  • Name
  • Endpoint configuration
  • TLS info
  • Orchestrator

The easiest way to see what a context looks like is to view the default context.

$ docker context ls

NAME          DESCRIPTION     DOCKER ENDPOINT                KUBERNETES ENDPOINT      ORCHESTRATOR

default *     Current...      unix:///var/run/docker.sock                             swarm

这显示了一个名为“default”的上下文。它被配置为通过local/var/run与集群通信/docker.sock公司Unix套接字。它没有配置Kubernetes端点。

NAME列中的星号表示这是活动上下文。这意味着所有docker命令都将针对“默认”上下文执行,除非使用环境变量(如docker_HOST和docker_context)重写,或者在命令行上使用--context和--HOST标志重写。

用docker context inspect再深入一点。在这个例子中,检查了名为default的上下文。

$ docker context inspect default

[

{

"Name": "default",

"Metadata": {

"StackOrchestrator": "swarm"

},

"Endpoints": {

"docker": {

"Host": "unix:///var/run/docker.sock",

"SkipTLSVerify": false

}

},

"TLSMaterial": {},

"Storage": {

"MetadataPath": "\u003cIN MEMORY\u003e",

"TLSPath": "\u003cIN MEMORY\u003e"

}

}

]

This context is using “swarm” as the orchestrator (metadata.stackOrchestrator). It is configured to talk to an endpoint exposed on a local Unix socket at /var/run/docker.sock (Endpoints.docker.Host), and requires TLS verification (Endpoints.docker.SkipTLSVerify).

Create a new context

You can create new contexts with the docker context create command.

The following example creates a new context called “docker-test” and specifies the following:

  • Default orchestrator = Swarm
  • Issue commands to the local Unix socket /var/run/docker.sock
$ docker context create docker-test \
  --default-stack-orchestrator=swarm \
  --docker host=unix:///var/run/docker.sock
 
Successfully created context "docker-test"

The new context is stored in a meta.json file below ~/.docker/contexts/. Each new context you create gets its own meta.json stored in a dedicated sub-directory of ~/.docker/contexts/.

Note: The default context behaves differently than manually created contexts. It does not have a meta.json configuration file, and it dynamically updates based on the current configuration. For example, if you switch your current Kubernetes config using kubectl config use-context, the default Docker context will dynamically update itself to the new Kubernetes endpoint.

You can view the new context with docker context ls and docker context inspect <context-name>.

The following can be used to create a config with Kubernetes as the default orchestrator using the existing kubeconfig stored in /home/ubuntu/.kube/config. For this to work, you will need a valid kubeconfig file in /home/ubuntu/.kube/config. If your kubeconfig has more than one context, the current context (kubectl config current-context) will be used.

$ docker context create k8s-test \
  --default-stack-orchestrator=kubernetes \
  --kubernetes config-file=/home/ubuntu/.kube/config \
  --docker host=unix:///var/run/docker.sock
 
Successfully created context "k8s-test"

You can view all contexts on the system with docker context ls.

$ docker context ls

NAME           DESCRIPTION   DOCKER ENDPOINT               KUBERNETES ENDPOINT               ORCHESTRATOR

default *      Current       unix:///var/run/docker.sock   https://35.226.99.100 (default)   swarm

k8s-test                     unix:///var/run/docker.sock   https://35.226.99.100 (default)   kubernetes

docker-test                  unix:///var/run/docker.sock                                     swarm

The current context is indicated with an asterisk (“*”).

Use a different context

You can use docker context use to quickly switch between contexts.

The following command will switch the docker CLI to use the “k8s-test” context.

$ docker context use k8s-test
 
k8s-test
Current context is now "k8s-test"

Verify the operation by listing all contexts and ensuring the asterisk (“*”) is against the “k8s-test” context.

$ docker context ls

NAME            DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT               ORCHESTRATOR

default         Current DOCKER_HOST based configuration   unix:///var/run/docker.sock   https://35.226.99.100 (default)   swarm

docker-test                                               unix:///var/run/docker.sock                                     swarm

k8s-test *                                                unix:///var/run/docker.sock   https://35.226.99.100 (default)   kubernetes

docker commands will now target endpoints defined in the “k8s-test” context.

You can also set the current context using the DOCKER_CONTEXT environment variable. This overrides the context set with docker context use.

Use the appropriate command below to set the context to docker-test using an environment variable.

Windows PowerShell:

> $Env:DOCKER_CONTEXT=docker-test

Linux:

$ export DOCKER_CONTEXT=docker-test

Run a docker context ls to verify that the “docker-test” context is now the active context.

You can also use the global --context flag to override the context specified by the DOCKER_CONTEXT environment variable. For example, the following will send the command to a context called “production”.

$ docker --context production container ls

Exporting and importing Docker contexts

The docker context command makes it easy to export and import contexts on different machines with the Docker client installed.

You can use the docker context export command to export an existing context to a file. This file can later be imported on another machine that has the docker client installed.

By default, contexts will be exported as a native Docker contexts. You can export and import these using the docker context command. If the context you are exporting includes a Kubernetes endpoint, the Kubernetes part of the context will be included in the export and import operations.

There is also an option to export just the Kubernetes part of a context. This will produce a native kubeconfig file that can be manually merged with an existing ~/.kube/config file on another host that has kubectl installed. You cannot export just the Kubernetes portion of a context and then import it with docker context import. The only way to import the exported Kubernetes config is to manually merge it into an existing kubeconfig file.

Let’s look at exporting and importing a native Docker context.

Exporting and importing a native Docker context

The following example exports an existing context called “docker-test”. It will be written to a file called docker-test.dockercontext.

$ docker context export docker-test
Written file "docker-test.dockercontext"

Check the contents of the export file.

$ cat docker-test.dockercontext
meta.json0000644000000000000000000000022300000000000011023 0ustar0000000000000000{"Name":"docker-test","Metadata":{"StackOrchestrator":"swarm"},"Endpoints":{"docker":{"Host":"unix:///var/run/docker.sock","SkipTLSVerify":false}}}tls0000700000000000000000000000000000000000000007716 5ustar0000000000000000

This file can be imported on another host using docker context import. The target host must have the Docker client installed.

$ docker context import docker-test docker-test.dockercontext
docker-test
Successfully imported context "docker-test"

You can verify that the context was imported with docker context ls.

The format of the import command is docker context import <context-name> <context-file>.

Now, let’s look at exporting just the Kubernetes parts of a context.

Exporting a Kubernetes context

You can export a Kubernetes context only if the context you are exporting has a Kubernetes endpoint configured. You cannot import a Kubernetes context using docker context import.

These steps will use the --kubeconfig flag to export only the Kubernetes elements of the existing k8s-test context to a file called “k8s-test.kubeconfig”. The cat command will then show that it’s exported as a valid kubeconfig file.

$ docker context export k8s-test --kubeconfig
Written file "k8s-test.kubeconfig"

Verify that the exported file contains a valid kubectl config.

$ cat k8s-test.kubeconfig
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data:
    <Snip>
    server: https://35.226.99.100
  name: cluster
contexts:
- context:
    cluster: cluster
    namespace: default
    user: authInfo
  name: context
current-context: context
kind: Config
preferences: {}
users:
- name: authInfo
  user:
    auth-provider:
      config:
        cmd-args: config config-helper --format=json
        cmd-path: /snap/google-cloud-sdk/77/bin/gcloud
        expiry-key: '{.credential.token_expiry}'
        token-key: '{.credential.access_token}'
      name: gcp

You can merge this with an existing ~/.kube/config file on another machine.

Updating a context

You can use docker context update to update fields in an existing context.

The following example updates the “Description” field in the existing k8s-test context.

$ docker context update k8s-test --description "Test Kubernetes cluster"
k8s-test
Successfully updated context "k8s-test"

Docker Context基本原理的更多相关文章

  1. Docker的基本原理及使用

    Docker 安装 https://docs.docker.com/engine/install/ubuntu/ 应用场景 Web 应用的自动化打包和发布. 自动化测试和持续集成.发布. 在服务型环境 ...

  2. Docker镜像构建的两种方式(六)--技术流ken

    镜像构建介绍 在什么情况下我们需要自己构建镜像那? (1)当我们找不到现有的镜像,比如自己开发的应用程序 (2)需要在镜像中加入特定的功能 docker构建镜像有两种方式:docker commit命 ...

  3. Docker原理探究

    问题思考:-------------------------------------Docker浅显原理理解-------------------------------------P1. ubunt ...

  4. Docker容器跨主机通信之:直接路由方式

    一.Docker网络基本原理 直观上看,要实现网络通信,机器需要至少一个网络接口(物理接口或虚拟接口)与外界相通,并可以收发数据包:此外,如果不同子网之间要进行通信,需要额外的路由机制. Docker ...

  5. Docker+Jenkins+Git发布SpringBoot应用

    Doccker Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之 ...

  6. Docker系列-(2) 镜像制作与发布

    上篇文章引入了Docker的基本原理和操作,本节文章主要介绍如何制作Docker镜像和发布. 镜像文件结构 Docker镜像的本质是一系列文件的集合,这些文件依次叠加,形成了最后的镜像文件,类似于下图 ...

  7. docker操作命令大全和后台参数

    一.命令行 可以通过运行 docker ,或者 docker help 命令得到命令行的帮助信息(我们以 CentOS 为操作环境为例): [root@iz2ze2bn5x2wqxdeq65wlpz ...

  8. docker 学习操作记录 5-2

    记录5-2 root@53d0a643a2c7:/# quit bash: quit: command not found root@53d0a643a2c7:/# exit exit -->@ ...

  9. docker简记

    title: docker学习简记 date: 2019-10-16 15:10:39 tags: docker Docker简记 1:Docker简介 1)出现背景 一款产品从开发到上线,从操作系统 ...

随机推荐

  1. 【缓存池】转-Mysql参数innodb_buffer_pool_size

    转自:https://blog.csdn.net/kk185800961/article/details/79378313/ 以下考虑主要为 Innodb 引擎 [ innodb_buffer_poo ...

  2. POJ3757 01分数规划

    题意:      有一个任务,给你提供n太服务器,让你在这n太服务器中选出k台完成这个任务,要求是每台服务器的工作时间相同,总的花费最小. 思路:      题目中给出对于每台服务器有这个式子: To ...

  3. 从苏宁电器到卡巴斯基第30篇:难忘的三年硕士时光 VIII

    自给自足 临近毕业答辩,别的导师的学生基本上都完成了各自的论文,也都开始交由第三方进行审核.而我们导师由于情况特殊,还没有机会看我们的论文,所以我们也打算和老师约一个时间,来给我们的论文提点意见,修改 ...

  4. 【opencv】Java实现的opencv3.x版本后Highhui报错

    随笔为博主原创,如需转载,请注明出处. opencv3.x以后Highgui不再使用,用Imgcodecs代替,引入import org.opencv.imgcodecs.Imgcodecs;即可. ...

  5. [论文阅读笔记] Fast Network Embedding Enhancement via High Order Proximity Approximati

    [论文阅读笔记] Fast Network Embedding Enhancement via High Order Proximity Approximation 本文结构 解决问题 主要贡献 主要 ...

  6. 【BUAA软工】技术规格说明书

    项目 内容 班级:北航2020春软件工程 博客园班级博客 作业:技术规格说明书 技术规格说明书 宏观技术 后端 WEB服务器 WEB服务器选取的是Springboot,作为当下Java语言最主流的WE ...

  7. slickgrid ( nsunleo-slickgrid ) 8 区域选择与复制粘贴

    区域选择 区域选择是通过插件CellRangeSelector实现的,默认不支持跨冻结列进行选择,修正了选择,支持跨冻结列,代码如下,通过判断选择的起点和终点所落在的冻结范围进行计算,如从左往右进行复 ...

  8. SprintBoot使用Validation

    1.为什么要使用Validation 在开发过程中有没有使用一堆的if来判断字段是否为空.电话号码是否正确.某个输入是否符合长度等对字段的判断.这样的代码可读性差,而且还不美观,那么使用Validat ...

  9. 24.Collection集合

    1.Collection集合 1.1数组和集合的区别[理解] 相同点 都是容器,可以存储多个数据 不同点 数组的长度是不可变的,集合的长度是可变的 数组可以存基本数据类型和引用数据类型 集合只能存引用 ...

  10. python文件对象几种操作模式区别——文件操作方法详解

    文件对象的字节模式/b模式(以utf-8编码为例) 读操作 写操作 指针操作 ASCII字节 返回bytes/字节类型的Ascii 写入bytes类型字节 例如:b'This is ascii' 使用 ...