Hello, readers! In this article, we will be focusing on Variants of kubectl Commands, in detail.
So, let us begin!! 🙂
Table of Contents
What is kubectl in Kubernetes?
Kubernetes serves as a strong container orchestration tool that helps us manage the workloads and containers and troubleshoot against errors or load.
In order to manage this, we have been introduced to kubectl. It is a software that enables us to run various commands against the Kubernetes Cluster. Further, we can do deployments, manage workloads and orchestrate the cluster and the engine through kubectl command line. It is a command line tool that enables us to easily deploy all our kubernetes resources through shooting short commands.
Please visit the below website to install kubectl tool for different environments–Kubectl tool for Kubernetes
Recommended Read: Top 10 Docker Commands
In this section, we will be specifically dealing with Kubernetes Pod related kubectl commands in daily use–
From the below commands,
- The first command help us get a list of all the pods in the default namespace .
- With describe command, we get more detailed information about the specific pod of default namespace such as image name, labels, container name, ports associated, etc.
- By specifying -n namespace, we actually get the list of pods for that particular namespace.
1. kubectl get pods 2. kubectl describe pod pod-name 3. kubectl get pods -n namespace
With the below command, we imperatively run a pod by assigning an image to it at the time of execution. Internally, it builds an YAML for the same.
kubectl run pod-name --image image-name -n namespace
When it comes to Replicasets, let us understand the working of the below mostly used commands-
1. kubectl get rs 2. kubectl describe rs replicaset-name 3. kubectl describe rs replicaset-name | grep -i image
- With kubectl get command, we list the replicasets in the default namespace
- The describe command helps us get detailed information about a particular replicaset
3. Deployment specific kubectl commands
- In the below first command, we get the list of deployments for a particular namespace.
- Here, we have created a deployment using imperative command for a particular image name and have stored it as a YAML file in redis.yaml file.
- In order to edit a deployment in a particular namespace, we can use the edit deployment command.
- In order to create a deployment at runtime and create a YAML out of it, we make use of create deployment command and attach -o yaml to it which returns a YAML structure of the deployment prior to creation. The dry-run command enables us to have a yaml structure without building the deployment.
1. kubectl get deploy -n namespace-name 2. kubectl create deployment deployment-name --image=image-name --dry-run=client -o yaml > redis.yaml 3. kubectl edit deployment deployment-name 4. kubectl create deployment --image=nginx nginx --dry-run -o yaml
4. Service specific kubectl commands
With Kubernetes Services, we can use the below commands to have services deployed at runtime. Let us have a look, one by one.
1. kubectl run pod-name --image=name -n namespace-name --expose --port=80 --dry-run-client -o yaml 2. kubectl run pod-name --image=name -n namespace-name --expose --port=80 3. kubectl expose pod redis --port=6379 --target-port=6379 --name=redis-service
- In the first command, we create and run a pod at runtime using image value exposing it to port 80 (HTTP) and have used dry-run to have a yaml structure ready prior to execution.
- With kubectl run command, we deploy a pod at run time, expose it to a service port.
- In the third command, we have attached a service to the pod redis and have deployed the same at run-time in an imperative manner.
Using the below command, we can create a Kubernetes Secret using a JSON file as a key-value pair. For the same, we need to provide the below details within the command-
- docker-server: The host name where the key would be accessed for the particular secret.
- docker-username: The key name
- docker-password: The JSON file path
kubectl create secret docker-registry gcr-secret \ --docker-server=eu.gcr.io \ --docker-username=_json_key \ --docker-password="$(cat /c/Users/test_namespace/gc.json)" \ --email@example.com
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.
For more such posts related to Kubernetes, Stay tuned with us.
Till then, Happy learning!! 🙂