LoginSignup
10
2

More than 1 year has passed since last update.

Kubernatesのコンテクスト/ネームスペース切り換えにKubieをどうぞ

Last updated at Posted at 2022-12-05

僕がいつもよく使っている、コンテクストやネームスペースの切り換えツールKubieを紹介します。

READMEに載っているハムスターのアイコンがかわいいですね。

これは何?

何かしら会社でKubernetesを触る人はdev/stg/prdなど複数のクラスタを扱うことが多いと思います。別のクラスタを扱う際には kubectl config use-context して切り換えたり、kubectl -n foobar ... のようにコマンドごとにネームスペースを指定するのですが、あまりにたくさんコマンドを打つので毎回これを打つのは面倒になってきます。

それらの切り替えを楽に行うために有名なkubectxやkubens、kubeswitchがあります。

Kubieはこのkubectxやkubensの強力な代替ツールとして作られています。

自分がこのKubieの一番の特徴だと思っているのは、コンテクストごとに別のシェルプロセスが立ち上がることで、これによってターミナルエミュレータの別タブで別のコンテクストを扱うということが簡単にできます。

READMEをみると各種インストール方法があります。Macの人は brew install kubie でどうぞ。

使い方

自分がよく使うのは以下の二つです

  • kubie ctx コンテクストの 切り換え
  • kubie ns ネームスペースの切り換え
$ kubie ctx
  prd-cluster
  stg-cluster
> dev-cluster
  3/5                                                                                                       0/0
> c

タイプした文字とクラスタ名がファジーにマッチングして、矢印キーで選んでエンターを押すと、コンテクストが切り替わり、以下のようにPS1が書き換わってターミナルに非常にわかりやすく表示されます。Qiitaでは表現できないですがいろも付いていて、今どのコンテクストに自分がいるのかが非常にわかりやすいです。

[dev-cluster|default] localhost: ~$ 

今は表示されているように、dev-cluster contextの default namespaceにいます。

ここで kubie ns でネームスペースを切り換えます:

[dev-zozo-platform|default] localhost: ~$ kubie ns

  foo-ns
  bar-ns
> baz-ns
  3/3                                                                                                       0/0
> 


[dev-cluster|baz-ns] localhost: ~$ 

同じようにファジーにマッチされ、ターミナルにわかりやすく今どのネームスペースにいるかを表示してくれます。

他にも kubie exec <context> <namespace> kubectl get pods のようにすると、コンテクストもnsもまたいで便利。

他のコマンドはヘルプを参照:

$ kubie
kubie 0.19.1

USAGE:
    kubie <SUBCOMMAND>

OPTIONS:
    -h, --help       Print help information
    -V, --version    Print version information

SUBCOMMANDS:
    ctx            Spawn a shell in the given context. The shell is isolated from other shells.
                       Kubie shells can be spawned recursively without any issue
    delete         Delete a context. Automatic garbage collection will be performed. Dangling
                       users and clusters will be removed
    edit           Edit the given context
    edit-config    Edit kubie's config file
    exec           Execute a command inside of the given context and namespace
    help           Print this message or the help of the given subcommand(s)
    info           View info about the current kubie shell, such as the context name and the
                       current namespace
    lint           Check the Kubernetes config files for issues
    ns             Change the namespace in which the current shell operates. The namespace
                       change does not affect other shells
    update         Check for a Kubie update and replace Kubie's binary if needed. This function
                       can ask for sudo-mode

まとめ

かなり便利なのに周りに使っている人が多くなかったので、Kubieの社内/社外布教のためにKubieのよいところを説明しました。よきkubectlライフを :wave:

10
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
10
2