In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
In this issue, the editor will bring you about how to create and manage Pod. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
The Pod synchronization process is the core main process of the kubelet process. The creation and management of Pod, the most critical part of the main process, will be analyzed below. This part of the logic is encapsulated in the kubeGenericRuntimeManager.SyncPod (kuberuntime_manager.go) method and mainly performs the following steps:
Calculate the Actions that needs to be performed based on the Pod Spec obtained from API Server and the Status of the current Pod
Kill drops the current Pod if needed
Kill the containers in the pod as needed (such as restarting)
Create a sandbox container for Pod as needed
Start the next init container
Start containers in Pod
The more complex steps are explained as follows:
1.computePodActions
Compare spec with status to calculate the actions required to achieve the desired state:
/ / computePodActions checks whether the pod spec has changed and returns the changes if true.
Func (m * kubeGenericRuntimeManager) computePodActions (pod * v1.Pod, podStatus * kubecontainer.PodStatus) podActions {
/ / 1. Compare the sandbox status to calculate whether a sandbox needs to be created and the current sandbox id
CreatePodSandbox, attempt, sandboxID: = m.podSandboxChanged (pod, podStatus)
Changes: = podActions {
KillPod: createPodSandbox
CreateSandbox: createPodSandbox
SandboxID: sandboxID
Attempt: attempt
ContainersToStart: [] int {}
ContainersToKill: make (map [kubecontainer.ContainerID] containerToKillInfo)
}
/ / 2. You need to create a new sandbox. Once you enter the branch, you must return, and then the code will no longer be executed.
If createPodSandbox {
....
/ / in the new sandbox branch, if there is an init container, take the first one and return
If len (pod.Spec.InitContainers)! = 0 {
/ / Pod has init containers, return the first one.
Changes.NextInitContainerToStart = & pod.Spec.InitContainers [0]
Return changes
}
/ / No init container exists. Go to work containers directly.
For idx, c: = range pod.Spec.Containers {
If containerSucceeded (& c, podStatus) & & pod.Spec.RestartPolicy = = v1.RestartPolicyOnFailure {
Continue
}
Changes.ContainersToStart = append (changes.ContainersToStart, idx)
}
Return changes
}
/ / 3. Sandbox is running. Start the init container. Find the next init container to execute
InitLastStatus, next, done: = findNextInitContainerToRun (pod, podStatus)
If! done {
If next! = nil {
InitFailed: = initLastStatus! = nil & & isContainerFailed (initLastStatus)
If initFailed & &! shouldRestartOnFailure (pod) {
Changes.KillPod = true
} else {
Changes.NextInitContainerToStart = next
}
}
/ / if the init is not completed, return it directly
Return changes
}
/ / 4. Init has been completed, and calculation requires the working container of kill&start
KeepCount: = 0
For idx, container: = range pod.Spec.Containers {
.
}
/ / 5. Is kill pod required?
If keepCount = = 0 & & len (changes.ContainersToStart) = 0 {
Changes.KillPod = true
}
Return changes
}
2. SyncPod
This approach is the key to pod management and implements the six steps described at the beginning of this article:
Func (m * kubeGenericRuntimeManager) SyncPod (pod * v1.Pod, _ v1.PodStatus, podStatus * kubecontainer.PodStatus, pullSecrets [] v1.Secret, backOff * flowcontrol.Backoff) (result kubecontainer.PodSyncResult) {
/ / 1. Calculate pod actions, see above
PodContainerChanges: = m.computePodActions (pod, podStatus)
.
/ / 2. Execute kill pod if necessary
If podContainerChanges.KillPod {
....
KillResult: = m.killPodWithSyncResult (pod, kubecontainer.ConvertPodStatusToRunningPod (m.runtimeName, podStatus), nil)
....
} else {
/ / 3. Kill pod is not required, but kill working container is required
For containerID, containerInfo: = range podContainerChanges.ContainersToKill {
....
If err: = m.killContainer (pod, containerID, containerInfo.name, containerInfo.message, nil); err! = nil {
...
Return
}
}
}
.
/ / 4. Create sandbox on demand
PodSandboxID: = podContainerChanges.SandboxID
If podContainerChanges.CreateSandbox {
.
PodSandboxID, msg, err = m.createPodSandbox (pod, podContainerChanges.Attempt)
....
}
....
/ / 5. Run next init container
If container: = podContainerChanges.NextInitContainerToStart; container! = nil {
....
If msg, err: = m.startContainer (podSandboxID, podSandboxConfig, container, pod, podStatus, pullSecrets, podIP); err! = nil {
StartContainerResult.Fail (err, msg)
Utilruntime.HandleError (fmt.Errorf ("init container start failed:% v:% s", err, msg))
Return
}
....
}
/ / 6. Run the working containers. Note that according to computePodActions, if NextInitContainerToStart is not empty, there is no ContainersToStart, that is, the loop will not be executed in the current SyncPod
For _, idx: = range podContainerChanges.ContainersToStart {
....
If msg, err: = m.startContainer (podSandboxID, podSandboxConfig, container, pod, podStatus, pullSecrets, podIP); err! = nil {
StartContainerResult.Fail (err, msg)
/ / known errors that are logged in other places are logged at higher levels here to avoid
/ / repetitive log spam
Switch {
Case err = = images.ErrImagePullBackOff:
Glog.V (3) .Infof ("container start failed:% v:% s", err, msg)
Default:
Utilruntime.HandleError (fmt.Errorf ("container start failed:% v:% s", err, msg))
}
Continue
}
}
Return
}
One thing to note in SyncPod is that during init containers startup, SyncPod runs only one init container (next) at a time, and then returns.
The above is the editor for you to share how to achieve the creation and management of Pod, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.