11 Kasım 2022 Cuma

Init Containers

Giriş
Şeklen şöyle
Açıklaması şöyle. Pod içinde birden fazla Init Container olabilir. Hepsi sırayla çalışırlar. 
Pods can have one or more Init containers which run before the application containers are started.

- Init containers contain utilities or setup scripts not present in application image. Init containers always runs to completion
- Each Init container must complete successfully before next one starts
- Because Init containers runs to completion before any application container starts, Init containers offers the mechanism to block or delay the application container start-up until a set of preconditions are met. - Once the preconditions are met, all the application containers can start in parallel.
Kullanım Örnekler
Bazı örnekler şöyle
Here are some scenarios for how to use Init containers,

- Clone a Git repository into a Volume
- Wait for sometime before starting the app container with a command like sleep 60
- Place values into a configuration file and run a template tool to dynamically generate a configuration file for the main app container. For example, place the POD_IP value in a configuration and generate the main app configuration file
1. spec bölümü altında initContainers tanımlanır
2. Her init container'a bir isim, bir image ve çalıştıracağı komut tanımlanır. Image olarak
- alpine/git
-  busybox:1.28
vs kullanılabilir

Örnek
Şöyle yaparız
apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
  labels:
    app.kubernetes.io/name: MyApp
spec:
  containers:
    - name : myapp-container
      image: busybox:1.28
      command: ['sh', '-c', 'echo Running! && sleep 3600']
  initContainers:
    - name : init-myservice
      image: busybox:1.28
      command: ['sh', '-c', 'until nslookup myservice.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting; sleep 2; done']
    - name : init-mydb
      image: busybox:1.28
      command: ['sh', '-c', 'until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting; sleep 2; done']
Açıklaması şöyle
This example defines a simple Pod that has two init containers. The first waits for myservice, and the second waits for mydb. Once both init containers complete, the Pod runs the app container from its spec section.
Örnek
Şöyle yaparız. Burada git clone komutu çalıştırılıyor ve pod üzerindeki bir volume'a indiriliyor.
#Pod with Init-container using emptyDir volume
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: webserver
  name: webserver
spec:
  containers:
  - image: nginx
    name: nginx-webserver
    volumeMounts:
    - name: web-data
      mountPath: /usr/share/nginx/html
  initContainers:
  - image: alpine/git
    name: git
    command:
    - git
    - clone
    - https://github.com/shamimice03/demo-init-container.git
    - /temp-repo
    volumeMounts:
    - name: web-data
      mountPath: /temp-repo
  volumes:
  - name: web-data
    emptyDir: {}
Şöyle yaparız. Burada git clone komutu çalıştırılıyor ve host üzerindeki bir dizine indiriliyor.
#Pod with Init-container using hostPath volume
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: webserver
  name: webserver
spec:
  containers:
  - image: nginx
    name: nginx-webserver
    volumeMounts:
    - name: web-data
      mountPath: /usr/share/nginx/html
  initContainers:
  - image: alpine/git
    name: git
    command:
    - git
    - clone
    - https://github.com/shamimice03/demo-init-container.git
    - /temp-repo
    volumeMounts:
    - name: web-data
      mountPath: /temp-repo
  volumes:
  - name: web-data
    hostPath:
      path: /root/web
      type: DirectoryOrCreate

Hiç yorum yok:

Yorum Gönder

Cluster Propotional Autoscaler - ReplicaSet Ekler/Siler

Giriş Açıklaması şöyle CPA aims to horizontally scale the number of Pod replicas based on the cluster’s scale. A common example is DNS ser...