什么是ReplicaSet?

ReplicaSet是下一代复本控制器。ReplicaSet和 Replication Controller之间的唯一区别是现在的选择器支持。Replication Controller只支持基于等式的selector(env=dev或environment!=qa),但ReplicaSet还支持新的,基于集合的selector(version in (v1.0, v2.0)或env notin (dev, qa))。在试用时官方推荐ReplicaSet。

大多数kubectl支持Replication Controller的命令也支持ReplicaSets。rolling-update命令有一个例外 。如果您想要滚动更新功能,请考虑使用Deployments。此外, rolling-update命令是必须的,而Deployments是声明式的,因此我们建议通过rollout命令使用Deployments。

虽然ReplicaSets可以独立使用,但是今天它主要被 Deployments 作为协调pod创建,删除和更新的机制。当您使用Deployments时,您不必担心管理他们创建的ReplicaSets。Deployments拥有并管理其ReplicaSets。

何时使用ReplicaSet?

ReplicaSet可确保指定数量的pod“replicas”在任何设定的时间运行。然而,Deployments是一个更高层次的概念,它管理ReplicaSets,并提供对pod的声明性更新以及许多其他的功能。因此,我们建议您使用Deployments而不是直接使用ReplicaSets,除非您需要自定义更新编排或根本不需要更新。

这实际上意味着您可能永远不需要操作ReplicaSet对象:直接使用Deployments并在规范部分定义应用程序。

[root@k8s-master mnt]# cat rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
spec:
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: myapp
image: wangyanglinux/myapp:v1
env:
- name: GET_HOSTS_FROM
value: dns
ports:
- containerPort: 80
[root@k8s-master mnt]#
[root@k8s-master mnt]# kubectl create -f rs.yaml
replicaset.apps/frontend created
[root@k8s-master mnt]# kubectl get pod
NAME READY STATUS RESTARTS AGE
frontend-4xs95 1/1 Running 0 12s
frontend-gd5th 1/1 Running 0 12s
frontend-tn9pn 1/1 Running 0 12s
[root@k8s-master mnt]# kubectl delete pod --all
pod "frontend-4xs95" deleted
pod "frontend-gd5th" deleted
pod "frontend-tn9pn" deleted
[root@k8s-master mnt]# kubectl get pod
NAME READY STATUS RESTARTS AGE
frontend-gh2w5 1/1 Running 0 3m58s
frontend-rd9pl 1/1 Running 0 3m58s
frontend-xg845 1/1 Running 0 3m58s
[root@k8s-master mnt]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
frontend-gh2w5 1/1 Running 0 4m38s tier=frontend
frontend-rd9pl 1/1 Running 0 4m38s tier=frontend
frontend-xg845 1/1 Running 0 4m38s tier=frontend
[root@k8s-master mnt]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
frontend-btqh8 1/1 Running 0 3s tier=frontend
frontend-gh2w5 1/1 Running 0 7m43s tier=frontend
frontend-rd9pl 1/1 Running 0 7m43s tier=frontend1
frontend-xg845 1/1 Running 0 7m43s tier=frontend
[root@k8s-master mnt]# kubectl delete pod --all
pod "frontend-btqh8" deleted
pod "frontend-gh2w5" deleted
pod "frontend-rd9pl" deleted
pod "frontend-xg845" deleted
[root@k8s-master mnt]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
frontend-mnmlm 1/1 Running 0 28s tier=frontend
frontend-x8rcp 1/1 Running 0 27s tier=frontend
frontend-zqs4n 1/1 Running 0 28s tier=frontend
[root@k8s-master mnt]# kubectl delete -f rs.yaml
replicaset.apps "frontend" deleted
[root@k8s-master mnt]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
frontend-mnmlm 0/1 Terminating 0 47s tier=frontend
frontend-x8rcp 0/1 Terminating 0 46s tier=frontend
frontend-zqs4n 0/1 Terminating 0 47s tier=frontend

Deployment是什么?

Deployment为Pod和Replica Set(下一代Replication Controller)提供声明式更新。

你只需要在Deployment中描述你想要的目标状态是什么,Deployment controller就会帮你将Pod和Replica Set的实际状态改变到你的目标状态。你可以定义一个全新的Deployment,也可以创建一个新的替换旧的Deployment。

