昨天介紹了 ReplicaSet,負責維護一組指定數量且穩定運行的 Pod(s),然而文中有提到,官方不推薦單使用 Replicaset,建議搭配 Deployment 一起使用。
那到底何為 Deployment 呢? 它與 Replicaset 又有何區別呢?
Deployment 是一種管理副本集的高階抽象 (high-level abstraction),在 Kubernetes 中透過設定期望狀態, Deployment Controller 會不斷監控資源以使現有狀態符合期望,而Deployment 最主要特色即是滾動式更新 RollingUpdate
、回滾 Rollback
以及AutoScaling
... 等。
Deployment 使用情境有以下幾種:
以下為 Deployment yaml file example
## deploment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
namespace: deployment-test
annotations:
monitoring: "true"
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
ports:
- containerPort: 80
resources:
limits:
memory: "2Gi"
cpu: "1000m"
requests:
memory: "1Gi"
cpu: "500m"
Deploy 基本操作指令如下:
## Create deployment resource
$ kubectl create -f deploment.yaml
## List deployment resources
## if no specific the parameter `-n`, it just list the deployments in default namespace
$ kubectl get deployments -n namespaceName
## get more
## notice that `deploy` is the short for `deployment`
$ kubectl describe deploy
## Output may like below
-----
Name: coredns
Namespace: kube-system
CreationTimestamp: Wed, 20 Sep 2023 14:49:17 +0800
Labels: k8s-app=kube-dns
Annotations: deployment.kubernetes.io/revision: 1
Selector: k8s-app=kube-dns
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 25% max surge
Pod Template:
Labels: k8s-app=kube-dns
Service Account: coredns
Containers:
coredns:
Image:
...
Wiki 解釋 Daemon:
在一個多工的電腦作業系統中,常駐程式是一種在後台執行,而不由使用者直接互動控制的電腦程式。
DaemonSet 確保節點運行 Pod 的副本,當節點新增到 cluster 時,Pod 也會加入其中; 當節點從 cluster 中刪除時,這些 Pod 就會被垃圾收集。刪除 DaemonSet 將清理它所建立的 Pod。
Daemon 典型用途如下:
今天先這樣,明天把 DaemonSet
補齊。
A positive attitude will have positive results, because attitudes are contagious.
共勉之