当前位置:   article > 正文

【Kubernetes实战】(十一)Pod 控制器 ReplicaSet相关命令_kubectl scalingreplicaset

kubectl scalingreplicaset

目录

一、环境安装

二、ReplicaSet介绍

三、ReplicaSet使用

1、创建副本集

2、查看副本集信息

3、查看运行的 Pod

4、扩缩容

4.1 扩容

4.2 缩容

5、镜像升级

6、删除 ReplicaSet

6.1 通过名称删除

6.2 通过yml文件删除


一、环境安装

参考

【Kubernetes实战】(四)MiniKube方式部署

【Kubernetes实战】(五)KubeAdm方式部署

【Kubernetes实战】(六)Kind方式部署

二、ReplicaSet介绍

Replication Set简称RS,随着Kubernetes的高速发展,官方已经推荐我们使用RS和Deployment来代替RC了,实际上RS和RC的功能基本一致,目前唯一的一个区别就是RC只支持基于等式的selector(env=dev或environment!=qa),但RS还支持基于集合的selector(version in (v1.0, v2.0)),这对复杂的运维管理就非常方便了。

查看ReplicaSet官方帮助

kubectl explain ReplicaSet
  1. KIND: ReplicaSet
  2. VERSION: apps/v1
  3. DESCRIPTION:
  4. ReplicaSet ensures that a specified number of pod replicas are running at
  5. any given time.
  6. FIELDS:
  7. apiVersion <string>
  8. APIVersion defines the versioned schema of this representation of an
  9. object. Servers should convert recognized schemas to the latest internal
  10. value, and may reject unrecognized values. More info:
  11. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
  12. kind <string>
  13. Kind is a string value representing the REST resource this object
  14. represents. Servers may infer this from the endpoint the client submits
  15. requests to. Cannot be updated. In CamelCase. More info:
  16. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
  17. metadata <Object>
  18. If the Labels of a ReplicaSet are empty, they are defaulted to be the same
  19. as the Pod(s) that the ReplicaSet manages. Standard object's metadata. More
  20. info:
  21. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
  22. spec <Object>
  23. Spec defines the specification of the desired behavior of the ReplicaSet.
  24. More info:
  25. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
  26. status <Object>
  27. Status is the most recently observed status of the ReplicaSet. This data
  28. may be out of date by some window of time. Populated by the system.
  29. Read-only. More info:
  30. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

可以一级一级的查看,想查看什么只需要在后面加上  .字段名  即可。例如查看rs下的spec字段

kubectl explain rs.spec
  1. KIND: ReplicaSet
  2. VERSION: apps/v1
  3. RESOURCE: spec <Object>
  4. DESCRIPTION:
  5. Spec defines the specification of the desired behavior of the ReplicaSet.
  6. More info:
  7. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
  8. ReplicaSetSpec is the specification of a ReplicaSet.
  9. FIELDS:
  10. minReadySeconds <integer>
  11. Minimum number of seconds for which a newly created pod should be ready
  12. without any of its container crashing, for it to be considered available.
  13. Defaults to 0 (pod will be considered available as soon as it is ready)
  14. replicas <integer>
  15. Replicas is the number of desired replicas. This is a pointer to
  16. distinguish between explicit zero and unspecified. Defaults to 1. More
  17. info:
  18. https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/#what-is-a-replicationcontroller
  19. selector <Object> -required-
  20. Selector is a label query over pods that should match the replica count.
  21. Label keys and values that must match in order to be controlled by this
  22. replica set. It must match the pod template's labels. More info:
  23. https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
  24. template <Object>
  25. Template is the object that describes the pod that will be created if
  26. insufficient replicas are detected. More info:
  27. https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller#pod-template

三、ReplicaSet使用

yml 文件

vim rs-nginx.yaml

replicas: 3:指定运行3个实例。

  1. apiVersion: apps/v1
  2. kind: ReplicaSet
  3. metadata:
  4. name: pc-replicaset
  5. namespace: dev
  6. spec:
  7. replicas: 3
  8. selector:
  9. matchLabels:
  10. app: nginx-pod
  11. template:
  12. metadata:
  13. labels:
  14. app: nginx-pod
  15. spec:
  16. containers:
  17. - name: nginx
  18. image: nginx:1.17.1

1、创建副本集

kubectl create -f rs-nginx.yaml 

2、查看副本集信息

kubectl get rs -n dev -o wide

3、查看运行的 Pod

kubectl get pods -n dev -o wide

删除一个pod后,查看是否能自动新建一个pod

 确实,删除完一个pod后,rs又自动新建一个pod

4、扩缩容

4.1 扩容

1)edit 方式
使用edit编辑副本集名为pc-replicaset的配置。

该命令进入的编辑页面与Vim用法一致。

kubectl edit rs pc-replicaset -n dev

将replicas修改为6

扩容后,查看rs

kubectl get rs -n dev -o wide

扩容后,查看pod

kubectl get pods -n dev -o wide

2)scale 方式

kubectl scale rs pc-replicaset --replicas=6 -n dev

4.2 缩容

1)edit 方式

使用方式同扩容,这里不再重复演示

2)scale 方式

kubectl scale rs pc-replicaset --replicas=2 -n dev

将replicas调整为2

 缩容后,查看rs

kubectl get rs -n dev -o wide

缩容后,查看pod

kubectl get pods -n dev -o wide

5、镜像升级

备注:也可以使用edit方式

例如将镜像从1.17.1升级到1.17.2

kubectl set image rs pc-replicaset nginx=nginx:1.17.2 -n dev


查看实例

6、删除 ReplicaSet

6.1 通过名称删除

kubectl delete rs pc-replicaset -n dev

6.2 通过yml文件删除

kubectl delete -f pc-replicaset.yml

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/189853
推荐阅读
相关标签
  

闽ICP备14008679号