k8s/環境構築

kubernetes1.34クラスター構築

YOOANT 2026. 4. 23. 20:17

▪️kubernetes 1.34(containerd,calico)

# RHEL 9 / Rocky Linux 9 専用 Kubernetes 1.34 ノードセットアップスクリプト

# 1. 環境変数の設定
KUBERNETES_VERSION="v1.34"
CRICTL_VERSION="v1.35.0"
K8S_PKG_VERSION="1.34.0"

echo "==== 1. セキュリティおよびファイアウォールの緩和 (RHELの核心) ===="
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
sudo setenforce 0 || true # すでに permissive 状態の場合はエラー無視
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
sudo systemctl disable --now firewalld || true

echo "==== 2. カーネルモジュールおよびネットワーク Sysctl 設定 ===="
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter

cat <<EOF | sudo 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
sudo sysctl --system

echo "==== 3. 必須パッケージのインストール (トラブル回避) ===="
# tar, socat, conntrack-tools, iproute-tc など、RHEL 最小インストール時に欠けている必須パッケージを含む
sudo dnf update -y
sudo dnf install -y dnf-plugins-core curl gnupg2 jq iproute tar socat conntrack-tools iproute-tc

echo "==== 4. Containerd ランタイムのインストールおよび Cgroup 設定 ===="
sudo dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo dnf install -y containerd.io

sudo mkdir -p /etc/containerd
sudo containerd config default | sudo tee /etc/containerd/config.toml
# Kubelet と Cgroup マネージャーを systemd に一致させる
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml

sudo systemctl enable --now containerd
# [重要] 設定変更後、Cgroup の変更をメモリに反映させるため必ず再起動
sudo systemctl restart containerd

echo "==== 5. crictl のインストール ===="
ARCH="$(uname -m)"
CRICTL_ARCH=$([[ "$ARCH" == "x86_64" ]] && echo "amd64" || echo "arm64")
curl -LO "https://github.com/kubernetes-sigs/cri-tools/releases/download/${CRICTL_VERSION}/crictl-${CRICTL_VERSION}-linux-${CRICTL_ARCH}.tar.gz"
sudo tar zxvf "crictl-${CRICTL_VERSION}-linux-${CRICTL_ARCH}.tar.gz" -C /usr/local/bin
rm -f "crictl-${CRICTL_VERSION}-linux-${CRICTL_ARCH}.tar.gz"

cat <<EOF | sudo tee /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF

echo "==== 6. Kubernetes 公式リポジトリの登録およびパッケージインストール ===="
cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/$KUBERNETES_VERSION/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/$KUBERNETES_VERSION/rpm/repodata/repomd.xml.key
exclude=kubelet kubeadm kubectl cri-tools kubernetes-cni
EOF

sudo dnf makecache
# RPM ビルド番号の不一致を避けるためワイルドカード(*)を使用
sudo dnf install -y kubelet-$K8S_PKG_VERSION* kubeadm-$K8S_PKG_VERSION* kubectl-$K8S_PKG_VERSION* --disableexcludes=kubernetes
sudo systemctl enable --now kubelet

echo "==== 7. Kubelet Node IP 設定 (インターフェース自動検知) ===="
# 外部通信に使用される実際のインターフェースとIPを自動抽出
INTERFACE=$(ip route get 8.8.8.8 | awk '{print $5}' | head -n 1)
LOCAL_IP=$(ip -o -4 addr show $INTERFACE | awk '{print $4}' | cut -d/ -f1 | head -n 1)

cat << EOF | sudo tee /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS=--node-ip=$LOCAL_IP
EOF
sudo systemctl restart kubelet

# Kubernetes の証明書情報が表示されたら、ディレクトリを作成してコピーする。

# CNI(Calico)
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.31.3/manifests/tigera-operator.yaml
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.31.3/manifests/custom-resources.yaml

# Joinkeyが出力されたら、workernodeに設定する。
sudo kubeadm init --pod-network-cidr=192.168.0.0/16

 

# 1. EPEL 레포지토리 활성화 sudo dnf install epel-release -y

# 2. bat 설치 sudo dnf install bat -y

# 1. EPEL 레포지토리 활성화 
sudo dnf install epel-release -y
# 2. bat 설치 
sudo dnf install bat -y
# 3. ~/.bashrc 파일 끝에 추가
alias cat='bat --paging=never'
alias b='bat'

'k8s > 環境構築' 카테고리의 다른 글

k8s環境構築(MacOS)  (0) 2026.04.13

日本語