kubectl 命令查找特定 pod 状态为就绪
kubectl command to find a particular pod status as ready
我想执行一个命令,该命令使用服务名称搜索 pod 并将其 pod 的状态标识为“就绪”
我尝试了一些命令,但它不起作用,并且不使用服务名称进行搜索。
kubectl get svc | grep my-service | --output="jsonpath={.status.containerStatuses[*].ready}" | cut -d' ' -f2
我也尝试使用循环,但脚本没有给出所需的输出。
你能帮我找出确切的命令吗?
每个服务都会创建一个端点,其中包含服务的 podIp 和其他信息。你可以用 endpoints
得到你 pods。 .它会向您显示 my-service
.
的准备好的 pod
使用这个命令:
kubectl get endpoints -n <Name_space> <service_name> -o json | jq -r 'select(.subsets != null) | select(.subsets[].addresses != null) | .subsets[].addresses[].targetRef.name'
你的命令是:
kubectl get endpoints my-service -o json | jq -r 'select(.subsets != null) | select(.subsets[].addresses != null) | .subsets[].addresses[].targetRef.name'
您可以运行获取 pod 状态的脚本
#!/usr/bin/env bash
for podname in $(kubectl get endpoints my-service -o json | jq -r 'select(.subsets != null) | .subsets[].addresses[].targetRef.name')
do
kubectl get pods -n demo $podname -o json | jq -r ' select(.status.conditions[].type == "Ready") | .status.conditions[].type ' | grep -x Ready
done
如果我没理解错的话,你想知道连接到特定 Endpoint
的特定 Pod
是否处于 "Ready"
状态。
使用 JSON PATH
您可以显示特定 namespace
中的所有 Pods
及其状态:
$ kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'
如果您要查找 Pods
连接到特定 Endpoint
的状态,您可以使用以下脚本:
#!/bin/bash
endpointName="web-1"
for podName in $(kubectl get endpoints $endpointName -o=jsonpath={.subsets[*].addresses[*].targetRef.name}); do
if [ ! -z $podName ]; then
kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}' | grep $podName
fi
done
for podName in $(kubectl get endpoints $endpointName -o=jsonpath={.subsets[*].notReadyAddresses[*].targetRef.name}); do
if [ ! -z $podName ]; then
kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}' | grep $podName
fi
done
请注意,您需要根据需要更改 Endpoint
名称,在上面的示例中我使用 web-1
名称。
如果此回复没有回答您的问题,请说明您的确切目的。
我想执行一个命令,该命令使用服务名称搜索 pod 并将其 pod 的状态标识为“就绪”
我尝试了一些命令,但它不起作用,并且不使用服务名称进行搜索。
kubectl get svc | grep my-service | --output="jsonpath={.status.containerStatuses[*].ready}" | cut -d' ' -f2
我也尝试使用循环,但脚本没有给出所需的输出。
你能帮我找出确切的命令吗?
每个服务都会创建一个端点,其中包含服务的 podIp 和其他信息。你可以用 endpoints
得到你 pods。 .它会向您显示 my-service
.
使用这个命令:
kubectl get endpoints -n <Name_space> <service_name> -o json | jq -r 'select(.subsets != null) | select(.subsets[].addresses != null) | .subsets[].addresses[].targetRef.name'
你的命令是:
kubectl get endpoints my-service -o json | jq -r 'select(.subsets != null) | select(.subsets[].addresses != null) | .subsets[].addresses[].targetRef.name'
您可以运行获取 pod 状态的脚本
#!/usr/bin/env bash
for podname in $(kubectl get endpoints my-service -o json | jq -r 'select(.subsets != null) | .subsets[].addresses[].targetRef.name')
do
kubectl get pods -n demo $podname -o json | jq -r ' select(.status.conditions[].type == "Ready") | .status.conditions[].type ' | grep -x Ready
done
如果我没理解错的话,你想知道连接到特定 Endpoint
的特定 Pod
是否处于 "Ready"
状态。
使用 JSON PATH
您可以显示特定 namespace
中的所有 Pods
及其状态:
$ kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}'
如果您要查找 Pods
连接到特定 Endpoint
的状态,您可以使用以下脚本:
#!/bin/bash
endpointName="web-1"
for podName in $(kubectl get endpoints $endpointName -o=jsonpath={.subsets[*].addresses[*].targetRef.name}); do
if [ ! -z $podName ]; then
kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}' | grep $podName
fi
done
for podName in $(kubectl get endpoints $endpointName -o=jsonpath={.subsets[*].notReadyAddresses[*].targetRef.name}); do
if [ ! -z $podName ]; then
kubectl get pod -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.phase}{"\n"}{end}' | grep $podName
fi
done
请注意,您需要根据需要更改 Endpoint
名称,在上面的示例中我使用 web-1
名称。
如果此回复没有回答您的问题,请说明您的确切目的。