Kubectl (Kubernetes) cheat sheet

Kubectl (Kubernetes) cheat sheet

This post contains a list of command to remember some basic operations for Kubernetes. It's not about explaining what Kubernetes is or how to use it, this is more about having a quick reference of useful commands you can use in your K8 clusters.

List info

// list replica sets
kubectl get rs

// list pods
kubectl get pods

// get running services (kubernetes and hw NodePort in the example)
kubectl get services

// more get commands
Kubectl get all
Kubectl get DEPLOY_NAME
Kubectl get DEPLOY_NAME -o yaml

// list deployments
kubectl get deployments

Basic commands

//logs 
kubectl logs [pod name]

//get inside a pod
Kubectl exec -it [pod name] /bin/bash

// check if port is exposed for specific deployment
Kubectl expose deployment NAME --type=NodePort

// modify port
Kubectl expose deployment NAME --type=NodePort --port=80

Labels

// list labels
kubectl get pods --show-labels

// add label
kubectl label [pod name] [label]
kubectl label [pod name] [label] --overwrite
e.g.
kubectl label my-pod env=development

// remove label (add - at the end)
kubectl label [pod name] [label]-

// filter by label
kubectl get pods --selector [label-filter]

### examples:

kubectl get pods --selector env=production

// negate
kubectl get pods --selector env!=production

//version range
kubectl get pods -l 'release-version in (1.0, 2.0)' —show-labels
kubectl get pods -l 'release-version not in (1.0, 2.0)'  —show-labels

// delete pods by label
kubectl delete pods -l 'state=disabled'

Deployments

// create deployment
kubectl create deployment NAME —image=DOCKER_IMAGE

// list deployments
kubectl get deployments

// change docker image for deployment
kubectl set image deployment/NAME CONTAINER_NAME=IMAGE

// history for deployments
kubectl rollout history deployment/NAME

// rever deployment
kubectl rollout undo deployment/NAME
kubectl rollout undo deployment/NAME —to-revision=X (base on history info)

// get deployment info 
Kubectl describe deployment NAME

Image credits:

Education vector created by stories - www.freepik.com


Share Tweet Send
0 Comments
Loading...