MinikubeでKubernetesを試す

まずは Vagrant でイメージを作成します。

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "bento/ubuntu-19.10"
  config.vm.provider "virtualbox" do |vb|
    vb.cpus = "2"
  end
  config.vm.network "private_network", ip: "192.168.33.100"

  config.vm.provision "shell", inline: <<-SHELL
    curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.9.0/bin/linux/amd64/kubectl
    chmod +x ./kubectl
    mv ./kubectl /usr/local/bin/kubectl

    curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.24.1/minikube-linux-amd64
    chmod +x minikube
    mv minikube /usr/local/bin/

    minikube version
    kubectl version

    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
    apt-get install docker-ce docker-ce-cli containerd.io -y

    ufw disable
  SHELL
end

起動します。

vagrant up
vagrant ssh

イメージ内で起動するプロセスを作ります。

git clone https://github.com/kubernetes-up-and-running/kuard.git
cd kuard
sudo make
sed -ie 's/FROM ARG_FROM/FROM alpine/' Dockerfile.kuard
sed -ie 's/ARG_FAKEVER\/ARG_ARCH/blue\/amd64/' Dockerfile.kuard
sudo docker build -t kuard-run:1 . -f Dockerfile.kuard
sudo docker run -d --name kuard  -p 8080:8080 kuard-run:1

ブラウザで http://192.168.33.100:8080/ を開いてみます。

f:id:taku-woohar:20200222211155p:plain
KUAR Demo

多分このような画面が出ると思います。

sudo docker stop kuard

で停止します。

Kubernetesを使います。

export CHANGE_MINIKUBE_NONE_USER=true
sudo -E minikube start --vm-driver=none

まず状態を確認します。

$ minikube status
minikube: Running
cluster: Running
kubectl: Correctly Configured: pointing to minikube-vm at 127.0.0.1

$ kubectl get node
NAME      STATUS    ROLES     AGE       VERSION
vagrant   Ready     <none>    15m       v1.8.0

$ kubectl get pods
No resources found.

Podを作ります。

$ kubectl run kuard --image=kuard-run:1 
deployment "kuard" created

$ kubectl get pods
NAME                     READY     STATUS    RESTARTS   AGE
kuard-86d4765c69-4crbk   1/1       Running   0          7m

とりあえず一旦削除します。

kubectl delete deployments/kuard

kuard.yml を用意します。

apiVersion: v1
kind: Pod
metadata:
  name: kuard
spec:
  containers:
  - name: kuard
    image: kuard-run:1
    ports:
      - containerPort: 8080
        name: http
        protocol: TCP

Podが作成されているのを確認します。

$ kubectl get pods
NAME      READY     STATUS    RESTARTS   AGE
kuard     1/1       Running   0          1m

$ kubectl describe  pods kuard
Name:         kuard
Namespace:    default
Node:         vagrant/10.0.2.15
Start Time:   Sat, 22 Feb 2020 13:19:44 +0000
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration={"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"kuard","namespace":"default"},"spec":{"containers":[{"image":"kuard-run:1","name":...
Status:       Running
IP:           172.17.0.4
Containers:
  kuard:
    Container ID:   docker://7b17419be3b56fb76e3539a0702782b9a7749252b214d859143064a7eb2aa1ab
    Image:          kuard-run:1
    Image ID:       docker://sha256:d4081f1dfbe6f426cd1ac49dbb16b2f073e5d18af7022e03fa2c674b77b7f7f4
    Port:           8080/TCP
    State:          Running
      Started:      Sat, 22 Feb 2020 13:19:45 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-hkpw5 (ro)
Conditions:
  Type           Status
  Initialized    True 
  Ready          True 
  PodScheduled   True 
Volumes:
  default-token-hkpw5:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-hkpw5
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     <none>
Events:
  Type    Reason                 Age   From               Message
  ----    ------                 ----  ----               -------
  Normal  Scheduled              1m    default-scheduler  Successfully assigned kuard to vagrant
  Normal  SuccessfulMountVolume  1m    kubelet, vagrant   MountVolume.SetUp succeeded for volume "default-token-hkpw5"
  Normal  Pulled                 1m    kubelet, vagrant   Container image "kuard-run:1" already present on machine
  Normal  Created                1m    kubelet, vagrant   Created container
  Normal  Started                1m    kubelet, vagrant   Started container


$ kubectl logs kuard
2020/02/22 13:19:45 Starting kuard version: v0.10.0-blue

Podを削除します。

$ kubectl delete pods/kuard
pod "kuard" deleted

以上です。