2019年6月28日金曜日

kubectl コマンド実行時に対象となるクラスターを表示し、実行を保留して誤爆を防ぐ kubectl ラッパーを書いた

kubectl コマンドで破壊的オペレーションを実行する際、実行時の current-context を出力することで、ユーザーに実行対象の Kubernetes クラスターを確認させ、かつユーザーの入力があるまで実行を保留し誤爆を防いでくれるやつです。

# Run the `source` command to load this file into the current shell like follow:
#   $ source kubectl-wrapper.sh

# kubect-wrapper displays the current-context and prompts for user confirmation
# when trying to execute kubectl's subcommands that can update the cluster's state.
kubectl() { (
  # if has 1 argument || has -h --help --dry-run option || Sub-commands that are not dangerous if executed without confirmation; then
  if [ $# -le 1 ] || { echo " $* " | grep -Eq -- ' -h | --help | --dry-run '; } \
    || { echo " $* " | grep -Eq ' api-resources | api-version | cluster-info | completion | config | describe | diff | explain | get | logs | top | version | wait '; }; then
    command kubectl "$@"
    return $?
  fi
  printf "\e[01;33m%s\n%s\e[0m" "# CurrentContext: " "#   "
  command kubectl config current-context
  printf "\e[01;33m%s\e[0m" "# Press Enter key to continue... "
  read -r
  command kubectl "$@"
)}

GKE に限らずですが、複数の k8s クラスターを使い分けてると、 context を誤爆しそうで怖かった。

gcloud container clusters get-credentials --zone asia-east1-a --project MY_PROJECT MY_KUBERNETES_CLUSTER_NAME

動作確認は bash 入りの自機でしかしていないので悪しからず…。

2019年6月16日日曜日

WSL1 上で Docker をカジュアルに利用する

2019-06-18 ここから追記。

元々書いていた方法だと、 build image 時に以下の問題が有ると怒られたので、おとなしく DOCKER_HOST 環境変数を使うようにしました…。

SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have ‘-rwxr-xr-x’ permissions. It is recommended to double check and reset permissions for sensitive files and directories.

以下、その手順。

1. タスクトレイアイコンの Docker を右クリック → Settings から設定を開く

2. □ Expose daemon on tcp://localhost:2375 without TLS にチェックを入れる

3. DOCKER_HOST 環境変数を Docker for Windows に向ける

export DOCKER_HOST=tcp://localhost:2375

~/.bashrc は dotfiles リポジトリで一括管理しているので、
Mac でも Windows でも同じ ~/.bashrc を使いたい。
ので、以下のようにいい感じに追記した。

# If exist docker.exe in $PATH, set env:DOCKER_HOST.
command -v docker.exe && export DOCKER_HOST=tcp://localhost:2375

4. docker run hello-world

$ curl -LRsS https://get.docker.com/ | bash
   :
   :
$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the “hello-world” image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

負けた。


2019-06-18 ここまで追記。

以下、元の記事を残しますが、非推奨です。


「Ubuntu いくつと Docker いくつ、特定のバージョン同士だと動作する!」とか「DOCKER_HOST=tcp://localhost:2375」みたいなのに疲れた人用。

1. (インストール済みの場合) WSL 上でインストールした Docker を削除する

最終的に以下な状態になれば OK 。

$ docker
docker: command not found

お手元の環境に合わせて適切に削除してください。

apt purge docker.io
apt purge docker-ce

など。

2. Docker for Windows をインストールする

こちら

コマンドプロンプト上から docker.exe が実行できることを確認して下さい。

C:\Users\djeeno>docker.exe

Usage:  docker [OPTIONS] COMMAND

A self-sufficient runtime for containers

...

実行できない場合は PATH が通ってないです。
Docker for Windows インストール後に一度も再起動していない場合は、再起動して OS 環境変数の PATH を読み直し。

3. WSL の PATH の通ったディレクトリ配下に以下のファイルを設置する。

こちら

手っ取り早く /usr/local/bin/docker にインストールしたい場合は以下を実行してください。

sudo curl -LRsS https://raw.githubusercontent.com/djeeno/windows/master/docker -o /usr/local/bin/docker
sudo chmod +x /usr/local/bin/docker

以下、ファイルの中身を転記。

#!/bin/sh

# This script executes a command with the same file name as the executable file itself,
# or a file name with ".exe" added after that.
# It's just like BusyBox.

command_name=$(basename "$0")

if command -v "${command_name}.exe" >/dev/null; then
  exec "${command_name}.exe" "$@"
else
  exec "${command_name}" "$@"
fi

ざっくり言うと、このファイルを docker という名前で実行すると、 PATH 配下にある docker.exe を実行してくれます。

おわり

$ type docker
docker is /usr/local/bin/docker

$ docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

シンプル。