如何编写shell脚本以获取Kubernetes集群中的Pod状态。

huangapple go评论71阅读模式
英文:

How to write shell script to get the pod status of kubernetes cluster

问题

在我的K8集群中,我有正在运行的Pod,Pod名称包含单词“inventory”(实际上有两个具有这个名称的Pod)。
我需要编写一个Shell脚本来删除部署,然后重新部署,然后检查特定的Pod是否正在运行,然后显示一条消息。
以下是我尝试的内容。

#!/bin/bash
cd /apps/application/application_yamls_develop/deployment-artifacts/inventory-service
kubectl delete -f inventory-service.yaml -n back-end
kubectl apply -f inventory-service.yaml -n back-end
sleep 20

pod_name="inventory"
namespace="back-end"

# 获取Pod的状态
pod_status=$(kubectl get pods -n "$namespace" -o jsonpath="{.items[?(@.metadata.name.contains('$pod_name'))].status.phase}")

# 检查是否有任何匹配的Pod正在运行
if [[ -n "$pod_status" ]]; then
  echo "Pod正在运行"
else
  echo "Pod未运行"
fi

但是这会产生以下错误。

error: error parsing jsonpath {.items[?(@.metadata.name.includes('inventory'))].status.phase}, unclosed array expect ]

有人能指出问题吗?

谢谢..!

英文:

In my k8 cluster I have pods running that the pod name contains the word "inventory" (actually 2pods with this name)
I need to write a shell script to delete deployment and re-deploy and then it should check the specific pod is running or not and then display a message.
Here is what I tried.

#!/bin/bash
cd /apps/application/application_yamls_develop/deployment-artifacts/inventory-service
kubectl delete -f inventory-service.yaml -n back-end
kubectl apply -f inventory-service.yaml -n back-end
sleep 20

pod_name="inventory"
namespace="back-end"

# Get the pod status
pod_status=$(kubectl get pods -n "$namespace" -o jsonpath="{.items[?(@.metadata.name.includes('$pod_name'))].status.phase}")

# Check if any matching pod is running
if [[ -n "$pod_status" ]]; then
  echo "Pod running"
else
  echo "Pod not running"
fi

But this is giving the bellow error.

error: error parsing jsonpath {.items[?(@.metadata.name.includes('inventory'))].status.phase}, unclosed array expect ]

Can someone please pointout the issue.

Thanks..!

答案1

得分: 2

你能试试这个

<!-- 开始代码片段: js 隐藏: false 控制台: true babel: false -->

<!-- 语言: lang-html -->

#!/bin/bash

pod_name="devopslk"
namespace="devops"

# 获取 pod 状态
pod_status=$(kubectl get pod "$pod_name" -n "$namespace" -o jsonpath='{.status.phase}')

# 检查是否有匹配的运行中的 pod
if [[ -n "$pod_status" ]]; then
  echo "Pod 正在运行"
else
  echo "Pod 未运行,当前状态:$pod_status"
fi

<!-- 结束代码片段 -->

如何编写shell脚本以获取Kubernetes集群中的Pod状态。

英文:

Can you try this

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

#!/bin/bash

pod_name=&quot;devopslk&quot;
namespace=&quot;devops&quot;

# Get the pod status
pod_status=$(kubectl get pod &quot;$pod_name&quot; -n &quot;$namespace&quot; -o jsonpath=&#39;{.status.phase}&#39;)

# Check if any matching pod is running
if [[ -n &quot;$pod_status&quot; ]]; then
  echo &quot;Pod running&quot;
else
  echo &quot;Pod not running, Current status: $pod_status&quot;
fi

<!-- end snippet -->

如何编写shell脚本以获取Kubernetes集群中的Pod状态。

答案2

得分: 1

在k8s 文档 中找到了答案。

# kubectl不支持JSONpath输出的正则表达式
# 以下命令不起作用
kubectl get pods -o jsonpath='{.items[?(@.metadata.name=~/^test$/)].metadata.name}'

# 以下命令实现了所需的结果
kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).spec.containers[].image'

