यह पेज दिखाता है कि किसी एप्लिकेशन Container के चलने से पहले Pod को initialize करने के लिए Init Container का उपयोग कैसे करें।
आपको कुबरनेट्स क्लस्टर की ज़रूरत पड़ेगी और क्यूब सीटीएल कमांड लाइन साधन को समनुरूप करना होगा ताकि वो आपके क्लस्टर के साथ संवाद कर सकें। हमारी सलाह है की इस टुटोरिअल को क्लस्टर में रन करने के लिए कम से कम दो नोड का इस्तेमाल करे जो कि कंट्रोल प्लेन होस्ट के तरह ना एक्ट करे। अगर आपके पास पहले से क्लस्टर नही है, आप minikube की मदद से वह बना सकते है या आप नीचे दिए हुए इन दो कुबरनेट्स प्लेग्राउंड का इस्तेमाल कर सकते हैं:
संस्करण की जांच करने के लिए, लिखें kubectl version.
इस अभ्यास में आप ऐसा Pod बनाते हैं जिसमें एक application Container और एक Init Container होता है। application container शुरू होने से पहले init container पूरा चलकर समाप्त हो जाता है।
यह Pod की configuration file है:
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: {}
configuration file में आप देख सकते हैं कि Pod में एक Volume है जिसे init container और application container share करते हैं।
init container shared Volume को /work-dir पर mount करता है, और application container
shared Volume को /usr/share/nginx/html पर mount करता है। init container नीचे दी गई कमांड चलाता है
और फिर समाप्त हो जाता है:
wget -O /work-dir/index.html http://info.cern.ch
ध्यान दें कि init container, nginx server की root directory में index.html फ़ाइल लिखता है।
Pod बनाएं:
kubectl apply -f https://k8s.io/examples/pods/init-containers.yaml
जांचें कि nginx container चल रहा है:
kubectl get pod init-demo
आउटपुट दिखाता है कि nginx container चल रहा है:
NAME READY STATUS RESTARTS AGE
init-demo 1/1 Running 0 1m
init-demo Pod में चल रहे nginx container के अंदर shell खोलें:
kubectl exec -it init-demo -- /bin/bash
अपनी shell में, nginx server पर GET request भेजें:
root@nginx:~# apt-get update
root@nginx:~# apt-get install curl
root@nginx:~# curl localhost
आउटपुट दिखाता है कि nginx वही web page serve कर रहा है जिसे init container ने लिखा था:
<html><head></head><body><header>
<title>http://info.cern.ch</title>
</header>
<h1>http://info.cern.ch - home of the first website</h1>
...
<li><a href="http://info.cern.ch/hypertext/WWW/TheProject.html">Browse the first website</a></li>
...