Working with Kubernetes

Kubernetes, also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.


  • Management of containers: Service discovery/load balancing, secrete and configuration management
  • Scale containers: self healing, horizontal scaling
  • Storage Orchestration: Robust networking and storage options

Here is the list of useful Kubernetes commands and examples.

Basic Commands

kubectl version 
kubectl cluster-info 

# pods, deployments, and services 
kubectl get all 

# create a deployment for a pod
kubectl run <container> -image=<image> 

# forward a port wot allow external access
kubectl port-forward <pod> <ports> 

# expose a port for a deployment
kubectl expose 

kubectl create <resource> 
kubectl apply <resource>

Checking Kubernetes is working

kubectl get all

# if you got this error:
# The connection to the server localhost:8080 was refused
# - did you specify the right host or port?
# run the following commands
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Checking Cluster

kubectl cluster-info


  • Namespaces provide a scope for names.
kubectl create namespace <ns-name>

kubectl get namespace
kubectl describe namespace <ns-name>

# use -n option to see the resources in the namespace
kubectl get all -n <ns-name> 

Kubernetes Objects

Kubernetes API primitives (objects) are the basic building blocks that represent the state of the cluster.

  • What kind of applications are running on which nodes
  • The resource available to the applications
  • The policies that define the behaviors of applications, (restart policies, fault-tolerance, and upgrade)
  • Types of Kubernetes Objects: Pods, Nodes, Services, Service Accounts
# list of resources (objects) - name and type
kubectl api-resources

kubectl api-resources -o name #name only

Managing Kubernetes Objects

You can use the “kubectl” command-line tool to create and manage Kubernetes objects.

  • Imperative commands: manage objects directly using a single command
kubectl create deployment nginx --image nginx
  • Imperative object configuration: using at least one definition file
kubectl apply -f nginx.yaml
  • Declarative object configuration: a user operates on object configuration files stored locally. Create, update, and delete operations are automatically detected per-object by kubectl.
kubectl diff -f configs/
kubectl apply -f configs/

Getting Object Information

  • The yaml format is usually used to define the object and its spec.
kubectl get <object-type>

kubectl get pods
kubectl get pods -n kube-system # system pods

kubectl get nodes
kubectl get nodes <node-name>
kubectl get nodes <node-name> -o yaml

kubectl describe node <node-name>

Checking nodes

kubectl get nodes

kubectl describe node <node_name>

Getting Definition Files

kubectl <cmd> --dry-run=client -o yaml 
kubectl run <pod-name> --image=<image-name> --dry-run -o yaml

# Examples

kubectl run nginx --image=nginx  --dry-run=client -o yaml 

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml 

kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml

kubectl create service nodeport nginx-service --tcp=80:80 --node-port=30080 --dry-run=client -o yaml

You can output to the file using:

kubectl <cmd> > <file-name.yaml>

kubectl run <pod-name> --image=<image-name> --dry-run=client -o yaml > pod.yaml

kubectl get <resource> <resource-name> -o yaml > resource.yaml 

kubectl get pod <pod-name> -o yaml > pod.yaml

kubectl get pod redis -o yaml > pod.yaml 

Deleting resources

#delete pod causes a deployment will recreate a pod
kubectl delete pod <pod-name>
kubectl delete pod <pod-name> --namespace <ns>

#delete a deployment
kubectl delete deployment <deployment-name>

#delete all
kubectl delete all --all --all-namespaces

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s