本篇依照官方教學Hello Minikube進行操作。
範例環境:
- MacBook Pro Apple M1 Pro (ARM64)
- macOS Ventura 13.0.1
- Docker Desktop 4.21.1
事前要求
參考「Mac ARM64 Kubernetes minikube安裝」以Homebrew安裝minikube。
建立Minikube cluster
Cluster是指Nodes的集合,為一組負責運行應用程式容器的機器(實體或虛擬機)。一個Node中含有多個Pods,每個Pod代表多個運行容器。
開啟終端機,輸入minikube start
建立minikube cluster。
% minikube start
😄 minikube v1.32.0 on Darwin 13.0.1 (arm64)
✨ Automatically selected the docker driver
📌 Using Docker Desktop driver with root privileges
👍 Starting control plane node minikube in cluster minikube
🚜 Pulling base image ...
💾 Downloading Kubernetes v1.28.3 preload ...
> preloaded-images-k8s-v18-v1...: 341.16 MiB / 341.16 MiB 100.00% 16.92 M
> gcr.io/k8s-minikube/kicbase...: 410.58 MiB / 410.58 MiB 100.00% 16.11 M
🔥 Creating docker container (CPUs=2, Memory=4000MB) ...
🐳 Preparing Kubernetes v1.28.3 on Docker 24.0.7 ...
▪ Generating certificates and keys ...
▪ Booting up control plane ...
▪ Configuring RBAC rules ...
🔗 Configuring bridge CNI (Container Networking Interface) ...
🔎 Verifying Kubernetes components...
▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟 Enabled addons: storage-provisioner, default-storageclass
🏄 Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
開啟Kubernates Dashboard
Kubernates Dashboard是管理Kubernates的網頁圖形介面,可用來部署容器應用程式到Kubernates cluster,監控應用程式狀態、排查錯誤和管理資源。
開啟新的終端機,輸入minikube dashboard
開啟Dashboard。
% minikube dashboard
🔌 Enabling dashboard ...
▪ Using image docker.io/kubernetesui/metrics-scraper:v1.0.8
▪ Using image docker.io/kubernetesui/dashboard:v2.7.0
💡 Some dashboard features require the metrics-server addon. To enable all features please run:
minikube addons enable metrics-server
🤔 Verifying dashboard health ...
🚀 Launching proxy ...
🤔 Verifying proxy health ...
🎉 Opening http://127.0.0.1:50530/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/ in your default browser...
執行後會在瀏覽器開啟Dashboard使用者介面。
建立Kubernates Deployment
Kubernates Deployment(部署)是用來管理Pods的建立、擴展、監控Pods健康狀態並在其終止時重啟的元件。
在終端機輸入kubectl create deployment hello-node --image=registry.k8s.io/e2e-test-images/agnhost:2.39 -- /agnhost netexec --http-port=8080
建立一個Deployment hello-node
來在Pods的容器中運行一個指定的image registry.k8s.io/e2e-test-images/agnhost:2.39
,port為8080
。
% kubectl create deployment hello-node --image=registry.k8s.io/e2e-test-images/agnhost:2.39 -- /agnhost netexec --http-port=8080
deployment.apps/hello-node created
檢視Kubernates Deployment
輸入kubectl get deployments
檢視Deployment。
% kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
hello-node 1/1 1 1 20s
檢視Pods
輸入kubectl get pods
檢視Pods。
% kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-node-ccf4b9788-5h7k2 1/1 Running 0 33s
檢視Kubernates cluster事件
輸入kubectl get events
檢視Kubernates cluster事件。
% kubectl get events
LAST SEEN TYPE REASON OBJECT MESSAGE
51s Normal Scheduled pod/hello-node-ccf4b9788-5h7k2 Successfully assigned default/hello-node-ccf4b9788-5h7k2 to minikube
51s Normal Pulling pod/hello-node-ccf4b9788-5h7k2 Pulling image "registry.k8s.io/e2e-test-images/agnhost:2.39"
42s Normal Pulled pod/hello-node-ccf4b9788-5h7k2 Successfully pulled image "registry.k8s.io/e2e-test-images/agnhost:2.39" in 8.741s (8.741s including waiting)
42s Normal Created pod/hello-node-ccf4b9788-5h7k2 Created container agnhost
42s Normal Started pod/hello-node-ccf4b9788-5h7k2 Started container agnhost
51s Normal SuccessfulCreate replicaset/hello-node-ccf4b9788 Created pod: hello-node-ccf4b9788-5h7k2
51s Normal ScalingReplicaSet deployment/hello-node Scaled up replica set hello-node-ccf4b9788 to 1
4m27s Normal Starting node/minikube Starting kubelet.
4m27s Normal NodeAllocatableEnforced node/minikube Updated Node Allocatable limit across pods
4m27s Normal NodeHasSufficientMemory node/minikube Node minikube status is now: NodeHasSufficientMemory
4m27s Normal NodeHasNoDiskPressure node/minikube Node minikube status is now: NodeHasNoDiskPressure
4m27s Normal NodeHasSufficientPID node/minikube Node minikube status is now: NodeHasSufficientPID
4m13s Normal RegisteredNode node/minikube Node minikube event: Registered Node minikube in Controller
檢視kubectl配置
輸入kubectl config view
檢視kubectl配置。
% kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority: /Users/user/.minikube/ca.crt
extensions:
- extension:
last-update: Thu, 28 Dec 2023 16:56:28 CST
provider: minikube.sigs.k8s.io
version: v1.32.0
name: cluster_info
server: https://127.0.0.1:50502
name: minikube
contexts:
- context:
cluster: minikube
extensions:
- extension:
last-update: Thu, 28 Dec 2023 16:56:28 CST
provider: minikube.sigs.k8s.io
version: v1.32.0
name: context_info
namespace: default
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
user:
client-certificate: /Users/user/.minikube/profiles/minikube/client.crt
client-key: /Users/user/.minikube/profiles/minikube/client.key
檢視Pods容器應用程式的log
輸入kubectl logs hello-node-ccf4b9788-5h7k2
檢視Pods hello-node-ccf4b9788-5h7k2
容器中應用程式的log。
% kubectl logs hello-node-ccf4b9788-5h7k2
I1228 09:00:13.812161 1 log.go:195] Started HTTP server on port 8080
I1228 09:00:13.812267 1 log.go:195] Started UDP server on port 8081
建立Kubernates Service
建立Kubernates Service(服務)是指對外曝露Pods容器中運行的網路應用程式。
輸入kubectl expose deployment hello-node --type=LoadBalancer --port=8080
將Deployment hello-node
管理的Pods容器中的服務對外曝露Port 8080。--type=LoadBalancer
的作用是配發一個固定外部IP給Service。
kubectl expose deployment hello-node --type=LoadBalancer --port=8080
service/hello-node exposed
檢視Serivce
輸入kubectl get services
檢視Service。
% kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-node LoadBalancer 10.96.217.22 <pending> 8080:32153/TCP 17s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6m29s
輸入minikube service hello-node
在瀏覽器開啟對外曝露Pods中的應用程式。
% minikube service hello-node
|-----------|------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|------------|-------------|---------------------------|
| default | hello-node | 8080 | http://192.168.49.2:32153 |
|-----------|------------|-------------|---------------------------|
🏃 Starting tunnel for service hello-node.
|-----------|------------|-------------|------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|------------|-------------|------------------------|
| default | hello-node | | http://127.0.0.1:50764 |
|-----------|------------|-------------|------------------------|
🎉 Opening service default/hello-node in default browser...
❗ Because you are using a Docker driver on darwin, the terminal needs to be open to run it.
執行後在瀏覽器會開啟如下:
在Dashboard可看到Kubernates的狀態。
刪除Service
輸入kubectl delete service hello-node
刪除hello-node
Service。
% kubectl delete service hello-node
service "hello-node" deleted
檢視Services可看見hello-node
Service已消失。
% kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 18h
刪除Deployment
輸入kubectl delete deployment hello-node
刪除hello-node
Deployment。
% kubectl delete deployment hello-node
deployment.apps "hello-node" deleted
檢視Pods可看見hello-node
Pods已消失。
% kubectl get pods
No resources found in default namespace.
停止Minikube cluster
輸入minikube stop
停止Minikube cluster。
% minikube stop
✋ Stopping node "minikube" ...
🛑 Powering off "minikube" via SSH ...
🛑 1 node stopped.
沒有留言:
張貼留言