在kubernetes --watch-only命令中添加时间戳

t3irkdon  于 12个月前  发布在  Kubernetes
关注(0)|答案(3)|浏览(157)

我正在运行一个Kubernetes作业,我想在那里监视状态。我同时运行各种--watch-only命令,例如kubectl get pods --watch-only,它会显示Pod的更新状态。但是,我想在输出中附加 timestamp 和一些 string
这个想法是知道状态何时改变,并以字符串的形式添加额外的信息。
我如何才能做到这一点?

x6492ojm

x6492ojm1#

让更多的可见性,以潜在的解决方案,在评论中张贴的问题,由原海报:
这是我发现到目前为止kubectl get pods --watch-only工作的地方。|while read line ; do echo -e“$(date +"%Y-%m-%d %H:%M:%S.%3N”)\t pods\t $line”; done
使用的命令:

  • 第一个月

解决方案是正确的,但需要从上述命令中进一步提取状态更改(PENDINGRUNNINGSUCCEEDED/COMPLETED)(假设进一步操作)。
从另一个Angular 来看,你可以使用official Kubernetes API library来监控pod和job的状态,并根据它们采取相应的行动(例如:当job成功时做一些事情)。
我使用Kubernetes Python API库创建了一个example应用,用于查看Pod和作业的状态。
假设:

  • 您有一个配置了kubectl的工作Kubernetes集群
  • 你已经安装了Python和所需的库:
  • $ pip install kubernetes

Job为例:

Pod示例

下面是Python 3中的示例代码,当pod的status设置为Succeeded时,它会监视pod并打印消息:

from kubernetes import client, config, watch
from datetime import datetime

config.load_kube_config()

v1 = client.CoreV1Api()
w = watch.Watch()
for event in w.stream(v1.list_namespaced_pod, namespace="default", watch=False):
    print("POD_NAME: " + event['object'].metadata.name) # print the name
    print("TIME: " + str(datetime.now())) # print the time 
    print("PHASE: " +  event['object'].status.phase) # print the status of the pod
    print("CUSTOM AMAZING TEXT HERE!")

    if (event['object'].status.phase == "Succeeded") and (event['type'] != "DELETED"): # do below when condition is met 
        print ("----> This pod succeeded, do something here!")
    print("---")

字符串
这将产生类似于下面的输出:

POD_NAME: pi-pjmm5
TIME: 2020-09-06 15:28:01.541244
PHASE: Pending
CUSTOM AMAZING TEXT HERE!
---
POD_NAME: pi-pjmm5
TIME: 2020-09-06 15:28:03.894063
PHASE: Running
CUSTOM AMAZING TEXT HERE!
---
POD_NAME: pi-pjmm5
TIME: 2020-09-06 15:28:09.044219
PHASE: Succeeded
CUSTOM AMAZING TEXT HERE!
----> This pod succeeded, do something here!
---

作业示例

下面是Python 3中的示例代码,当作业状态设置为Succeeded时,它会监视作业并打印一条消息:

from kubernetes import client, config, watch
from datetime import datetime

config.load_kube_config()

v1 = client.BatchV1Api()
w = watch.Watch()
for event in w.stream(v1.list_namespaced_job, namespace="default", watch=False):
    print("JOB_NAME: " + event['object'].metadata.name)
    print("TIME: " + str(datetime.now()))
    print("STATUS: " + event['type'])
    print("CUSTOM AMAZING TEXT HERE!")

    if (event['object'].status.succeeded == 1) and (event['type'] != "DELETED"): 
        print ("----> This job succeeded, do something here!")
    print("---")


这将产生类似于下面的输出:

JOB_NAME: pi
TIME: 2020-09-06 15:32:49.909096
STATUS: ADDED
CUSTOM AMAZING TEXT HERE!
---
JOB_NAME: pi
TIME: 2020-09-06 15:32:49.936856
STATUS: MODIFIED
CUSTOM AMAZING TEXT HERE!
---
JOB_NAME: pi
TIME: 2020-09-06 15:32:56.998511
STATUS: MODIFIED
CUSTOM AMAZING TEXT HERE!
----> This job succeeded, do something here!
---

ou6hu8tu

ou6hu8tu2#

--timestamps

kubectl logs -f --timestamps ...

字符串

cnjp1d6j

cnjp1d6j3#

ts(来自moreutils)或gnomon(来自npm i -g gnomon)有助于标准时间戳输出:
即:

  • ts
$ kubectl get po -w | ts -i
00:00:00 NAME        READY   STATUS    RESTARTS   AGE
00:00:00 jenkins-0   2/2     Running   1661       55d
00:00:57 jenkins-0   1/2     OOMKilled   1661       55d
00:00:15 jenkins-0   1/2     CrashLoopBackOff   1661       55d
00:04:49 jenkins-0   2/2     Running            1662       55d
00:02:19 jenkins-0   1/2     OOMKilled          1662       55d
00:00:15 jenkins-0   1/2     CrashLoopBackOff   1662       55d
  • gnomon
$ kubectl get po -w | gnomon
   0.0234s   NAME        READY   STATUS             RESTARTS   AGE
287.3296s   jenkins-0   1/2     CrashLoopBackOff   1660       55d
158.1299s   jenkins-0   2/2     Running            1661       55d
  14.4898s   jenkins-0   1/2     OOMKilled          1661       55d
130.1794s   jenkins-0   1/2     CrashLoopBackOff   1661       55d

相关问题