一个典型的用例如下:

  • 使用Deployment来创建ReplicaSet。ReplicaSet在后台创建pod。检查启动状态,看它是成功还是失败。
  • 然后,通过更新Deployment的PodTemplateSpec字段来声明Pod的新状态。这会创建一个新的ReplicaSet,Deployment会按照控制的速率将pod从旧的ReplicaSet移动到新的ReplicaSet中。
  • 如果当前状态不稳定,回滚到之前的Deployment revision。每次回滚都会更新Deployment的revision。
  • 扩容Deployment以满足更高的负载。
  • 暂停Deployment来应用PodTemplateSpec的多个修复,然后恢复上线。
  • 根据Deployment 的状态判断上线是否hang住了。
  • 清除旧的不必要的ReplicaSet。

代码展示:

[root@k8s-master mnt]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: wangyanglinux/myapp:v1
ports:
- containerPort: 80
[root@k8s-master mnt]#

下面的代码包括了扩容和回退和更新。

[root@k8s-master mnt]# kubectl apply -f deployment.yaml --record
deployment.apps/nginx-deployment created
[root@k8s-master mnt]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-64ddb75745-fkfxl 0/1 ContainerCreating 0 1s
nginx-deployment-64ddb75745-slcwp 0/1 ContainerCreating 0 1s
nginx-deployment-64ddb75745-vwnqw 0/1 ContainerCreating 0 1s
[root@k8s-master mnt]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-64ddb75745-fkfxl 1/1 Running 0 55s
nginx-deployment-64ddb75745-slcwp 1/1 Running 0 55s
nginx-deployment-64ddb75745-vwnqw 1/1 Running 0 55s
[root@k8s-master mnt]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-64ddb75745 3 3 3 65s
[root@k8s-master mnt]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-64ddb75745-fkfxl 1/1 Running 0 2m45s 10.244.2.6 k8s-node01 <none> <none>
nginx-deployment-64ddb75745-slcwp 1/1 Running 0 2m45s 10.244.1.9 k8s-node02 <none> <none>
nginx-deployment-64ddb75745-vwnqw 1/1 Running 0 2m45s 10.244.1.8 k8s-node02 <none> <none>
[root@k8s-master mnt]# curl 10.244.2.6
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master mnt]# kubectl scale deployment nginx-deployment --replicas 10
deployment.apps/nginx-deployment scaled
[root@k8s-master mnt]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-64ddb75745-9c55m 0/1 ContainerCreating 0 3s <none> k8s-node01 <none> <none>
nginx-deployment-64ddb75745-9gs9v 0/1 ContainerCreating 0 3s <none> k8s-node01 <none> <none>
nginx-deployment-64ddb75745-fkfxl 1/1 Running 0 4m10s 10.244.2.6 k8s-node01 <none> <none>
nginx-deployment-64ddb75745-nbl92 0/1 ContainerCreating 0 3s <none> k8s-node01 <none> <none>
nginx-deployment-64ddb75745-p4f2z 0/1 ContainerCreating 0 3s <none> k8s-node02 <none> <none>
nginx-deployment-64ddb75745-qtmhl 0/1 ContainerCreating 0 3s <none> k8s-node02 <none> <none>
nginx-deployment-64ddb75745-rgcsl 0/1 ContainerCreating 0 3s <none> k8s-node02 <none> <none>
nginx-deployment-64ddb75745-slcwp 1/1 Running 0 4m10s 10.244.1.9 k8s-node02 <none> <none>
nginx-deployment-64ddb75745-vwnqw 1/1 Running 0 4m10s 10.244.1.8 k8s-node02 <none> <none>
nginx-deployment-64ddb75745-zzmsn 0/1 ContainerCreating 0 3s <none> k8s-node01 <none> <none>
[root@k8s-master mnt]# kubectl describe deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Fri, 20 Dec 2019 15:10:03 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{"kubernetes.io/change-cause":"kubectl apply --filename=de ployment.y...
kubernetes.io/change-cause: kubectl apply --filename=deployment.yaml --record=true
Selector: app=nginx
Replicas: 10 desired | 10 updated | 10 total | 10 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: wangyanglinux/myapp:v1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-64ddb75745 (10/10 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 11m deployment-controller Scaled up replica set nginx-deployment-64ddb75745 to 3
Normal ScalingReplicaSet 7m28s deployment-controller Scaled up replica set nginx-deployment-64ddb75745 to 10
[root@k8s-master mnt]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=deployment.yaml --record=true [root@k8s-master mnt]# kubectl set image deployment/nginx-deployment nginx=wangyanglinux/myapp:v2
deployment.apps/nginx-deployment image updated
[root@k8s-master mnt]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-64ddb75745-rgcsl 0/1 Terminating 0 30m 10.244.1.12 k8s-node02 <none> <none>
nginx-deployment-64ddb75745-slcwp 0/1 Terminating 0 35m 10.244.1.9 k8s-node02 <none> <none>
nginx-deployment-6bd7755b5b-2qwdf 1/1 Running 0 23s 10.244.2.12 k8s-node01 <none> <none>
nginx-deployment-6bd7755b5b-2tpzn 1/1 Running 0 23s 10.244.2.11 k8s-node01 <none> <none>
nginx-deployment-6bd7755b5b-5d2fn 1/1 Running 0 23s 10.244.1.14 k8s-node02 <none> <none>
nginx-deployment-6bd7755b5b-87vdh 1/1 Running 0 23s 10.244.2.13 k8s-node01 <none> <none>
nginx-deployment-6bd7755b5b-fl56p 1/1 Running 0 14s 10.244.1.16 k8s-node02 <none> <none>
nginx-deployment-6bd7755b5b-gpz9q 1/1 Running 0 12s 10.244.1.17 k8s-node02 <none> <none>
nginx-deployment-6bd7755b5b-hhfbn 1/1 Running 0 11s 10.244.2.15 k8s-node01 <none> <none>
nginx-deployment-6bd7755b5b-k5rm7 1/1 Running 0 23s 10.244.1.13 k8s-node02 <none> <none>
nginx-deployment-6bd7755b5b-skh7w 1/1 Running 0 14s 10.244.1.15 k8s-node02 <none> <none>
nginx-deployment-6bd7755b5b-t494q 1/1 Running 0 13s 10.244.2.14 k8s-node01 <none> <none>
[root@k8s-master mnt]# curl 10.244.1.14
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
[root@k8s-master mnt]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-64ddb75745 0 0 0 35m
nginx-deployment-6bd7755b5b 10 10 10 64s
[root@k8s-master mnt]# kubectl rollout undo deployment/nginx-deployment
deployment.apps/nginx-deployment rolled back
[root@k8s-master mnt]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-deployment-64ddb75745-48d5d 1/1 Running 0 11s
nginx-deployment-64ddb75745-4ctzd 1/1 Running 0 13s
nginx-deployment-64ddb75745-8g7f9 1/1 Running 0 13s
nginx-deployment-64ddb75745-bnwlj 1/1 Running 0 9s
nginx-deployment-64ddb75745-gq29d 1/1 Running 0 13s
nginx-deployment-64ddb75745-rp52v 1/1 Running 0 13s
nginx-deployment-64ddb75745-sxch9 1/1 Running 0 10s
nginx-deployment-64ddb75745-tt8tp 0/1 ContainerCreating 0 5s
nginx-deployment-64ddb75745-vtx8w 1/1 Running 0 13s
nginx-deployment-64ddb75745-zr8c7 1/1 Running 0 7s
nginx-deployment-6bd7755b5b-2qwdf 1/1 Terminating 0 3m14s
nginx-deployment-6bd7755b5b-2tpzn 1/1 Terminating 0 3m14s
nginx-deployment-6bd7755b5b-87vdh 1/1 Terminating 0 3m14s
[root@k8s-master mnt]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-64ddb75745 10 10 10 38m
nginx-deployment-6bd7755b5b 0 0 0 3m29s
[root@k8s-master mnt]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-64ddb75745-48d5d 1/1 Running 0 43s 10.244.2.18 k8s-node01 <none> <none>
nginx-deployment-64ddb75745-4ctzd 1/1 Running 0 45s 10.244.1.18 k8s-node02 <none> <none>
nginx-deployment-64ddb75745-8g7f9 1/1 Running 0 45s 10.244.1.19 k8s-node02 <none> <none>
nginx-deployment-64ddb75745-bnwlj 1/1 Running 0 41s 10.244.1.21 k8s-node02 <none> <none>
nginx-deployment-64ddb75745-gq29d 1/1 Running 0 45s 10.244.2.17 k8s-node01 <none> <none>
nginx-deployment-64ddb75745-rp52v 1/1 Running 0 45s 10.244.1.20 k8s-node02 <none> <none>
nginx-deployment-64ddb75745-sxch9 1/1 Running 0 42s 10.244.2.19 k8s-node01 <none> <none>
nginx-deployment-64ddb75745-tt8tp 1/1 Running 0 37s 10.244.2.20 k8s-node01 <none> <none>
nginx-deployment-64ddb75745-vtx8w 1/1 Running 0 45s 10.244.2.16 k8s-node01 <none> <none>
nginx-deployment-64ddb75745-zr8c7 1/1 Running 0 39s 10.244.1.22 k8s-node02 <none> <none>
[root@k8s-master mnt]# curl 10.244.2.19
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master mnt]# kubectl rollout status deployment/nginx-deployment
deployment "nginx-deployment" successfully rolled out
[root@k8s-master mnt]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
2 kubectl apply --filename=deployment.yaml --record=true
3 kubectl apply --filename=deployment.yaml --record=true

