Docker Context基本原理

介绍

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

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

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

有关使用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 .

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-testCurrent 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-testWritten file “docker-test.dockercontext”

Check the contents of the export file.

$ cat docker-test.dockercontextmeta.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.dockercontextdocker-testSuccessfully 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 .

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 --kubeconfigWritten file “k8s-test.kubeconfig”

Verify that the exported file contains a valid kubectl config.

$ cat k8s-test.kubeconfigapiVersion: v1clusters:- cluster: certificate-authority-data: server: https://35.226.99.100 name: clustercontexts:- context: cluster: cluster namespace: default user: authInfo name: contextcurrent-context: contextkind: Configpreferences: {}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-testSuccessfully updated context “k8s-test”

Docker Context基本原理相关推荐

  1. 详解Docker的基本原理与实践操作

    详解Docker的基本原理与实践操作,Web时代,应用变得越来越强大,与此同时也越来越复杂.集群部署.隔离环境.灰度发布以及动态扩容缺一不可,而容器化则成为中间的必要桥梁. 本文我们就来探索Docke ...

  2. Docker 的基本原理及快速入门

    什么是docker Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来加入了 Linu ...

  3. Docker 入门终极指南:边学边用

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者:jartto 出处:http://jartto.w ...

  4. Docker 极简入门教程,傻瓜都能看懂!

    富 Web 时代,应用变得越来越强大,与此同时也越来越复杂.集群部署.隔离环境.灰度发布以及动态扩容缺一不可,而容器化则成为中间的必要桥梁. 本文我们就来探索一下 Docker 的神秘世界,从零到一掌 ...

  5. 昨天有读者说他不会 docker,今天就给你肝出来了

    富 Web 时代,应用变得越来越强大,与此同时也越来越复杂.集群部署.隔离环境.灰度发布以及动态扩容缺一不可,而容器化则成为中间的必要桥梁. 本文我们就来探索一下 Docker 的神秘世界,从零到一掌 ...

  6. 终于有人把Docker讲清楚了

    终于有人把Docker讲清楚了 51CTO 2020-07-29 13:59:29 富 Web 时代,应用变得越来越强大,与此同时也越来越复杂.集群部署.隔离环境.灰度发布以及动态扩容缺一不可,而容器 ...

  7. CoreOS容器云企业实战(3)--Docker技术实践

    0x1 Docker概述 1)Docker介绍 Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一 ...

  8. Docker入门讲解

    作者:jartto 链接:http://39sd.cn/D8EED 富 Web 时代,应用变得越来越强大,与此同时也越来越复杂.集群部署.隔离环境.灰度发布以及动态扩容缺一不可,而容器化则成为中间的必 ...

  9. 写给前端的 Docker 入门终极指南,别再说不会用 Docker 了!

    点击上方 前端Q,关注公众号 回复加群,加入前端Q技术交流群 富 Web 时代,应用变得越来越强大,与此同时也越来越复杂.集群部署.隔离环境.灰度发布以及动态扩容缺一不可,而容器化则成为中间的必要桥梁 ...

最新文章

  1. 高可用高性能负载均衡软件HAproxy详解指南-第三章:HAproxy实例
  2. [转帖]ASP.NET中常用的优化性能的方法
  3. 数据如何写入到HBase
  4. 【深度学习】绝了!分割mask生成动漫人脸!爆肝数周,从零搭建
  5. 【机器学习】KNN算法代码练习
  6. 服务治理之Eureka--基本介绍
  7. 用手写一个工具的过程讲清楚Go反射的使用方法和应用场景
  8. vsftp账号_Linux下vsftp的重新安装和配置虚拟用户
  9. 七、VueJs 填坑日记之渲染一个列表
  10. Composite_组合模式_PHP语言描述
  11. 单片机课程设计——交通灯
  12. 【计算理论】计算理论总结 ( 上下文无关文法 | 乔姆斯基范式 | 乔姆斯基范式转化步骤 | 示例 ) ★★
  13. dvi接口引脚定义_为什么越来越多人用RS232接口,却还分不清DB9、DB25的引脚定义?...
  14. 《工作五年,决定你一生的财富》读后感
  15. Markdown编辑器简单大概语法学习
  16. 大型网站架构系列:电商网站架构案例
  17. python操作jira修改status及写入comment
  18. 计算机图形学(三种画线算法)
  19. 计算机视觉知识学习总结
  20. 使用海康摄像头实现实时监控

热门文章

  1. 2022-2028年中国在线旅行预订市场投资分析及前景预测报告
  2. 2022-2028年中国电力行业节能减排投资分析及前景预测报告
  3. 【Sql Server】DataBase-事务与系统元数据函数
  4. RPC远程调用通俗理解
  5. pandas数据框,统计某列或者某行数据元素的个数
  6. 用0到9十个数字,每个数字使用一次,构成两个五位数a和b,并且a+20295=b.求a,b
  7. Imagination 的神经网络加速器和 Visidon 的去噪算法被证明是完美的搭档
  8. 车联网,挖掘数据价值
  9. 生成性对抗网络技术实现
  10. 2020年Yann Lecun深度学习笔记(下)