The Wayback Machine - https://web.archive.org/web/20240118173410/https://www.geeksforgeeks.org/kubernetes-kubectl/
Open In App
Related Articles

Kubernetes – Kubectl

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Pre-requisite: Kubernetes

Kubectl is a command line tool using which we can run commands and perform various operations like creating resources, depleting resources, and listing down the resources inside a Kubernetes cluster. Kubernetes provides and maintains this tool. Below we will see its syntax, what resources it creates and how to list them, and also see their logs.

kubectl

 

Installation of  Kubectl:

To install Kubectl you can follow the below sources for the respective environment.

  • Linux
  • macOS
  • Windows

Once you install Kubectl on a respective environment you can cross-check by running the below command.

$ kubectl version 

Syntax:

$ kubectl [command] [TYPE] [NAME] [flags]
  • Command: This will include the operation mentioned below you want to perform regarding the specific resource.
    • Create
    • get
    • describe
    • delete
  • Type – Here you have to mention the specific resource provided by Kubernetes that you want to create like pods, namespaces, secrets, deployment, replica sets, and many more. This is case-sensitive.
  • NAME – Here we will provide the name which is case-sensitive.
  • flags – This will use to pass the flags like address or port number.

How to run the commands for resources that kubectl provides.

Create a Resource Using Kubectl:

In Kubernetes, a resource gets created inside a namespace which resource using which we can create other resources in a separate environment.

First, we are creating a namespace inside which we will create our Pod.

$ kubectl create namespace geeksforgeeksnamespace

We can create a Pod using the below YAML file.

apiVersion: v1
kind: Pod
metadata:
 name: geeksforgeeks
 namespace: geeksforgeeksnamespace
spec:
 containers:
 - name: nginx
   image: nginx:1.14.2
   ports:
   - containerPort: 80

The above file is a configuration file using which we are creating a pod of name geeksforgeeks in namespace geeksforgeeksnamespace which is an Nginx server. Similarly, we can create other resources like secrets, replica sets, etc by just changing the name in front of the kind and mentioning the key value in metadata and spec accordingly.

$ kubectl create -f geeksforgeekspod.yaml

To Get the Resources:

Command to see all the namespaces.

kubectl get namespaces

To see the namespace geeksforgeeksnamespace run the below command.

$ kubectl get namespace

Command to see the list of all pods.

$ kubectl get pods

This will show the pods in the default namespace. To list down the pods in a particular namespace we have to pass the flag -n and the name of the namespace.

$ kubectl get pods -n NAME_OF_NAMESPACE

So if we have to list down the pod that we have created in namespace geeksforgeeksnamespace, run the below command.

$ kubectl get pod -n geeksforgeeksnamespace 

Describe a Resource:

To describe the resource we have to run kubectl describe command and then mention the resource and the namespace that resource is in.

$ kubectl describe resource_name -n namespace_name

So now we will describe our pod.

$ kubectl describe pod geeksforgeeks -n geeksforgeeksnamespace
Name:         geeksforgeeks
Namespace:    geeksforgeeksnamespace
Priority:     0
Node:         cluc-control-plane/172.19.0.8
Start Time:   Sun, 15 May 2022 11:17:36 +0000
Labels:       <none>
Annotations:  cni.projectcalico.org/containerID: ce6d80d1a912
a3edda623aeab52c2995aee87e7c225d6258f888d70a36c79b68
             cni.projectcalico.org/podIP: 10.244.206.106/32
             cni.projectcalico.org/podIPs: 10.244.206.106/32
Status:       Running
IP:           10.244.206.106
IPs:
 IP:  10.244.206.106
Containers:
 nginx:
   Container ID:   containerd://b265bb64e9cea231b9e52a150f2
   1399b4d051000c2c5dc9bc03cf1de42d060ae
   Image:          nginx:1.14.2
   Image ID:       docker.io/library/nginx@sha256:f7988fb6c02e0
   ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
   Port:           80/TCP
   Host Port:      0/TCP
   State:          Running
     Started:      Sun, 15 May 2022 11:19:20 +0000
   Ready:          True
   Restart Count:  0
   Environment:    <none>
   Mounts:
     /var/run/secrets/kubernetes.io/serviceaccount
      from kube-api-access-gz2zs (ro)
Conditions:
 Type              Status
 Initialized       True 
 Ready             True 
 ContainersReady   True 
 PodScheduled      True 
Volumes:
 kube-api-access-gz2zs:
   Type:                    Projected (a volume that contains 
                             injected data from multiple sources)
   TokenExpirationSeconds:  3607
   ConfigMapName:           kube-root-ca.crt
   ConfigMapOptional:       <nil>
   DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                            node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:                      <none>

Delete the Resources:

kubectl delete command is used to delete the resources by passing their name and namespace if we are deleting other resources other than a namespace.

$ kubectl delete resource
 resource_name -n namespace_name

Now we will delete the pod and namespace.

$ kubectl delete pod 
geeksforgeeks -n geeksforgeeksnamespace

You can verify whether the pod is deleted or not by running kubectl get pod -n geeksforgeeksnamespace.

Now we will delete the namespace.

$ kubectl delete namespace 
geeksforgeeksnamespace

Note: If we delete the namespace before the pod then the pod will delete automatically with other resources in that namespace.

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 30 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials