K8s滚动更新算法

huangapple go评论85阅读模式
英文:

k8s rolling update algorithm

问题

我们已经创建了自己的自定义资源(CRD),我们需要添加滚动更新的支持。由于K8s支持部署等功能,我们希望重用这样的逻辑。是否有任何库可以(部分地)用于支持滚动更新?或者我们可以学习和遵循这个逻辑,因为我们不想重复造轮子?任何参考资料/库都会有所帮助。

我在这里找到了一些信息:链接

英文:

We have created our own custom resource a.k.a CRD and we need to add support for rolling update, as K8s is supporting it for deployments etc we want to reuse such logic, is there any lib which we can use (maybe partially) which we can use to support it? Or maybe learn and follow the logic as we don't want to re-invent the wheel? Any reference/lib would be helpful.

I've struggled to find this here.

答案1

得分: 2

发布了社区维基答案以总结问题。

Clark McCauley 提出了很好的建议:
>你可能正在寻找包含逻辑的这里

这是对k8s源代码的引用,所以你可能找不到更好的思路来源 K8s滚动更新算法

// rolloutRolling implements the logic for rolling a new replica set.
func (dc *DeploymentController) rolloutRolling(ctx context.Context, d *apps.Deployment, rsList []*apps.ReplicaSet) error {
	newRS, oldRSs, err := dc.getAllReplicaSetsAndSyncRevision(ctx, d, rsList, true)
	if err != nil {
		return err
	}
	allRSs := append(oldRSs, newRS)

	// Scale up, if we can.
	scaledUp, err := dc.reconcileNewReplicaSet(ctx, allRSs, newRS, d)
	if err != nil {
		return err
	}
	if scaledUp {
		// Update DeploymentStatus
		return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
	}

	// Scale down, if we can.
	scaledDown, err := dc.reconcileOldReplicaSets(ctx, allRSs, controller.FilterActiveReplicaSets(oldRSs), newRS, d)
	if err != nil {
		return err
	}
	if scaledDown {
		// Update DeploymentStatus
		return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
	}

	if deploymentutil.DeploymentComplete(d, &d.Status) {
		if err := dc.cleanupDeployment(ctx, oldRSs, d); err != nil {
			return err
		}
	}

	// Sync deployment status
	return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
}
英文:

Posted community wiki answer to summarise the problem.

[Clark McCauley](https://stackoverflow.com/users/13906951/clark-mccauley "1,093 reputation") well suggested:
>You're probably looking for the logic contained here.

This is a reference to the k8s source code, so you probably won't find a better source of ideas K8s滚动更新算法

// rolloutRolling implements the logic for rolling a new replica set.
func (dc *DeploymentController) rolloutRolling(ctx context.Context, d *apps.Deployment, rsList []*apps.ReplicaSet) error {
	newRS, oldRSs, err := dc.getAllReplicaSetsAndSyncRevision(ctx, d, rsList, true)
	if err != nil {
		return err
	}
	allRSs := append(oldRSs, newRS)

	// Scale up, if we can.
	scaledUp, err := dc.reconcileNewReplicaSet(ctx, allRSs, newRS, d)
	if err != nil {
		return err
	}
	if scaledUp {
		// Update DeploymentStatus
		return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
	}

	// Scale down, if we can.
	scaledDown, err := dc.reconcileOldReplicaSets(ctx, allRSs, controller.FilterActiveReplicaSets(oldRSs), newRS, d)
	if err != nil {
		return err
	}
	if scaledDown {
		// Update DeploymentStatus
		return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
	}

	if deploymentutil.DeploymentComplete(d, &d.Status) {
		if err := dc.cleanupDeployment(ctx, oldRSs, d); err != nil {
			return err
		}
	}

	// Sync deployment status
	return dc.syncRolloutStatus(ctx, allRSs, newRS, d)
}

huangapple
  • 本文由 发表于 2022年3月21日 14:53:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/71553777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定