5 Variants of Kubectl commands to know!

Filed Under: Random
Kubectl Commands

Hello, readers! In this article, we will be focusing on Variants of kubectl Commands, in detail.

So, let us begin!! 馃檪


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


1. Pod related kubectl commands

In this section, we will be specifically dealing with Kubernetes Pod related kubectl commands in daily use–

From the below commands,

  1. The first command help us get a list of all the pods in the default namespace .
  2. 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.
  3. 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

2. Replicaset related kubectl commands

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
  1. With kubectl get command, we list the replicasets in the default namespace
  2. The describe command helps us get detailed information about a particular replicaset

3. Deployment specific kubectl commands

There is a very little yet major difference between Deployments and Replicasets. Deployments work and look after all other resources as well.

  1. In the below first command, we get the list of deployments for a particular namespace.
  2. 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.
  3. In order to edit a deployment in a particular namespace, we can use the edit deployment command.
  4. 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

  1. 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.
  2. With kubectl run command, we deploy a pod at run time, expose it to a service port.
  3. 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.

5. Secret related commands

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-

  1. docker-server: The host name where the key would be accessed for the particular secret.
  2. docker-username: The key name
  3. docker-password: The JSON file path
  4. docker-email
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)" \
--docker-email=xyz@gmail.com

Conclusion

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!! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content