Solution Commands And Arguments
- Good example use of a command in a pod definition file:
```
apiVersion: v1
kind: Pod
metadata:
name: ubuntu-sleeper-2
spec:
containers:
- name: ubuntu
image: ubuntu
command:
- “sleep”
- “5000” ```
- name: ubuntu
image: ubuntu
command:
- Another good example:
```
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”] ```
- How to check the commands that a pod runs?
- Run
kubectl describe pod <pod_name>and then under theContainerstab you’ll see theCommandsection and that is where all of the commands a container runs will be.
- Run
- How to create a pod with an Ubuntu image and with the
sleepcommand. - To do this, create this definition file:
```
apiVersion: v1
kind: Pod
metadata:
name: ubuntu-sleeper-2
spec:
containers:
- name: ubuntu
image: ubuntu
command:
- “sleep”
- “5000” ```
- name: ubuntu
image: ubuntu
command:
- Can also do it in an array like so:
```
apiVersion: v1
kind: Pod
metadata:
name: ubuntu-sleeper-2
spec:
containers:
- name: ubuntu image: ubuntu command: [ “sleep 5000” ] ```
- Always need the command as the first item.
- Can also specify it as
args: ``` apiVersion: v1 kind: Pod metadata: name: ubuntu-sleeper-2 spec: containers:- name: ubuntu image: ubuntu command: [ “sleep” ] args: [ “5000” ] ```
- Then
kubectl create -fto create the file. - All elements in the
commandstring have to be a string and not a number (i.e. everything needs to be within double quotations). kubectl applycan only be applied to some pods you can change and some pods you can’t.- To get past these restrictions, use
kubectl replace --force -f <definition_file>- This command deletes the pod and recreates it based on the edited one.
- Best practice however to delete the pod, update the local file and then re-deploying the pod again.
- Commands in a Docker image which run at startup? That would be
ENTRYPOINT.- Anything that comes in
CMDafter that is an argument to theENTRYPOINTcommands.
- Anything that comes in
- The
commandfield in a definition file always overrides theENTRYPOINTof a Docker image. For examplecommand: ["--color", "pink"]in a definition file would take precedence over a DockerENTRYPOINTfield. - Can run the following:
kubectl run webapp-green --image=kodekloud/webapp-color --dry-run=client -o yaml kubectl run nginx --image=nginx -- <arg1> <arg2>- The
--separates thekubectlutility and the arguments that are passed through to thenginxapplication inside. - Can also override commands as well:
kubectl run nginx --image=nginx --command -- <cmd ><arg1> <arg2> kubectl run webapp-green --image=kodekloud/webapp-color -- --color green