😽

久々におうちkubernetes環境を立てて、Cloudflareから接続できるようにした

に公開

以前画面の壊れたRyzenノートとかにk3sやkindなども入れてましたが、特に使うこともありませんでした。
今回最近のkubernetesがどうなってるか調べたところ、以前より簡単に設定できそうなので入れてみました。

環境

PC : Minisforum UM870 Slim (Ryzen7乗ったミニPC)
メモリ : DDR5 96GB(値上がり前に買えてよかった)
ホストOS:Proxmox。k8s動かすのでUbuntu + Qemuぐらいでもいいかもしれません
Kubernetes:1.34, cri-o, cilium, cloudflared使用

参考

https://kubernetes.io/ja/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/
https://zenn.dev/kznrluk/articles/0cc24e14fec36b
https://zenn.dev/kazugmx/articles/f75ac57d7e9b27
https://techblog.ap-com.co.jp/entry/2025/06/19/090000

入れた感想

試しに入れて遊んでみるのはいいですが、これの不具合時のメンテはしたくないですね・・

手順

まずswapを無効化します

$ sudo nano /etc/fstab
/swap で始まる行の頭に#を付けて無効化

次にFirewallを無効化します (恐らく今回は必要ない)

$ sudo systemctl status ufw

続いてcri-o, kubectl, kubeadmを入れていきます。これは各nodeで行っています。

$ CRIO_VERSION=v1.34
$ sudo apt-get update
$ sudo apt-get install -y apt-transport-https ca-certificates curl gpg

$ curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.34/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
$ echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.34/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

# cri-o repos
$ sudo curl -fsSL https://download.opensuse.org/repositories/isv:/cri-o:/stable:/$CRIO_VERSION/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/cri-o-apt-keyring.gpg
$ echo "deb [signed-by=/etc/apt/keyrings/cri-o-apt-keyring.gpg] https://download.opensuse.org/repositories/isv:/cri-o:/stable:/$CRIO_VERSION/deb/ /" | sudo tee /etc/apt/sources.list.d/cri-o.list


$ sudo apt-get update
$ sudo apt-get install -y kubelet kubeadm kubectl cri-o
$ sudo apt-mark hold kubelet kubeadm kubectl
ネットワークを使う設定もします。
$ sudo modprobe br_netfilter
$ sudo sysctl -w net.ipv4.ip_forward=1

criの有効化

$ sudo systemctl enable --now crio

kubernatesの初期化

$ sudo kubeadm init

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

sudo kubeadm join <IP>:<PORT> --token <token> \
        --discovery-token-ca-cert-hash sha256:<hash>

<token>, <hash>は実際のものを使ってください。

Cilium CLIのインストール

$ CILIUM_CLI_VERSION=$(curl -s https://raw.githubusercontent.com/cilium/cilium-cli/main/stable.txt)
$ CLI_ARCH=amd64
$ if [ "$(uname -m)" = "aarch64" ]; then CLI_ARCH=arm64; fi
curl -L --fail --remote-name-all https://github.com/cilium/cilium-cli/releases/download/${CILIUM_CLI_VERSION}/cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}
$ sha256sum --check cilium-linux-${CLI_ARCH}.tar.gz.sha256sum
$ sudo tar xzvfC cilium-linux-${CLI_ARCH}.tar.gz /usr/local/bin
$ rm cilium-linux-${CLI_ARCH}.tar.gz{,.sha256sum}

$ cilium install

当初cilium statusしたらErrorが返って来ましたが、少し待つ必要があったようです。

Helmインストール

node1で、

$ curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

$ helm repo add stable https://charts.helm.sh/stable
$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm repo add cloudflare https://cloudflare.github.io/helm-charts

$ helm repo update

Cloudflaredのインストール

$ helm install cloudflared cloudflare/cloudflare-tunnel \ --namespace cloudflared \ --create-namespace \ --set tunnel.token="YOUR_TUNNEL_TOKEN"

↑これはうまくいかなかったので、yaml作ってkubectl applyしました。
 
cloudflared.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: cloudflared
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cloudflared
  namespace: cloudflared
  labels:
    app: cloudflared
spec:
  replicas: 2
  selector:
    matchLabels:
      app: cloudflared
  template:
    metadata:
      labels:
        app: cloudflared
    spec:
      containers:
      - name: cloudflared
        image: docker.io/cloudflare/cloudflared:latest
        imagePullPolicy: IfNotPresent
        args:
        - tunnel
        - --no-autoupdate
        - --protocol
        - http2
        - run
        - --token
        - "..."
        resources:
          requests:
            memory: "128Mi"
            cpu: "200m"
          limits:
            memory: "256Mi"
            cpu: "500m"

Cloudflare側でtunnelセットアップして、自分で持っているドメインを入れてみました。
nginxのserviceも入れてみました。

たまに切れる問題

Cloudflaredを入れてしばらくたつと接続できなくなる問題が多発してました。
いろいろググったところ、どうもlivenessProbeの設定が要らなかったようで。消したら接続が切れることが無くなりました。

Looks like I have issue with the livenessProbe settings. When I removed them everything seems to work fine.

    livenessProbe:       
      httpGet:
        # Cloudflared has a /ready endpoint which returns 200 if and only if         
        # it has an active connection to the edge.         
        path: /ready
        port: 2000
        failureThreshold: 1
        initialDelaySeconds: 10
        periodSeconds: 10

またcri-oはdockerのimage名の解決にdocker.io/libraryを付けないと、同名のコンテナがある際に判別できずイメージがpullできないようです(docker.io/library/nginx みたいに)

Discussion