Pages

Showing posts with label DevOps. Show all posts
Showing posts with label DevOps. Show all posts

Friday, February 24, 2023

Kubernetes Cheat Sheet

 Kubernetes cheat sheet


Listing the below commands which will be useful for administering the kubernetes environment.

 

Version:

# Get kubernetes version: The Server Version line indicates the version of Kubernetes that is running on the Kubernetes cluster.

kubectl version


Pods:

# Get the details of all pods in one namespace

kubectl get pods -n <namespace>


#  Get the details of all pods in all namespaces

kubectl get pods --all-namespaces 


#  Get the details of all pods in the current namespace, with more details

kubectl get pods -o wide  




Deployments:

# Get all  the deployments 

kubectl get deployments


# Check the history of deployments including the revision

Kubectl rollout history deployment <deployment name>


# Rolling  restart of the deployment

Kubectl rollout restart deployment <deployment name>



Interacting with pods:

# Login to pod

Kubectl exec -it <podname> -- bash


#Run the commands in the pod

Kubectl exec <podname> -- ls /





configmap :


# Get all configmap in current namespace

Kubectl get cm


# Edit configmap 

Kubectl edit cm <configmap name>


Secrets:


# Display all the secrets 

Kubectl get secrets


# Display all the secrets 

Kubectl get secrets --all-namespaces 



Services:


# Display all the services 

Kubectl get svc


# List Services Sorted by Name

kubectl get services --sort-by=.metadata.name



Logs:

# Check the logs of the pod

kubectl logs <podname>


# Tail the logs of the pod

kubectl logs -f <podname>


Copy :

# copy  the data from local directory to  the pod

kubectl cp $HOME/data <pod>:/opt/app/data



Storage:


# List all PersistentVolumes

kubectl get pv


# List all PersistentVolumes sorted by capacity

kubectl get pv --sort-by=.spec.capacity.storage


# Describe specific persistent volume

kubectl describe pv <pv_name>


# List all persistent volumes claims

kubectl get pvc


# Describe specific persistent volume claim

kubectl describe pvc <pvc_name>



Create/Delete resources:



# Create specific resource

kubectl apply -f <deployment>.yaml


# Create specific resource using URL

kubectl apply -f https://gcs.io/pod


# Describe specific persistent volume claim

kubectl delete -f <deployment>.yaml



Formatting output:


# Append the following to command to get output in json format 


-o json  


# Append the following to command to get output in yaml format 


-o yaml


# Append the following to command to get output in plain text format with additional information for pods.


-o wide


# Print a table using a comma separated list of custom columns


-o=custom-columns=<spec> 


Cluster details:


# Display the cluster details


kubectl cluster-info


Below bash alias are more useful:


    alias k="kubectl"

    alias allpods="kubectl get pods --all-namespaces"

    alias kc="kubectl create -f"

    alias kg="kubectl get"

    alias pods="kubectl get pods"

    alias ktop="kubectl top nodes"

    alias rcs="kubectl get rc"

    alias sv="kubectl get services"

    alias dep="kubectl get deployment"

    alias kd="kubectl describe"

    alias kdp="kubectl describe pod "

    alias kds="kubectl describe service "

    alias nodes="kubectl get nodes"

    alias klogs="kubectl logs"

    alias ns="kubectl get ns"

    alias deploys="kubectl get deployment"

    alias events="kubectl get events"

    alias kexec="kubectl exec -it "

    alias sec="kubectl get secrets"