ラズパイ内のKubernetesでPodを動かす

Page content

自分の RaspberryPi 4B でk8sを動かすことに成功したので、 備忘録としてその記録をまとめておく。

インストール

公式に出ているkubeadmでインストールする方法は、難しすぎて 実質不可能なので、おとなしくk3sを使う。

公式のクイックスタートのコマンドそのままだと書き込みできずにエラー発生するので、 以下のように権限644を付加する。1

curl -sfL https://get.k3s.io | K3S_KUBECONFIG_MODE="644" INSTALL_K3S_EXEC="server" sh -

もしくは、設定ファイル/etc/rancher/k3s/config.yamlで権限付加を明記し、サービス再起動でもいいだろう。

write-kubeconfig-mode: 644

サービス再起動は以下のコマンドでできる。2

停止

sudo systemctl stop k3s-agent

起動

sudo systemctl start k3s-agent

これでk8sのインストールは完了

PodとServiceの起動

基本的に公式のチュートリアル に従ってPodの導入とServiceの起動ができる。

しかし、imageはarmには対応していないようなので、別途指定する必要がある。 今回は、動かせばいいだけなので、適当にnginxのimageを使用した。

そして変更してできたmanifest、load-balancer-example.yamlが以下。

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: load-balancer-example
  name: hello-world
spec:
  replicas: 5 # 1でも動きます
  selector:
    matchLabels:
      app.kubernetes.io/name: load-balancer-example
  template:
    metadata:
      labels:
        app.kubernetes.io/name: load-balancer-example
    spec:
      containers:
      - image: docker.io/arm64v8/nginx # arm対応imageを指定
        name: hello-world
        ports:
        - containerPort: 80 # Pod側の待受ポート

以下のコマンドでDeploymentを作成。

kubectl apply -f load-balancer-example.yaml

以下のコマンドでServiceを作成。 今回は、ポート番号は外側8081で構えて、内側の80に渡したい。 そのため、以下のコマンドで作成する。

kubectl expose deployment hello-world --type=LoadBalancer --name=my-service --port=8081 --target-port=80

Serviceの詳細は以下で出力できます。

kubectl describe services my-service

ローカルIPが以下のように出力されるはずです。 VIPは知りません。悪い気はしないのでよしとします。

LoadBalancer Ingress:     xxx.xxx.xxx.xxx (VIP)

Podの起動状態は以下のコマンドで確認できます。

kubectl get pods --output=wide
NAME                          READY   STATUS    RESTARTS   AGE    IP           NODE          NOMINATED NODE   READINESS GATES
hello-world-xxxxxxxxx-xxxxx   1/1     Running   0          ----   ----------   hostname      <none>           <none>

Podが起動すれば、自身のIPを指定して、http://xxx.xxx.xxx.xxx:8081でアクセスできます。 コマンドなら以下でも。

curl http://xxx.xxx.xxx.xxx:8081

クリーンアップ

公式のチュートリアル丸写しですが、いつか消えそうなので念の為…

Serviceの削除

kubectl delete services my-service

Deployment、ReplicaSet、およびHello Worldアプリケーションが動作しているPodの削除。

kubectl delete deployment hello-world

感想

ほぼ公式チュートリアルで動くのだが、イメージだけ変える必要があるって話。 それだけなのに、なんか仰々しくなっちゃった。