Kubectl is a powerful command-line tool for managing Kubernetes clusters. One of its key functionalities is the ability to deploy Docker images onto a Kubernetes cluster using the `run` option. This allows you to start a new deployment, replica set, or job using a Docker image.
Running Docker Images on Kubernetes with kubectl run
Example 1: Deploying a simple NGINX web server as a Deployment:
kubectl run nginx --image=nginx --replicas=3
This command creates a new Deployment named ‘nginx’ with 3 replicas using the NGINX Docker image. To verify, you can check the status of the Deployment:
kubectl get deployments kubectl get pods
Example 2: Creating a Job to perform a batch task:
kubectl run batch-job --image=busybox --restart=OnFailure -- echo "Batch job completed"
This command creates a Job named ‘batch-job’ using the BusyBox image to echo a message. To verify, check the status of the Job:
kubectl get jobs kubectl logs
Example 3: Running a Pod with a specific command:
kubectl run test-pod --image=ubuntu --restart=Never --command -- sleep 3600
This creates a Pod named ‘test-pod’ using the Ubuntu image to sleep for 3600 seconds. To verify, check the status of the Pod:
kubectl get pods kubectl logs
Example 4: Running a Pod with environment variables:
kubectl run env-pod --image=nginx --env="MESSAGE=Hello, Kubernetes!"
This creates a Pod named ‘env-pod’ using the NGINX image with an environment variable set. To verify, inspect the Pod’s environment:
kubectl exec -it-- env | grep MESSAGE
Example 5: Deploying a Redis instance as a StatefulSet:
kubectl run redis --image=redis --restart=Always --port=6379
This command creates a StatefulSet named ‘redis’ using the Redis image with port 6379 exposed. To verify, check the StatefulSet and its Pods:
kubectl get statefulsets kubectl get pods
Example 6: Running a Pod with resource limits:
kubectl run resource-pod --image=busybox --restart=Never --limits='cpu=0.5,memory=512Mi'
This command creates a Pod named ‘resource-pod’ using the BusyBox image with specified CPU and memory limits. To verify, describe the Pod and check its resource limits:
kubectl describe pods
Example 7: Creating a CronJob to run scheduled tasks:
kubectl run cron-job --image=busybox --schedule="*/1 * * * *" -- echo "Scheduled task executed"
This command creates a CronJob named ‘cron-job’ using the BusyBox image to echo a message every minute. To verify, check the status of the CronJob:
kubectl get cronjobs kubectl logs
Example 8: Running a Pod with a specific service account:
kubectl run sa-pod --image=busybox --restart=Never --serviceaccount=my-service-account
This command creates a Pod named ‘sa-pod’ using the BusyBox image with a specific service account ‘my-service-account’. To verify, inspect the Pod’s service account:
kubectl get pods sa-pod -o jsonpath='{.spec.serviceAccountName}'
Example 9: Deploying a Pod with labels:
kubectl run labeled-pod --image=nginx --labels="app=nginx,env=prod"
This command creates a Pod named ‘labeled-pod’ using the NGINX image with labels ‘app=nginx’ and ‘env=prod’. To verify, list Pods with the specified labels:
kubectl get pods --selector=app=nginx,env=prod
Example 10: Running a Pod with a specific namespace:
kubectl run namespace-pod --image=busybox --restart=Never --namespace=my-namespace
This command creates a Pod named ‘namespace-pod’ using the BusyBox image in the ‘my-namespace’ namespace. To verify, list Pods in the specific namespace:
kubectl get pods --namespace=my-namespace
Also check similar articles.
Expose Kubernetes Services Easily with kubectl expose
How to Create Kubernetes Resources from Files or Stdin
Overriding –databases Option in mysqldump
Creating Tab-Separated Output Files with mysqldump
Handling Failed SSL Session Data Reuse in mysqldump
Discussion about this post