LoginSignup
5

More than 3 years have passed since last update.

Mac で Terminal の k8s 操作環境を改善する

Last updated at Posted at 2018-12-26

Kubernetes完全ガイド impress top gearシリーズを読みながら設定した時のメモ。

kubectl コマンドを補完する

kubectl コマンドの補完、Pod/Node 名などの補完が出来る

Enabling shell autocompletion

自分の場合 zsh を使っており、~/.zshrcに以下を追加して対応

~/.zshrc
# kubectl completion
source <(kubectl completion zsh)

これで補完されるようになった

alias を作成する

以下を追加

~/.zshrc
# kuebctlc alias
alias k='kubectl'

これで以下のように打てる。

$k get nodes
NAME                                              STATUS    ROLES     AGE       VERSION
ip-172-31-0-56.ap-northeast-1.compute.internal    Ready     <none>    6d        v1.11.5
ip-172-31-19-51.ap-northeast-1.compute.internal   Ready     <none>    6d        v1.11.5
ip-172-31-23-75.ap-northeast-1.compute.internal   Ready     <none>    6d        v1.11.5

プロンプトに context/namespace 情報を表示する

kube-ps1を利用します。

$ brew update
$ brew install kube-ps1

・・・()

You may wish to add the GOROOT-based install location to your PATH:
  export PATH=$PATH:/usr/local/opt/go/libexec/bin
==> kubernetes-cli
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

zsh completions have been installed to:
  /usr/local/share/zsh/site-functions
==> kube-ps1
Make sure kube-ps1 is loaded from your ~/.zshrc and/or ~/.bashrc:
  source "/usr/local/opt/kube-ps1/share/kube-ps1.sh"
  PS1='$(kube_ps1)'$PS1

自分は zshなので ~/.zshrcに追記

~/.zshrc
# kubec-ps1
source "/usr/local/opt/kube-ps1/share/kube-ps1.sh"
PS1='$(kube_ps1)'$PS1
$source ~/.zshrc

無事表示できた。

(⎈ |arn:aws:eks:ap-northeast-1:xxxx:cluster/eks-second-cluster:default)  ~

ただ、クラスター名が長すぎて画面が狭くなってしまうという問題が。。

Trim cluster name & namespace name #53

変えればいい。

For the cluster, you can do a rename (alias) with kubectl:
kubectl config rename-context old_name new_name

やってみる。

$kubectl config rename-context arn:aws:eks:ap-northeast-1:xxxx:cluster/eks-second-cluster eks-second-cluster
Context "arn:aws:eks:ap-northeast-1:xxx:cluster/eks-second-cluster" renamed to "eks-second-cluster".
(⎈ |eks-second-cluster:default) ~

context を変えて長すぎないようにした。
この状態でも kubectlコマンドも正しく実行可能。

cluster/namespace の切り替えを補完する

kubectx を使う。

ahmetb /kubectx

Mac の場合、brew install kubectx で終わり。
これで補完も効いてくれる。

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