9
11

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 5 years have passed since last update.

MinikubeでオールインワンK8sクラスタ環境の構築

Last updated at Posted at 2019-03-19

OpenShiftばかり触ってて実はKubernetesの環境を持っていないので、Minishiftと同じように、K8sのオールインワンのmini環境を構築できるMinikubeを使って最小構成クラスタを構築してみる。

基本はInstall Minikube - Kubernetesの通りに作業すれば特に問題なく構築できる。

何番煎じかはわからない←

環境

項目 内容
ホストOS Windows 10 Home
ハイパーバイザ VirtualBox 5.2.26
Minikube v0.35.0
kubectl v1.13.0

インストール

Windows 10 Homeでの構築なので、別途VirtualBoxを導入しておく。(Win10Proであれば、Hyper-Vで動くはず)

Minikubeのインストール

Windows版はインストーラもあるけど、ひとまずマニュアルインストールを試す。

Install Minikube / Windows manual installationから、minikube-windows-amd64のWindows版exeファイルをダウンロードする(記事作成時点でv0.35.0)

ダウンロードしたらminikube-windows-amd64.exeminikube.exeにリネームし、パスの通ったディレクトリへ配置する。

image.png

image.png

実行確認

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\zaki> minikube version
minikube version: v0.35.0
PS C:\Users\zaki>

kubectlのインストール

(Minishiftと違って)Minikube環境ではCLIツールであるkubectlコマンドは同梱されていないらしいので、別途セットアップする。

Install and Set Up kubectl - Kubernetes

PowerShellやChocolateyを使った手順も載っているが、もっとも単純な、実行バイナリを直接ダウンロードする手順で取得する。

PowerShellで以下実行 (コマンドの意味はこちら)

PS C:\local\kubectl-v1.13.0-amd64> Invoke-WebRequest https://storage.googleapis.com/kubernetes-release/release/v1.13.0/bin/windows/amd64/kubectl.exe -OutFile kubectl.exe

image.png

パスを追加

image.png

実行確認

PS C:\Users\zaki> kubectl version
Client Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.0", GitCommit:"ddf47ac13c1a9483ea035a79cd7c10005ff21a6d", GitTreeState:"clean", BuildDate:"2018-12-03T21:04:45Z", GoVersion:"go1.11.2", Compiler:"gc", Platform:"windows/amd64"}
Unable to connect to the server: dial tcp 192.168.99.103:8443: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
PS C:\Users\zaki>

※ 上記はMinishiftがすでにインストール済みの環境であり、設定が共通($HOME/.kubeを見ている)のため、現在稼働していない192.168.99.103を見ようとしてエラーになっている)

Kubernetesクラスタの起動

minikube startで起動(初回はVM作成)が行われる。
ただし、ハイパーバイザのデフォルトがHyper-V…と思ったけど、Minishiftと違って最初からVirtualBoxっぽい。

明示的に指定するには--vm-driverオプションでvirtualboxを指定する。

詳細はminishift start --helpで確認できる。

PS C:\Users\zaki> minikube start --help
Starts a local kubernetes cluster using VM. This command
assumes you have already installed one of the VM drivers: virtualbox/parallels/vmwarefusion/kvm/xhyve/hyperv.

[...]

      --vm-driver string                  VM driver is one of: [virtualbox parallels vmwarefusion kvm xhyve hyperv hyperkit kvm2 vmware none] (default "virtualbox")

というわけで起動。

PS C:\Users\zaki> minikube start --vm-driver virtualbox
o   minikube v0.35.0 on windows (amd64)
>   Creating virtualbox VM (CPUs=2, Memory=2048MB, Disk=20000MB) ...
@   Downloading Minikube ISO ...
 20.94 MB / 184.42 MB [====>---------------------------------------]  11.35% 17s

初回のVM作成処理が始まると、VirtualBox上にクラスタ用のVMが作成される

image.png

PS C:\Users\zaki> minikube start --vm-driver virtualbox
o   minikube v0.35.0 on windows (amd64)
>   Creating virtualbox VM (CPUs=2, Memory=2048MB, Disk=20000MB) ...
@   Downloading Minikube ISO ...
 184.42 MB / 184.42 MB [============================================] 100.00% 0s
