statefulset 控制器

应用程序存在“有状态”和“无状态”两种类别,因为无状态类应用的 Pod 资源可按需增加、减少或重构,而不会对由其提供的服务产生除了并发响应能力之外的其他严重影响。Pod 资源的常用控制器中,Deployment、ReplicaSet 和 DaemonSet 等常用于管理无状态应用.但实际情况是,应用本身就是分布式的集群,各应用实例彼此之间存在着关联关系,甚至是次序、角色方面的相关性,其中的每个实例都有其自身的独特性而无法轻易由其他实例所取代.

使用 statefulset 需要提前将有状态服务的运维管理过程制作成脚本注入到 statefulset 文件当中,但使用起来依然十分困难,好在 kubenerter 支持TPR\CRD 第三方资源或 API 聚合但需要使用 GO 语言重铸 K8s,所以出现很多诸如CoreOS 的 Operator 封装协助管理有状态服务的第三方插件

有状态应用副本集

有状态服务关注群体 cattle 而有状态服务关注个体 pet 之分,k8s 从 1.3 支持 petSet 之后到 1.5 改名为 statefulset,它可以管理具有以下特性的服务

  • 稳定且唯一的网络标识符
  • 稳定且持久的存储
  • 优雅、有序且平稳的部署和拓展
  • 优雅、有序且平稳的终止和删除
  • 优雅、有序且平稳的滚动更新

且应当有三个组件组成:

  • headless service: 确保解析的名称直达后端 podIP 地址并且给每一个 pod 配置一个唯一的名称
  • Statefulset: 管理有状态应用
  • volumeClaimTemplate: 每一个 pod 都需要自己独立的 PVC 和 PV

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kRI6s3B0-1624628667161)(https://raw.githubusercontent.com/ma823956028/image/master/picgo/20200913120105.png)]

statefulset模板

[root@master-0 ~]# kubectl explain sts
KIND:     StatefulSet
VERSION:  apps/v1

DESCRIPTION:
     StatefulSet represents a set of pods with consistent identities. Identities
     are defined as: - Network: A single stable DNS and hostname. - Storage: As
     many VolumeClaims as requested. The StatefulSet guarantees that a given
     network identity will always map to the same storage identity.

FIELDS:
   apiVersion <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata <Object>

   spec <Object>
     Spec defines the desired identities of pods in this set.

   status <Object>
     Status is the current status of Pods in this StatefulSet. This data may be
     out of date by some window of time.
[root@master-0 ~]# kubectl explain sts.spec
KIND:     StatefulSet
VERSION:  apps/v1

RESOURCE: spec <Object>

DESCRIPTION:
     Spec defines the desired identities of pods in this set.

     A StatefulSetSpec is the specification of a StatefulSet.

FIELDS:
   podManagementPolicy <string>
     podManagementPolicy controls how pods are created during initial scale up,
     when replacing pods on nodes, or when scaling down. The default policy is
     `OrderedReady`, where pods are created in increasing order (pod-0, then
     pod-1, etc) and the controller will wait until each pod is ready before
     continuing. When scaling down, the pods are removed in the opposite order.
     The alternative policy is `Parallel` which will create pods in parallel to
     match the desired scale without waiting, and on scale down will delete all
     pods at once.

   replicas <integer>       # 副本数
     replicas is the desired number of replicas of the given Template. These are
     replicas in the sense that they are instantiations of the same Template,
     but individual replicas also have a consistent identity. If unspecified,
     defaults to 1.

   revisionHistoryLimit <integer>
     revisionHistoryLimit is the maximum number of revisions that will be
     maintained in the StatefulSet's revision history. The revision history
     consists of all revisions not represented by a currently applied
     StatefulSetSpec version. The default value is 10.

   selector <Object> -required-     # 标签选择器
     selector is a label query over pods that should match the replica count. It
     must match the pod template's labels. More info:
     https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors

   serviceName <string> -required-  # 无头服务的 service
     serviceName is the name of the service that governs this StatefulSet. This
     service must exist before the StatefulSet, and is responsible for the
     network identity of the set. Pods get DNS/hostnames that follow the
     pattern: pod-specific-string.serviceName.default.svc.cluster.local where
     "pod-specific-string" is managed by the StatefulSet controller.

   template <Object> -required-     # pod 模板,挂载关联某个 PVC
     template is the object that describes the pod that will be created if
     insufficient replicas are detected. Each pod stamped out by the StatefulSet
     will fulfill this Template, but have a unique identity from the rest of the
     StatefulSet.

   updateStrategy <Object>
     updateStrategy indicates the StatefulSetUpdateStrategy that will be
     employed to update Pods in the StatefulSet when a revision is made to
     Template.

   volumeClaimTemplates <[]Object>  # volume 模板
     volumeClaimTemplates is a list of claims that pods are allowed to
     reference. The StatefulSet controller is responsible for mapping network
     identities to claims in a way that maintains the identity of a pod. Every
     claim in this list must have at least one matching (by name) volumeMount in
     one container in the template. A claim in this list takes precedence over
     any volumes in the template, with the same name.

statefulset 资源升级

自Kubernetes 1.7版本起,StatefulSet 资源支持自动更新机制,其更新策略将由 spec.updateStrategy 字段定义,默认为 RollingUpdate,即滚动更新,另一个可用策略为 OnDelete,即删除 Pod 资源重建以完成更新,这也是Kubernetes 1.6及之前版本唯一可用的更新策略,StatefulSet 资源的更新机制可用于更新 Pod 资源中的容器镜像、标签、注解和系统资源配额等

暂存更新操作

当用户需要设定一个更新操作,但又不希望它立即执行时,可将更新操作予以暂存,待条件满足后再手动触发其执行更新,StatefulSet 资源的分区更新机制能够实现此项功能,在设定更新操作之前,将.spec.update-Strategy.rollingUpdate.partition 字段的值设置为 Pod 资源的副本数量,即比 Pod 资源的最大索引号大1,这就意味着,所有的 Pod 资源都不会处于可直接更新的分区,于此特性我们可以得以实现金丝雀部署

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nGChVqwP-1624628667163)(https://raw.githubusercontent.com/ma823956028/image/master/picgo/20200913133531.png)]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值