22、k8s-pod的控制器-第一种-ReplicaSet(RS)

作用:ReplicaSet的主要作用是保证一定数量的pod能够正常的运行、他会:

·持续监听这些pod的运行状态、一旦发生故障、就会重启或重建、

·同时它还支持对pod数量的扩容、缩减pod和版本镜像的 升级

 

1、创建Rs:新建文件
--------------------------------------
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: pc-replicaset
  namespace: dev

spec:
  replicas: 3
  selector:
   matchLabels:
     app: nginx-pod
  template:
   metadata:
     labels:
       app: nginx-pod
   spec:
     containers:
     - name: nginx
       image: nginx:1.17.1

------------------------------------------------
2、创建replicaset:kubectl create -f pc-replicaset.yaml
3、查看:kubectl get rs pc-replicaset -ndev -o wide
-----------------------------------------------------------------------------
NAME            DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
pc-replicaset   3         3         3       82s   nginx        nginx:1.17.1   app=nginx-pod

#DESIRED    期望的副本数量
#CURRENT    当前副本的数量
#READY      已经准备好提供服务的副本数量
----------------------------------------------------------------------------------------------------

 

replicaset的功能 pod扩增缩减

1、命令行输入:kubectl edit rs pc-replicaset -ndev        #相当于vim文档编辑、只不过edit是动态的
-------------------------------------------------
#修改replicas的数量、增加或减少pod的数量
#注意这里edit修改后保存退出即可立马生效
spec:
  replicas: 6
  selector:
    matchLabels:
      app: nginx-pod
--------------------------------------------------
2、we!保存退出
3、查看rs: kubectl get rs -ndev -o wide pc-replicaset
---------------------------------------------------------------------------------------------
#这时候pod就增加为了6个
NAME            DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
pc-replicaset   6         6         6       37m   nginx        nginx:1.17.1   app=nginx-pod
-----------------------------------------------------------------------------------------------
4、查看rs下的pod:kubectl get pods -ndev



扩展:使用scale也可以实现pod数量的增加与减少
1、kubectl scale rs pc-replicaset --replicas=2 -ndev
2、查看:kubectl get rs pc-replicaset -ndev
#此时rs下的pod变为了2个
------------------------------------------------------------------
NAME            DESIRED   CURRENT   READY   AGE
pc-replicaset   2         2         2       42m
------------------------------------------------------------------

 

replicaset的功能 :镜像版本的更换

输入:kubectl edit rs pc-replicaset -ndev
--------------------------------------
#修改镜像版本
    spec:
      containers:
      - image: nginx:1.17.2
---------------------------------------
2、查看rs: kubectl get rs pc-replicaset -ndev -o wide
-----------------------------------------------------------------------------------------------
NAME            DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES         SELECTOR
pc-replicaset   2         2         2       46m   nginx        nginx:1.17.2   app=nginx-pod

#发现IMAGES从1.7.1变为了1.7.2了
---------------------------------------------------------------------------------------------

扩展:使用命令直接修改镜像版本(没有edit方便)
输入:kubectl set image rs pc-replicaset nginx=nginx:1.7.1 -ndev
查看rs: kubectl get rs pc-replicaset -ndev -o wide
----------------------------------------------------------------------------------------------
NAME            DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES        SELECTOR
pc-replicaset   2         2         2       49m   nginx        nginx:1.7.1   app=nginx-pod
----------------------------------------------------------------------------------------------

 

删除replicaset

方法1:直接删除replicaset
    ·kubectl delete rs pc-replicaset -ndev
    
方法2:使用yaml删除:(推荐)
    ·kubectl delete -f pc-replicaset.yaml
    
 
    
扩展(不建议这么做):只删除replicaset控制器而保留pod    --cascade=false
    ·kubectl delete rs pc-replicaset -ndev --cascade=false
    
查看replicaset控制器:kubectl get rs pc-replicaset -ndev -o wide

 

posted @ 2024-07-01 00:12  little小新  阅读(8)  评论(0编辑  收藏  举报