KubeKeyによるKubernetesクラスタとKubeSphereの導入ガイド

はじめに

本記事では、CentOS 7.6環境においてKubernetes v1.22.17とKubeSphere v3.3.0を対象とした導入方法を解説します。 KubeSphereはKubernetesを基盤とした分散オペレーティングシステムを目指しており、サードパーティ製アプリケーションやクラウドネイティブコンポーネントを容易に統合できるアーキテクチャを提供します。 KubeKeyツールを使用することで、Linuxサーバー上にKubernetesクラスタとKubeSphereを同時に展開するか、既存のKubernetesクラスタにKubeSphereを後から追加することが可能です。 KubeSphereは主にKubernetesリソースの視覚的管理やDevOps機能を提供するGUIツールとして機能します。 KubeKeyで構築されたクラスタでは、etcdやkubeletはsystemdで管理され、kube-apiserver、kube-controller-manager、kube-schedulerといったマスターコンポーネントは静的Podとして起動します。

KubeKey(kk)とは

KubeSphere公式サイト:https://www.kubesphere.io/zh/
KubeKeyはGo言語で開発された新しいインストールツールであり、従来のAnsibleベースのインストーラに代わるものです。 KubeKeyは柔軟なインストールオプションを提供し、KubeSphereとKubernetesを個別に、または同時にインストールできます。 KubeSphereのインストールにはデフォルトのストレージクラスが必要です。 デフォルトのストレージクラスが存在しない場合、KubeSphereは自動的にOpenEBSストレージ(本質的にはhostPathタイプ)を作成します。

シングルノードでのKubeSphere導入(All-in-One)

単一サーバー上にKubeSphereとKubernetesを迅速に展開します。

KubernetesとKubeSphereの同時展開

# 基本的な依存関係のインストールと設定
yum install socat conntrack ebtables ipset -y
yum install vim lsof net-tools zip unzip tree wget curl bash-completion pciutils gcc make lrzsz tcpdump bind-utils -y
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config 
setenforce 0
echo "SELinux無効化確認:"; getenforce && grep 'SELINUX=disabled' /etc/selinux/config
systemctl stop firewalld.service && systemctl disable firewalld.service
echo "ファイアウォール無効化確認:"; systemctl status firewalld.service | grep -E 'Active|disabled'
sed -ri 's/.*swap.*/#&/' /etc/fstab
swapoff -a
echo "スワップ無効化確認:"; grep -i 'swap' /etc/fstab; free -h | grep -i 'swap'
# Dockerは手動でインストールすることも可能(未インストールの場合はKubeKeyが自動インストール)

# KubeKey v3.0.7のダウンロード
export KKZONE=cn
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.7 sh -
chmod +x kk
# 上記が失敗した場合の代替手段
wget https://kubernetes.pek3b.qingstor.com/kubekey/releases/download/v3.0.7/kubekey-v3.0.7-linux-amd64.tar.gz
tar xf kubekey-v3.0.7-linux-amd64.tar.gz && chmod a+x kk

# サポートされているKubernetesバージョンの確認
./kk version --show-supported-k8s

# クラスタ作成(単一ノード)
./kk create cluster --with-kubernetes v1.22.17 --with-kubesphere v3.3.0

# Podの状態確認
kubectl get pod --all-namespaces
# KubeSphereインストールログの確認
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f

# kubectlの自動補完設定
yum -y install bash-completion
echo 'source <(kubectl completion bash)' >>~/.bashrc
kubectl completion bash >/etc/bash_completion.d/kubectl

# ログイン情報(インストール出力に従う)
# Console: http://192.168.xx.xx:30880
# Account: admin
# Password: P@88w0rd

マルチノード展開

最低3台のサーバーを準備します。

設定ファイルを用いたクラスタ作成

# 基本的な依存関係のインストールと設定(シングルノードと同様)
# ...(省略)...

# KubeKeyのダウンロード(シングルノードと同様)
# ...(省略)...

# 設定ファイルの生成
./kk create config --with-kubernetes v1.22.17 --with-kubesphere v3.3.0 -f config.yaml

