LoginSignup
1
1

More than 3 years have passed since last update.

Kindを使ったkubernetesテスト

Last updated at Posted at 2019-08-29

Kindとは

Screen Shot 2019-08-28 at 20.09.04.png

Kind(Kubernetes IN Docker)
とはその名の通り、Kubernetes クラスターを Docker 上で動かすツールです。

使用理由

gitlabをscript管理に使っており、その際にCIツールとしてgitlab-runnerを使っています。
その中で、docker buildでイメージを作成し、それをkubernetes上で構築してテストをしたいと考えたときに、テスト環境用として使おうと思い使いはじめました。

prerequisite of Kind

dockerが使える状態であること
Go言語使用可能であること
kubectlが使える状態であること

dockerに関しては動いていること前提なので、下2つをdockerホストに用意する。

Goのインストール

Download

https://golang.org/dl/
こちらより、dockerが動いているサーバのOSにあったバイナリをダウンロードします。

ダウンロードが完了したら、解凍をします。
以下は、現時点(2019/08/28)のものです。

tar xvf go1.12.9.linux-amd64.tar.gz

配置とPath設定

# mv go /usr/local/

.bash_profileに以下を追記

export GOROOT=/usr/local/go
export GOPATH=/usr/local/go/bin
export PATH=$PATH:$GOROOT/bin

Kubectlのインストール

Download

https://kubernetes.io/docs/tasks/tools/install-kubectl/
ここより環境に合わせて、ダウンロードします。
今回はLinuxの例を見ていきます。

# curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/linux/amd64/kubectl

この後、権限設定をします。

# chmod +x ./kubectl

そして、/usr/local/bin/に配置します。

# mv ./kubectl /usr/local/bin/kubectl

Kindのダウンロード

$ GO111MODULE="on" go get sigs.k8s.io/kind@v0.4.0

pathを確認します。
goと同階層にあることを確認します。

# ls -ltr /usr/local/go/bin/
total 70248
-rwxr-xr-x 1 root root  3525802 Aug 15 18:50 gofmt
-rwxr-xr-x 1 root root 14613596 Aug 15 18:50 go
-rwxr-xr-x 1 root root 17422189 Aug 15 18:51 godoc
drwxr-xr-x 3 root root       17 Aug 28 06:34 pkg
-rwxr-xr-x 1 root root 36367031 Aug 28 07:01 kind

kindの構築

以下の構成ファイルを用いて、clusterを作成します。

cluster.yml
apiVersion: kind.sigs.k8s.io/v1alpha3
kind: Cluster
nodes:
  - role: control-plane
  - role: worker
  - role: worker
  - role: worker

できたら、以下のコマンドを打ちます。

# kind create cluster --config cluster.yml
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.15.0) 🖼
 ✓ Preparing nodes 📦📦📦📦
 ✓ Creating kubeadm config 📜
 ✓ Starting control-plane 🕹️
 ✓ Installing CNI 🔌
 ✓ Installing StorageClass 💾
 ✓ Joining worker nodes 🚜
Cluster creation complete. You can now use the cluster with:

export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
kubectl cluster-info

できたか確認してみましょう。

# export KUBECONFIG="$(kind get kubeconfig-path --name="kind")"
# kubectl cluster-info
Kubernetes master is running at https://127.0.0.1:39489
KubeDNS is running at https://127.0.0.1:39489/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

Nodeは少し時間を置くと、Readyになります。

# kubectl get nodes
NAME                 STATUS   ROLES    AGE     VERSION
kind-control-plane   Ready    master   3m48s   v1.15.0
kind-worker          Ready    <none>   3m9s    v1.15.0
kind-worker2         Ready    <none>   3m9s    v1.15.0
kind-worker3         Ready    <none>   3m9s    v1.15.0

以上で、終了となります。

Kindでのimageの取り扱い

以下のコマンドでローカルの Docker image を kind で作成した Kubernetes にアップロードできます。
これをしないと、ErrImagePullImagePullBackOffとなり続けます。

# kind load docker-image Image名:tag

アップロードした Docker image を使う際、 imagePullPolicy: IfNotPresent もしくは imagePullPolicy: Never をmanifestに記載します。

1
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
1
1