Running a command inside a pod:


Below is a short explanation of the options used in the above command:

--command  -- : after the "--" we can specify the command we want to run inside the container. It overrides the "ENTRYPOINT" command used in the dockerfile when building the image.
--rm : it instructs Kubernetes to delete the container image when the pod stops.
-it : "-i" runs the container in interactive mode, and "-t" displays the output on the terminal.
--restart=Never : it instructs Kubernetes to not restart a container
when it exits or crashes.

Running a command inside a running pod:

We start by getting the name of the running pod using the below the command:


We can then run our command inside the running pod:


Automatically generating a Yaml file:

We use the below command to generate a YAML file for a kubernetes object:


 --dry-run : prevents kubectl from creating the pod, it only creates the Yaml file for the resource.
 -o yaml : creates the configuration file for the resource in a Yaml format.

Remark:

Because of the "--dry-run" option, the resource was not created. If we want to create the pod, we need to use the below command:


The  kubectl diff:

After making changes for example to a Yaml deployment file, we could run the below command to see what would happen if case we apply the changes to our cluster:


We can see than the number of replicas would change if we apply the changes.

Watching the pods:

We could use the below command to watch the pods "live":


The above command displays all the states of the pod "demo-95333875c-z9xh2" from its creation until it reaches the "running" state.

Attaching a container to the terminal:

We could attach a container to the terminal and view its output on our screen, using the below command:


Connecting directly to a pod:

To connect directly to an application running inside a pod, we can "publish" the pod's port on the host, and access the application through the "published" port:


Port 26017 on the host is forwarded to port 25013 on the pod where the application is running.

Running command in pods with multiple containers:

The command "kubectl exec" runs its command in the first container if a pod hosts more than one container.

If we want to run a command on a specific container, we use the "-c"
option as below:


The
container_name is the name we gave to the container in our Yaml file:


Brando Sabatini and Ikbal C ©. Powered by Blogger.