# 設定ファイルの編集
vim config.yaml
# ホスト情報、ノードロール、外部etcdクラスタの設定など
# etcd:
#   type: external
#   external:
#     endpoints:
#     - https://192.168.566.4:2379
#     - https://192.168.566.5:2379
#     - https://192.168.566.6:2379
#     caFile: /etc/etcd/pki/ca.pem
#     certFile: /etc/etcd/pki/server.pem
#     keyFile: /etc/etcd/pki/server-key.pem

# 設定ファイルを使用したクラスタ作成
./kk create cluster -f config.yaml

# インストール後の確認(シングルノードと同様)
kubectl get pod --all-namespaces
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f

オフライン環境でのKubernetes v1.22.17とKubeSphere v3.3.2の導入

オンライン環境での準備

# KubeKeyのダウンロード
export KKZONE=cn
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.7 sh -
chmod +x kk

# マニフェストファイルの作成 (manifest.yaml)
cat > manifest.yaml<<'EOF'
apiVersion: kubekey.kubesphere.io/v1alpha2
kind: Manifest
metadata:
  name: k8s
spec:
  arches:
  - amd64
  operatingSystems:
  - arch: amd64
    type: linux
    id: centos
    version: "7"
    repository:
      iso:
        localPath:
        url: https://github.com/kubesphere/kubekey/releases/download/v3.0.7/centos7-rpms-amd64.iso
  kubernetesDistributions:
  - type: kubernetes
    version: v1.22.17
  components:
    helm:
      version: v3.9.0
    cni:
      version: v0.9.1
    etcd:
      version: v3.4.13
    containerRuntimes:
    - type: docker
      version: 20.10.8
    crictl:
      version: v1.24.0
    docker-registry:
      version: "2"
    harbor:
      version: v2.5.3
    docker-compose:
      version: v2.2.2
  images:
  # (多数のイメージリストが続く。スペースの都合上、実際の使用時にはマニフェストに含めること)
  # 例: registry.cn-beijing.aliyuncs.com/kubesphereio/kube-apiserver:v1.22.17
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-apiserver:v1.22.17
  # ...(その他必要なイメージ)
EOF

# アーティファクト(オフラインパッケージ)のエクスポート
export KKZONE=cn
./kk artifact export -m manifest.yaml -o kubesphere.tar.gz

オフラインサーバーでの展開

# KubeKeyファイルとkubesphere.tar.gzをオフラインサーバーに転送

# 設定ファイルの作成
./kk create config --with-kubesphere v3.3.2 --with-kubernetes v1.22.17 -f config.yaml

# 設定ファイルの編集(レジストリ設定を追加)
vim config.yaml
# roleGroups:
#   registry:
#   - ks1
# registry:
#   type: harbor
#   privateRegistry: ""
#   namespaceOverride: ""
#   registryMirrors: []
#   insecureRegistries: []

# イメージレジストリの初期化(Harborのインストール)
./kk init registry -f config.yaml -a kubesphere.tar.gz

# Harborプロジェクト作成スクリプトのダウンロードと実行
curl -O https://raw.githubusercontent.com/kubesphere/ks-installer/master/scripts/create_project_harbor.sh
# スクリプトを編集:プロジェクト名'kubesphereio'とURL'https://dockerhub.kubekey.local'を設定
chmod +x create_project_harbor.sh
./create_project_harbor.sh

# 設定ファイルを再度編集(レジストリ認証情報を追加)
vim config.yaml
# registry:
#   type: harbor
#   auths:
#     "dockerhub.kubekey.local":
#       username: admin
#       password: Harbor12345
#   privateRegistry: "dockerhub.kubekey.local"
#   namespaceOverride: "kubesphereio"
#   registryMirrors: []
#   insecureRegistries: []
# addons: []

# クラスタ作成(アーティファクトを使用)
./kk create cluster -f config.yaml -a kubesphere.tar.gz --with-packages

# インストール後の確認
kubectl get pod --all-namespaces

既存のKubernetesクラスタへのKubeSphere v3.3.0インストール(オンライン)

