Skip to content

Instantly share code, notes, and snippets.

@peltho
Created April 7, 2020 10:18
Show Gist options
  • Save peltho/a882c73c529a010600c6fc00758c8502 to your computer and use it in GitHub Desktop.
Save peltho/a882c73c529a010600c6fc00758c8502 to your computer and use it in GitHub Desktop.
Kubernetes cheatsheet

Kubernetes

What is it?

It's a system for automating deployment, scaling and management of containerized apps.

Lexique

Word  Meaning 
Node It's a machine from a cluster (VM or physical)
Pod It's a super-container (network / storage / application) of containerized applications (docker of docker)
Kubelet Agent responsible to communicate the cluster's master and the other nodes
Minikube Lightweight Kubernetes version allowing a cluster deployment with a single node locally
Overview

Node overview

Basic usage

Basically you are going to interact in a cluster with a command-line utility: kubectl

List all nodes of the cluster: kubectl get nodes

List all deployments: kubectl get deployments

Open an internal proxy to access the app: kubectl proxy

Create a deployment of an app:

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

List all pods of the cluster: kubectl get pods

Get advanced details of pods: kubectl describe pods

Store pod names into a global variable:

export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')

Display logs from a given pod:

kubectl logs $POD_NAME

Open a bash session into a pod's container:

kubectl exec -it $POD_NAME bash

Deploy from a yaml config file:

kubectl apply -f {filepath}

Config documentation

Services (exposing the app publicly)

A Service routes traffic across a set of Pods. They allow your applications to receive traffic by exposing pods.

Service configuration

x < y: y is a superset of x.

ClusterIP < NodePort < LoadBalancer < ExternalName

ClusterIP: Expose the service on an internal IP in the cluster. NodePort: Makes a Service accessible from outside the cluster using <NodeIP>:<NodePort>.

LoadBalancer: Creates an external load balancer and assigns a fixed external IP to the service.

ExternalName: Exposes the service using a name by returning a CNAME record.

Service and labels

Getting list of current services:

kubectl get services

Create a service:

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

Describe a service:

kubectl describe services/kubernetes-bootcamp

Create an environment variable with the node port:

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

Now the the app can be accessed externally:

curl $(minikube ip):$NODE_PORT or curl 172.17.0.102:$NODE_PORT

Get advanced details of deployments:

kubectl describe deployments

Once you get the label, you can fetch a resource more precisely:

kubectl get [pods|services] -l {label}

Store the pod's name into a variable:

export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}
{{end}}')

Attach a new label to a pod: kubectl label pod $POD_NAME app=v1

Now can be picked with: kubectl get pods -l app=v1

Delete a service:

kubectl delete service -l {label}

This deletes the service but not the application which continues to live inside the cluster !

Scaling

Get the number of replicas from the deployment: kubectl get rs

Scale up (by 4): kubectl scale deployments/kubernetes-bootcamp --replicas=4

Then pods are 4: kubectl get pods

Get intermediary level of details (to get IP addresses of pods): kubectl get pods -o wide

Each time you'll try to curl the app, it will be on a different node because of internal load balancer of the service.

Scale down:

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

Verify it quickly with: kubectl get deployments or kubectl get pods -o wide

Rolling-update (with zero downtime)

Basically it consists of updating the image of the running application:

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

Check status of update: kubectl rollout status deployments/kubernetes-bootcamp.

Rollback:

kubectl rollout undo deployments/kubernetes-bootcamp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment