Ashish Singh Baghel's Blog

Basic Kubernetes (K8s) Commands | Minikube | Pods | Services

March 02, 2020

Kubernetes Basics

Using minikube to create a single node cluster

minikube cli screenshot

Check if minikube is installed

minikube version

Start the cluster

minikube start
minikube status

minikube can download the appropriate version of kubectl

minikube kubectl -- get pods -A

Check if kubectl is installed

kubectl version

Check cluster details

kubectl cluster-info

View nodes in the cluster

kubectl get nodes

Deploy an app

kubectl create deployment kubernetes-bootcamp

List your deployment

kubectl get deployments

Check existing pods

kubectl get pods
kubectl get pods -o wide

Describe pods

kubectl describe pods

Check logs of a container running in pod

kubectl logs $POD_NAME

Execute command directly inside container running in a pod

kubectl exec $POD_NAME env

Start a bash session in a pod container

kubectl exec -ti $POD_NAME bash

To close your container connection type exit.

List current services from your cluster

kubectl get services

Create a new service and expose it to external traffic with NodePort as paramter

kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080

Describe a service

kubectl describe services/kubernetes-bootcamp

Test your app on the externally exposed port

curl $(minikube ip):$NODE_PORT

Note: You can also run following command to check your deployment minikube will start the tunnel for your service and open it in default browser…

minikube service kubernetes-bootcamp

Query list of pods using label

kubectl get pods -l run=kubernetes-bootcamp

Query list of services using label

kubectl get services -l run=kubernetes-bootcamp

Apply a new label to the object

kubectl label pod $POD_NAME app=v1

Deleting a service

kubectl delete service -l run=kubernetes-bootcamp

Scale up the service

kubectl scale deployments/kubernetes-bootcamp --replicas=4

Scale down the service

kubectl scale deployments/kubernetes-bootcamp --replicas=2

Rolling update, Update the image of your application

kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2

Confirm an update

kubectl rollout status deployments/kubernetes-bootcamp

Rollback an update

kubectl rollout undo deployments/kubernetes-bootcamp

Delete a deployment

kubectl delete deployment kubernetes-bootcamp

Stop minikube

minikube stop

© 2021 Ashish Singh Baghel, Built with Gatsby