LoginSignup
24
9

More than 3 years have passed since last update.

kubectl editしたくない人のためのkubectl patchコマンド

Last updated at Posted at 2019-07-03

kubectl patchコマンド

kubectl editコマンドで立ち上がるエディタでリソースを編集するのが煩わしい我々のために🤔
kubectl patchコマンドが用意されている、ありがたく活用させてもらおう。

patchコマンドならエディタで手書きする必要がなくなるのが嬉しいポイントだ。
ワンライナーでリソースを変更できるのでシェルスクリプトなどに組み込むのにも向いている。

例1 コンテナイメージを変更する

イメージを変更するワンライナー

kubectl patch deployment sample-deployment -p '{"spec":{"template":{"spec":{"containers":[{"name":"sample-app","image":"sample-image:1.0"}]}}}}'

一時的にイメージのバージョンを変更したい場合などに活用できる。

例2 レプリカ数を変更する

レプリカ数を変更するワンライナー

kubectl patch deployment sample-deployment -p '{"spec":{"replicas":0}}'

一時的にレプリカ数を変更したい場合に活用できる。
ちなみに0も指定でき、簡易的なアンデプロイ状態になる。

例3 ServiceのTypeを変更する

TypeNodePortに変更するワンライナー

kubectl patch service sample-service -p '{"spec":{"type":"NodePort"}}'

とりいそぎClusterIPからNodePortに変更してアクセスしたい場合などに活用できる。

実行例

# before
$ kubectl get svc sample-service -o jsonpath='{.spec.type}'
ClusterIP

# patch実行
$ kubectl patch service sample-service -p '{"spec":{"type":"NodePort"}}'
service/sample-service patched

# after
$ kubectl get svc sample-service -o jsonpath='{.spec.type}'
NodePort
$ kubectl get svc sample-service -o jsonpath='{.spec.ports[0].nodePort}'
32206
$

上記のように-o jsonpath=に確認したいパスを指定すると値を確認できるが、以下のようにjqで絞り込む方が記号が少なく簡潔に書けるのでおすすめである。

kubectl get svc sample-service -o json | jq -r .spec.type
kubectl get svc sample-service -o json | jq -r .spec.ports[0].nodePort
24
9
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
24
9