Skip to main content
Kubernetes Commands Notes

Kubernetes Commands Notes

··692 words·4 mins
Mike Curtis
Author
Mike Curtis
Dedicated to Technology

Getting Started
#

Run a Pod
#

A Pod is a group of one or more containers that share storage (volumes) and networking resources (IP address, ports).

command Function
kubectl run nginx --image=nginx deploys a single Pod named nginx using the image nginx
kubectl get pods -o wide provides a list of running pods, includes IP and node information
kubectl describe pod nginx Provides detailed information about the pod named ngnix
kubectl delete pod nginx Deletes a Pod named nginx

Run a deployment
#

command Function
kubectl create deployment nginx --image=nginx deploys a deployment named nginx using the image nginx
kubectl get pods -o wide provides a list of running pods, includes IP and node information
kubectl delete deployment nginx Provides detailed information about the deployment named ngnix

Run a multi instance deployment
#

command Function
kubectl create deployment nginx --image=nginx --replicas=2 deploys a deployment named nginx using the image nginx with two instances
kubectl get pods -o wide provides a list of running pods, includes IP and node information
kubectl delete deployment nginx Provides detailed information about the deployment named ngnix

List Pods
#

command Function
kubectl get all Lists all pods and deployments in the default namespace
kubectl get pods -o wide provides a list of running pods, includes IP and node information
kubectl get all -n <name space> Lists all pods and deployments in the specified namespace

List Deployments
#

command Function
kubectl get deployments.apps Lists all deployments in the default namespace
kubectl get deployments --all-namespaces Lists all deployments in all namespaces
kubectl get deployments -A Lists all deployments in all namespaces, just a short version
kubectl get deployments -n <namespace-name> Lists all deployments in the specified namespace

ConfigMaps
#

command Function
kubectl get configmaps Lists configMaps
kubectl create configmap dem-heros --from-file=heros.txt Creates a configMap
kubectl create cm dem-heros --from-file=heros.txt shorthand version Creates a configMap
kubectl describe cm dem-heros Provides detailed information about the configMap
kubectl delete cm dem-heros removes configMap

Secrets
#

command Function
kubectl get secrets Lists secrets
kubectl create secret generic mysql-secret --type=kubernetes.io/basic-auth --from-literal=password=alta3 Creates a secret from a cli command
kubectl create secret -f secret.yaml Creates a secret from a file
kubectl describe secret mysql-secret Provides detailed information about the secret
kubectl delete secret mysql-secret removes secret

Namespaces
#

command Function
kubectl get namespace Lists all namespace
kubectl create ns demo creates a namespace called demo
kubectl describe ns demo Provides information about a namespace such as LimitRange resource, resource quota, and description
kubectl delete ns demo removes namespace called demo

Logs
#

command Function
kubectl describe portainer-agent-7c9df8687-45m5s Gets information about the pod
kubectl logs portainer-agent-7c9df8687-45m5s Gets detailed logging information about the container
kubectl logs deportainer-agent-7c9df8687-45m5smo -c container2 Gets detailed logging information about the specified container when a pod has more than one container -c may be needed.
kubectl logs portainer-agent-7c9df8687-45m5s --all-containers Gets detailed logging information about the all containers.
kubectl logs portainer-agent-7c9df8687-45m5s -n portainer --all-containers Gets detailed logging information about the all containers in a specified namespace
kubectl logs portainer-agent-7c9df8687-45m5s -n portainer --all-containers Gets detailed logging information about the all containers in a specified namespace
kubectl logs portainer-agent-7c9df8687-45m5s -n portainer f Follows detailed logging information about the container in a specified namespace

Labels
#

command Function
kubectl label pod label-demo app=nginx Can be used to add to existing labels. cannot overwrite existing labels
kubectl label pod label-demo app=web --overwrite Can be used to overwrite a existing label.
kubectl label pod label-demo app- Can be used to remove a existing label.
kubectl get pods -L app Can be used to list Labels with a certain name
kubectl get pods --selector=app=nginx Can be used to list pods that have a specified label and value

Remove Deployments
#

kubectl delete deployment <deployment-name> -n <namespace>

Non Out of the box commands
#

Metrics
#

command Function
kubectl top nodes Provides the memory and CPU usage for a node
kubectl top pods -A Provides the memory and CPU usage for pods in all name spaces
kubectl top pods -n <namespace> Provides the memory and CPU usage for pods in the specified namespace
kubectl top pods -n <namespace> Provides the memory and CPU usage for pods in the specified namespace

Related