diff --git a/.vuepress/config.js b/.vuepress/config.js index f55c1fd..2640092 100644 --- a/.vuepress/config.js +++ b/.vuepress/config.js @@ -6,7 +6,7 @@ module.exports = { head: [ // ['meta', {name: 'keywords', content: 'Kubernetes教程,Kubernetes安装,K8S教程,K8S安装,Kubernetes管理界面'}], ['link', { rel: 'icon', href: '/favicon.png' }], - ['link', { rel: 'manifest', href: '/manifest.json' }], + // ['link', { rel: 'manifest', href: '/manifest.json' }], ['script', {}, ` var _hmt = _hmt || []; (function() { @@ -38,16 +38,16 @@ module.exports = { return dateFns.format(timestamp, 'YYYY-MM-DD HH:mm:ss') } }, - '@vuepress/pwa': { - serviceWorker: true, - // popupComponent: 'KbSWUpdatePopup', - updatePopup: { - '/': { - message: "Kuboard官网已更新", - buttonText: "点击刷新" - } - } - }, + // '@vuepress/pwa': { + // serviceWorker: true, + // // popupComponent: 'KbSWUpdatePopup', + // updatePopup: { + // '/': { + // message: "Kuboard官网已更新", + // buttonText: "点击刷新" + // } + // } + // }, '@vssue/vuepress-plugin-vssue': { // set `platform` rather than `api` platform: 'github', @@ -261,6 +261,14 @@ module.exports = { 'k8s-bg/architecture/controller', ] }, + { + title: '容器', + collapsable: true, + children: [ + 'k8s-intermediate/container/images', + 'k8s-intermediate/container/env', + ] + }, { title: '工作负载', collapsable: true, diff --git a/README.md b/README.md index d6f6caf..4805f84 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ title: Kuboard官网_Kubernetes教程_K8S安装_管理界面 description: Kuboard是一款免费的Kubernetes管理界面_同时该网站还提供Kubernetes安装文档_K8S_部署_入门_免费中文Kubernetes教程_以及在Kubernetes上部署SpringCloud的详细文档 meta: - name: keywords - content: Kubernetes教程,K8S教程,Kubernetes安装,K8S安装 + content: Kubernetes教程,K8S教程,Kubernetes安装,K8S安装,Kubernetes actionText: 在线体验 actionText2: 开始使用 → actionLink2: /overview/ @@ -62,6 +62,9 @@ Kubernetes教程的主要依据是:Kubernetes 官网文档,以及使用 Kubo * 节点 * 集群内通信 * 控制器 + * 容器 + * 容器镜像 + * 容器的环境变量 * 工作负载 * 容器组 - 概述 * 容器组 - 生命周期 diff --git a/learning/README.md b/learning/README.md index 2af500d..c804154 100644 --- a/learning/README.md +++ b/learning/README.md @@ -49,6 +49,9 @@ meta: * [节点](/learning/k8s-bg/architecture/nodes.html) * [集群内通信](/learning/k8s-bg/architecture/com.html) * [控制器](/learning/k8s-bg/architecture/controller.html) + * 容器 + * [容器镜像](/learning/k8s-intermediate/container/images.html) + * [容器的环境变量](/learning/k8s-intermediate/container/env.html) * 工作负载 * [容器组 - 概述](/learning/k8s-intermediate/workload/pod.html) * [容器组 - 生命周期](/learning/k8s-intermediate/workload/pod-lifecycle.html) diff --git a/learning/k8s-intermediate/container/env.md b/learning/k8s-intermediate/container/env.md new file mode 100644 index 0000000..1f196b4 --- /dev/null +++ b/learning/k8s-intermediate/container/env.md @@ -0,0 +1,34 @@ +--- +vssueId: 123 +layout: LearningLayout +description: Kubernetes教程_容器镜像在Kubernetes的Pod中使用容器镜像之前,您必须将其推送到一个镜像仓库(或者使用仓库中已经有的容器镜像)。在 Kubernetes 的 Pod 定义中定义容器时,必须指定容器所使用的镜像,容器中的image字段支持与docker命令一样的语法,包括私有镜像仓库和标签。 +meta: + - name: keywords + content: Kubernetes 教程,K8S 教程,容器环境变量 +--- + +# 容器的环境变量 + +Kubernetes为容器提供了一系列重要的资源: +* 由镜像、一个或多个数据卷合并组成的文件系统 +* 容器自身的信息 +* 集群中其他重要对象的信息 + +## 容器的信息 + +在容器中执行 `hostname` 命令或者在libc 中执行 [gethostname](http://man7.org/linux/man-pages/man2/gethostname.2.html) 函调用,获得的是容器所在 Pod 的名字。 + +Pod 的名字,以及 Pod 所在名称空间可以通过 [downward API](https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/) 注入到容器的环境变量里。 + +用户也可以为容器自定义环境变量,请参考 [使用ConfigMap配置您的应用程序](/learning/k8s-intermediate/config/config-map.html) + +## 集群的信息 + +在容器创建时,集群中所有的 Service 的连接信息将以环境变量的形式注入到容器中。例如,已创建了一个名为 `Foo` 的 Service,此时再创建任何容器时,该容器将包含如下环境变量: + +``` +FOO_SERVICE_HOST= +FOO_SERVICE_PORT= +``` + +详细信息请参考 [Service-服务发现]](/learning/k8s-intermediate/service/service-details.html#服务发现) diff --git a/learning/k8s-intermediate/container/images.assets/image-20191007194641973.png b/learning/k8s-intermediate/container/images.assets/image-20191007194641973.png new file mode 100644 index 0000000..1dfcc1b Binary files /dev/null and b/learning/k8s-intermediate/container/images.assets/image-20191007194641973.png differ diff --git a/learning/k8s-intermediate/container/images.md b/learning/k8s-intermediate/container/images.md new file mode 100644 index 0000000..234ee06 --- /dev/null +++ b/learning/k8s-intermediate/container/images.md @@ -0,0 +1,57 @@ +--- +vssueId: 122 +layout: LearningLayout +description: Kubernetes教程_容器镜像在Kubernetes的Pod中使用容器镜像之前,您必须将其推送到一个镜像仓库(或者使用仓库中已经有的容器镜像)。在 Kubernetes 的 Pod 定义中定义容器时,必须指定容器所使用的镜像,容器中的image字段支持与docker命令一样的语法,包括私有镜像仓库和标签。 +meta: + - name: keywords + content: Kubernetes教程,K8S教程,容器镜像 +--- + +# 容器镜像 + +在 Kubernetes 的 Pod 中使用容器镜像之前,您必须将其推送到一个镜像仓库(或者使用仓库中已经有的容器镜像)。在 Kubernetes 的 Pod 定义中定义容器时,必须指定容器所使用的镜像,容器中的 `image` 字段支持与 `docker` 命令一样的语法,包括私有镜像仓库和标签。 + +例如:`my-registry.example.com:5000/example/web-example:v1.0.1` 由如下几个部分组成: + +my-registry.example.com:5000/example/web-example:v1.0.1 + +* 蓝色部分:registry 地址 +* 绿色部分:registry 端口 +* 紫色部分:repository 名字 +* 红色部分:image 名字 +* 棕色部分:image 标签 + +如果您使用 `hub.dokcer.com` Registry 中的镜像,可以省略 registry 地址和 registry 端口。例如:`nginx:latest`,`eipwork/kuboard` + +更多内容请参考:[使用私有仓库中的docker镜像](/learning/k8s-intermediate/private-registry.html) + +## 更新镜像 + +Kubernetes中,默认的镜像抓取策略是 `IfNotPresent`,使用此策略,kubelet在发现本机有镜像的情况下,不会向镜像仓库抓取镜像。如果您期望每次启动 Pod 时,都强制从镜像仓库抓取镜像,可以尝试如下方式: +* 设置 container 中的 `imagePullPolicy` 为 `Always` +* 省略 `imagePullPolicy` 字段,并使用 `:latest` tag 的镜像 +* 省略 `imagePullPolicy` 字段和镜像的 tag +* 激活 [AlwaysPullImages](https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#alwayspullimages) 管理控制器 + +在 Kuboard 中,可以在界面中直接指定 container 的 imagePullPolicy,如下图所示: + +![Kubernetes教程_指定镜像抓取策略](./images.assets/image-20191007194641973.png) + +imagePullPolicy 字段和 image tag的可能取值将影响到 kubelet 如何抓取镜像: +* `imagePullPolicy: IfNotPresent` 仅在节点上没有该镜像时,从镜像仓库抓取 +* `imagePullPolicy: Always` 每次启动 Pod 时,从镜像仓库抓取 +* `imagePullPolicy` 未填写,镜像 tag 为 `:latest` 或者未填写,则同 `Always` 每次启动 Pod 时,从镜像仓库抓取 +* `imagePullPolicy` 未填写,镜像 tag 已填写但不是 `:latest`,则同 `IfNotPresent` 仅在节点上没有该镜像时,从镜像仓库抓取 +* `imagePullPolicy: Never`,Kubernetes 假设本地存在该镜像,并且不会尝试从镜像仓库抓取镜像 + + +::: tip 重要 +* 在生产环境部署时,您应该避免使用 `:latest` tag,如果这样做,您将无法追踪当前正在使用的镜像版本,也无法正确地执行回滚动作 +* 如果要 100% 确保所有的容器都使用了同样的镜像版本,可以尝试使用镜像的 [digest](https://docs.docker.com/engine/reference/commandline/pull/#pull-an-image-by-digest-immutable-identifier),例如 `sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2`。 Digest 唯一地标识了容器镜像的版本,并且永远不会改变。 +* 容器引擎的镜像缓存机制使得 `imagePullPolicy: Always` 仍然是高效的。在 Docker 中,如果镜像已经存在,抓取尝试非常快速,先检查镜像所有的 layer 是否有更新,如果该镜像在镜像仓库中所有 layer 的 digest 与本地所有 layer 的 digest 相同,则不会再下载镜像。 +::: + + +## 使用私有仓库中的docker镜像 + +请参考 [使用私有仓库中的docker镜像](/learning/k8s-intermediate/private-registry.html)