目的
lxd上でmicrok8sクラスタを自動構築する様な仕組みを作った。
構築
ホスト側
カーネルの準備(raspberry piを用いる場合)
apt-get -y update
apt-get -y upgrade
reboot
apt-get -y install linux-headers
apt-get install linux-headers-$(uname -r)
apt-get -y install linux-modules-extra-raspi
プロファイルの作成とダウンロード(raspberry piを用いない場合はこちらから始める)
lxc profile create mk8s
wget https://raw.githubusercontent.com/ubuntu/microk8s/master/tests/lxc/microk8s.profile
cat microk8s.profile | lxc profile edit mk8s
master
lxdcli-master
CONTAINERNAME k8s-master
FROM ubuntu/22.04 -p default -p mk8s
RUN sleep 60
RUN rm -rf /var/lib/apt/lists/* && apt-get clean
RUN echo "alias kubectl='microk8s kubectl'" >> /root/.bashrc
RUN echo "alias watch='watch '" >> /root/.bashrc
RUN touch /root/.bash_profile
RUN echo "source ~/.bashrc" >> /root/.bash_profile
RUN systemctl restart systemd-resolved
RUN echo "nameserver 8.8.8.8" >> /etc/resolv.conf
RUN dhclient
### リポジトリアップデート
RUN export DEBIAN_FRONTEND=noninteractive
RUN apt-get -y update
RUN dpkg --configure -a
### snapインストール
RUN apt-get -y install snapd
RUN snap install core
RUN snap install snap-store
### k8sインストール
RUN snap install microk8s --classic && echo "done"
RUN touch token
RUN microk8s add-node >> token
ADDR . /root/token
worker
lxdfile-worker
CONTAINERNAME k8s-worker
FROM ubuntu/22.04 -p default -p mk8s
RUN sleep 60
RUN rm -rf /var/lib/apt/lists/* && apt-get clean
RUN systemctl restart systemd-resolved
RUN echo "nameserver 8.8.8.8" >> /etc/resolv.conf
RUN dhclient
### リポジトリアップデート
RUN export DEBIAN_FRONTEND=noninteractive
RUN apt-get -y update
RUN dpkg --configure -a
### snapインストール
RUN apt-get -y install snapd
RUN snap install core
RUN snap install snap-store
### k8sインストール
RUN snap install microk8s --classic && echo "done"
ADD token /root/token
RUN cat token | grep " --worker" | /bin/bash
RUN sleep 30
NUMBER 2
ビルド
lxdcli build lxdfile-master
lxdcli build lxdfile-worker
確認
VMの起動確認
root@shoma:/home/shoma/lxdcli# lxc list
+--------------+---------+-----------------------------+-----------------------------------------------+-----------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+--------------+---------+-----------------------------+-----------------------------------------------+-----------+-----------+
| k8s-master | RUNNING | 10.85.165.213 (eth0) | fd42:d04c:aa87:84fb:216:3eff:fe97:b4e1 (eth0) | CONTAINER | 0 |
| | | 10.1.235.192 (vxlan.calico) | | | |
+--------------+---------+-----------------------------+-----------------------------------------------+-----------+-----------+
| k8s-worker | RUNNING | 10.85.165.214 (eth0) | fd42:d04c:aa87:84fb:216:3eff:fe77:2fa (eth0) | CONTAINER | 0 |
| | | 10.1.254.128 (vxlan.calico) | | | |
+--------------+---------+-----------------------------+-----------------------------------------------+-----------+-----------+
| k8s-worker-0 | RUNNING | 10.85.165.219 (eth0) | fd42:d04c:aa87:84fb:216:3eff:fe32:f001 (eth0) | CONTAINER | 0 |
| | | 10.1.29.128 (vxlan.calico) | | | |
+--------------+---------+-----------------------------+-----------------------------------------------+-----------+-----------+
| k8s-worker-1 | RUNNING | 10.85.165.254 (eth0) | fd42:d04c:aa87:84fb:216:3eff:fe87:e741 (eth0) | CONTAINER | 0 |
| | | 10.1.230.0 (vxlan.calico) | | | |
+--------------+---------+-----------------------------+-----------------------------------------------+-----------+-----------+
| k8s-worker-2 | RUNNING | 10.85.165.156 (eth0) | fd42:d04c:aa87:84fb:216:3eff:fe3f:8122 (eth0) | CONTAINER | 0 |
| | | 10.1.140.0 (vxlan.calico) | | | |
+--------------+---------+-----------------------------+-----------------------------------------------+-----------+-----------+
kubernetesの構築確認
root@k8s-master:~# microk8s kubectl get node
NAME STATUS ROLES AGE VERSION
k8s-master Ready <none> 19m v1.27.5
k8s-worker-0 Ready <none> 12m v1.27.5
k8s-worker-1 Ready <none> 11m v1.27.5
k8s-worker-2 Ready <none> 10m v1.27.5
k8s-worker Ready <none> 18m v1.27.5
参考記事