飞天使-k8s知识点20-kubernetes实操5-pod更新与暂停-statefulset

文章目录

        • [资源调度 Deployment:扩缩容](#资源调度 Deployment:扩缩容)
        • [资源调度 Deployment:更新的暂停与恢复](#资源调度 Deployment:更新的暂停与恢复)
        • [资源调度 StatefulSet:定义一个有状态服务](#资源调度 StatefulSet:定义一个有状态服务)
资源调度 Deployment:扩缩容
复制代码
扩容和缩容,常用的功能
scale

[root@kubeadm-master1 ~]# kubectl scale --help
Set a new size for a Deployment, ReplicaSet, Replication Controller, or StatefulSet.

 Scale also allows users to specify one or more preconditions for the scale action.

 If --current-replicas or --resource-version is specified, it is validated before the scale is attempted, and it is
guaranteed that the precondition holds true when the scale is sent to the server.

Examples:
  # Scale a replicaset named 'foo' to 3.
  kubectl scale --replicas=3 rs/foo

  # Scale a resource identified by type and name specified in "foo.yaml" to 3.
  kubectl scale --replicas=3 -f foo.yaml

  # If the deployment named mysql's current size is 2, scale mysql to 3.
  kubectl scale --current-replicas=2 --replicas=3 deployment/mysql

  # Scale multiple replication controllers.
  kubectl scale --replicas=5 rc/foo rc/bar rc/baz

  # Scale statefulset named 'web' to 3.
  kubectl scale --replicas=3 statefulset/web

实操部分

[root@kubeadm-master1 ~]# kubectl get rs
NAME                           DESIRED   CURRENT   READY   AGE
nginx-deploy-845964f5bf        4         4         4       61m
nginx-deploy-968b78ccf         0         0         0       41m
nginx-deployment-67dfd6c8f9    1         1         1       56d
tomcat-deployment-6c44f58b47   1         1         1       56d
[root@kubeadm-master1 ~]# kubectl get pod
NAME                                 READY   STATUS      RESTARTS   AGE
client                               1/1     Running     0          50d
my-pod                               1/1     Running     0          9d
my-pod1                              0/1     Completed   6          20h
net-test1                            1/1     Running     138        57d
net-test2                            1/1     Running     14         57d
nginx-deploy-845964f5bf-dqdzt        1/1     Running     0          6m33s
nginx-deploy-845964f5bf-k8vbj        1/1     Running     0          6m35s
nginx-deploy-845964f5bf-pxs78        1/1     Running     0          10s
nginx-deploy-845964f5bf-xk49f        1/1     Running     0          6m34s
nginx-deployment-67dfd6c8f9-5s6nz    1/1     Running     1          56d
tomcat-deployment-6c44f58b47-4pz6d   1/1     Running     1          56d
[root@kubeadm-master1 ~]# kubectl scale --replicas=2 deploy nginx-deploy
deployment.apps/nginx-deploy scaled
[root@kubeadm-master1 ~]# kubectl get rs
NAME                           DESIRED   CURRENT   READY   AGE
nginx-deploy-845964f5bf        2         2         2       61m
nginx-deploy-968b78ccf         0         0         0       41m
nginx-deployment-67dfd6c8f9    1         1         1       56d
tomcat-deployment-6c44f58b47   1         1         1       56d
[root@kubeadm-master1 ~]# kubectl get pod
NAME                                 READY   STATUS      RESTARTS   AGE
client                               1/1     Running     0          50d
my-pod                               1/1     Running     0          9d
my-pod1                              0/1     Completed   6          20h
net-test1                            1/1     Running     138        57d
net-test2                            1/1     Running     14         57d
nginx-deploy-845964f5bf-k8vbj        1/1     Running     0          7m1s
nginx-deploy-845964f5bf-xk49f        1/1     Running     0          7m
nginx-deployment-67dfd6c8f9-5s6nz    1/1     Running     1          56d
tomcat-deployment-6c44f58b47-4pz6d   1/1     Running     1          56d
资源调度 Deployment:更新的暂停与恢复
复制代码
kubectl rollout pause 和 kubectl rollout resume 的详细示例。

假设你有一个名为 my-deployment 的 Deployment,它运行的是 my-app:v1 的镜像。你想要把镜像更新为 my-app:v2,但是你希望在更新之前先暂停 Deployment,以便进行一些配置的修改。

首先,用以下命令暂停 Deployment:

kubectl rollout pause deployment/my-deployment
然后,你可以修改 Deployment 的配置。比如,你可以用以下命令更换镜像:

kubectl set image deployment/my-deployment my-app=my-app:v2
此时,即使你修改了 Deployment 的配置,也不会触发新的滚动更新,因为 Deployment 正处于暂停状态。

当你准备好进行更新时,你可以用以下命令恢复 Deployment:

kubectl rollout resume deployment/my-deployment
恢复后,Deployment 控制器会开始新的滚动更新,使用你新设置的 my-app:v2 镜像。
资源调度 StatefulSet:定义一个有状态服务
复制代码



复制代码
Kubernetes提供了StatefulSet来解决这个问题,其具体如下:

StatefulSet给每个Pod提供固定名称,Pod名称增加从0-N的固定后缀,Pod重新调度后Pod名称和HostName不变。
StatefulSet通过Headless Service给每个Pod提供固定的访问域名。
StatefulSet通过创建固定标识的PVC保证Pod重新调度后还是能访问到相同的持久化数据。
headless service
复制代码
 PersistentVolume (PV) 和 PersistentVolumeClaim (PVC) 都是 Kubernetes 中用于管理存储的资源,但它们的角色和用途有所不同。

PersistentVolume (PV) 是集群中的一部分存储,它被管理员提前配置好。这可以是节点内部的某个目录,如一个本地路径(在单节点的测试环境中常见),或者是某种网络存储,如 Google Compute Engine persistent disk,AWS Elastic Block Store volume,NFS mount,或者其他存储系统。PV 是集群资源,与 Pod 的生命周期无关,即使使用 PV 的 Pod 被删除,PV 也会保留其数据。

PersistentVolumeClaim (PVC) 则是用户在 PV 中申请存储空间的请求。PVC 可以请求特定大小和访问模式的 PV。例如,用户可以请求一个大小为 10Gi 和 ReadWriteOnce 访问模式的 PV。Kubernetes 会寻找一个匹配的 PV 并将其绑定到 PVC。

简单来说,PV 是代表集群中的实际存储空间,而 PVC 是 Pod 对这个存储空间的需求或请求。



Statefulset的YAML定义与其他对象基本相同,主要有两个差异点:

serviceName指定了Statefulset使用哪个Headless Service,需要填写Headless Service的名称。
volumeClaimTemplates是用来申请持久化声明PVC ,这里定义了一个名为data的模板,它会为每个Pod创建一个PVC,storageClassName指定了持久化存储的类型,在PV、PVC和StorageClass会详细介绍;volumeMounts是为Pod挂载存储。当然如果不需要存储的话可以删除volumeClaimTemplates和volumeMounts字段。
金丝雀发布


参考文档:https://support.huaweicloud.com/basics-cce/kubernetes_0015.html

相关推荐
AAA@峥2 小时前
容器数据不丢失:Docker 分层存储 + Volume 共享、备份迁移完整指南
运维·docker·容器
十年磨一剑~3 小时前
docker 为何每次 docker tag才能 docker push
docker·容器·eureka
gs801405 小时前
记一次多 Agent 架构在单节点 K8s 触发的 PID 耗尽与 Pod 驱逐(Evicted)大摸排
容器·架构·kubernetes
bukeyiwanshui6 小时前
20260701 k8s Metric Server
容器·贪心算法·kubernetes
江湖有缘6 小时前
基于Docker环境部署tillywork开源工作管理工具
docker·容器·开源
鸿儒5179 小时前
MR-50国产化GPU docker配置方法
docker·容器·mr
Zhu75810 小时前
在k8s集群部署ApacheHadoop单节点单数据副本
云原生·容器·kubernetes
Akamai中国10 小时前
Akamai收购安全企业浏览器提供商LayerX
人工智能·云原生·云计算·云服务
摇滚侠10 小时前
云原生 Java 架构师的第一课 K8s+Docker+KubeSphere+DevOps 51-60
java·云原生·kubernetes