Custom Columns
k get deployments.apps -n=admin2406 -o=json
k get deployments.apps -n=admin2406 -o=custom-columns=DEPLOYMENT:.metadata.name,CONTAINER_IMAGE:.spec.template.spec.containers[0].image,READY_REPLICAS:.status.readyReplicas,NAMESPACE:.metadata.namespace > /opt/admin2406_data
K8s Object 의 특정 정보를 취합해서 파일로 저장하고 싶을 때 custom-columns 옵션을 활용할 수 있다. json 옵션을 사용했을 때 출력되는 계층을 따라 특정 정보에 접근할 수 있다.
kubectl logs
kubectl logs -l app=log-lab --prefix
--prefix 옵션으로 로그 앞에 어느 파드의 어느 컨테이너에서 생성된 로그인지 확인 가능
kubectl logs -l app=log-lab --prefix --since=10m
--since 옵션으로 최근 시간 내에 생성된 로그만 확인 가능
kubectl logs -l app=log-lab --prefix --since=10m --tail=100
--tail 옵션으로 최근 로그 갯수 제한 가능
Some useful grep
kubectl logs -l app=log-lab --prefix --since=10m --tail=100 | grep -iE 'error|warn'
grep -E 로 여러 키워드를 매칭해서 필터링 가능, -i 옵션까지 추가하면 대소문자 구분하지 않고 확인
kubectl logs -l app=log-lab --prefix --since=10m --tail=100 | grep -F "connection refused"
grep -F 로 문자열 검색
kubectl logs -l app=log-lab --prefix --since=10m --tail=100 | grep -w "ERROR"
grep -w 로 단어 단위로 매칭
kubectl logs -l app=log-lab --prefix --since=10m --tail=100 | grep -C2 'error'
grep -C2 (Context 의 C) 로 매칭된 로그 앞 뒤로 2줄을 추가로 보여줄 수 있다
kubectl logs -l app=log-lab --prefix --since=10m --tail=100 | grep -A2 'error'
grep -A2 (After 의 A) 로 매칭된 로그 뒤 2줄을 추가로 보여줄 수 있다
kubectl logs -l app=log-lab --prefix --since=10m --tail=100 | grep -B2 'error'
grep -B2 (Before 의 B) 로 매칭된 로그 전 2줄을 추가로 보여줄 수 있다
kubectl logs -l app=log-lab --prefix -f | grep -E 'ERROR|WARN'
-f 로 실시간 로그 확인, grep 조합으로 원하는 로그만 추출 가능
kubectl alias 및 자동완성 설정
echo '[[ $commands[kubectl] ]] && source <(kubectl completion zsh)' >> ~/.zshrc
echo 'alias k=kubectl' >> ~/.zshrc
echo 'compdef __start_kubectl k' >> ~/.zshrc
source ~/.zshrc
K9s
K9s 는 터미널 환경에서 실행되는 TUI, Text-based User Interface 도구이다.
brew install k9s
설치 후 k9s 를 입력하면 현재 kubectl 컨텍스트에 설정된 클러스터 상태를 확인할 수 있다.
핵심 기능
:pods,:deploy,:svc등의 명령어로 리소스 및 네임스페이스를 빠르게 이동할 수 있다.l: Pod 로그 확인s: 컨테이너 쉘 접속d: 리소스 describe
Node Rolling Duration Investigation
kubectl get nodes -l eks.amazonaws.com/nodegroup=${NODE_GROUP_NAME} -o json | jq -r '
.items[]
| (.status.conditions[] | select(.type=="Ready" and .status=="True") | .lastTransitionTime) as $ready
| select($ready != null)
| (.metadata.creationTimestamp | fromdateiso8601) as $c
| ($ready | fromdateiso8601) as $r
| [
.metadata.name,
(.spec.providerID | split("/")[-1]),
.metadata.annotations["alpha.kubernetes.io/provided-node-ip"],
.metadata.labels["topology.kubernetes.io/zone"],
.metadata.creationTimestamp[0:19],
$ready[0:19],
"\($r - $c)s"
] | @tsv
' | sort -k5 | column -t -s $'\t'현재 노드 Create, Ready 시간 및 AZ 확인
aws autoscaling describe-scaling-activities \
--auto-scaling-group-name "$ASG" \
--region ap-east-1 | jq -r '
["INSTANCE","ACTION","AZ","START(UTC)","END(UTC)","DUR","STATUS","PROG"],
(.Activities
| sort_by(.StartTime) | .[]
| (if (.Description | test("Launching")) then "Launch"
elif (.Description | test("Terminating")) then "Terminate"
else "?" end) as $action
| ((.Description | capture("(?<i>i-[0-9a-f]+)").i) // "-") as $iid
| ((.Details? | if . then (. | fromjson | .["Availability Zone"]) else null end) // "-") as $az
| ((.StartTime[0:19] + "Z") | fromdateiso8601) as $s
| (if .EndTime then (.EndTime[0:19] + "Z") | fromdateiso8601 else null end) as $e
| [ $iid,
$action,
$az,
.StartTime[0:19],
(.EndTime[0:19] // "-"),
(if $e then "\($e - $s)s" else "running" end),
.StatusCode,
(.Progress | tostring)
]
) | @tsv
' | column -t -s $'\t'노드 Launch/Terminate 시간 trace
- Node Group Update Config 에서
max_unavailable=1(기본값) 일 경우, 새로운 노드가 하나씩 Launch 되고 기존 노드가 하나씩 Terminate 된다. - 즉, Launch 시간, Terminate 시간 차이가 기존 노드 drain duration 이 된다.
Pod Eviction Failure 429 발생 시 기본적으로 1분마다 eviction retry 를 실행한다.
- Node Group
force_update_version=True일 경우, 19번의 retry 실패 후 Node drain timeout 으로 노드가 강제종료된다. - 즉, 기존 노드 drain 은 3-5분 걸리는데 비해 약 20분 가량까지 노드 drain 이 지연되어 Rolling Update 시간이 늘어날 수 있다.