# 前提条件:
# - Kubernetes v1.20.x, v1.21.x, v1.22.x, v1.23.x, v1.24.x, v1.25.x, v1.26.x
#   (v1.20.xを除き、エッジノード機能に制限がある場合あり)
# - 最低ハードウェア要件: CPU 1コア以上、メモリ2GB以上
# - デフォルトのストレージクラスが設定済みであること

# KubeSphereインストーラのダウンロードと適用
wget -c https://github.com/kubesphere/ks-installer/releases/download/v3.3.0/kubesphere-installer.yaml
kubectl apply -f kubesphere-installer.yaml

wget -c https://github.com/kubesphere/ks-installer/releases/download/v3.3.0/cluster-configuration.yaml
# 必要に応じてcluster-configuration.yamlを編集
kubectl apply -f cluster-configuration.yaml

# インストールログの確認
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f

# 利用開始: admin/P@88w0rdでコンソール(ポート30880)にアクセス

既存のKubernetesクラスタへのKubeSphere v3.3.0インストール(オフライン)

# 1. オフライン環境にDockerレジストリまたはHarborを準備
# 2. オンライン環境でイメージリストとダウンロードツールを取得
wget https://github.com/kubesphere/ks-installer/releases/download/v3.3.0/images-list.txt
curl -L -O https://github.com/kubesphere/ks-installer/releases/download/v3.3.0/offline-installation-tool.sh
chmod +x offline-installation-tool.sh

# 3. イメージをダウンロード
./offline-installation-tool.sh -s -l images-list.txt -d ./kubesphere-images

# 4. KubeSphereインストーラファイルをダウンロード
curl -L -O https://github.com/kubesphere/ks-installer/releases/download/v3.3.0/cluster-configuration.yaml
curl -L -O https://github.com/kubesphere/ks-installer/releases/download/v3.3.0/kubesphere-installer.yaml

# 5. 設定ファイル(cluster-configuration.yaml)を編集し、ローカルレジストリを指定
# spec.local_registry: dockerhub.kubekey.local

# 6. イメージをローカルレジストリにプッシュ
./offline-installation-tool.sh -l images-list.txt -d ./kubesphere-images -r dockerhub.kubekey.local

# 7. インストーラを適用
kubectl apply -f kubesphere-installer.yaml
kubectl apply -f cluster-configuration.yaml

KubeSphereのアンインストール

# アンインストールスクリプトのダウンロードと実行
wget -c https://raw.githubusercontent.com/kubesphere/ks-installer/release-3.1/scripts/kubesphere-delete.sh
bash kubesphere-delete.sh

ノードの追加

# ワーカーノードの追加
# 既存クラスタから設定ファイルを生成
./kk create config --from-cluster
vim sample.yaml  # 新しいノード情報をhostsとroleGroupsに追加
./kk add nodes -f sample.yaml

# マスターノードの追加(高可用性)
# 同様に設定ファイルを生成・編集し、新しいマスターノードとロードバランサーの情報を追加
./kk add nodes -f sample.yaml

ノードの削除

# 設定ファイルを生成(持っていない場合)
./kk create config --from-cluster
# ノードを削除
./kk delete node <nodeName> -f sample.yaml

KubeKeyコマンド一覧

./kk --help
# 主なコマンド:
# add         ノード追加
# alpha       アルファ機能
# artifact    オフラインパッケージ管理
# certs       証明書管理
# completion  シェル補完スクリプト生成
# create      クラスタまたは設定ファイル作成
# delete      ノードまたはクラスタ削除
# init        環境初期化
# upgrade     クラスタアップグレード
# version     バージョン表示

# 使用例:
./kk create cluster --with-kubernetes v1.22.17 --with-kubesphere v3.3.0
./kk create config --with-kubernetes v1.22.17 -f config.yaml
./kk create cluster -f config.yaml
./kk delete node ks2 -f config.yaml
./kk delete cluster

タグ: KubeKey KubeSphere Kubernetes クラスタ構築 CentOS

6月17日 18:22 投稿