사이드카 컨테이너 사용 시 기본 컨테이너 정의

사이드카 컨테이너 사용 시 기본 컨테이너 정의

해당 서비스에 대해 SSO를 활성화하기 위해 클러스터의 여러 포드에 대한 사이드카 컨테이너로 keycloak 게이트키퍼를 사용합니다.

하지만 실행하려고 할 때 로그 등을 보려고 합니다. 그냥 들어가는 대신 어떤 컨테이너를 사용할 것인지 묻습니다. 플래그를 kubectl exec -it PODNAME전달하지 않을 때 와 같은 용도로 사용할 기본 컨테이너를 정의하는 방법이 있나요 -c?

답변1

귀하가 요청한 것은 현재 작동하고 있지만 매우 제한적입니다. 에 따르면kubectl exec 문서플래그를 놓칠 수 있습니다 -c.

-c, --container="": 컨테이너 이름입니다. 생략하면 포드의 첫 번째 컨테이너가 선택됩니다.

date그러나 , bash또는 같은 일부 작업/명령도 지정했습니다 sh.

기본적으로 첫 번째 컨테이너를 사용하여 Pod 123456-7890에서 'date'를 실행하여 출력 가져오기 kubectl exec 123456-7890 date

나는 이것이 매우 제한적이라고 언급합니다.첫 번째 컨테이너YAML 매니페스트에 지정된 목록에서. 플래그를 사용하는 경우 -c원하는 플래그를 지정할 수 있습니다 execute.

spec:
  containers:
  - image: httpd
    name: httpd
  - image: busybox
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
    name: busybox

산출

어떤 명령도 사용하지 않고:

$ kubectl exec -ti test-pod
error: you must specify at least one command for the container

command 를 사용하면 date포드의 YAML/설명에서 첫 번째 컨테이너를 사용합니다.

$ kubectl exec -ti test-pod -- date
Defaulting container name to httpd.
Use 'kubectl describe pod/test-pod -n default' to see all of the containers in this pod.
Mon Jan  4 14:06:27 UTC 2021

Date지정된 포드를 사용한 명령

$ kubectl exec -ti test-pod -c busybox -- date
Mon Jan  4 14:06:36 UTC 2021

Kubectl exec 주석 - 기본 컨테이너

중 하나에서는Github 개선 사항안정적인 kubernetes 버전(1.23)에 이 기능을 도입할 계획이 있다는 정보를 찾을 수 있습니다.

다음과 같습니다:

kubectl annotate pod test-pod kubectl.kubernetes.io/default-exec-container=<conatinerName>

Kubectl 로그 주석 - 기본 컨테이너

유사한 기능이지만 관련 logs없음은 exec에 도입되었습니다 kubectl 1.18. 에서 언급되었습니다.Github 스레드. 이를 달성하려면 새 주석을 추가해야 합니다.kubectl.kubernetes.io/default-logs-container=<containerName>

test-pod포드 시나리오 busyboxhttpd

$ kubectl logs test-pod
error: a container name must be specified for pod test-pod, choose one of: [busybox httpd]

$ kubectl annotate pod test-pod kubectl.kubernetes.io/default-logs-container=httpd
pod/test-pod annotated

$ kubectl logs test-pod
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.4.1.12. Set the 'ServerName' directive globally to suppress this message
[Mon Jan 04 14:05:08.191117 2021] [mpm_event:notice] [pid 1:tid 140379730310272] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Mon Jan 04 14:05:08.191428 2021] [core:notice] [pid 1:tid 140379730310272] AH00094: Command line: 'httpd -D FOREGROUND'

$ kubectl logs test-pod -c httpd
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.4.1.12. Set the 'ServerName' directive globally to suppress this message
[Mon Jan 04 14:05:08.191117 2021] [mpm_event:notice] [pid 1:tid 140379730310272] AH00489: Apache/2.4.46 (Unix) configured -- resuming normal operations
[Mon Jan 04 14:05:08.191428 2021] [core:notice] [pid 1:tid 140379730310272] AH00094: Command line: 'httpd -D FOREGROUND'

관련 정보