3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Raspberry Piでk3sクラスタを建ててみた

Last updated at Posted at 2024-02-14

Raspberry Piでk3sクラスタを建ててみたので、構築手順を簡単にまとめてみました。

k3sとは

(IoTやエッジコンピューティングに最適化された)軽量なKubernetesです。

前提

  • 2台のRaspberry Pi(Masterノード1台&Workerノード1台)を使ってk3sクラスタを構築する
  • MasterノードとWorkerノードには固定IPアドレスが割り振られていて、相互に通信が可能な状態であること

動作環境

  • Masterノード
    • Raspberry Pi4 ModelB 4GB
    • Raspberry Pi OS(64-bit)
    • hostname: master
  • Workerノード
    • Raspberry Pi4 ModelB 4GB
    • Raspberry Pi OS(64-bit)
    • hostname: worker01

共通の設定(Master&Worker)

Raspberry Pi OSではデフォルトでcgroupsが有効化されていないので、/boot/cmdline.txtの末尾に以下を追加します。

cgroup_memory=1 cgroup_enable=memory

Kubernetesではswapの利用は非推奨なので、無効化しておきます。

$ sudo swapoff --all
$ sudo systemctl stop dphys-swapfile.service
$ sudo systemctl disable dphys-swapfile.service

再起動します。

$ sudo reboot

Masterノード

以下コマンドでk3sをインストールします。

※デフォルトではk3クラスタにアクセスするための設定ファイルは /etc/rancher/k3s/k3s.yamlに保存されますが、kubectlを使う際に該当ファイルへのアクセス権限が不足することによってエラーが発生する場合があるようなので、--write-kubeconfig-modeオプションを忘れずに指定してください。

$ curl -sfL https://get.k3s.io | sh -s - --write-kubeconfig-mode 644

インストール後、ノードを確認してみると以下が表示されると思います。

$ kubectl get nodes
NAME     STATUS   ROLES                  AGE     VERSION
master   Ready    control-plane,master   2m36s   v1.28.6+k3s2

Workerノードの設定を行う前に、(k3sクラスタに参加するための)node-tokenを取得しておきます。

$ sudo cat /var/lib/rancher/k3s/server/node-token

Workerノード

以下コマンドでk3s(agent)をインストールします。K3S_URLとK3S_TOKENは実環境に合わせて変更してください。

curl -sfL https://get.k3s.io | K3S_URL=https://<MasterノードのIPアドレス>:6443 K3S_TOKEN=<上記のToken> sh -

ノードを確認してみると、MasterノードとWorkerノードの両方が表示されるようになると思います。

$ kubectl get nodes
master     Ready    control-plane,master   13m   v1.28.6+k3s2
worker01   Ready    <none>                 30s   v1.28.6+k3s2

Nginxを起動してみる

定番のNginxで動作確認してみます。
以下のyamlファイルを作成しておきます。

nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30000
  selector:
    app: nginx

以下コマンドを実行します。

$ kubectl apply -f nginx.yaml
deployment.apps/nginx created
service/nginx created

podの状態を確認してみます。

$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7c79c4bf97-nk7fd   1/1     Running   0          22s

別端末(ノートPC)のブラウザからアクセスしてみます。

http://<WorkerノードのIPアドレス>:30000/

image.png

参考URL

3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?