K8s(五)ReplicaSet控制器

发布时间:2024年01月18日

控制器

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

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

ReplicaSet控制器

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

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

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

以下是一个replicatest的示例文件

#查看帮助
kubectl explain rs 
apiVersion   <string>
kind <string>
metadata     <Object>
spec <Object>
status       <Object>
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
#全部被关闭,只剩俩

?

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

#原本状态
curl 10.10.179.34:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="<http://nginx.org/>">nginx.org</a>.<br/>
Commercial support is available at
<a href="<http://nginx.com/>">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

#修改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
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.1.17</h3></body></html>[
#清理
kubectl delete -f rs.yaml

文章来源:https://blog.csdn.net/qq_72569959/article/details/135666066
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。