• About Us
  • Privacy & Policy
HowTo's
  • Home
  • Commands
  • Linux
  • SCM
  • Git
  • Database
  • MySQL
  • Kubernetes
  • Docker
No Result
View All Result
  • Home
  • Commands
  • Linux
  • SCM
  • Git
  • Database
  • MySQL
  • Kubernetes
  • Docker
No Result
View All Result
HowTo's
No Result
View All Result
Home Kubernetes

Execute Commands in Kubernetes Pods with kubectl exec

June 26, 2024
in Kubernetes, Kubernetes Commands Examples, Kubernetes Commands Tutorial, Kubernetes Tutorial
A A
0
13
SHARES
116
VIEWS
Share on FacebookShare on Twitter

The kubectl exec command in Kubernetes allows you to execute commands inside a running container of a pod. This is particularly useful for troubleshooting, debugging, or interacting with applications directly within the Kubernetes cluster.

Here are several examples demonstrating the usage of kubectl exec:

Example 1: Execute a shell (/bin/sh) in a pod named my-pod:

        kubectl exec -it my-pod -- /bin/sh
    

This command opens an interactive shell session within the my-pod pod, allowing you to run commands as if you were logged into the container.

Example 2: Run a command directly in a pod without opening an interactive shell:

        kubectl exec my-pod -- ls /var/log
    

Here, kubectl exec runs the ls /var/log command inside my-pod, listing files and directories in the /var/log directory of the container.

Example 3: Execute a command using a specific container within a multi-container pod:

        kubectl exec -it my-pod -c my-container -- /bin/bash
    

This example runs an interactive bash shell session inside the container named my-container within the pod my-pod.

Example 4: Send input to a specific container’s stdin:

        echo "Hello, Kubernetes!" | kubectl exec -i my-pod -c my-container -- /usr/bin/env tee /tmp/greeting.txt
    

Here, the echo command output is redirected to the stdin of the container my-container and written to /tmp/greeting.txt.

Example 5: Execute a command in a pod using a specific namespace:

        kubectl exec -n my-namespace my-pod -- ps aux
    

This command runs ps aux inside my-pod in the namespace my-namespace, showing processes running in the container.

Example 6: Run a command with custom environment variables:

        kubectl exec my-pod -- env VAR=value bash
    

Here, VAR=value sets an environment variable VAR with value value before running bash in the container.

Example 7: Execute a command with different working directory:

        kubectl exec -it my-pod -- /bin/sh -c "cd /path/to/directory && ls"
    

This command changes the directory to /path/to/directory and lists its contents.

Example 8: Get output of a command executed inside a pod:

        kubectl exec my-pod -- cat /var/log/app.log
    

Here, kubectl exec retrieves the contents of /var/log/app.log from my-pod and outputs it to the console.

Example 9: Execute a command as a specific user in a pod:

        kubectl exec -it my-pod -- /bin/bash -u 1000
    

This command runs an interactive bash shell as user with UID 1000 inside my-pod.

Example 10: Execute a command with resource limitations:

        kubectl exec -it my-pod --limits=cpu=100m,memory=256Mi -- /bin/bash
    

Here, the command sets CPU and memory limits before starting an interactive bash shell session in my-pod.

To verify if a kubectl exec command executed successfully, you can check for the expected output or changes within the pod or container. For commands that interactively open a shell, ensure the shell prompt appears, indicating the session has started. For non-interactive commands, verify the output matches the expected result.

Also check similar articles.

Attach to Running Containers with kubectl attach
Retrieve Container Logs Using kubectl logs
Detailed Resource Inspection with kubectl describe
Update Node Taints with kubectl taint
Prepare Nodes for Maintenance with kubectl drain

Tags: KubernetesKubernetes Commands ExamplesKubernetes Commands TutorialKubernetes Tutorial
Previous Post

Attach to Running Containers with kubectl attach

Next Post

Port Forwarding in Kubernetes with kubectl port-forward

Related You may like!

howto

Interacting with Kubernetes Plugins using kubectl plugin

June 26, 2024
howto

Configuring kubectl and kubeconfig Files

June 26, 2024

Exploring Kubernetes API Versions with kubectl api-versions

June 26, 2024

Understanding Kubernetes API Resources with kubectl api-resources

June 26, 2024

Generating Shell Completion Code with kubectl completion

June 26, 2024

Managing Kubernetes Annotations with kubectl annotate

June 26, 2024
Next Post
howto

Port Forwarding in Kubernetes with kubectl port-forward

howto

Running a Kubernetes API Proxy with kubectl proxy

howto

Copy Files to and from Kubernetes Containers with kubectl cp

Discussion about this post

Latest Updated

howto

How to Use -iname for Case-Insensitive Filename Searches in find

August 21, 2024
howto

Search for Files with Case-Insensitive Pattern Matching Using -ilname in find

August 21, 2024
howto

Find Files by Group Name with -group in find Command

August 21, 2024
howto

Locate Files by Group ID Using -gid in find Command

August 21, 2024
howto

How to Search for Filesystems with -fstype in find Command

August 21, 2024

Trending in Week

  • howto

    Using BTRFS Subvolume for User Home Directory in Linux

    22 shares
    Share 9 Tweet 6
  • Downloading Docker Images from a Registry

    13 shares
    Share 5 Tweet 3
  • Configuring SSL Connection Mode in mysqldump

    17 shares
    Share 7 Tweet 4
  • Omit Tablespace Information in mysqldump Output

    13 shares
    Share 5 Tweet 3
  • Setting MySQL Dump Compatibility Mode

    18 shares
    Share 7 Tweet 5
  • Setting Network Buffer Length in mysqldump

    13 shares
    Share 5 Tweet 3
  • Logging out from Docker Registries

    13 shares
    Share 5 Tweet 3
  • Using Extended INSERT Syntax in mysqldump

    12 shares
    Share 5 Tweet 3
  • Understanding Kubernetes API Resources with kubectl api-resources

    12 shares
    Share 5 Tweet 3
  • Setting Connection Protocol in mysqldump

    12 shares
    Share 5 Tweet 3
  • About Us
  • Privacy & Policy

© 2024 All Rights Reserved. Howto.swebtools.com.

No Result
View All Result

© 2024 All Rights Reserved. Howto.swebtools.com.