Profile PictureDevlopr Lightxplor4r
Open menu

5 min read, 736 words

Kubernetes Commands cheatsheet

Excerpt : All kubernertes commands for reference

Kubernetes Commands cheatsheet

Set docker without root permission

sudo usermod -aG docker $USER && newgrp docker

Setup Kubectl

https://kubernetes.io/releases/download/

Setup Minikube

For running Kubernetes cluster locally
https://minikube.sigs.k8s.io/docs/start/

Kubernetes commands

Create

kubectl create -f Filename

Get

kubectl get nodes
kubectl create -f t2.yml

to create deployment

kubectl get deployments

create deployment

kubectl create deployment NAME --image=image -- [COMMAND][args ...]
kubectl create deployment mydep --image=nginx --replicas=4

Get

Prints a table of the most important information about the specified resources. you can filter the list using a label selector and the --selector flag.
If the desired resource type is namespaced you will only see results in your current namespaces unless you pass --all-namespace
kubectl get {resource name}
List all pods in ps format
kubectl get pods
or
kubectl get po
List all pds in varous different namespaces
kubectl get pods --all-namespaces

get all pods with more information

kubectl get pods -o wide

get services

kubectl get services

Expose

Expose a resources as a new kubernertes services. Looks up a deployment, services, resplica set, replication controller or pod by name and uses the selector for that resource as the selectorfor a new service on the specified port.
Syntax:
kubectl expose (-f FILENAME | TYPE NAME) [ --port=port] [--portocol=TCP|UDP|SCTP] ---target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]
Create a service for an nginx deployment, which serves on port 80 and connects to the containers on port 8000
kubectl expose deployment nginx --port=80 --raget-port=8000
--port - currently using

get services

kubectl get svc

Delete Resources

Delete resources by filenames, stdin, resources and names, or by resources and label selector. JSON and YAML formats are accepted. Only one type the arguments may be specified: filenames, resources and names or resources and label selector
$ kubectl delete ([-f FILENAME] [-k DIRECTORY] | TYPE [(NAME | -l label | --all])
Delete pods and services with label name = myLabel
kubectl delete pods, services -l name=myLabel
Delete all pods
kubectl delete pods --all
Example :
kubectl get deployments
kubectl get pods
kubectl delete -f t2.yaml
kubectl get pods
kubectl delete pod mydep-3434354354
Even if we delete pod, kubernetes will respawn a new pod based on the replicass metnrioned before.

Autoscale

Creates an autoscaler that automatically chooses and sets the number of pods that run in a kubernetes cluster.
Looks up a Deploymen, ReplicaSet, StatefulSet, pr ReplicationController by name and creates an autoscaler that uses the given resource as a reference
An autoscaler can automatically increase or decrease number of pods deployed within the system as needed.
$ kubectl autoscale (-f FILENAME | TYPE NAME | TYPE/NAME) [--min=MINPODS] --max=MAXPODS [--cpu-percent=CPU]
Autoscale a deployment "foo", with the number of pods between 2 and 10, no target CPU utilization specified so a default autoscaling policy wll be used:
kubectl autoscale deployment foo --min=2 --max=10

Scale :

Set a new size for a Deployment, ReplicaSet, Replication Controller, or StatefulSet. Scale also allows users to specify one or more preconditions for the cscale action.
If --current-replicas or --resource-version is specified, it is validated before the scale is attempted and it is guaranteed that the precondition holds true when the scale is sent to the server.
$ kubectl scale [--resource-version=version] [--current-replicas=count] --replicas=COUNT (-f FILENAME | TYPE NAME)
Scale statefulset named 'web' to 3
kubectl scale --replicas=3 statefulset/web
eg.
kubectl scale --replicas=replicacount filename/type
kubectl scale --replicas=4 deployment mydep

Describe:

Print a detailed description of the selected resources, including related resources such as events or controllers. Yo may select a single object by name all objects of that type, provide a ame prefix, or label selector
$ kubectl describe (-f FILENAME | TYPE [NAME_PREFIX | -l label] | TYPE/NAME)
eg.
Describe a pod
kubectl describe pods/nginx

Logs

Prints th e logs for a container in a pod odr specified resource. If the pod has only one container, the container name is optional.
$ kubectl logs [-f] [-p] (POD | TYPE/NAME) [-c CONTAINER]
Return snapshot logs from all containers in pods defined by label app=nginx
kubectl logs -lapp=nginx --all-containers=true
eg.
kubectl get po

get all pods

kubectl logs mydep-w34rerwerwr

Exec

Execute a command in a container.
$ kubectl exe(POD | TYPEE/NAME) [-c CONTAINER] [flags] -- COMMAND [args...]
Get output from running 'date' command from pod mypod using the first container by default
kubectl exec mypod -- date
For ref: https://www.youtube.com/playlist?list=PLVHgQku8Z934suC9LSE6vaAKjOH_MfRbE

© 2023 xplor4r. Built with love by Sujay