Kubernetes for nodeJS dev

Course notes from Dan Wahlin’s excellent pluralsight course titled: Kubernetes from Developers: Core Concepts

Introduction

Running locally

Web UI dashboard

Run locally

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yamlkubectl describe secret -n kube-system
# copy first token which is of type service-account-token
kutectl proxy
# open in browser http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
# paste on token

Nodes

Master node

  • Store, which is like a DB of nodes in the cluster.
  • Controller manager, managing requests.
  • Scheduler, when updates will occur.
  • API server, $kudectl, to communicate with node.

Pods

Useful pod commands

# run a pod
kubectl run [podname] --image=nginx
# get pods
kubectl get pods
# port forward so pod can be called externally
kubectl port-forward [podname] 8080:80
# delete pod
kubectl delete pod [podname]
# information about a pod
kubectl describe pod [podname]
# exec into pod, (browse file structure)
kubectl exec [podname] -it sh

YAML file

apiVersion: v1
kind: Pod
metadata:
name: my-nginx
labels:
app: nginx
rel: stable
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80

Useful YAML pod commands

# create the pod from yaml file
kubectl create -f file.yml
# create will error if exists, use apply to take down and create
kubectl apply -f file.yml
# edit in console
kubectl edit
# patch specific property
kubectl patch
# delete, (alternative to just deleting a pod)
kubectl delete -f file.yml

Probes

  • Liveness probe, determines health of pod, (e.g. hit health check endpoint).
  • Readiness probe, when it’s ready for requests to start being made.

Deployments / ReplicaSets

Useful deployment commands

# get deployment
kubectl get deployment --show-labels
# get deployment by label
kubectl get deployment -l app=nginx
# delete deployment
kubectl delete deployment [deploymentname]
# scale deployment by 5 pods
kubectl scale deployment [deploymentname] --replicas=5

YAML file

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
labels:
app: my-nginx
spec:
replicas: 2
selector:
matchLabels:
app: my-nginx
template:
metadata:
labels:
app: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80

Useful YAML deployment commands

# create the deployment from yaml file
kubectl create -f file.yml
# create will error if exists, use apply to take down and create
kubectl apply -f file.yml
# delete, (alternative to just deleting a deployment)
kubectl delete -f file.yml

Zero downtime deployments

Rolling example. Two existing and two new pods. Adds first new pod, removes first old pod, adds second new pod, removed second old pod.
This happens by default when we run kubectl apply.

Services

Types

  1. NodePort, has IP on node and sets up static port.
  2. LoadBalancer, external IP as LB for nodes.
  3. ExternalName, service to DNS.
# port forward a deployment or service as well as a pod
kubectl port-forward deployment/[deploymentname] 8080:80
kubectl port-forward service/[servicename] 8080:80

YAML file

apiVersion: v1
kind: Service
metadata:
name: nginx-clusterip
spec:
type: ClusterIP
selector:
app: my-nginx
ports:
- port: 8080
targetPort: 80

Storage

Types

  1. hostPath, mounts to nodes filesystem.
  2. nfs, mounted to network file system volume on network.
  3. configMap/secret, key value pairs for secrets.
  4. persistentVolumeClaim, persistent storage, e.g. NFS, cloud.
  5. cloud, global storage.
  6. plus more…

persistentVolumeClaim

PVC, pv claim, link between the pod and the PV.

Storage class

ConfigMaps

Useful configMap commands

# get a configMap
kubectl get cm [configmapname] -o yaml
# create a configMap from env file
# settings.config
testEnvVal1=1
testEnvVal2=2
kubectl create cm [configmapname] --from-env-file=setting.config

Secrets

# create a secret
kubectl get secrets
# create a secret
kubectl create secret generic [configmapname] --from-literal=pwd=my-password

YAML examples

Course

Software dev — Javascript, node, express, mongo, react, redux, rxjs, es6, ramda