LoginSignup
5
2

More than 5 years have passed since last update.

kubernetes1.7でkubectl applyでinit containerを再作成する

Posted at

はじめに

kubernetesにはinit containerという機能があります。init containerはpodの起動前に実行されるコンテナを定義することができ、例えばpodの作成前にassetsなどの静的ファイルを共有ボリュームに格納させて、nginxで配信するみたいな事ができます。このinit containerですが、kubernetesの1.8以前だと、設定を変更して再度applyしても再作成されないという問題がありました。

対策

以下のissueに書いてあるまんまですが、annotationsにpod.alpha.kubernetes.io/init-containers: nullpod.beta.kubernetes.io/init-containers: nullを入れることで解決します。
https://github.com/kubernetes/kubernetes/issues/47264

例をあげると以下のような感じになります。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
      annotations:
        pod.alpha.kubernetes.io/init-containers: null
        pod.beta.kubernetes.io/init-containers: null
    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
        command:
        - wget
        - "-O"
        - "/work-dir/index.html"
        - http://kubernetes.io
        volumeMounts:
        - name: workdir
          mountPath: "/work-dir"
      dnsPolicy: Default
      volumes:
      - name: workdir
        emptyDir: {}

ちなみに、この状態だとkubectl applyする際にvalidateに引っかかるため--validate=falseのオプションを追加することで回避できます。
kubernetes1.8だと解消してるみたいなので、最新のクラスタ使ってる方は上記の回避策は不要です。

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2