Zenn
Open16

kubernetes on Ansibleをやってみる

GunseiKPaseriGunseiKPaseri

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

winget install -e --id Kubernetes.kubectl
winget install Kubernetes.kind
GunseiKPaseriGunseiKPaseri

この状態では何も動いていない。

kind create cluster

これでローカルPCでは動いている状態になる。

GunseiKPaseriGunseiKPaseri

動作テスト

>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
GunseiKPaseriGunseiKPaseri

k8sはクラスタという単位で管理される。
複数台で動作するk8sを連携する方法として以下があるようだ

  • k8sクラスタを1つに統合
  • マルチクラスタ管理

ChatGPT曰く以下の対応らしい

クラウド概念 Kubernetes の対応
1台のマシン Kubernetes ノード (Node)
1つの AZ ノードグループ (Node Pool)
1つのリージョン 1つの Kubernetes クラスタ (Cluster)
複数リージョン マルチクラスタ構成 (Multi-Cluster)

kindはDocker上のコンテナとしてnodeを作るところには留意する

とりあえずマルチクラスタの事は忘れて1つのクラスタを育ててみる

GunseiKPaseriGunseiKPaseri

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

GunseiKPaseriGunseiKPaseri

とか思ったけどそもそもDockerDesktopあったらKubernetes動かせなかった?と思ったら動かせた。

これを軸にいろいろやってみる

GunseiKPaseriGunseiKPaseri

スイッチ入れると見えるようになった

>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から簡単に動かせるようになってるっぽい

GunseiKPaseriGunseiKPaseri

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

GunseiKPaseriGunseiKPaseri

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

GunseiKPaseriGunseiKPaseri

Ubuntu server 24.10

VMWareで作ったのでしくじり
VirtualBoxの内部ネットワークはLANセグメントに相当するらしいので、とりあえず同じ状況にはしておく
https://qiita.com/zeffy1014/items/788803a732bae5121d3c

Netplanで以下のように設定する

/etc/netplan/01-network-manager-all.yaml
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を利用
設定方法も上と同様。

GunseiKPaseriGunseiKPaseri

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

GunseiKPaseriGunseiKPaseri

ansibleのインストールはドキュメントを参考に
https://docs.ansible.com/ansible/2.9_ja/installation_guide/intro_installation.html#ubuntu-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]
ログインするとコメントできます