-   "minikube" IP address is 192.168.99.100
-   Configuring Docker as the container runtime ...
-   Preparing Kubernetes environment ...
@   Downloading kubelet v1.13.4
@   Downloading kubeadm v1.13.4
-   Pulling images required by Kubernetes v1.13.4 ...
-   Launching Kubernetes v1.13.4 using kubeadm ...
:   Waiting for pods: apiserver proxy etcd scheduler controller addon-manager dns
-   Configuring cluster permissions ...
-   Verifying component health .....
+   kubectl is now configured to use "minikube"
=   Done! Thank you for using minikube!
PS C:\Users\zaki>

Minishiftより簡素なのか、起動も早い…

起動完了したら、kubectlの出番

バージョン

PS C:\Users\zaki> kubectl version
Client Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.0", GitCommit:"ddf47ac13c1a9483ea035a79cd7c10005ff21a6d", GitTreeState:"clean", BuildDate:"2018-12-03T21:04:45Z", GoVersion:"go1.11.2", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.4", GitCommit:"c27b913fddd1a6c480c229191a087698aa92f0b1", GitTreeState:"clean", BuildDate:"2019-02-28T13:30:26Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"}
PS C:\Users\zaki>

さっきは接続エラーになっていたサーバに接続できて、クラスタの状態を取得できている

クラスタ情報

PS C:\Users\zaki> kubectl cluster-info
Kubernetes master is running at https://192.168.99.100:8443
KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
PS C:\Users\zaki>

Kubernatesクラスタの停止

stopを引数に実行。VMが停止する。

PS C:\Users\zaki> minikube stop
:   Stopping "minikube" in virtualbox ...
-   "minikube" stopped.
PS C:\Users\zaki>

インストール直後のクラスタの状態

node

PS C:\Users\zaki> kubectl get node
NAME       STATUS   ROLES    AGE     VERSION
minikube   Ready    master   5m33s   v1.13.4

namespace

PS C:\Users\zaki> kubectl get namespace
NAME          STATUS   AGE
default       Active   32m
kube-public   Active   31m
kube-system   Active   32m

oc project <namespace>に相当するコマンドはないっぽい。。

all

PS C:\Users\zaki> kubectl get all --all-namespaces -o wide
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE    IP           NODE       NOMINATED NODE   READINESS GATES
kube-system   pod/coredns-86c58d9df4-b8p67               1/1     Running   0          19m    172.17.0.2   minikube   <none>           <none>
kube-system   pod/coredns-86c58d9df4-fzfx4               1/1     Running   0          19m    172.17.0.3   minikube   <none>           <none>
kube-system   pod/etcd-minikube                          1/1     Running   0          18m    10.0.2.15    minikube   <none>           <none>
kube-system   pod/kube-addon-manager-minikube            1/1     Running   0          18m    10.0.2.15    minikube   <none>           <none>
kube-system   pod/kube-apiserver-minikube                1/1     Running   0          18m    10.0.2.15    minikube   <none>           <none>
kube-system   pod/kube-controller-manager-minikube       1/1     Running   0          18m    10.0.2.15    minikube   <none>           <none>
kube-system   pod/kube-proxy-6647b                       1/1     Running   0          19m    10.0.2.15    minikube   <none>           <none>
kube-system   pod/kube-scheduler-minikube                1/1     Running   0          18m    10.0.2.15    minikube   <none>           <none>
kube-system   pod/kubernetes-dashboard-ccc79bfc9-ksl8g   1/1     Running   0          3m3s   172.17.0.4   minikube   <none>           <none>
kube-system   pod/storage-provisioner                    1/1     Running   0          19m    10.0.2.15    minikube   <none>           <none>

NAMESPACE     NAME                           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE    SELECTOR
default       service/kubernetes             ClusterIP   10.96.0.1       <none>        443/TCP         19m    <none>
kube-system   service/kube-dns               ClusterIP   10.96.0.10      <none>        53/UDP,53/TCP   19m    k8s-app=kube-dns
kube-system   service/kubernetes-dashboard   ClusterIP   10.109.210.17   <none>        80/TCP          3m3s   app=kubernetes-dashboard

NAMESPACE     NAME                        DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE   CONTAINERS   IMAGES                          SELECTOR
kube-system   daemonset.apps/kube-proxy   1         1         1       1            1           <none>          19m   kube-proxy   k8s.gcr.io/kube-proxy:v1.13.4   k8s-app=kube-proxy