K8s中RS和Deployment的更多相关文章

  1. K8S中RC与Deployment的区别

    原文:http://fx114.net/qa-81-152379.aspx replication controller与deployment的区别 replication controller Re ...

  2. 【转】K8S中部署Helm

    K8S中的包管理工具 1. 客户端Helm(即Helm)  通过脚本安装:curl https://raw.githubusercontent.com/helm/helm/master/scripts ...

  3. k8s中几个基本概念的理解,pod,service,deployment,ingress的使用场景

    k8s 总体概览 前言 Pod 副本控制器(Replication Controller,RC) 副本集(Replica Set,RS) 部署(Deployment) 服务(Service) ingr ...

  4. k8s如何管理Pod(rc、rs、deployment)

    是豆荚,可以把容器想像成豆荚里的豆子,把一个或多个关系紧密的豆子包在一起就是豆荚(一个Pod).在k8s中我们不会直接操作容器,而是把容器包装成Pod再进行管理(关于Pod,大家可以参考第十期的分享“ ...

  5. k8s工作负载资源之deployment

    首先我们要理解:一个应用跑在k8s集群上了,那么这个应用就是一个工作负载(workloads). 在k8s中会用pod的来承载这个应用,那么负责管理这个pod的东西就叫工作负载资源(workload ...

  6. k8s 中的 Pod 细节了解

    k8s中Pod的理解 基本概念 k8s 为什么使用 Pod 作为最小的管理单元 如何使用 Pod 1.自主式 Pod 2.控制器管理的 Pod 静态 Pod Pod的生命周期 Pod 如何直接暴露服务 ...

  7. k8s 中 Pod 的控制器

    k8s 中 Pod 的控制器 前言 Replication Controller ReplicaSet Deployment 更新 Deployment 回滚 deployment StatefulS ...

  8. k8s中yaml文常见语法

    在k8s中,所有的配置都是 json格式的.但为了读写方便,通常将这些配置写成yaml 格式,其运行的时候,还是会靠yaml引擎将其转化为json,apiserver 也仅接受json的数据类型. y ...

  9. 从harbor部署到在k8s中使用

    一.概述 harbor是什么呢?英文单词的意思是:港湾.港湾用来存放集装箱(货物的),而docker的由来正是借鉴了集装箱的原理,所以harbor是用于存放docker的镜像,作为镜像仓库使用.官方的 ...

随机推荐

  1. spark教程(八)-SparkSession

    spark 有三大引擎,spark core.sparkSQL.sparkStreaming, spark core 的关键抽象是 SparkContext.RDD: SparkSQL 的关键抽象是 ...

  2. T100——查询 r类 报表开发流程

    报表开发流程:1.建立入口程序 如r类的作业:cxmr500步骤: azzi900中建立程序代号 azzi910中建立作业代号 设计器--规格--签出 设计器--程序--签出 adzp168(r.a) ...

  3. vue axios拦截跳转

    第一步:添加需要拦截的页面 { path: '/control', name: 'Control', meta: { requireAuth: true }, 第二步:页面拦截 router.befo ...

  4. RPC一般指远程过程调用协议

    RPC一般指远程过程调用协议 RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议 ...

  5. Java枚举相关知识

    JAVA枚举 很多编程语言都提供了枚举的概念,但是java直到1.5之后才提出了枚举的概念,出现比这个语言本身晚10年. 主要作用是用于定义有限个数对象的一种结构(多例设计),枚举就属于多例设计并且其 ...

  6. 单元操作和仓储模式 repository+unitOfWork

    仓储和工作单元模式是用来在数据访问层和业务逻辑层之间创建一个抽象层.应用这些模式,可以帮助用来隔离你的程序在数据存储变化. 在数据源层和业务层之间增加一个repository层进行协调,有如下作用:1 ...

  7. HTTP请求管道事件BeginRequest和EndRequest

    这是一个测试示例,代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  8. sass之mixin的全局引入(vue3.0)

    sass之mixin的全局引入(vue3.0) 1.scss文件(mixin.scss) /* 渐变 */ @mixin gradual($color, $color1){ background: $ ...

  9. Caffe测试单独的算子

    最近有一个需求是测试单独算子在CPU.Caffe使用的GPU.cuDNN上的性能,一个是使用caffe的time问题,还有一个是使用单独的test功能. time选项的使用,大家都比较熟悉,单独的te ...

  10. nginx 配置简单的静态页面

    nginx 文件服务配置,MIME和 default_type https://blog.csdn.net/qq_26711103/article/details/81116900 nginx 静态页 ...