Kubernetes Basics

Here you can find basic k8 commands I gathered while following K8s Tutorials that are available in the kubernetes site. The tutorials are free, interactive and can be done from your browser without installing any SW.

As I progress in my studies I will be updating the cheatsheet, so you can expect more changes in the future.

Install kubectl and minikube

k8s
brew install kubectl
brew install minikube

minikube

Start, stop, pause, unpause, delete all minkiube

minikube
minikube start
minikube stop
minikube status
minikube pause
minikube unpause
minikube delete --all

Ssh into minkube

minikube
minikube ssh [flags]

On minikube open your ports for your app

minikube
minikube service list
minikube service my-service

Source minikube get started

K8s Cluster

Kubernetes coordinates a highly available cluster of computers that are connected to work as a single unit. The abstractions in Kubernetes allow you to deploy containerized applications to a cluster without tying them specifically to individual machines.

Kubernetes automates the distribution and scheduling of application containers across a cluster in a more efficient way.

A Kubernetes cluster consists of two types of resources:

  • The Control Plane coordinates all activities in your cluster, such as scheduling applications, maintaining applications' desired state, scaling applications, and rolling out new updates.
  • Nodes are the workers that run applications

Cluster Overview

Cluster Overview

See k8 version

k8s
kubectl version

See nodes in k8

k8s
kubectl get nodes

Pods

A Pod (as in a pod of whales or pea pod) is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.

A Pod's contents are always co-located and co-scheduled, and run in a shared context. A Pod models an application-specific "logical host": it contains one or more application containers which are relatively tightly coupled.

Pods Overview

Pods Overview

Source

Create a deployment

k8s
kubectl create deployment kubernetes-bootcamp --image=grc.io/google-samples/kubernetes-bootcamp:v1

See deployments

k8s
kubectl get deployments

Create a proxy

k8s
kubectl proxy

See PODs running in default namespace

k8s
kubectl get pods

View containers inside the POD

Describe works with most K8s primitives: node, pod, deployments.

k8s
kubectl describe pods

See container logs

k8s
kubectl logs <POD_NAME>

Execute commands inside a container

k8s
kubectl exec <POD_NAME> -- env
kubectl exec -ti <POD_NAME> -- bash

Wait for a POD to be ready by label

k8s
kubectl wait --for=condition=ready pod -l app=inventory

Once you see the output condition met of the above commands it means your microservice is ready to receive requests.

You can also monitor the status manually with watch however you need to exit manually with Ctrl-C

k8s
kubectl get --watch pods

Services

Services allow your applications to receive traffic. Services can be exposed in different ways by specifying a type in the ServiceSpec:

  • ClusterIP (default) - Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.
  • NodePort - Exposes the Service on the same port of each selected Node in the cluster using NAT. Makes a Service accessible from outside the cluster using <NodeIP>:<NodePort>. Superset of ClusterIP.
  • LoadBalancer - Creates an external load balancer in the current cloud (if supported) and assigns a fixed, external IP to the Service. Superset of NodePort.
  • ExternalName - Maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record with its value. No proxying of any kind is set up. This type requires v1.7 or higher of kube-dns, or CoreDNS version 0.0.8 or higher.

Services match a set of Pods using labels and selectors, a grouping primitive that allows logical operation on objects in Kubernetes. Labels are key/value pairs attached to objects and can be used in any number of ways:

  • Designate objects for development, test, and production
  • Embed version tags
  • Classify an object using tags

Services Overview

Services Overview

Source

List current services

k8s
kubectl get services

Create a service & expose it

For minikube, NodePort is used.

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

See port opened externally

k8s
kubectl describe services/kubernetes-bootcamp

See the POD label

k8s
kubectl describe deployment

Get POD and service by label

k8s
kubectl get pods -l app=kubernetes-bootcamp
kubectl get services -l app=kubernetes-bootcamp

Get POD port and set it as env var

k8s
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT

Get POD name and set it as env var

k8s
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME

Add a new label to the POD

This action will append only and will not remove any existing labels

k8s
kubectl label pods $POD_NAME version=v1

delete a service by label

k8s
kubectl delete service -l app=kubernetes-bootcamp

Scaling

Scaling is accomplished by changing the number of replicas in a Deployment.

Scaling will increase the number of Pods to the new desired state. Kubernetes also supports autoscaling of Pods

Services have an integrated load-balancer that will distribute network traffic to all Pods of an exposed Deployment.

Initial State

Initial State

Scaling Deployment

Scaling Deployment

See ReplicaSet created by the deployment

k8s
kubectl get rs

Scale the deployment

In this case is scale to 4 replicas

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

# verify by
kubectl get deployments
kubectl get pods -o wide
kubectl describe deployments/kubernetes-bootcamp

Find the expose IP that is load balancing

k8s
kubectl describe services/kubernetes-bootcamp

Rolling Updates

Rolling updates allow Deployments' update to take place with zero downtime by incrementally updating Pods instances with new ones.

if a Deployment is exposed publicly, the Service will load-balance the traffic only to available Pods during the update. An available Pod is an instance that is available to the users of the application.

1. Initial State

1. Initial State

2. Rolling update started

2. Rolling update started

3. Rolling update in progress

3. Rolling update in progress

4. Rolling update finished

4. Rolling update finished

Update image

Update the image of the application to version 2

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

# You can review by checking the POD status
kubectl get pods

kubectl rollout status deployments/kubernetes-bootcamp

Rollback

Rollback the deployment to the last working version

k8s
kubectl rollout undo deployments/kubernetes-bootcamp

ConfigMaps & Secrets

There are several ways to set environment variables for a Docker container in Kubernetes, including:

ConfigMaps and Secrets are stored as key-value pairs.

Create a ConfigMap

k8s
kubectl create configmap sys-app-name --from-literal name=my-system
configmap/sys-app-name created

Create a Secret

k8s
kubectl create secret generic sys-app-credentials --from-literal username=bob --from-literal password=bobpwd

Bonus Materials

These are some materials I find very useful to learn K8s.