initコンテナ
- 環境ごとに変わるコマンドなどを実行させる
- ボリュームをマウントしてから出ないと実行できないコマンドなどを実行させる
- Pod起動後に必要なコマンドを実行させる
initコンテナのyamlファイル
- myserviceとmydbを2秒毎に待ち受けて、見つかれば処理終了
myapp.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<span class="na">apiVersion </span><span class="pi">:</span> <span class="s">V1の</span> <span class="na">種類</span><span class="pi">:</span> <span class="s">ポッドの</span> <span class="na">メタデータ</span><span class="pi">:</span> <span class="na">名前</span><span class="pi">:</span> <span class="s">myappの-ポッド</span> <span class="na">ラベル</span><span class="pi">:</span> <span class="na">アプリ</span><span class="pi">:</span> <span class="s">myappという</span> <span class="na">スペック</span><span class="pi">:</span> <span class="na">コンテナ</span><span class="pi">:</span> <span class="pi">- </span> <span class="na">名前</span><span class="pi">:</span> <span class="s">myappのコンテナの</span> <span class="na">画像</span><span class="pi">:</span> <span class="s">busyboxの:1.28 </span> <span class="na">コマンド</span><span class="pi">:</span> <span class="pi">[ </span><span class="s1">' </span><span class="s">SH' </span><span class="pi">、</span> <span class="s1">' </span><span class="s">-c' </span><span class="pi">、</span> <span class="s1">" </span><span class="s">エコー</span><span class="s">アプリが</span><span class="s">あります</span><span class="s">ランニング!</span><span class="s">&& </span><span class="s">sleep </span><span class="s">3600 ' </span><span class="pi">] </span><span class="na">initContainers </span><span class="pi">:</span><span class="pi">- </span><span class="na">名前</span><span class="pi">:</span> <span class="s">INIT-MYSERVICEの</span> <span class="na">画像</span><span class="pi">:</span> <span class="s">busyboxの:1.28 </span> <span class="na">コマンド</span><span class="pi">:</span> <span class="pi">[ </span><span class="s1">' </span><span class="s">SH' </span><span class="pi">、</span> <span class="s1">' </span><span class="s">-c' </span><span class="pi">、</span> <span class="s2">" </span><span class="s">まで</span><span class="s">nslookupを</span><span class="s">。MYSERVICE $(猫</span><span class="s">/var/run/secrets /kubernetes.io/serviceaccount/namespace).svc.cluster.local。 </span><span class="s">やる</span><span class="s">エコー</span><span class="s">待ち</span><span class="s">のため</span><span class="s">MYSERVICEを、</span><span class="s">睡眠</span><span class="s">2; </span><span class="s">行わ" </span><span class="pi">] </span><span class="pi">- </span><span class="na">名前</span><span class="pi">:</span><span class="s">INIT-mydbという</span><span class="na">イメージ</span><span class="pi">:</span><span class="s">busyboxの:1.28 </span><span class="na">コマンド</span><span class="pi">:</span><span class="pi">[ </span><span class="s1">' </span><span class="s">SH' </span><span class="pi">、</span><span class="s1">' </span><span class="s">-c' </span><span class="pi">、</span><span class="s2">" </span><span class="s">まで </span><span class="s">nslookupコマンド</span> <span class="s">mydb。$(cat </span><span class="s">/var/run/secrets/kubernetes.io/serviceaccount/namespace) .svc.cluster.local; </span><span class="s">やる</span><span class="s">エコー</span><span class="s">待ち</span><span class="s">のため</span><span class="s">MYDBを。</span><span class="s">睡眠</span><span class="s">2; </span><span class="s">「終了</span><span class="pi">]</span> |
起動
1 |
kubectl apply -f myapp.yaml |
確認
1 |
kubectl get -f myapp.yaml |
serviceもdbもいないのでまだREADYになっていない
1 2 |
名前READYステータス再起動年齢 myapp-pod 0/1 Init:0/2 0 3分15秒 |
serviceを作成する
service.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<span class="nn">--- </span> <span class="na">apiVersion </span><span class="pi">:</span> <span class="s">v1 </span> <span class="na">種類</span><span class="pi">:</span> <span class="s">サービス</span> <span class="na">メタデータ</span><span class="pi">:</span> <span class="na">名前</span><span class="pi">:</span> <span class="s">myservice </span> <span class="na">仕様</span><span class="pi">:</span> <span class="na">ポート</span><span class="pi">:</span> <span class="pi">- </span> <span class="na">プロトコル</span><span class="pi">:</span> <span class="s">TCP </span> <span class="na">ポート</span><span class="pi">:</span> <span class="m">80 </span> <span class="na">targetPort </span><span class="pi">:</span> <span class="m">9376 </span> <span class="nn">--- </span> <span class="na">apiVersion </span><span class="pi">:</span> <span class="s">v1 </span> <span class="na">種類</span><span class="pi">:</span> <span class="s">サービス</span> <span class="na">メタデータ</span><span class="pi">:</span> <span class="na">名前</span><span class="pi">:</span> <span class="s">mydb </span> <span class="na">仕様</span><span class="pi">:</span> <span class="na">ポート</span><span class="pi">:</span> <span class="pi">- </span> <span class="na">プロトコル</span><span class="pi">:</span> <span class="s">TCP </span> <span class="na">ポート</span><span class="pi">:</span> <span class="m">80 </span> <span class="na">targetPort </span><span class="pi">:</span> <span class="m">9377</span> |
起動
1 |
kubectl apply -f service.yaml |
確認
1 2 3 |
yuta:〜$ kubectl get -f myapp.yaml 名前READYステータス再起動年齢 myapp-pod 1/1ランニング0 6分15秒 |
ログ確認コマンド
1 |
kubectl describe -f myapp.yaml |
1 |
kubectlログmyapp-pod -c init-myservice |
参考
https://kubernetes.io/ja/docs/concepts/workloads/pods/init-containers/