新聞中心
配置 Pod 初始化
本文介紹在應(yīng)用容器運行前,怎樣利用 Init 容器初始化 Pod。

專業(yè)從事網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計,高端網(wǎng)站制作設(shè)計,小程序制作,網(wǎng)站推廣的成都做網(wǎng)站的公司。優(yōu)秀技術(shù)團(tuán)隊竭力真誠服務(wù),采用H5建站+CSS3前端渲染技術(shù),成都響應(yīng)式網(wǎng)站建設(shè)公司,讓網(wǎng)站在手機、平板、PC、微信下都能呈現(xiàn)。建站過程建立專項小組,與您實時在線互動,隨時提供解決方案,暢聊想法和感受。
在開始之前
你必須擁有一個 Kubernetes 的集群,同時你的 Kubernetes 集群必須帶有 kubectl 命令行工具。 建議在至少有兩個節(jié)點的集群上運行本教程,且這些節(jié)點不作為控制平面主機。 如果你還沒有集群,你可以通過 Minikube 構(gòu)建一個你自己的集群,或者你可以使用下面任意一個 Kubernetes 工具構(gòu)建:
- Katacoda
- 玩轉(zhuǎn) Kubernetes
要檢查版本,請輸入 ?kubectl version?。
創(chuàng)建一個包含 Init 容器的 Pod
本例中你將創(chuàng)建一個包含一個應(yīng)用容器和一個 Init 容器的 Pod。Init 容器在應(yīng)用容器啟動前運行完成。
下面是 Pod 的配置文件:
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
# These containers are run during pod initialization
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: {}
配置文件中,你可以看到應(yīng)用容器和 Init 容器共享了一個卷。
Init 容器將共享卷掛載到了 ?/work-dir? 目錄,應(yīng)用容器將共享卷掛載到了 ?/usr/share/nginx/html? 目錄。 Init 容器執(zhí)行完下面的命令就終止:
wget -O /work-dir/index.html http://info.cern.ch
請注意 Init 容器在 nginx 服務(wù)器的根目錄寫入 ?index.html?。
創(chuàng)建 Pod:
kubectl create -f https://K8S.io/examples/pods/init-containers.yaml
檢查 nginx 容器運行正常:
kubectl get pod init-demo
結(jié)果表明 nginx 容器運行正常:
NAME READY STATUS RESTARTS AGE
init-demo 1/1 Running 0 1m通過 shell 進(jìn)入 init-demo Pod 中的 nginx 容器:
kubectl exec -it init-demo -- /bin/bash
在 shell 中,發(fā)送個 GET 請求到 nginx 服務(wù)器:
root@nginx:~# apt-get update
root@nginx:~# apt-get install curl
root@nginx:~# curl localhost結(jié)果表明 nginx 正在為 Init 容器編寫的 web 頁面服務(wù):
http://info.cern.ch
http://info.cern.ch - home of the first website
...
Browse the first website
... 分享標(biāo)題:創(chuàng)新互聯(lián)kubernetes教程:Kubernetes配置Pod初始化
當(dāng)前URL:http://m.5511xx.com/article/dpjpdgs.html


咨詢
建站咨詢
