配置 Pod 初始化

您所在的位置:网站首页 jsd节点工具 配置 Pod 初始化

配置 Pod 初始化

2023-07-26 09:42| 来源: 网络整理| 查看: 265

Information in this document may be out of date

This document has an older update date than the original, so the information it contains may be out of date. If you're able to read English, see the English version for the most up-to-date information: Configure Pod Initialization

配置 Pod 初始化

本文介绍在应用容器运行前,怎样利用 Init 容器初始化 Pod。

准备开始

你必须拥有一个 Kubernetes 的集群,同时你必须配置 kubectl 命令行工具与你的集群通信。 建议在至少有两个不作为控制平面主机的节点的集群上运行本教程。 如果你还没有集群,你可以通过 Minikube 构建一个你自己的集群,或者你可以使用下面的 Kubernetes 练习环境之一:

Killercoda玩转 Kubernetes要获知版本信息,请输入 kubectl version.创建一个包含 Init 容器的 Pod

本例中你将创建一个包含一个应用容器和一个 Init 容器的 Pod。Init 容器在应用容器启动前运行完成。

下面是 Pod 的配置文件:

pods/init-containers.yaml apiVersion: v1 kind: Pod metadata: name: init-demo spec: containers: - name: nginx image: nginx ports: - containerPort: 80 volumeMounts: - name: workdir mountPath: /usr/share/nginx/html # 这些容器在 Pod 初始化期间运行 initContainers: - name: install image: busybox:1.28 command: - wget - "-O" - "/work-dir/index.html" - http://info.cern.ch volumeMounts: - name: workdir mountPath: "/work-dir" dnsPolicy: Default volumes: - name: workdir emptyDir: {}

配置文件中,你可以看到应用容器和 Init 容器共享了一个卷。

Init 容器将共享卷挂载到了 /work-dir 目录,应用容器将共享卷挂载到了 /usr/share/nginx/html 目录。 Init 容器执行完下面的命令就终止:

wget -O /work-dir/index.html http://info.cern.ch

请注意 Init 容器在 nginx 服务器的根目录写入 index.html。

创建 Pod:

kubectl apply -f https://k8s.io/examples/pods/init-containers.yaml

检查 nginx 容器运行正常:

kubectl get pod init-demo

结果表明 nginx 容器运行正常:

NAME READY STATUS RESTARTS AGE init-demo 1/1 Running 0 1m

通过 shell 进入 init-demo Pod 中的 nginx 容器:

kubectl exec -it init-demo -- /bin/bash

在 shell 中,发送个 GET 请求到 nginx 服务器:

root@nginx:~# apt-get update root@nginx:~# apt-get install curl root@nginx:~# curl localhost

结果表明 nginx 正在为 Init 容器编写的 web 页面服务:

http://info.cern.ch http://info.cern.ch - home of the first website ... Browse the first website ... 接下来进一步了解同一 Pod 中的容器间的通信。进一步了解 Init 容器。进一步了解卷。进一步了解 Init 容器排错。反馈

此页是否对你有帮助?

感谢反馈。如果你有一个关于如何使用 Kubernetes 的具体问题需要答案,可以访问 Stack Overflow. 在 GitHub 仓库上登记新的问题 报告问题 或者 提出改进建议.

最后修改 March 17, 2023 at 4:42 PM PST: [zh-cn] sync task files of task-6 (e79e5c51be)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3