본문 바로가기
IT 잡지식/DevOps

[CKA] KodeKloud - Commands and Arguments

by 쯀리♥️ 2024. 7. 2.

안녕하세요, 쯀리입니다.
오늘은  Commands and Arguments에 관해 알아보도록 하겠습니다. 

https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/

 

Define a Command and Arguments for a Container

This page shows how to define commands and arguments when you run a container in a Pod. Before you begin You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run

kubernetes.io

 


Commands and Arguments

Commands

  • 명령어(Command): 컨테이너가 시작될 때 실행할 기본 명령어를 지정합니다.
  • 형식: YAML 파일의 command 필드를 사용합니다.
  • 기본 동작: 명령어가 지정되지 않으면, Docker 이미지의 ENTRYPOINT가 사용됩니다.

Arguments

  • 인자(Arguments): 명령어에 전달할 인자를 지정합니다.
  • 형식: YAML 파일의 args 필드를 사용합니다.
  • 기본 동작: 인자가 지정되지 않으면, Docker 이미지의 CMD가 사용됩니다

아래는 nginx 컨테이너를 실행하면서 명령어와 인자를 지정하는 예시입니다.

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: example-container
    image: nginx
    command: [ "nginx" ]
    args: [ "-g", "daemon off;" ]​
 

 


Quiz

1. How many PODs exist on the system?

controlplane ~ ➜  k get pods
NAME             READY   STATUS    RESTARTS   AGE
ubuntu-sleeper   1/1     Running   0          20s

 

2. What is the command used to run the podubuntu-sleeper?

controlplane ~ ➜  k describe pod ubuntu-sleeper
Name:             ubuntu-sleeper
Namespace:        default
Priority:         0
...

Command:
   sleep
   4800

sleep 4800

3. Create a pod with the ubuntu image to run a container to sleep for 5000 seconds. Modify the file ubuntu-sleeper-2.yaml.

controlplane ~ ➜  cat ubuntu-sleeper-2.yaml 
###
apiVersion: v1 
kind: Pod 
metadata:
  name: ubuntu-sleeper-2 
spec:
  containers:
  - name: ubuntu
    image: ubuntu
    command: ["sleep", "5000"]
    

controlplane ~ ➜  k apply -f ubuntu-sleeper-2.yaml 
pod/ubuntu-sleeper-2 created

controlplane ~ ➜  k get pods
NAME               READY   STATUS    RESTARTS   AGE
ubuntu-sleeper     1/1     Running   0          9m19s
ubuntu-sleeper-2   1/1     Running   0          7s

 

4. Create a pod using the file named ubuntu-sleeper-3.yaml. There is something wrong with it. Try to fix it!

controlplane ~ ➜  cat ubuntu-sleeper-3.yaml 
###
apiVersion: v1 
kind: Pod 
metadata:
  name: ubuntu-sleeper-3 
spec:
  containers:
  - name: ubuntu
    image: ubuntu
    command:
      - "sleep"
      - 1200
  
 controlplane ~ ➜  vi ubuntu-sleeper-3.yaml 
...
spec:
  containers:
  - name: ubuntu
    image: ubuntu
    command:
      - "sleep"
      - "1200"

 command는 String형식으로 들어가야합니다! 
띄어서 대괄호 안에 넣어주던가, -로 나누어 작성하셔야합니다.

5. Update pod ubuntu-sleeper-3 to sleep for 2000 seconds.
Note: Only make the necessary changes. Do not modify the name of the pod. Delete and recreate the pod if necessary.

controlplane ~ -> k delete pods ubuntu-sleeper-3
pod "ubuntu-sleeper-3" deleted

controlplane ~ ➜  k apply -f /tmp/kubectl-edit-3898901996.yaml
pod/ubuntu-sleeper-3 created

