kubernetes之ReplicaSet
參考:https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/
ReplicaSet是什么?
ReplicaSet是kubernetes中的一種副本控制器,主要作用是控制由其管理的pod,使pod副本的數量始終維持在預設的個數。Kubernetes中還有一個類似功能的控制器:Replication Controller。ReplicaSet是Replication Controller的下一代副本控制器,目前兩者只在標簽選擇器支持的查找方式有區別,ReplicaSet支持等式查找與集合查找兩種方式,Replication Controller只支持等式查找。
kubectl命令同時支持ReplicaSet與Replication Controller兩種副本控制器,但是kubectl rolling-update只支持Replication Controller類型的副本控制器,也就是說ReplicaSet無法通過kubectl rolling-update命令滾動升級。如果想要使用rolling-update功能的話,kubernetes官方推薦使用Deployments,并且因為Deployments是聲明式的而rolling-update是命令式,所以推薦使用rollout命令實現滾動升級(kubectl管理對象的方式大體上分成兩種,一種是命令式,另一種是聲明式,聲明式又細分成命令聲明式與對角聲明式,三種方式不能混用,所以才有此說)。
實現上kubernetes官方推薦不要直接使用ReplicaSet,用Deployments取而代之,Deployments是比ReplicaSet更高級的概念,它會管理ReplicaSet并提供很多其它有用的特性,最重要的是Deployments支持聲明式更新,聲明式更好相比于命令式更新的好處是不會丟失歷史變更。總結起來就是:不要再直接使用ReplicaSet。
示例
apiVersion: apps/v1 kind: ReplicaSet metadata:name: frontendlabels:app: guestbooktier: frontend spec:# this replicas value is default# modify it according to your casereplicas: 3selector:matchLabels:tier: frontendmatchExpressions:- {key: tier, operator: In, values: [frontend]}template:metadata:labels:app: guestbooktier: frontendspec:containers:- name: php-redisimage: gcr.io/google_samples/gb-frontend:v3resources:requests:cpu: 100mmemory: 100Mienv:- name: GET_HOSTS_FROMvalue: dns# If your cluster config does not include a dns service, then to# instead access environment variables to find service host# info, comment out the 'value: dns' line above, and uncomment the# line below.# value: envports:- containerPort: 80將以上內容保存在frontend.yaml文件并提供給集群,集群創建ReplicaSet與由它控制的pod:
$ kubectl create -f http://k8s.io/examples/controllers/frontend.yaml replicaset "frontend" created $ kubectl describe rs/frontend Name: frontend Namespace: default Selector: tier=frontend,tier in (frontend) Labels: app=guestbooktier=frontend Annotations: <none> Replicas: 3 current / 3 desired Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template:Labels: app=guestbooktier=frontendContainers:php-redis:Image: gcr.io/google_samples/gb-frontend:v3Port: 80/TCPRequests:cpu: 100mmemory: 100MiEnvironment:GET_HOSTS_FROM: dnsMounts: <none>Volumes: <none> Events:FirstSeen LastSeen Count From SubobjectPath Type Reason Message--------- -------- ----- ---- ------------- -------- ------ -------1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-qhloh1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-dnjpy1m 1m 1 {replicaset-controller } Normal SuccessfulCreate Created pod: frontend-9si5l $ kubectl get pods NAME READY STATUS RESTARTS AGE frontend-9si5l 1/1 Running 0 1m frontend-dnjpy 1/1 Running 0 1m frontend-qhloh 1/1 Running 0 1m如何寫ReplicaSet spec?
ReplicaSet寫法比較簡單,需要注意的只有標簽選擇器。.spec.selector字段定義的標簽選擇器必需與.spec.template.metadata.labels中定義的標簽匹配,也就是標簽選擇必需能選中它控制下的pod。另外建議.metadata.labels中的值與.spec.template.metadata.labels相同,這樣的話有利于通過標簽組織管理資源,這一特性非強制,不同的話對ReplicaSet本身沒有影響。
用戶不應該再通過其它直接、間接的方式創建與上述ReplicaSet標簽選擇器匹配的pod,否則ReplicaSet會認為是它自己創建了pod并把pod納入自己管轄。或者在定義其它的副本控制器與,選擇器與上述ReplicaSet重疊。以上兩種情況都會導致pod的控制權混亂的問題。
ReplicaSet支持的操作
刪除ReplicaSet與它控制下的pod
kubectl delete命令將刪除ReplicaSet及其pod。具體的過程是kubectl delete首先將副本的數量調整到0,然后等待kubernetes控制面將pod刪除,再后再刪除ReplicaSet,kubectl delete可以被中斷,重新運行后繼續刪除處理。
只刪除ReplicaSet
只需要運行kubectl delete命令時加上--cascade=false選項,ReplicaSet被刪除,其控制的pod不受影響。
從ReplicaSet隔離pod
更改pod標簽,使其無法與ReplicaSet標簽選擇器匹配就可以將pod從ReplicaSet隔離出來,這時pod副本個數變少,ReplicaSet自動補足。
ReplicaSet擴縮容
只需簡單的更新ReplicaSet spec中的.spec.replicas值就可實現。
ReplicaSet水平自動擴縮容器
通過HPA控制ReplicaSet根據負荷如cpu使用情況自動擴縮容。HPA spec如下:
apiVersion: autoscaling/v1 kind: HorizontalPodAutoscaler metadata:name: frontend-scaler spec:scaleTargetRef:kind: ReplicaSetname: frontendminReplicas: 3maxReplicas: 10targetCPUUtilizationPercentage: 50創建HPA對象:
kubectl create -f https://k8s.io/examples/controllers/hpa-rs.yaml以上根據CPU使用情況,在3到10之間動態的調整副本數量。
總結
以上是生活随笔為你收集整理的kubernetes之ReplicaSet的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: nyoj663弟弟的作业
- 下一篇: 【笔记】编程的原则:改善代码质量的101