我们已经在我们的部署资源中定义了 K8s liveness 和 readiness probes (我们已经在那里挑战了 liveness ...),我们需要使用client-golib 来访问这个 liveness probe,我们怎么做?
我已经用client-golib试过了
https://github.com/kubernetes/client-go
如下:
client.Discovery().RESTClient().Get()
Run Code Online (Sandbox Code Playgroud)
我也试着玩的去图书馆,但没有发现任何部署的财产client.CoreV1()。但是我确实找到了service pod等等。我在这里错过了什么?
PodList, err := client.CoreV1().Pods("mynamespace").List(metav1.ListOptions{LabelSelector: "run=liveness-app"})
最后,我需要根据部署中定义的活性探针获取 pod 活性状态。我的意思是生或死
如何执行此操作取决于您想要做什么。
其中Deployment包含一个 PodTemplate,用于创建每个副本。
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
Run Code Online (Sandbox Code Playgroud)
您可以使用 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
}
Run Code Online (Sandbox Code Playgroud)
注意:仅Deployment包含所需的 PodTemplate,因此要查看任何状态,您必须查看创建的 Pod。
您可以使用与.xml 文件的选择器Deployment中相同的标签列出从部署创建的 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
}
}
Run Code Online (Sandbox Code Playgroud)
Statusa 的部分包含Pod一些conditions:-informationProbe和containerStatuses:with restartCount:,也在上面的 Go 示例中进行了说明。使用您的自定义逻辑来使用此信息。
每当livenessProbe失败时,Pod 就会重新启动。
的例子Pod Status
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
676 次 |
| 最近记录: |