Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of rolling update Mechanism in kubernetes

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article shares with you the content of a sample analysis of the rolling update mechanism in kubernetes. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

# # 0. Basics of command line and dependency

SynopsisPerform a rolling update of the given ReplicationController.Replaces the specified controller with new controller, updating one pod at a time to use thenew PodTemplate. The new-controller.json must specify the same namespace as theexisting controller and overwrite at least one (common) label in its replicaSelector.kubectl rolling-update OLD_CONTROLLER_NAME-f NEW_CONTROLLER_SPECExamples// Update pods of frontend-v1 using new controller data in frontend-v2.json.$ kubectl rolling-update frontend-v1-f frontend-v2.json// Update pods of frontend-v1 using JSON data passed into stdin.$ cat frontend-v2.json | kubectl rolling-update frontend-v1-f-

ReplicationController, referred to as rc, is a collection of a certain type of pod in the kubernet system. Rc has a key parameter called replicas, that is, the number of pod.

So what's the use of rc? This is to solve the problem that if some of the pod on the cluster fails, start the container on another host and import the business traffic to the correctly started pod. In other words, rc ensures the availability of cluster services. When you have many services started in a cluster, you need to use programs to monitor the health of these services and dynamically ensure that services are available.

What is the correspondence between rc and pod? Rc selects some pod as his scope of control through selector. As long as the label of pod conforms to seletor, it belongs to this rc. Here are examples of pod and rc.

Xx-controller.json

"spec": {"replicas": 1, "selector": {"name": "redis", "role": "master"}

Xx-pod.json

"labels": {"name": "redis"}

Kubernetes is referred to as k8s for short. If you are interested in the basic concepts, you can read this article.

# # 1.kubctl entry

/ cmd/kubectl/kubctl.go

Func main () {runtime.GOMAXPROCS (runtime.NumCPU ()) cmd: = cmd.NewKubectlCommand (cmdutil.NewFactory (nil), os.Stdin, os.Stdout, os.Stderr) if err: = cmd.Execute (); err! = nil {os.Exit (1)}}

# # 2. Actual call

The source code is in the pkg package, / pkg/kubectl/cmd/cmd.go, and each subcommand implements a unified interface. The line rollingupdate is:

Cmds.AddCommand (NewCmdRollingUpdate (f, out))

The implementation of this function is: / pkg/kubectl/cmd/rollingupdate.go

Func NewCmdRollingUpdate (f * cmdutil.Factory, out io.Writer) * cobra.Command {cmd: = & cobra.Command {Use: "rolling-update OLD_CONTROLLER_NAME-f NEW_CONTROLLER_SPEC", / / rollingupdate is deprecated. Aliases: [] string {"rollingupdate"}, Short: "Perform a rollingupdate of the given ReplicationController.", Long: rollingUpdate_long, Example: rollingUpdate_example, Run: func (cmd * cobra.Command, args [] string) {err: = RunRollingUpdate (f, out, cmd) Args) cmdutil.CheckErr (err)},}}

You can see that the execution function of the actual call is RunRollingUpdate, which is to the point.

Func RunRollingUpdate (f * cmdutil.Factory, out io.Writer, cmd * cobra.Command, args [] string) error {... Mapper, typer: = f.Object () / / TODO: use resource.Builder instead obj, err: = resource.NewBuilder (mapper, typer, f.ClientMapperForCommand ()). NamespaceParam (cmdNamespace). RequireNamespace () FilenameParam (filename). Do (). Object () if err! = nil {return err} newRc, ok: = obj. (* api.ReplicationController) if! ok {return cmdutil.UsageError (cmd, "% s does not specify a valid ReplicationController", filename)}

This is the code to create a new rc, where resource is the base class for all kubneter resources (pod,service,rc). You can see that the new rc takes all the information from the json parameter file and then escapes to the class ReplicationController.

If oldName = = newName {return cmdutil.UsageError (cmd, "% s cannot have the same name as the existing ReplicationController% s", filename, oldName)} var hasLabel bool for key, oldValue: = range oldRc.Spec.Selector {if newValue, ok: = newRc.Spec.Selector [key] Ok & & newValue! = oldValue {hasLabel = true break}} if! hasLabel {return cmdutil.UsageError (cmd, "% s must specify a matching key with non-equal value in Selector for% s", filename, oldName)}

As you can see here, there are two restrictions on the new rc and the old rc, one is that the old and new names need to be different, and the other is that at least one item in the selector of rc needs to have a different value.

Updater: = kubectl.NewRollingUpdater (newRc.Namespace, client) / / fetch rc oldRc, err: = client.ReplicationControllers (newRc.Namespace) .Get (oldName) if err! = nil {return err}. Err = updater.Update (out, oldRc, newRc, period, interval, timeout) if err! = nil {return err}

When doing rolling update, there are two restrictions, one is that the name of the new rc needs to be different from the old one, and the second is that at least one tag has a different value. Among them, namespace is used by K8s for multi-tenant resource isolation, which can be ignored.

# # 3. Data structure and implementation

NewRollingUpdater appears in this code, which is in the / pkg/kubectl/rollingupdate.go file above, which is closer to the body.

/ / RollingUpdater provides methods for updating replicated pods in a predictable,// fault-tolerant way.type RollingUpdater struct {/ / Client interface for creating and updating controllers c client.Interface / / Namespace for resources ns string}

You can see that inside the RollingUpdater here is a k8s client structure to send commands to api server.

Func (r * RollingUpdater) Update (out io.Writer, oldRc, newRc * api.ReplicationController, updatePeriod, interval, timeout time.Duration) error {oldName: = oldRc.ObjectMeta.Name newName: = newRc.ObjectMeta.Name retry: = & RetryParams {interval, timeout} waitForReplicas: = & RetryParams {interval, timeout} if newRc.Spec.Replicas

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report