NAMESPACE     NAME                                   READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS             IMAGES                                          SELECTOR
kube-system   deployment.apps/coredns                2/2     2            2           19m    coredns                k8s.gcr.io/coredns:1.2.6                        k8s-app=kube-dns
kube-system   deployment.apps/kubernetes-dashboard   1/1     1            1           3m3s   kubernetes-dashboard   k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1   addonmanager.kubernetes.io/mode=Reconcile,app=kubernetes-dashboard,version=v1.10.1

NAMESPACE     NAME                                             DESIRED   CURRENT   READY   AGE    CONTAINERS             IMAGES                                          SELECTOR
kube-system   replicaset.apps/coredns-86c58d9df4               2         2         2       19m    coredns                k8s.gcr.io/coredns:1.2.6                        k8s-app=kube-dns,pod-template-hash=86c58d9df4
kube-system   replicaset.apps/kubernetes-dashboard-ccc79bfc9   1         1         1       3m3s   kubernetes-dashboard   k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1   addonmanager.kubernetes.io/mode=Reconcile,app=kubernetes-dashboard,pod-template-hash=ccc79bfc9,version=v1.10.1
PS C:\Users\zaki>

dashboard

minikube dashboardを実行することで起動するので、ブラウザでアクセスすれば見れる。

PS C:\Users\zaki> minikube dashboard
-   Enabling dashboard ...
-   Verifying dashboard health ...
-   Launching proxy ...
-   Verifying proxy health ...
-   Opening http://127.0.0.1:52301/api/v1/namespaces/kube-system/services/http:kubernetes-dashboard:/proxy/ in your default browser...

image.png

echoserverチュートリアル

Install Minikube - Kubernetesでの構築が終わったら、最後に What's next という項目があるので、その内容をいくつかやってみる

Running Kubernetes Locally via Minikube - Kubernetes

Podの実行

see: Quickstart

kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.10 --port=8080

PS C:\Users\zaki> kubectl run hello-minikube --image=k8s.gcr.io/echoserver:1.10 --port=8080
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/hello-minikube created
PS C:\Users\zaki>
PS C:\Users\zaki> kubectl get all
NAME                                  READY   STATUS              RESTARTS   AGE
pod/hello-minikube-6fd785d459-2xq6j   0/1     ContainerCreating   0          14s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   35m

NAME                             READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-minikube   0/1     1            0           14s

NAME                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-minikube-6fd785d459   1         1         0       14s

hello-minikubeという名前で、k8s.gcr.io/echoserver:1.10イメージベースでDeployment/ReplicaSet/Podが作成された。
(上のログのPodの状態はREADYの値が0/1になっており、STATUSContainerCreating(コンテナ作成中)となっていて、起動処理中の状態)

時間がたてば

PS C:\Users\zaki> kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
hello-minikube-6fd785d459-2xq6j   1/1     Running   0          5m25s
PS C:\Users\zaki>

Runningになった (Podが実行中状態)

Service作成

PS C:\Users\zaki> kubectl expose deployment hello-minikube --type=NodePort
service/hello-minikube exposed
PS C:\Users\zaki> kubectl get svc
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
hello-minikube   NodePort    10.108.176.189   <none>        8080:30198/TCP   7s
kubernetes       ClusterIP   10.96.0.1        <none>        443/TCP          37m

Serviceが作成され、外部からアクセスする準備ができた。

アクセスのためのURLを確認するにはminikube service listを実行すると一覧が見れる

PS C:\Users\zaki> minikube service list
|-------------|----------------------|-----------------------------|
|  NAMESPACE  |         NAME         |             URL             |
|-------------|----------------------|-----------------------------|
| default     | hello-minikube       | http://192.168.99.100:30198 |
| default     | kubernetes           | No node port                |
| kube-system | kube-dns             | No node port                |
| kube-system | kubernetes-dashboard | No node port                |
|-------------|----------------------|-----------------------------|
PS C:\Users\zaki>

http://192.168.99.100:30198にアクセスすれば、Podで動作しているechoserverにアクセスできることがわかる。

PS C:\Users\zaki> $resp =Invoke-WebRequest http://192.168.99.100:30198
PS C:\Users\zaki> $resp.Content


Hostname: hello-minikube-6fd785d459-2xq6j

Pod Information:
        -no pod information available-

Server values:
        server_version=nginx: 1.13.3 - lua: 10008

Request Information:
        client_address=172.17.0.1
        method=GET
        real path=/
        query=
        request_version=1.1
        request_scheme=http
        request_uri=http://192.168.99.100:8080/