controlplane ~ ➜  k get pods
NAME               READY   STATUS    RESTARTS   AGE
ubuntu-sleeper     1/1     Running   0          15m
ubuntu-sleeper-2   1/1     Running   0          6m27s
ubuntu-sleeper-3   1/1     Running   0          4s

controlplane ~ ➜  k describe pods ubuntu-sleeper-3
...
Command:
      sleep
      2000

 

6. Inspect the file Dockerfile given at /root/webapp-color directory. What command is run at container startup?
Startup에 가장먼저 실행되는 command는 ENTRYPOINT를 참고하면 됩니다.

controlplane ~/webapp-color ➜  cat Dockerfile
FROM python:3.6-alpine

RUN pip install flask

COPY . /opt/

EXPOSE 8080

WORKDIR /opt

ENTRYPOINT ["python", "app.py"]

정답은 python app.py

7. Inspect the file Dockerfile2 given at /root/webapp-color directory. What command is run at container startup?

controlplane ~/webapp-color ➜  cat Dockerfile2
FROM python:3.6-alpine

RUN pip install flask

COPY . /opt/

EXPOSE 8080

WORKDIR /opt

ENTRYPOINT ["python", "app.py"]

CMD ["--color", "red"]

ENTRYPOINT 명령어에 전달할 인자를 CMD로 지정합니다.

정답: python app.py --color red

★ 8. Inspect the two files under directory webapp-color-2. What command is run at container startup?
Assume the image was created from the Dockerfile in this directory.
Dockerfile로 Startup되는 명령어는 python app.py --color red이지만
yaml파일로 command가 덮어써짐으로  --color green  으로 덮어써집니다.. 

 

controlplane ~/webapp-color-2 ➜  cat Dockerfile 
FROM python:3.6-alpine

RUN pip install flask

COPY . /opt/

EXPOSE 8080

WORKDIR /opt

ENTRYPOINT ["python", "app.py"]

CMD ["--color", "red"]

controlplane ~/webapp-color-2 ➜  cat webapp-color-pod.yaml
apiVersion: v1 
kind: Pod 
metadata:
  name: webapp-green
  labels:
      name: webapp-green 
spec:
  containers:
  - name: simple-webapp
    image: kodekloud/webapp-color
    command: ["--color","green"]

 

9. Inspect the two files under directory webapp-color-3. What command is run at container startup?
Assume the image was created from the Dockerfile in this directory.

Dockerfile로 만들어진 이미지라고 해도 yaml파일을 살펴보면 command명령어는
python app.py --color pink

controlplane ~/webapp-color-3 ➜  cat webapp-color-pod-2.yaml 
apiVersion: v1 
kind: Pod 
metadata:
  name: webapp-green
  labels:
      name: webapp-green 
spec:
  containers:
  - name: simple-webapp
    image: kodekloud/webapp-color
    command: ["python", "app.py"]
    args: ["--color", "pink"]

 

10 . Create a pod with the given specifications. By default it displays a blue background. Set the given command line arguments to change it to green.

Pod Name: webapp-green
Image: kodekloud/webapp-color
Command line arguments: --color=green
controlplane ~ ➜  vi webapp-green.yaml
### 
apiVersion: v1 
kind: Pod 
metadata:
  name: webapp-green
  labels:
      name: webapp-green 
spec:
  containers:
  - name: simple-webapp
    image: kodekloud/webapp-color
    command: ["python", "app.py"]
    args: ["--color", "green"]
    
controlplane ~ ➜  k apply -f webapp-green.yaml 
pod/webapp-green created

 


 

  • Commands와 Arguments를 사용하여 Kubernetes에서 컨테이너의 동작을 제어할 수 있습니다.
  • command는 컨테이너가 시작될 때 실행할 기본 명령어를 지정하며, args는 그 명령어에 전달할 인자를 지정합니다.

다음 시간에는 Env Variables : 변수에 관해 알아보도록 하겠습니다!

 

 

 


참조

 Udemy Labs - Certified Kubernetes Administrator with Practice Tests