LoginSignup
13
9

More than 3 years have passed since last update.

Minikubeを触ってみる、gcr.ioのプライベートレジストリからimageをpullしてみる

Last updated at Posted at 2017-05-04

logo.png

Kubernetesのクラスタをローカルで作れるminikubeへの手を付け方をまとめる。

kubernetes/minikube: Run Kubernetes locally
https://github.com/kubernetes/minikube

現在最新バージョンは0.18.0。

What is Minikube?

Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a VM on your laptop for users looking to try out Kubernetes or develop with it day-to-day.

シングルノードのkubernetesを作って実験とかいろいろできるやつです。

インストール

まずVirtualboxなどの仮想化ツールが必要なので入っていることを確認する。詳細はREADME.mdを参照。

minikubeはまだhomebrewではインストールできるようになっていない(プルリクは出ている)。
brew caskでインストールできるようになっていました brew cask install minikube

手動で入れたいときはreleaseに書いてある通りにやる。

curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.18.0/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
$ minikube
Minikube is a CLI tool that provisions and manages single-node Kubernetes clusters optimized for development workflows.

Usage:
  minikube [command]

Available Commands:
  addons           Modify minikube's kubernetes addons
  completion       Outputs minikube shell completion for the given shell (bash)
  config           Modify minikube config
  dashboard        Opens/displays the kubernetes dashboard URL for your local cluster
  delete           Deletes a local kubernetes cluster.
  docker-env       sets up docker env variables; similar to '$(docker-machine env)'
  get-k8s-versions Gets the list of available kubernetes versions available for minikube.
  ip               Retrieve the IP address of the running cluster.
  logs             Gets the logs of the running localkube instance, used for debugging minikube, not user code.
  mount            Mounts the specified directory into minikube.
  service          Gets the kubernetes URL(s) for the specified service in your local cluster
  ssh              Log into or run a command on a machine with SSH; similar to 'docker-machine ssh'
  start            Starts a local kubernetes cluster.
  status           Gets the status of a local kubernetes cluster.
  stop             Stops a running local kubernetes cluster.
  version          Print the version of minikube.

Flags:
      --alsologtostderr                  log to standard error as well as files
  -h, --help                             help for minikube
      --log_backtrace_at traceLocation   when logging hits line file:N, emit a stack trace (default :0)
      --log_dir string                   If non-empty, write log files in this directory (default "")
      --logtostderr                      log to standard error instead of files
      --show-libmachine-logs             Deprecated: To enable libmachine logs, set --v=3 or higher
      --stderrthreshold severity         logs at or above this threshold go to stderr (default 2)
      --use-vendored-driver              Use the vendored in drivers instead of RPC
  -v, --v Level                          log level for V logs
      --vmodule moduleSpec               comma-separated list of pattern=N settings for file-filtered logging

Use "minikube [command] --help" for more information about a command.

kubernetesを操作するkubectlも入れる必要がある。こいつはhomebrewで入れられる。

brew install kubectl 

自分の場合はgcloud components install kubectlで入っていたのでこの手順はスキップ。一応gcloud components updateして1.6.2が入っている状態になった。

はじめてのminikubeクラスタ

README.mdのQuickstartをやってみる。
結構時間がかかる・・・。

$ minikube start
Starting local Kubernetes cluster...
Starting VM...
Downloading Minikube ISO
 89.51 MB / 89.51 MB [==============================================] 100.00% 0s
SSH-ing files into VM...
Setting up certs...
Starting cluster components...
Connecting to cluster...
Setting up kubeconfig...
Kubectl is now configured to use the cluster.

当然kubectl get poしても何もない。
では動かしてみる。
docker runのようにkubectl runするとdeploymentが作られる。

$ kubectl run hello-minikube --image=gcr.io/google_containers/echoserver:1.4 --port=8080
deployment "hello-minikube" created
$ kubectl get deploy
NAME             DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
hello-minikube   1         1         1            0           48s
$ kubectl get po
NAME                             READY     STATUS              RESTARTS   AGE
hello-minikube-938614450-zwlkn   0/1       ContainerCreating   0          1m

そのうちSTATUSがRunningになったら動いている。

minikube dashboardというのがあったので開いてみる。

$ minikube dashboard
Opening kubernetes dashboard in default browser...

ブラウザが立ち上がって、kubectl proxyと全く同様なものが出てくる。

 2017-05-03 at 23.33.17.png

startのときに~/.kube/configを設定してくれているので、あとはkubectlで普通のkubernetesと全く同じように操作できそう。
minikube stopすると止まる。

gcr.io(Google Container Registory)のからimageをpullする

Kubernetes on GKEだとGCP内の権限はほとんど考えなくてよかったが、minikubeではちゃんと認証情報を教えてあげないといけない。

Using Google Container Registry (GCR) with Minikube · Ryan Eschinger Consulting
https://ryaneschinger.com/blog/using-google-container-registry-gcr-with-minikube/

ここを参考にする。

ImagePullSecretsとしてGCPのサービスアカウントの権限を与えてやれば良い。
だけど今回は試すだけなので、gcloud auth print-access-tokenで取れる一時的なアクセストークンを使う。

まずは以下のように普通にsecretを作る。
ユーザー名はoauth2accesstokenとする。
docker-emailは、権限を持っている適当なユーザー名。
namespaceは、いま何も指定していないのでdefault

kubectl create secret docker-registry gcr \
          --docker-server=https://gcr.io \
          --docker-username=oauth2accesstoken \
          --docker-password="$(gcloud auth print-access-token)" \
          --docker-email=youremail@example.com

作ったsecretをimagePullSecretsとする。

kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "gcr"}]}'

以下のようなdeploymentを作って、

sample.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  name: sample-deployoment
  namespace: default
spec:
  selector:
    matchLabels:
      name: sample
  replicas: 2
  template:
    spec:
      containers:
        - image: gcr.io/{youriamge}/{yourimage}:latest
          name: sample-container
    metadata:
      labels:
        name: sample

apply.

kubectl apply -f sample.yaml
NAME                                 READY     STATUS    RESTARTS   AGE
sample-deployoment-145882439-n7213   1/1       Running   0          3m
sample-deployoment-145882439-zrzg5   1/1       Running   0          3m

いい感じにpullされて走り出してくれました。

もしここでContainerCreatingなどで止まったように見えたらkubectl describe poでログをみましょう。

まとめ

本当に簡単にkubernetesがローカルに作れた。
既存のgkeで動いているdeployment.yamlを入れてみたりしても問題なく動きそう。

ただ違いがまだよく分かっていないので調べる必要がある。
基本的にKubernetesの一部の機能がサポートされていないという違いだと思うけれど。

13
9
1

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
13
9