在过滤语句中部分名称也不起作用,您必须在此处设置完整的Pod名称:

$ k -n $ns get po -o jsonpath='{.items[?(@.metadata.name=="test")].metadata.name}'

$ k -n $ns get po -o jsonpath='{.items[?(@.metadata.name=="test-56b87dd7dc-4fcwn")].metadata.name}'
test-56b87dd7dc-4fcwn

所以,首先您需要获取所有的Pod,然后以某种方式对它们进行筛选,以获取所需的Pod,然后检查其状态。或者按照k8s文档中建议的使用jq

或者同时获取名称和状态:

$ k -n $ns get po -o jsonpath="{range .items[*]}[{.metadata.name},{.status.phase}]{'\n'}{end}" |\
grep -Ei ".*$pod_name.*running" || echo fail

然后再次使用grep筛选所需的Pod,您将获得其状态。

英文:

Found answer in k8s docs

# kubectl does not support regular expressions for JSONpath output
# The following command does not work
kubectl get pods -o jsonpath=&#39;{.items[?(@.metadata.name=~/^test$/)].metadata.name}&#39;

# The following command achieves the desired result
kubectl get pods -o json | jq -r &#39;.items[] | select(.metadata.name | test(&quot;test-&quot;)).spec.containers[].image&#39;

And partial name in filter statement won't work also you have to set full pod name here:

$ k -n $ns get po -o jsonpath=&#39;{.items[?(@.metadata.name==&quot;test&quot;)].metadata.name}&#39;

$ k -n $ns get po -o jsonpath=&#39;{.items[?(@.metadata.name==&quot;test-56b87dd7dc-4fcwn&quot;)].metadata.name}&#39; 
test-56b87dd7dc-4fcwn

So first you need to get all pods and grep them some how to get the needed pod and than check it status. Or use jq as suggested in k8s docs.

Or get names and statuses at the same time:

    $ k -n $ns get po -o jsonpath=&quot;{range .items[*]}[{.metadata.name},{.status.phase}]{&#39;\n&#39;}{end}&quot; |\
    grep -Ei &quot;.*$pod_name.*running&quot; || echo fail

And then again grep needed pod and you'll get the status.

答案3

得分: 0

这可以通过使用下面的脚本中指定的 pod 标签来实现(根据您提供的修改后的内容)。

#!/bin/bash
# 命名空间名称
namespace="backend"
# 提供在 pod 定义中指定的标签名称
appname="inventory"

# 获取 pod 名称
pod_name=$(kubectl get pod -n $namespace -l app=$appname -o=jsonpath="{range .items[*]}{.metadata.name}")

# 获取 pod 状态
pod_status=$(kubectl get pods -n $namespace -o=jsonpath="{.items[?(@.metadata.name == '$pod_name')].status.phase}")

# 检查是否有匹配的运行中的 pod
if [[ ! -z "$pod_status" ]]; then
  echo "Pod running"
else
  echo "Pod not running"
fi

注意:这个脚本是用于在指定的命名空间中查找具有特定标签的 pod,并检查它们的运行状态。

英文:

This can be achieved using pod label as specified in below script(modified accordingly which is provided by you)

#!/bin/bash
#Namespace Name
namespace=&quot;backend&quot;
#Provide label name which is specified in pod definition
appname=&quot;inventory&quot;

# Get the pod name
pod_name=$(kubectl get pod -n $namespace -l app=$appname -o=jsonpath=&quot;{range .items[*]}{.metadata.name}&quot;)

# Get the pod status
pod_status=$(kubectl get pods -n $namespace -o=jsonpath=&quot;{.items[?(@.metadata.name == &#39;$pod_name&#39;)].status.phase}&quot;)

# Check if any matching pod is running
if [[ ! -z &quot;$pod_status&quot; ]]; then
  echo &quot;Pod running&quot;
else
  echo &quot;Pod not running&quot;
fi

huangapple
  • 本文由 发表于 2023年6月2日 15:04:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定