Open2

k3s をざっくりキャッチアップ (TBD)

zuribozuribo

Get Started

0. 環境構築

検証環境として、AWS の EC2 インスタンスを使う。

  • インスタンスタイプ: t4g.small
    • 理由: 無料トライアルが使えた + k3s が Arm インスタンスに特化してるらしい。
  • AMI: Amazon Linux 2
    • 理由: 執筆時点での一番新しい Amazon Linux ディストリビューションは Amazon Linux 2023 だが、インストールがうまくいかなかった。

1. インストール

公式ページ通りに実行する。

$ curl -sfL https://get.k3s.io | sh -
[INFO]  Finding release for channel stable
[INFO]  Using v1.28.4+k3s2 as release
[INFO]  Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.28.4+k3s2/sha256sum-arm64.txt
[INFO]  Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.28.4+k3s2/k3s-arm64
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Finding available k3s-selinux versions
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Creating /usr/local/bin/ctr symlink to k3s
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
[INFO]  systemd: Enabling k3s unit
Created symlink from /etc/systemd/system/multi-user.target.wants/k3s.service to /etc/systemd/system/k3s.service.
[INFO]  systemd: Starting k3s

systemd のサービスが起動したっぽいので、一応確認しておく。

$ sudo systemctl status k3s
● k3s.service - Lightweight Kubernetes
   Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2023-12-24 10:31:01 UTC; 1min 52s ago
     Docs: https://k3s.io
  Process: 1426 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)
  Process: 1423 ExecStartPre=/sbin/modprobe br_netfilter (code=exited, status=0/SUCCESS)
  Process: 1419 ExecStartPre=/bin/sh -xc ! /usr/bin/systemctl is-enabled --quiet nm-cloud-setup.service (code=exited, status=0/SUCCESS)
 Main PID: 1429 (k3s-server)
    Tasks: 91
   Memory: 1.1G
   CGroup: /system.slice/k3s.service
           ├─1429 /usr/local/bin/k3s server
           ├─1480 containerd
           ├─2069 /var/lib/rancher/k3s/data/33ad323bfc7d43ab2da5bcb2b8f057cfa...
           ├─2074 /var/lib/rancher/k3s/data/33ad323bfc7d43ab2da5bcb2b8f057cfa...
           ├─2181 /var/lib/rancher/k3s/data/33ad323bfc7d43ab2da5bcb2b8f057cfa...
           ├─3280 /var/lib/rancher/k3s/data/33ad323bfc7d43ab2da5bcb2b8f057cfa...
           └─3357 /var/lib/rancher/k3s/data/33ad323bfc7d43ab2da5bcb2b8f057cfa...

Dec 24 10:31:46 ip-172-31-30-79.ec2.internal k3s[1429]: I1224 10:31:46.215280...
Dec 24 10:31:46 ip-172-31-30-79.ec2.internal k3s[1429]: I1224 10:31:46.215299...
Dec 24 10:31:46 ip-172-31-30-79.ec2.internal k3s[1429]: I1224 10:31:46.215319...
Dec 24 10:31:46 ip-172-31-30-79.ec2.internal k3s[1429]: I1224 10:31:46.215468...
Dec 24 10:31:46 ip-172-31-30-79.ec2.internal k3s[1429]: I1224 10:31:46.316313...
Dec 24 10:31:46 ip-172-31-30-79.ec2.internal k3s[1429]: I1224 10:31:46.673927...
Dec 24 10:31:46 ip-172-31-30-79.ec2.internal k3s[1429]: I1224 10:31:46.674003...
Dec 24 10:31:46 ip-172-31-30-79.ec2.internal k3s[1429]: I1224 10:31:46.922093...
Dec 24 10:31:46 ip-172-31-30-79.ec2.internal k3s[1429]: I1224 10:31:46.922188…s"
Dec 24 10:31:59 ip-172-31-30-79.ec2.internal k3s[1429]: I1224 10:31:59.363183...
Hint: Some lines were ellipsized, use -l to show in full.

2. サーバー実行

公式ページ通りに実行する。

$ sudo k3s kubectl get node
sudo: k3s: command not found

k3s コマンドが見つからないらしいので、k3s ファイルを探す。

$ sudo yum install -y mlocate
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                               | 3.6 kB     00:00
Package mlocate-0.26-8.amzn2.aarch64 already installed and latest version
Nothing to do

$ sudo updatedb

$ locate --regex 'k3s$'
/etc/rancher/k3s
/usr/local/bin/k3s
/var/lib/rancher/k3s
/var/lib/rancher/k3s/data/33ad323bfc7d43ab2da5bcb2b8f057cfafe445d8fd0bac8c1c27c45473d508db/bin/k3s

どうやら /usr/local/bin/ 配下にインストールされた模様。

sudo で実行しないといけないので、sudo にパスを設定しておく。

$ sudo sed -i -e '/secure_path/s/$/:\/usr\/local\/bin/' /etc/sudoers

無事にノードを取ってこれた。

$ sudo k3s kubectl get node
NAME                           STATUS   ROLES                  AGE   VERSION
ip-172-31-30-79.ec2.internal   Ready    control-plane,master   29m   v1.28.4+k3s2

Appendix

Amazon Linux 2023 ではうまくインストールできなかった件

公式ページに書かれてる通りに実行。

$ curl -sfL https://get.k3s.io | sh -
[INFO]  Finding release for channel stable
[INFO]  Using v1.28.4+k3s2 as release
[INFO]  Downloading hash https://github.com/k3s-io/k3s/releases/download/v1.28.4+k3s2/sha256sum-arm64.txt
[INFO]  Downloading binary https://github.com/k3s-io/k3s/releases/download/v1.28.4+k3s2/k3s-arm64
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Finding available k3s-selinux versions
[ERROR]  Failed to find the k3s-selinux policy, please install:
    dnf install -y container-selinux
    dnf install -y https://rpm.rancher.io/k3s/stable/common/centos/8/noarch/

エラーが出たので、言われた通りに実行。

$ sudo dnf install -y container-selinux
Last metadata expiration check: 0:02:37 ago on Sun Dec 24 10:24:01 2023.
Dependencies resolved.
================================================================================
 Package              Arch      Version                    Repository      Size
================================================================================
Installing:
 container-selinux    noarch    2:2.222.0-325.amzn2023     amazonlinux     55 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 55 k
Installed size: 67 k
Downloading Packages:
container-selinux-2.222.0-325.amzn2023.noarch.r 810 kB/s |  55 kB     00:00
--------------------------------------------------------------------------------
Total                                           448 kB/s |  55 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Running scriptlet: container-selinux-2:2.222.0-325.amzn2023.noarch        1/1
  Installing       : container-selinux-2:2.222.0-325.amzn2023.noarch        1/1
  Running scriptlet: container-selinux-2:2.222.0-325.amzn2023.noarch        1/1
uavc:  op=setenforce lsm=selinux enforcing=0 res=1uavc:  op=load_policy lsm=seli  Verifying        : container-selinux-2:2.222.0-325.amzn2023.noarch        1/1

Installed:
  container-selinux-2:2.222.0-325.amzn2023.noarch

Complete!

$ sudo dnf install -y https://rpm.rancher.io/k3s/stable/common/centos/8/noarch/
Last metadata expiration check: 0:04:10 ago on Sun Dec 24 10:24:01 2023.
Cannot open /var/cache/dnf/commandline-4e2c1171a059e757/packages/.: Is a directory