tjtjtjのメモ

自分のためのメモです。誰かの役には立ちません。

さくらのナレッジ minikube

さくらのナレッジをやってみるシリーズ。今回は minikube

簡単にローカルKubernetes環境を構築できるツール「Minikube」 https://knowledge.sakura.ad.jp/15320/

準備

minikube はじめる

version 確認

> minikube version
minikube version: v0.31.0

minikube start でクラスタ作成。なんかエラー出てるが、please enjoy minikube! だそうです。

> minikube start
C:\Users\zun\.kube>minikube start
Starting local Kubernetes v1.10.0 cluster...
Starting VM...
Downloading Minikube ISO
 178.87 MB / 178.87 MB [============================================] 100.00% 0s
Getting VM IP address...
E1222 11:17:41.088917   11420 start.go:210] Error parsing version semver:  Version string empty
Moving files into cluster...
Downloading kubeadm v1.10.0
Downloading kubelet v1.10.0
Finished Downloading kubelet v1.10.0
Finished Downloading kubeadm v1.10.0
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Stopping extra container runtimes...
Starting cluster components...
Verifying kubelet health ...
Verifying apiserver health ...Kubectl is now configured to use the cluster.
Loading cached images from config file.


Everything looks great. Please enjoy minikube!

クラスタ確認。これは kubectl

> 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'.
> kubectl get node
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   12m   v1.10.0

kubectl cluster-info で表示された url を curl してみる。403 forbidden

> curl -k https://192.168.99.100:8443
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "forbidden: User \"system:anonymous\" cannot get path \"/\"",
  "reason": "Forbidden",
  "details": {

  },
  "code": 403
}

こっちも 403 forbidden

> curl -k https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {

  },
  "status": "Failure",
  "message": "services \"kube-dns:dns\" is forbidden: User \"system:anonymous\" cannot get services/proxy in the namespace \"kube-system\"",
  "reason": "Forbidden",
  "details": {
    "name": "kube-dns:dns",
    "kind": "services"
  },
  "code": 403
}

kubectl cluster-info dump で大量の情報が得られるようだ。

}

kubectl cluster-info dump { "kind": "NodeList", "apiVersion": "v1", "metadata": { "selfLink": "/api/v1/nodes", "resourceVersion": "1367" }, : "items": [] } Cluster info dumped to standard output

> kubectl cluster-info dump

minikube dashboard でブラウザにダッシュボードが表示された

> minikube dashboard
Opening http://127.0.0.1:52124/api/v1/namespaces/kube-system/services/http:kubernetes-dashboard:/proxy/ in your default browser...

kubectl get でいろいろみる

>kubectl get nodes
NAME       STATUS    ROLES     AGE       VERSION
minikube   Ready     master    3m        v1.10.0

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

>kubectl get deploy
No resources found.

> kubectl get pod
No resources found.

> kubectl get namespace
NAME          STATUS    AGE
default       Active    2m
kube-public   Active    2m
kube-system   Active    2m

minikube stop で virtualboxvm:minikube が停止する

> minikube stop
Stopping local Kubernetes cluster...
Machine stopped.

minikube delete で virtualbox のお掃除。virtualboxvm を直接削除した時もこれやれば大丈夫。

> minikube delete
Deleting local Kubernetes cluster...
Machine deleted.

クラスタ内のDockerにアクセスする

virtualbox と docker for windows といえば hyper-v 問題。ここはパスして次に進む。

Minikube環境内でコンテナを実行する

httpd.yml

apiVersion: v1
kind: Pod
metadata:
  name: httpd
  labels:
    app: httpd
spec:
  containers:
  - name: httpd
    image: httpd
    ports:
    - containerPort: 80 

create 前

> kubectl get pod
No resources found.

create

> kubectl create -f httpd.yml
pod "httpd" created

create 後

> kubectl get pod
NAME      READY     STATUS    RESTARTS   AGE
httpd     1/1       Running   0          29s

Minikube環境内のコンテナにアクセスする

ここは自前yaml作ってみた。

httpd-deployment.yml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: httpd-deployment
spec:
  replicas: 2                # レプリカ数の指定
  template:                  # Podのテンプレート(PodTemplate)
    metadata:
      labels:                # ラベル指定は必須
        app: httpd-deployment
    spec:
      containers:
      - name: httpd
        image: httpd
        ports:
        - containerPort: 80

create と delete を繰り返した。

kubectl create -f httpd-deployment.yml
kubectl delete deployment httpd-deployment

httpd-service.yml

apiVersion: v1
kind: Service
metadata:
  name: httpd-service
spec:
  type: LoadBalancer
  ports:
    - port: 80
  selector:
    app: httpd-deployment

create と delete を繰り返した。

kubectl create -f httpd-service.yml
kubectl delete service httpd-service
> kubectl get deployment
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
httpd-deployment   2         2         2            2           16m

> kubectl get service
NAME            TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
httpd-service   LoadBalancer   10.109.139.219   <pending>     80:31873/TCP   13m
kubernetes      ClusterIP      10.96.0.1        <none>        443/TCP        1h

minikube で確認

minikube service httpd-service 

こうなった

f:id:tjtjtjofthedead:20181222174336p:plain
itworks!