K8s(五)ReplicaSet控制器

控制器

在Kubernetes(简称K8s)中,控制器是负责管理和维护集群中资源状态的组件。控制器监视集群中的对象,并根据它们的预期状态来采取行动,以确保系统的期望状态与实际状态保持一致。

对于自主式pod来说,删除pod之后pod就直接消失了,如果因为一些误操作或pod错误退出,就不会自动恢复,这个时候就需要使用k8s的控制器,使用控制器创建的pod可以进行故障的恢复与自愈,并且也可以做资源调度、配置管理等内容

ReplicaSet控制器

ReplicaSet是Kubernetes中的一种控制器,用于确保一组Pod副本的运行。它定义了所需的Pod副本数量,并监控它们的运行状态,以确保始终有指定数量的副本在运行。

用的不多,大多数环境中使用deployment资源,deployment的功能包括ReplicaSet

定义ReplicaSet时,需要定义要创建的pod的模板,相当于pod做了多份的负载均衡

以下是一个replicatest的示例文件

#查看帮助
kubectl explain rs 
apiVersion   
kind 
metadata     
spec 
status       
kubectl explain rs.spec
minReadySeconds
replicas
selector
template

kubectl explain rs.spec.template.spec #与pod的spec相同

cat > rs.yaml << EOF
apiVersion: apps/v1
kind: ReplicaSet
metadata: 
  name: rstest
  namespace: default
spec:
  replicas: 5  #副本数
  selector:    #筛选器,与pod关联
    matchLabels:
       user: ws #匹配标签user=ws的pod
  template:    #pod模板
    metadata:
      labels:   #pod标签
        user: ws
    spec:
      containers:
          - name: test1 
            image: docker.io/library/nginx
            imagePullPolicy: IfNotPresent
            ports:
              - containerPort: 80
            startupProbe:      #启动探测
              periodSeconds: 5
              initialDelaySeconds: 10
              timeoutSeconds: 5
              httpGet:
                 scheme: HTTP
                 port: 80
                 path: /
EOF
kubectl apply -f rs.yaml
kubectl get pods -w | grep Running
rstest-2qbrw   1/1     Running   0          2m34s
rstest-6j9p6   1/1     Running   0          2m34s
rstest-ltpn5   1/1     Running   0          2m34s
rstest-z7h27   1/1     Running   0          2m34s
rstest-z8cnf   1/1     Running   0          2m34s
#desired期望3,current当前启动5,ready就绪5
kubectl get rs
NAME     DESIRED   CURRENT   READY   AGE
rstest   5         5         5       2m56s、
#退出其中一个pod,删除或异常退出都可以
kubectl delete pods rstest-hrvtj
#创建了一个新pod
kubectl get pods -w | grep Running
rstest-6j9p6   1/1     Running   0          6m41s
rstest-hrvtj   1/1     Running   0          32s
rstest-ltpn5   1/1     Running   0          6m41s
rstest-z7h27   1/1     Running   0          6m41s
rstest-z8cnf   1/1     Running   0          6m41s
rstest-rmxcq   0/1     Running             0          1s
rstest-rmxcq   0/1     Running             0          10s
rstest-rmxcq   1/1     Running             0          10s

 
  

扩容与缩容、更新镜像

#扩容与缩容
#修改yaml文件
...
  replicas: 6  #副本数
...
kubectl apply -f rs.yaml
#创建了一个新pod
kubectl get pods -w | grep Running
rstest-6j9p6   1/1     Running   0          12m
rstest-ltpn5   1/1     Running   0          12m
rstest-rmxcq   1/1     Running   0          5m29s
rstest-z7h27   1/1     Running   0          12m
rstest-z8cnf   1/1     Running   0          12m
rstest-zwgnl   0/1     Running             0          1s
rstest-zwgnl   0/1     Running             0          10s
rstest-zwgnl   1/1     Running             0          10s
#修改yaml文件
...
  replicas: 2  #副本数
...
kubectl apply -f rs.yaml
#全部被关闭,只剩俩

 K8s(五)ReplicaSet控制器_第1张图片

#手动更新镜像,ReplicaSet无法实现滚动更新

#原本状态
curl 10.10.179.34:80



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

#修改yaml文件 ... image: docker.io/library/tomcat ... kubectl apply -f rs.yaml #删除原有pods,因为replicatest无法实现滚动更新,而deployment可以 kubectl delete pods rstest-6j9p6 kubectl delete pods rstest-z8cnf #因为某些原因没法起来,不过问题不大,注释掉探活部分就起来了 kubectl get pods -w NAME READY STATUS RESTARTS AGE rstest-c2m98 1/1 Running 0 3m54s rstest-xkqnl 1/1 Running 0 3m54s #当前状态,说明当前镜像已经被修改 curl 10.10.234.124:8080 HTTP Status 404 – Not Found

HTTP Status 404 – Not Found


Type Status Report

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.


Apache Tomcat/10.1.17

[ #清理 kubectl delete -f rs.yaml

K8s(五)ReplicaSet控制器_第2张图片

你可能感兴趣的:(云原生,kubernetes,云原生,容器)