kubernetes on Ansibleをやってみる

ゆくゆくは他マシンにも入れるがとりあえず普段使いのWindowsで実験的に導入。
Dockerが使えるkindの評判がいいのでこれで動かしてみる。
winget install -e --id Kubernetes.kubectl
winget install Kubernetes.kind

この状態では何も動いていない。
kind create cluster
これでローカルPCでは動いている状態になる。

動作テスト
>kubectl get pod
No resources found in default namespace.
>kubectl run hello-world --image hello-world --restart=Never
pod/hello-world created
>kubectl get pod
NAME READY STATUS RESTARTS AGE
hello-world 0/1 Completed 0 7s
>kubectl logs pod/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-starte
>kubectl delete pod/hello-world
pod "hello-world" deleted

とりあえず未指定で作ったクラスタはこういう状況になる。オプション等を付け足すことでコントロールプレーン以外にワーカーノードが作成される。
参考:https://qiita.com/fruscianteee/items/bee47afe3222f0d073ce
>kind get clusters
kind
>kubectl get node
NAME STATUS ROLES AGE VERSION
kind-control-plane Ready control-plane 77m v1.32.2

k8sはクラスタという単位で管理される。
複数台で動作するk8sを連携する方法として以下があるようだ
- k8sクラスタを1つに統合
- マルチクラスタ管理
ChatGPT曰く以下の対応らしい
クラウド概念 | Kubernetes の対応 |
---|---|
1台のマシン | Kubernetes ノード (Node) |
1つの AZ | ノードグループ (Node Pool) |
1つのリージョン | 1つの Kubernetes クラスタ (Cluster) |
複数リージョン | マルチクラスタ構成 (Multi-Cluster) |
kindはDocker上のコンテナとしてnodeを作るところには留意する
とりあえずマルチクラスタの事は忘れて1つのクラスタを育ててみる

kind、単一マシン内でkubernetesするツールとしての側面が強いので、複数台で動かすなら素直にkubeadmとかk3sとか使えという話だった。そりゃそうか

まあなんか雰囲気はつかめたのでこの辺を参考にやる
ケチなのでwslもクラスタに参加させる
とか思ったけどそもそもDockerDesktopあったらKubernetes動かせなかった?と思ったら動かせた。
これを軸にいろいろやってみる

スイッチ入れると見えるようになった
>kubectl config use-context docker-desktop
Switched to context "docker-desktop".
>kubectl get node
NAME STATUS ROLES AGE VERSION
docker-desktop Ready control-plane 2m v1.31.4
でもこれも単一ノードで連携できないやつなのかな。
ちなみにkindはDockerDesktopの4.38から簡単に動かせるようになってるっぽい

なんか調べてたらcozystackっての出てきたけどこれ割とよさそう? k8sの上に乗るやつかな

k8sの環境構築がだるそうなので宣言的に書きたい
→nixosでk8sノードを立ててみるか(???
別にnixos詳しくないけど

なんかうまくいかなかったので素直にubuntuにkubeadm入れよう
代わりにansibleを使ってみる。

とりあえずこれに従ってansible環境の練習をする

Ubuntu server 24.10
VMWareで作ったのでしくじり
VirtualBoxの内部ネットワークはLANセグメントに相当するらしいので、とりあえず同じ状況にはしておく
Netplanで以下のように設定する
network:
version: 2
ethernets:
ens37:
dhcp4: no
addresses: [192.168.111.1/24]
sudo chmod 600 /etc/netplan/01-network-manager-all.yaml
sudo netplan try
……と思ったけどなんかVMWareだとVagrantの有料プラグインがいる?ので素直にVirtualBoxを利用
設定方法も上と同様。

勘違いしてたけどVagrantはVMWareとかVirtualBoxを操作するアプリってだけなのね

ansibleのインストールはドキュメントを参考に
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo apt-add-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible
しようとしたが、どうもppaはubuntu23.04以降非対応っぽいので上は無視する
$ sudo apt install ansible-core
$ ansible --version
ansible [core 2.16.3]