Site icon Mr. 沙先生

Minikube with Docker driver on MacOS

Minikube 是由 Google 發布的一個 Kubernetes 輕量級工具,可以很容易的在開發者環境建立一個 Kubernetes Cluster,由於現在 Kubernetes 當道,這類的需求非常廣泛,相同類型的也有 K3s, Kind 可以用

寫這篇主要是記錄一下新手上路的過程,上一次使用 Minikube 也一段時間了才發現當時沒有寫下來 XDD …

Minikube 依賴 Virtual Machine or Container 作為 Kubernetes Cluster 的底層,可以選擇 Virtualbox 或者是 Docker 等等作為 driver,而系統資源至少必須保留 2 CPU, 2GB RAM, 1GB Swap 啟動 Signle-node Kubernetes Cluster。

Installation & Config

brew 一行指令安裝將 Docker, Minikube

$ brew install docker minikube

先把 Minikube 預設的 driver 設為 Docker (macOS 預設為 Hyperkit) 可以減少每次 minikube start 都要指定 driver 的指令,執行更快

$ minikube config set driver docker

然後就可以無腦 minikube start

$ minikube start

😄  minikube v1.18.1 on Darwin 10.15.7
✨  Using the docker driver based on user configuration
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🔥  Creating docker container (CPUs=2, Memory=1990MB) ...
🐳  Preparing Kubernetes v1.20.2 on Docker 20.10.3 ...
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v4
🌟  Enabled addons: storage-provisioner, default-storageclass

🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

測試一下 deploymentservice 的使用和 Kubernetes 是不是都通用

# nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
   -  protocol: TCP
      port: 80
      targetPort: 80

拿最簡單的 nginx 來試試看

$ kubectl apply -f nginx.yml
deployment.apps/nginx-deployment created
service/nginx created

這時候應該可以拿到 Pods, Service

$ kubectl get pods
$ kubectl get services

另外 minikube 也支援將 Docker tunnel 到本機,透過瀏覽器 127.0.0.1 連到 Service

$ minikube service nginx --url

🏃  Starting tunnel for service nginx.
http://127.0.0.1:64666

測試完成後只要 minikube delete 就能將資源全數刪除,相當方便

$ minikube delete

另外幾個也很常用的 minikube 指令

$ minikube pause
$ minkube stop
$ minikube start -p aged --kubernetes-version=v1.16.1
$ minikube delete --all

Exit mobile version