LoginSignup
0
1

Windows環境で実行する「つくって、壊して、直して学ぶ Kubernetes入門」

Posted at

つくって、壊して、直して学ぶ Kubernetes入門をほぼ読み終えました。

Windows環境の自分は、一部MacやLinuxを想定している部分に戸惑いました。
例:載っているコマンドがbashで実行する前提etc
(それを考えてもいい本だと思います)

私がつまづいた部分、解消の仕方、少し便利にできる部分をこの記事では紹介します。

Docker Desktop
kind
kubectl

この3点に関しては正常にインストールできているものとします。
(念のため今後記事を作成する予定でいます)

kindが動かない時

該当ページ;P055以降
状況:kindコマンドでクラスター作成や、クラスター情報取得ができない
直し方:Docker Desktopを起動する

kind versionは動作するが、kind create clusterやkind get clustersは動作しない。
そんな時はDocker Desktopを起動していない可能性があります。

Windows環境でkindを正常に動作させるにはDocker Desktopが起動している必要があります

kindのエラー例(nmrisはユーザ名)
nmris:nmris> kind version
kind v0.22.0 go1.20.13 windows/amd64

nmris:nmris> kind create cluster
ERROR: failed to create cluster: failed to list nodes:command "docker ps -a --filter label=io.x-k8s.kind.cluster=kind --format '{{.Names}}'" failed with error: exit status 1

Command Output: error during connect: this error may indicate that the docker daemon is not running: Get "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/json?all=1&filters=%7B%22label%22%3A%7B%22io.x-k8s.kind.cluster%3Dkind%22%3Atrue%7D%7D": open //./pipe/docker_engine: The system cannot find the file specified.

nmris:nmris> kind get clusters
ERROR: failed to list clusters: command "docker ps -a --filter label=io.x-k8s.kind.cluster --format '{{.Label "io.x-k8s.kind.cluster"}}'" failed with error: exit status 1

Command Output: error during connect: this error may indicate that the docker daemon is not running: Get "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/json?all=1&filters=%7B%22label%22%3A%7B%22io.x-k8s.kind.cluster%22%3Atrue%7D%7D": open //./pipe/docker_engine: The system cannot find the file specified.

直すにはDocker Desktopを起動しましょう。
それだけでkindが正常に動くはずです。

curl localhost:8080が失敗する

該当ページ;P091以降
状況:curl localhost:8080でエラーが変える
直し方:curl.exe localhost:8080にコマンドを変更する(修復法の一例)

WindowsではcurlはInvoke-WebRequestのエイリアスであるため、明確にcurl.exeで指定する必要がある

curlのエラー例
curl : URI プレフィックスが認識されません。
発生場所 行:1 文字:1
+ curl localhost:8080
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotImplemented: (:) [Invoke-WebRequest], NotSupportedException
    + FullyQualifiedErrorId : WebCmdletIEDomNotSupportedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

直し方はcurl.exeと明示的に指定するだけです。

いちいち.yamlを.txtに変更してメモ帳で開きたくない

該当ページ;あちこち
状況:メモ帳で開くために拡張子変更するのがめんどくさい
直し方:notepad anything.yaml(ファイル名)でyamlを直接メモ帳で開ける

Powershellでnotepad ファイル名を実行すると、指定したファイルをメモ帳で開くことができる

notepadコマンド例
nmris:chapter-06> notepad .\cronjob-hello-server.yaml

これでコマンドから直接yamlをメモ帳から開けるようになり作業効率が上がりました。

kubectlのエイリアスが指定できない

該当ページ;P104以降
状況:alias k=kubectlでエラーが返る
直し方:Powershell用のエイリアス作成コマンドを実行する

Powershellではaliasコマンドでエイリアスコマンドを作成することはできません

aliasコマンドでエイリアスを作成すると、TABで自動補完されなくなるため、1つ下のエイリアスに自動補完を設定する方法も実施してください

  1. $PROFILEの作成
  2. $PROFILEへコマンド追記
  3. Powershellの実行ポリシーの変更

上記3つを完了させてください。
1から順番に紹介しますが、2,3は順不同です。

まずPowershellに$PROFILEを作成します。
下記コマンドで$PROFILEが存在するか確認してください。
存在しない場合は作成して、メモ帳で開いてください。

$PROFILEの存在確認
# プロファイル確認
Test-Path $PROFILE

# 無い場合
New-Item -path $PROFILE -type file -force

# ある場合、もしくはない場合で作成後
notepad $PROFILE

メモ帳で下記のコマンドを追記します
-Name kの部分を

エイリアスの作成
# kubectl = k
Set-Alias -Name k -Value kubectl

最後にPowershellの実行ポリシーを変更します。
デフォルトではすべてのスクリプトの実行が制限されているため$PROFILEに書かれているコマンドも実行することができないためです。

実行ポリシーの緩和
Set-ExecutionPolicy RemoteSigned

実行ポリシーの緩和後、Powershellを再起動します。
再起動後、起動時にエラーが発生せず、k get podsなどのコマンドが通れば成功です。

kubectlのエイリアスへ自動補完を設定する

該当ページ;P104以降
状況:エイリアスに対して自動補完がきかない
直し方:$PROFILEに設定用コマンドを追記する

$PROFILEへ以下を追記
# notepadを起動(ここは追記する必要はありません)
notepad $PROFILE

# エイリアスkに対しての自動補完対応
# -CommandName k の部分は自分が設定したエイリアスを指定します
kubectl completion powershell | Out-String | Invoke-Expression
Register-ArgumentCompleter -CommandName k -ScriptBlock $__kubectlCompleterBlock

PowershellだけでBase64エンコードできない

該当ページ;P188以降
状況:PowershellだけではBase64エンコードできない
直し方:下記コマンドを実行する

Base64作成関数
function enBase64(){
$plainPW = Read-Host "Please enter your PASSWORD"
$byte = ([System.Text.Encoding]::Default).GetBytes($plainPW)
$b64enc = [Convert]::ToBase64String($byte)
echo $b64enc
}

# コマンド実行
enBase64

enBase64を実行すると「"Please enter your PASSWORD"」と表示され、入力した文字がBase64で符号化され出力されます。

参考にしたサイト

curlのエイリアスについて
https://zenn.dev/yu1low/articles/f559f35c7087ef

Powershellの$PROFILEについて
https://qiita.com/tomoko523/items/87ccaec05a433b02f67e

Windowsの標準機能でBase64エンコード/デコードする方法
https://hanlabo.co.jp/memorandum/3292/

0
1
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
0
1