DEV Community

Jain Wun
Jain Wun

Posted on

4 4

[K8s學習筆記] 透過depolyment操控pod


Deployment

Create a Simple Deployment

最簡單的deployment就是啟動某個image,然後不帶其他設定

kubectl create deployment <name-of-depl> --image=<image-name>
Enter fullscreen mode Exit fullscreen mode

可以透過 kubectl get deployment 檢查deploy的狀態

等到狀態變成ready後就可以用 kubectl get pod 看到所有running的pod

pod name是由replicaset id + pod id組成

通常也不會去動replicaset,他會在deployment時由K8s自動設定

Pod info

要看pod的更詳細訊息可以用

kubectl describe pod <pod_name>
Enter fullscreen mode Exit fullscreen mode

會分別列出到pod成功running前的各步驟花了多久時間

Debug a Pod

如果deploy失敗或是要查看log,可以使用

kubectl logs <pod-name>
Enter fullscreen mode Exit fullscreen mode

要連進某個pod的terminal可以透過

kubectl exec -it <pod_name> -- bin/bash
Enter fullscreen mode Exit fullscreen mode

用法和在docker要連進contianer的terminal非常類似

Edit the Deployment

kubectl edit deployment <name-of-depl>
Enter fullscreen mode Exit fullscreen mode

當你修改並儲存deployment的設定後,pod就會自動重建 (舊的被刪掉,直接創新的)

但是舊的replicaset並不會被刪掉,只會把剛剛那個Pod從中移除

Delete the Deployment

要連帶pod + replicaset 一併刪除的話

kubectl delete deployment <depl-name>
Enter fullscreen mode Exit fullscreen mode

可以透過kubectl get replicaset去確認

Create Deployment with YAML File

比較常見的做法還是先寫好YAML檔再去deploy

kubectl apply -f <filename>
Enter fullscreen mode Exit fullscreen mode

deployment這種型態會根據kind還有apiVersion來決定

apiVersion: apps/v1
kind: Deployment
Enter fullscreen mode Exit fullscreen mode

如果以這種方式deploy的,往後只要修改檔案,pod就會自動重建了


Reference

Kubectl Basic Commands - Create and Debug Pod in a Minikube cluster | Kubernetes Tutorial 18

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay