2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

プロキシあり環境で kindを使ってKubernetesクラスタを作成する。

2
Posted at

はじめに

kindとは、Dockerコンテナを用いてKubernetesクラスタをローカルの環境に作成するツールです。
この記事は Rocky Linux 8 minimalのインストール後の環境で、kindをインストールしてkubernetsクラスタの作成までの手順を示したものになります。

タイトルのとおりプロキシありの環境で試したので、所々でプロキシの設定手順がはさまりますが、不要であれば該当の手順は飛ばして実行していただければと思います。

手順

本手順は以下の環境で動作確認を行ったものになります。

対象 バージョン
OS Rocky Linux 8.5
kind 0.11.1
Docker 20.10.12
kubectl v1.23.3

プロファイルにproxyの設定を登録する

プロキシ環境下であれば、yumでプロキシを通すために以下の設定を追加しておく。

cat <<EOF >> /etc/profile
PROXY='proxy.url:port"
export http_proxy=$PROXY
export HTTP_PROXY=$PROXY
export https_proxy=$PROXY
export HTTPS_PROXY=$PROXY
EOF

kubectlをインストールする

kindインストール時にkubectlは必要となりませんが、kubernetesの運用で必須となるためこのタイミングでインストールしておきます。

Dockerの公式ドキュメントに沿って、手順を実施します。

cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubectl

Dockerをインストールする

Dockerの公式ドキュメントに沿って、手順を実施します。

# yum-utilsをインストールする
yum install -y yum-utils

# Dockerの取得先となるリポジトリを登録する
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

# Dockerをインストールする
yum install docker-ce docker-ce-cli containerd.io

Dockerのサービスファイルにプロキシ設定を追加する

# dockerのサービスファイルを作成する
systemctl enable docker

# サービスファイルに以下のコメントにあるようなプロキシの情報を追記する
vi /usr/lib/systemd/system/docker.service

#---------------------------------------
# [Service]
# Environment="HTTP_PROXY=http://proxy.url:port/" "HTTPS_PROXY=https://proxy.url:port/"
#----------------------------------------
# もしこれで通らない場合は HTTPSをhttpにすると通るかも "HTTPS_PROXY=http://proxy.url:port/"

# サービスファイルをリロードする
systemctl daemon-reload

# 以下のコマンドで登録したproxyの情報が取得できればOK
docker info | grep proxy

疎通確認

Dockerが正しく動作するか確認する。

# Docker起動
systemctl start docker

# 疎通用のコマンド
docker run hello-world
# Hello from Docker!が返ってくればOK。

kindをインストールする

kindのquick start手順どおりにインストールを行う。

curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64
chmod +x ./kind
mv ./kind /usr/bin/kind

動作確認としてクラスタを作成する

作成するだけであれば、このコマンドだけでクラスタを作成できます。

kind create cluster

実行時のログは以下の通りです。

# kind create cluster
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.21.1) 🖼 
 ✓ Preparing nodes 📦  
 ✓ Writing configuration 📜 
 ✓ Starting control-plane 🕹️ 
 ✓ Installing CNI 🔌 
 ✓ Installing StorageClass 💾 
Set kubectl context to "kind-kind"
You can now use your cluster with:

kubectl cluster-info --context kind-kind

Have a question, bug, or feature request? Let us know! https://kind.sigs.k8s.io/#community 🙂

クラスタ作成後にはコンテナが起動されていることを確認できます。

# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                       NAMES
3210094bf5a1   kindest/node:v1.21.1   "/usr/local/bin/entr…"   23 minutes ago   Up 23 minutes   127.0.0.1:37273->6443/tcp   kind-control-plane

デフォルトで動作させた場合はcontrol-planeのみが起動されます。

Kubernetes ではマスターノードである「control-plane」と、実際にコンテナが起動するワーカーノード「data-plane」で構成されます。
デフォルトの設定では、control-planeにはコンテナを作成しないようになっているので、実際に何かを動かす場合はワーカーノードを含めた形でクラスタを作成する必要があります。

参考:control-plane1台、data-palne2台で起動する場合の手順

参考までに3台構成でクラスタを作成する場合の手順も書きます。

# kindクラスタの設定ファイル作成
cat <<EOF > /tmp/config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
EOF

# 設定ファイルを用いたクラスタの作成
kind create cluster --config /tmp/config.yaml

この手順で実行すると以下のように3台構成でクラスタを作成されます。

# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED         STATUS         PORTS                       NAMES
a8453887439d   kindest/node:v1.21.1   "/usr/local/bin/entr…"   8 minutes ago   Up 8 minutes                               kind-worker
cf3f8a47c905   kindest/node:v1.21.1   "/usr/local/bin/entr…"   8 minutes ago   Up 8 minutes   127.0.0.1:33673->6443/tcp   kind-control-plane
ed2fb5d895fb   kindest/node:v1.21.1   "/usr/local/bin/entr…"   8 minutes ago   Up 8 minutes                               kind-worker2

クラスタ削除手順

最後に作成したクラスタを削除します。

kind delete cluster

さいごに

今回kindは初めて触ってみましたが、実に簡単にKubernetesクラスタの構築ができました。
開発用としてや、手元で何かを動かしてみるという用途であれば、kindを利用すれば楽に環境を汚すことなくできるので非常に便利なツールだと思います。

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?