리셋 되지 말자

[k8s] Pod 생성시간 계산하기 - Python 본문

오케스트레이션

[k8s] Pod 생성시간 계산하기 - Python

kyeongjun-dev 2023. 1. 30. 18:43

now = datetime.datetime.now().replace(microsecond = 0)

api server에 요청하여 Pod 정보 획득

kubectl을 이용해서 해당 Pod의 생성 시간을 획득할 수 있다. (Z는 utc timezon을 의미한다고 한다)

kubectl get pod <pod 이름> -o json | jq -r ".metadata.creationTimestamp"
2023-01-27T08:20:42Z

Python을 이용해 생성 시간 변환 - 초(second)

datetime 모듈을 이용해 Pod가 몇초전에 생성되었는지 구한다. microsecond는 0으로 치환하지 않아도 되지만, 옵션으로 추가했다.

import datetime

string="2023-01-27T08:20:42Z"

pod_create_time = datetime.datetime.strptime(string, "%Y-%m-%dT%H:%M:%SZ")

>>> pod_create_time
datetime.datetime(2023, 1, 27, 8, 20, 42)

now_time = datetime.datetime.now().replace(microsecond=0)
>>> now_time
datetime.datetime(2023, 1, 30, 18, 40, 33)

(now_time - pod_create_time).seconds
37191
Comments