Request Headers:
        host=192.168.99.100:30198
        user-agent=Mozilla/5.0 (Windows NT; Windows NT 10.0; ja-JP) WindowsPowerShell/5.1.17763.316

Request Body:
        -no body in request-


PS C:\Users\zaki>

dashboard

echoserverのPodが動作している状態でdashboardを見ると、こんな感じ

image.png

各リソースの詳細(おまけのメモ)

Deployment

PS C:\Users\zaki> kubectl get deploy hello-minikube -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2019-03-19T13:26:08Z"
  generation: 1
  labels:
    run: hello-minikube
  name: hello-minikube
  namespace: default
  resourceVersion: "2948"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/hello-minikube
  uid: 8e6ef4dd-4a4a-11e9-9d31-080027b9884e
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      run: hello-minikube
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: hello-minikube
    spec:
      containers:
      - image: k8s.gcr.io/echoserver:1.10
        imagePullPolicy: IfNotPresent
        name: hello-minikube
        ports:
        - containerPort: 8080
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2019-03-19T13:26:32Z"
    lastUpdateTime: "2019-03-19T13:26:32Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2019-03-19T13:26:08Z"
    lastUpdateTime: "2019-03-19T13:26:32Z"
    message: ReplicaSet "hello-minikube-6fd785d459" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

ReplicaSet

PS C:\Users\zaki> kubectl get rs hello-minikube-6fd785d459 -o yaml
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
  annotations:
    deployment.kubernetes.io/desired-replicas: "1"
    deployment.kubernetes.io/max-replicas: "2"
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2019-03-19T13:26:08Z"
  generation: 1
  labels:
    pod-template-hash: 6fd785d459
    run: hello-minikube
  name: hello-minikube-6fd785d459
  namespace: default
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: Deployment
    name: hello-minikube
    uid: 8e6ef4dd-4a4a-11e9-9d31-080027b9884e
  resourceVersion: "2947"
  selfLink: /apis/extensions/v1beta1/namespaces/default/replicasets/hello-minikube-6fd785d459
  uid: 8e71147d-4a4a-11e9-9d31-080027b9884e
spec:
  replicas: 1
  selector:
    matchLabels:
      pod-template-hash: 6fd785d459
      run: hello-minikube
  template:
    metadata:
      creationTimestamp: null
      labels:
        pod-template-hash: 6fd785d459
        run: hello-minikube
    spec:
      containers:
      - image: k8s.gcr.io/echoserver:1.10
        imagePullPolicy: IfNotPresent
        name: hello-minikube
        ports:
        - containerPort: 8080
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  fullyLabeledReplicas: 1
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1

Pod

PS C:\Users\zaki> kubectl get pod hello-minikube-6fd785d459-2xq6j -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2019-03-19T13:26:08Z"
  generateName: hello-minikube-6fd785d459-
  labels:
    pod-template-hash: 6fd785d459
    run: hello-minikube
  name: hello-minikube-6fd785d459-2xq6j
  namespace: default
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: hello-minikube-6fd785d459
    uid: 8e71147d-4a4a-11e9-9d31-080027b9884e
  resourceVersion: "2946"
  selfLink: /api/v1/namespaces/default/pods/hello-minikube-6fd785d459-2xq6j
  uid: 8e743dd5-4a4a-11e9-9d31-080027b9884e
spec:
  containers:
  - image: k8s.gcr.io/echoserver:1.10
    imagePullPolicy: IfNotPresent
    name: hello-minikube
    ports:
    - containerPort: 8080
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-qr2gn
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: minikube
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-qr2gn
    secret:
      defaultMode: 420
      secretName: default-token-qr2gn
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2019-03-19T13:26:08Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2019-03-19T13:26:32Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2019-03-19T13:26:32Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2019-03-19T13:26:08Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://1bf516296ef57c298d753510d7edd54c9c009055a5d89f7475ddfb12e298905d
    image: k8s.gcr.io/echoserver:1.10
    imageID: docker-pullable://k8s.gcr.io/echoserver@sha256:cb5c1bddd1b5665e1867a7fa1b5fa843a47ee433bbb75d4293888b71def53229
    lastState: {}
    name: hello-minikube
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: "2019-03-19T13:26:32Z"
  hostIP: 10.0.2.15
  phase: Running
  podIP: 172.17.0.5
  qosClass: BestEffort
  startTime: "2019-03-19T13:26:08Z"
9
11
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
9
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?