Useful Kubectl commands in Kubernetes

In this article, I have clubbed important commands which are used in Kubernetes on a daily basis.

Kubectl cluster-info

No alt text provided for this image

kubectl get nodes

No alt text provided for this image

Specifying alias for Kubernetes

alias k =kubectl

Below is the sample output

No alt text provided for this image

Setup AutoComplete in Kubectl

Install bash-completion package on your master node. (Ubuntu)

apt-get install bash-completion

The above commands create /usr/share/bash-completion/bash_completion, which is the main script of bash-completion

apt-get update

Check the bash-completion install directory

ls /usr/share/bash-completion/

Add the following to your ~/.bashrc file:

source /usr/share/bash-completion/bash_completion

Setup autocomplete for Kubectl.

complete -F __start_kubectl k

Start typing `g` and then press TAB which will then autocomplete `get`.

kubectl g TAB

kubectl get

kubectl get de TAB

kubectl get deployment

Creating objects such as PODs, deployments etc. from multiple files

kubectl apply -f ./firstfile.yaml -f ./secondfile.yaml

Get all pods in namespaces

Below example shows details of the pod using the format 

No alt text provided for this image

To get all pods in all name spaces

kubectl get pods --all-namespaces

Below command is used to get details of the pod

kubectl describe pods <pod-name> -ns <name space in which pod exists>

No alt text provided for this image

Get POD YAML file

kubectl get pod <pod name> -o yaml

Get documentation of POD manifests

kubectl explain pods

List all services in the namespace

kubectl get services                        

List a particular deployment

kubectl get deployment <deployment name>                

 Running command in a POD

Create the Pod as an example:

 

kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml

Verify that the Container is running:

kubectl get pod shell-demo

Get a shell to the running Container:

kubectl exec -it shell-demo -- /bin/bash

Note: The double dash symbol “–” is used to separate the arguments you want to pass to the command from the kubectl arguments. In your shell, list the root directory:

root@shell-demo:/# 

Merging multiple YAML manifests files into one.

This is just a YAML containing multiple Kubernetes Resources, separated by the three dashes —--. In the below example, three yaml files are combined to one using ---

Two YAML files is for PODs and one is for replica set

No alt text provided for this image

Command to check API Versions and API resources in Kubernetes

kubectl api-versions

kubectl api-resources -o wide

Api-versions for few of the k8s objects

Below objects are used with core V1 apiVersions

bindings

componentstatuses

configmaps

endpoints

events

limitranges

namespaces

nodes

persistentvolumeclaims

persistentvolumes

pods

podtemplates

replicationcontrollers

resourcequotas

secrets

serviceaccounts

services

Below objects are used with apps/V1

controllerrevisions

daemonsets

deployments

replicasets

statefulsets

Below objects are used with networking.k8s.io/v1

ingressclasses

ingresses

networkpolicies

Below objects are used with rbac.authorization.k8s.io/v1

clusterrolebindings

clusterroles

rolebindings

roles






Sandeep Kumar

Helping build a better Internet | Tech Sales & Solution Innovator | Cybersecurity Advocate | Partner Ecosystem builder | IIT Palakkad

4 年

Nice One Atul..

Akhil Mahajan

Data & Analytics Leader | Architect | Procter & Gamble

4 年

Great work Atul Sharma

Raghin Raghavan

Sr. Release Engineer Salesforce

4 年

It's a nice quick reference. Works more than juz a cheatsheet.. Nice work Atul

要查看或添加评论,请登录

Atul Sharma的更多文章

  • My experience with GCP PCA exam

    My experience with GCP PCA exam

    50 questions in 2hrs. No pass percentage is mentioned.

    1 条评论
  • Basic Docker Commands & usage -1

    Basic Docker Commands & usage -1

    How to check version of docker $docker version Start a container docker run nginx Unable to find image 'nginx:latest'…

    4 条评论
  • Deploying a self-managed Kubernetes cluster in Azure using Kubeadm

    Deploying a self-managed Kubernetes cluster in Azure using Kubeadm

    Create a resource Group via Azure CLI prompt or cloud shell $ az group create --name k8s-lab-rg3 --location eastus $ az…

    4 条评论
  • Kubernetes Networking

    Kubernetes Networking

    Kubernetes provides network model/principles and additional tools implement it. Network policies are implemented by the…

    3 条评论

社区洞察

其他会员也浏览了