要检查部署状态,您需要检查此部署创建的 pod 状态。
示例:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: myapp
name: myapp
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- image: myimage
name: myapp
livenessProbe:
# your desired liveness check
您可以使用 client-go 从部署中获取所需的 PodTemplate
例如:
clientset := kubernetes.NewForConfigOrDie(config)
deploymentClient := clientset.AppsV1().Deployments("mynamespace")
deployment, err := deploymentClient.Get("myapp", metav1.GetOptions{})
for _, container := range deployment.Spec.Template.Spec.Containers {
container.LivenessProbe // add your logic
}
注意: Deployment 仅包含所需的 PodTemplate,因此要查看任何状态,您必须查看已创建的 Pod。
豆荚
您可以使用与部署选择器中相同的标签列出从部署创建的 Pod。
Pod 示例列表:
pods, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{
LabelSelector: "app=myapp",
})
// check the status for the pods - to see Probe status
for _, pod := range pods.Items {
pod.Status.Conditions // use your custom logic here
for _, container := range pod.Status.ContainerStatuses {
container.RestartCount // use this number in your logic
}
}
Pod 的 Status 部分包含条件:with some Probe-information 和 containerStatuses: with restartCount:,在上面的 Go 示例中也有说明。使用您的自定义逻辑来使用此信息。
只要 livenessProbe 失败,Pod 就会重新启动。
Pod 状态示例:
status:
conditions:
- lastProbeTime: null
lastTransitionTime: "2020-09-15T07:17:25Z"
status: "True"
type: Initialized
containerStatuses:
- containerID: docker://25b28170c8cec18ca3af0e9c792620a3edaf36aed02849d08c56b78610dec31b
image: myimage
imageID: docker-pullable://myimage@sha256:a432251b2674d24858f72b1392033e0d7a79786425555714d8e9a656505fa08c
name: myapp
restartCount: 0
希望能帮助您解决问题。