開啟一個新的終端機,並執行:
# Start a new terminal, and leave this running.
# 開啟新終端機,讓此指令持續執行。
minikube dashboard
接著,切換回您執行 minikube start 的終端機。
說明:
若您不想讓 minikube 自動開啟網頁瀏覽器,請使用 --url 旗標執行 dashboard 子指令。
minikube 會輸出一個 URL,您可以在偏好的瀏覽器中開啟。
開啟一個新的終端機,並執行:
# Start a new terminal, and leave this running.
# 開啟新終端機,讓此指令持續執行。
minikube dashboard --url
接著,您可以使用此 URL 並切換回您執行 minikube start 的終端機。
建立 Deployment
Kubernetes Pod 是由一個或多個容器組成的群組,
以便集中管理並共用網路資源。本教學中的 Pod 只有一個容器。Kubernetes
Deployment 會檢查 Pod 的健康狀態,
並在 Pod 的容器終止時將其重新啟動。建議使用 Deployment 來管理 Pod 的建立和擴展。
-
使用 kubectl create 指令建立一個管理 Pod 的 Deployment。
Pod 會根據提供的 Docker 映像檔執行容器。
# Run a test container image that includes a webserver
kubectl create deployment hello-node --image=registry.k8s.io/e2e-test-images/agnhost:2.53 -- /agnhost netexec --http-port=8080
-
查看 Deployment:
輸出類似如下:
NAME READY UP-TO-DATE AVAILABLE AGE
hello-node 1/1 1 1 1m
(Pod 可能需要一些時間才能就緒。若看到「0/1」,請稍候幾秒後再試。)
-
查看 Pod:
輸出類似如下:
NAME READY STATUS RESTARTS AGE
hello-node-5f76cf6ccf-br9b5 1/1 Running 0 1m
-
查看叢集事件:
-
查看 kubectl 設定:
-
查看 Pod 中容器的應用程式日誌(請將 Pod 名稱替換為您從 kubectl get pods 取得的名稱)。
說明:
請將 kubectl logs 指令中的 hello-node-5f76cf6ccf-br9b5 替換為 kubectl get pods 輸出中的 Pod 名稱。
kubectl logs hello-node-5f76cf6ccf-br9b5
輸出類似如下:
I0911 09:19:26.677397 1 log.go:195] Started HTTP server on port 8080
I0911 09:19:26.677586 1 log.go:195] Started UDP server on port 8081
建立 Service
預設情況下,Pod 只能透過其在 Kubernetes 叢集內的內部 IP 位址存取。
若要讓 hello-node 容器可從 Kubernetes 虛擬網路外部存取,
您必須將 Pod 公開為 Kubernetes Service。
警告:
agnhost 容器有一個 /shell 端點,這對於除錯很有用,但若暴露於公開網際網路上則相當危險。
請勿在對外公開的叢集或正式環境叢集上執行此指令。
-
使用 kubectl expose 指令將 Pod 對外公開:
kubectl expose deployment hello-node --type=LoadBalancer --port=8080
`--type=LoadBalancer` 旗標表示您希望將 Service 公開至叢集外部。
測試映像檔中的應用程式碼只監聽 TCP 通訊埠 8080。若您使用
kubectl expose 公開不同的通訊埠,用戶端將無法連線到該通訊埠。
-
查看您建立的 Service:
輸出類似如下:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-node LoadBalancer 10.108.144.78 <pending> 8080:30369/TCP 21s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23m
在支援負載平衡器的雲端供應商上,系統會分配一個外部 IP 位址來存取 Service。
在 minikube 上,LoadBalancer 類型讓您可透過 minikube service 指令存取 Service。
-
執行以下指令:
minikube service hello-node
這會開啟一個瀏覽器視窗,顯示您的應用程式及其回應。
啟用附加元件
minikube 工具內建了一組 附加元件,可在本機 Kubernetes 環境中啟用、停用,或在瀏覽器中開啟。
-
列出目前支援的附加元件:
輸出類似如下:
addon-manager: enabled
dashboard: enabled
default-storageclass: enabled
efk: disabled
freshpod: disabled
gvisor: disabled
helm-tiller: disabled
ingress: disabled
ingress-dns: disabled
logviewer: disabled
metrics-server: disabled
nvidia-driver-installer: disabled
nvidia-gpu-device-plugin: disabled
registry: disabled
registry-creds: disabled
storage-provisioner: enabled
storage-provisioner-gluster: disabled
-
啟用附加元件,例如 metrics-server:
minikube addons enable metrics-server
輸出類似如下:
The 'metrics-server' addon is enabled
-
查看安裝該附加元件後建立的 Pod 和 Service:
kubectl get pod,svc -n kube-system
輸出類似如下:
NAME READY STATUS RESTARTS AGE
pod/coredns-5644d7b6d9-mh9ll 1/1 Running 0 34m
pod/coredns-5644d7b6d9-pqd2t 1/1 Running 0 34m
pod/metrics-server-67fb648c5 1/1 Running 0 26s
pod/etcd-minikube 1/1 Running 0 34m
pod/influxdb-grafana-b29w8 2/2 Running 0 26s
pod/kube-addon-manager-minikube 1/1 Running 0 34m
pod/kube-apiserver-minikube 1/1 Running 0 34m
pod/kube-controller-manager-minikube 1/1 Running 0 34m
pod/kube-proxy-rnlps 1/1 Running 0 34m
pod/kube-scheduler-minikube 1/1 Running 0 34m
pod/storage-provisioner 1/1 Running 0 34m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/metrics-server ClusterIP 10.96.241.45 <none> 80/TCP 26s
service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 34m
service/monitoring-grafana NodePort 10.99.24.54 <none> 80:30002/TCP 26s
service/monitoring-influxdb ClusterIP 10.111.169.94 <none> 8083/TCP,8086/TCP 26s
-
查看 metrics-server 的輸出:
輸出類似如下:
NAME CPU(cores) MEMORY(bytes)
hello-node-ccf4b9788-4jn97 1m 6Mi
若您看到以下訊息,請等待後再試:
error: Metrics API not available
-
停用 metrics-server:
minikube addons disable metrics-server
輸出類似如下:
metrics-server was successfully disabled
清理
現在您可以清理在叢集中建立的資源:
kubectl delete service hello-node
kubectl delete deployment hello-node
停止 Minikube 叢集:
選擇性地刪除 Minikube 虛擬機器:
# Optional
minikube delete
若您想再次使用 minikube 來學習更多 Kubernetes 知識,則不需要刪除它。
結論
本頁說明了啟動並執行 minikube 叢集的基本步驟,您現在已準備好部署應用程式。
接下來
2.1 - 使用 ConfigMap 設定 Redis
本頁提供了一個實際範例,說明如何使用 ConfigMap 設定 Redis,並延伸自設定 Pod 以使用 ConfigMap 任務。
學習目標
- 建立一個包含 Redis 設定值的 ConfigMap
- 建立一個掛載並使用該 ConfigMap 的 Redis Pod
- 驗證設定是否正確套用。
開始之前
您需要有一個 Kubernetes 叢集,且必須設定 kubectl 命令列工具使其能與叢集通訊。
建議在至少有兩個未擔任控制平面主機之節點的叢集上執行本教學。
如果您還沒有叢集,可以使用 Minikube 建立一個,
或使用以下其中一個 Kubernetes 練習環境:
若要確認版本,請輸入 kubectl version.
實際範例:使用 ConfigMap 設定 Redis
請按照以下步驟,使用 ConfigMap 中的資料設定 Redis 快取。
首先,建立一個設定區塊為空的 ConfigMap:
cat <<EOF >./example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: example-redis-config
data:
redis-config: ""
EOF
套用上述建立的 ConfigMap,以及 Redis Pod 的設定檔:
kubectl apply -f example-redis-config.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml
查看 Redis Pod 設定檔的內容,並注意以下事項:
spec.volumes[1] 建立了一個名為 config 的卷
spec.volumes[1].configMap.items[0] 下的 key 與 path,將 example-redis-config ConfigMap 中的 redis-config 鍵,以名為 redis.conf 的檔案形式呈現在 config 卷中
spec.containers[0].volumeMounts[1] 接著將 config 卷掛載至 /redis-master
結果會將 example-redis-config ConfigMap 中 data.redis-config 的資料,以 /redis-master/redis.conf 的形式於 Pod 內呈現。
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis:8.0.2
command:
- redis-server
- "/redis-master/redis.conf"
env:
- name: MASTER
value: "true"
ports:
- containerPort: 6379
resources:
limits:
cpu: "0.1"
volumeMounts:
- mountPath: /redis-master-data
name: data
- mountPath: /redis-master
name: config
volumes:
- name: data
emptyDir: {}
- name: config
configMap:
name: example-redis-config
items:
- key: redis-config
path: redis.conf
查看已建立的物件:
kubectl get pod/redis configmap/example-redis-config
您應該會看到以下輸出:
NAME READY STATUS RESTARTS AGE
pod/redis 1/1 Running 0 8s
NAME DATA AGE
configmap/example-redis-config 1 14s
回想一下,我們先前將 example-redis-config ConfigMap 中的 redis-config 鍵留白:
kubectl describe configmap/example-redis-config
您應該會看到空的 redis-config 鍵:
Name: example-redis-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
redis-config:
使用 kubectl exec 進入 Pod,並執行 redis-cli 工具以確認目前的設定:
kubectl exec -it pod/redis -- redis-cli
確認 maxmemory:
127.0.0.1:6379> CONFIG GET maxmemory
應顯示預設值 0:
同樣地,確認 maxmemory-policy:
127.0.0.1:6379> CONFIG GET maxmemory-policy
也應回傳其預設值 noeviction:
1) "maxmemory-policy"
2) "noeviction"
現在,讓我們在 example-redis-config ConfigMap 中新增一些設定值:
apiVersion: v1
kind: ConfigMap
metadata:
name: example-redis-config
data:
redis-config: |
maxmemory 2mb
maxmemory-policy allkeys-lru
套用更新後的 ConfigMap:
kubectl apply -f example-redis-config.yaml
確認 ConfigMap 已更新:
kubectl describe configmap/example-redis-config
您應該會看到剛才新增的設定值:
Name: example-redis-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
redis-config:
----
maxmemory 2mb
maxmemory-policy allkeys-lru
再次透過 kubectl exec 使用 redis-cli 確認 Redis Pod 是否已套用設定:
kubectl exec -it pod/redis -- redis-cli
確認 maxmemory:
127.0.0.1:6379> CONFIG GET maxmemory
它仍為在預設值 0:
同樣地,maxmemory-policy 仍維持 noeviction 的預設設定:
127.0.0.1:6379> CONFIG GET maxmemory-policy
回傳:
1) "maxmemory-policy"
2) "noeviction"
設定值沒有改變是因為 Pod 需要重新啟動才能取得相關 ConfigMap 更新後的值。讓我們刪除 Pod 並重新建立:
kubectl delete pod redis
kubectl apply -f https://raw.githubusercontent.com/kubernetes/website/main/content/en/examples/pods/config/redis-pod.yaml
最後再次確認設定值:
kubectl exec -it pod/redis -- redis-cli
確認 maxmemory:
127.0.0.1:6379> CONFIG GET maxmemory
現在應回傳更新後的值 2097152:
1) "maxmemory"
2) "2097152"
同樣地,maxmemory-policy 也已更新:
127.0.0.1:6379> CONFIG GET maxmemory-policy
現在已反映期望的值 allkeys-lru:
1) "maxmemory-policy"
2) "allkeys-lru"
刪除已建立的資源以清理環境:
kubectl delete pod/redis configmap/example-redis-config
接下來