본문 바로가기
Compute/kubernetis

[CKA] 03. kubectl logs 명령어 사용 문제

by 조청유곽 2025. 1. 11.
반응형

01. 쿠버네티스에서 특정 Pod의 로그를 가져와 특정 패턴을 검색한 결과를 파일 /opt/error-logs에 저장한다.

 

[precondition]  

위 문제의 테스트를 위해서 먼저 error가 출력되는 pod를 생성한다.

[root@k8s-master ~]# vi 1-test.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: frontend
spec:
  containers:
  - name: frontend
    image: alpine:latest
    command: ['sh', '-c', 'echo "test is started"']

[root@k8s-master ~]# kubectl apply -f 1-test.yaml 
pod/frontend created
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl logs frontend
test is started
[root@k8s-master ~]#

 

[Solve]

[root@k8s-master ~]# kubectl logs frontend | grep -i started
test is started
[root@k8s-master ~]# kubectl logs frontend | grep -i started > /opt/error-logs
[root@k8s-master ~]# cat /opt/error-logs 
test is started
[root@k8s-master ~]#

02. 쿠버네티스에서 특정 Pod의 로그를 가져와 특정 패턴을 검색한 결과를 파일로 저장한다.
: 01번과 동일한 문제로 복습의 개념으로 진행 

 

[precondition]

문제에서 제시하는 특정로그를 출력하는 pod를 먼저 생성한다. 

[root@k8s-master ~]# vi 7-test.yaml

apiVersion: v1
kind: Pod
metadata:
  name: foo
spec:
  containers:
  - name: foo
    image: busybox:1.28
    command: ['sh', '-c', 'echo "unable-to-access-website" && sleep 3600']
    
[root@k8s-master ~]# kubectl apply -f 7-test.yaml 
pod/foo created
[root@k8s-master ~]# 
[root@k8s-master ~]# kubectl logs foo 
unable-to-access-website
[root@k8s-master ~]#

 

[solve]

[root@k8s-master ~]# kubectl logs foo | grep -i unable-to-access-website
unable-to-access-website

 

grep의 -i 옵션이 의미하는 바는 grep --help로 쉽게 알 수 있다. 

결론부터 말하면 대소문자 무시

[root@k8s-master ~]# grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE.
Example: grep -i 'hello world' menu.h main.c

Pattern selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression
  -F, --fixed-strings       PATTERN is a set of newline-separated strings
  -G, --basic-regexp        PATTERN is a basic regular expression (default)
  -P, --perl-regexp         PATTERN is a Perl regular expression
  -e, --regexp=PATTERN      use PATTERN for matching
  -f, --file=FILE           obtain PATTERN from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         force PATTERN to match only whole words
  -x, --line-regexp         force PATTERN to match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

 

반응형