Otama's Playground > サーバー > Proxmox × cloud-initでKubernetes環境を自動構築|テンプレート化方法を紹介

Proxmox × cloud-initでKubernetes環境を自動構築|テンプレート化方法を紹介

最近、自分でKubernetesの環境を作り直すことがちょくちょくあって、「あれ?前回どうやってたっけ?」と毎回ログを漁るのが面倒になってきたので、もう少し楽に再現できるようにテンプレートっぽいものを作ってみました。

前回の記事(こちら)で書いた流れをベースにしていますが、今回は cloud-init 用の設定ファイルを作り込みつつ、少しだけ整理・分割してみています。毎回手動でやるのも良いけど、「再現性」と「忘れたときの自分のため」にも自動化しておくとちょっと安心ですね。

というわけで、今回の内容は Proxmox + cloud-init でKubernetesの環境をサクッと作るテンプレートを作った話 です。あくまで自分用のメモなので、CNIの設定とかはまだ入れてなかったりしますが、それでもかなり手間は減りました。

手順

大まかな手順は前の記事と同様です。cloud-initの設定ファイルだけ今回用にカスタマイズしていきます。

yamlの作成

以下記事の内容をcloud-initの設定ファイルに落とし込んでいきます。CNI回りはまだ色々確認中なので、テンプレートに含めていません。

フェーズ毎に作っていく

IPv4フォワーディングを有効化

YAML
#cloud-config
write_files:
  - path: /usr/local/bin/enable-ipv4-forwarding.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      cat <<EOF | tee /etc/modules-load.d/k8s.conf
      overlay
      br_netfilter
      EOF
      
      modprobe overlay
      modprobe br_netfilter
      
      cat <<EOF | tee /etc/sysctl.d/k8s.conf
      net.bridge.bridge-nf-call-iptables  = 1
      net.bridge.bridge-nf-call-ip6tables = 1
      net.ipv4.ip_forward                 = 1
      EOF
      
      sysctl --system
      
runcmd:
  - /usr/local/bin/enable-ipv4-forwarding.sh

コンテナランタイムのインストール、変数の置換がうまくいかないのでバージョンはいったん直指定にしてます

YAML
#cloud-config
packages:
  - software-properties-common
  - curl
  
write_files:
  - path: /usr/local/bin/install-crio.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      curl -fsSL https://download.opensuse.org/repositories/isv:/cri-o:/stable:/v1.32/deb/Release.key | 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:/v1.32/deb/ /" | tee /etc/apt/sources.list.d/cri-o.list

      apt-get update
      apt-get install -y cri-o
      
      systemctl start crio
      
runcmd:
  - /usr/local/bin/install-crio.sh

swapの無効化

YAML
#cloud-config
write_files:
  - path: /usr/local/bin/disable-swap.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
      
runcmd:
  - /usr/local/bin/disable-swap.sh

kubeadmのインストール

YAML
#cloud-config
write_files:
  - path: /usr/local/bin/install-kubernetes.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.33/deb/Release.key | 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.33/deb/ /" | tee /etc/apt/sources.list.d/kubernetes.list
          
      apt-get update
      apt-get install -y kubelet kubeadm kubectl
      apt-mark hold kubelet kubeadm kubectl
      
runcmd:
  - /usr/local/bin/install-kubernetes.sh

helmのインストール

YAML
#cloud-config
packages:
  - apt-transport-https

write_files:
  - path: /usr/local/bin/install-helm.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | tee /usr/share/keyrings/helm.gpg > /dev/null
      echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | tee /etc/apt/sources.list.d/helm-stable-debian.list
      
      apt-get update
      apt-get install helm
      
runcmd:
  - /usr/local/bin/install-helm.sh
1個のyamlに整理
YAML
#cloud-config
packages:
  - software-properties-common
  - curl
  - apt-transport-https

write_files:
  - path: /usr/local/bin/enable-ipv4-forwarding.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      cat <<EOF | tee /etc/modules-load.d/k8s.conf
      overlay
      br_netfilter
      EOF
      
      modprobe overlay
      modprobe br_netfilter
      
      cat <<EOF | tee /etc/sysctl.d/k8s.conf
      net.bridge.bridge-nf-call-iptables  = 1
      net.bridge.bridge-nf-call-ip6tables = 1
      net.ipv4.ip_forward                 = 1
      EOF
      
      sysctl --system
  - path: /usr/local/bin/install-crio.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      curl -fsSL https://download.opensuse.org/repositories/isv:/cri-o:/stable:/v1.32/deb/Release.key | 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:/v1.32/deb/ /" | tee /etc/apt/sources.list.d/cri-o.list

      apt-get update
      apt-get install -y cri-o
      
      systemctl start crio
  - path: /usr/local/bin/disable-swap.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
  - path: /usr/local/bin/install-kubernetes.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.33/deb/Release.key | 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.33/deb/ /" | tee /etc/apt/sources.list.d/kubernetes.list
          
      apt-get update
      apt-get install -y kubelet kubeadm kubectl
      apt-mark hold kubelet kubeadm kubectl
  - path: /usr/local/bin/install-helm.sh
    permissions: '0755'
    content: |
      #!/bin/bash
      curl https://baltocdn.com/helm/signing.asc | gpg --dearmor | tee /usr/share/keyrings/helm.gpg > /dev/null
      echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/helm.gpg] https://baltocdn.com/helm/stable/debian/ all main" | tee /etc/apt/sources.list.d/helm-stable-debian.list
      
      apt-get update
      apt-get install helm
      
runcmd:
  - /usr/local/bin/enable-ipv4-forwarding.sh
  - /usr/local/bin/install-crio.sh
  - /usr/local/bin/disable-swap.sh
  - /usr/local/bin/install-kubernetes.sh
  - /usr/local/bin/install-helm.sh

埋め込み

proxmoxのホストでsnippetにコピーしそれをVMに埋め込みます

Bash
# snippetsにコピー
cat << EOF | sudo tee /var/lib/vz/snippets/install-kubeadm.yaml
...作成したyamlをコピペ
EOF

# cloud-init設定ファイルの埋め込み
vmTemplateId = 9000
qm set ${vmTemplateId} --cicustom "vendor=local:snippets/install-kubeadm.yaml"

起動

起動してkubeadmとcri-oのインストール状況を確認

Bash
# Activeなら成功
systemctl status crio

# command not foundでなければ成功
kubeadm version

# command not foundでなければ成功
helm version

終わり

というわけで、今回のテンプレート化で環境構築の手間はだいぶ軽減されました。とはいえ、今のところすべての処理を runcmd で直列に実行しているため、パフォーマンスや柔軟性の面ではまだ改善の余地があります。

本当はこちらのように並列実行できるようにするともう少しスマートになるんですが、そこはまた気が向いたら、ということで(笑)
今回の設定ファイルはとりあえず「動くものを最短で」重視で作っているので、今後の拡張も視野に入れつつ育てていければいいなと思っています。

同じように「Kubernetes の環境を何度も作り直してるよ〜」という方の参考になれば幸いです。それでは、また。

返信を残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です