Kubernetes Introduction

Build a docker image

  • Dockerfile default filename
  • FROM base image to be used for container
  • ADD copies files/directories/remote file urls to container filesystem. Tarball and Remote URL (git, http) handling
  • COPY same as ADD without tar and remote url handling
  • CMD

kubectl –namespace <DEV|STAGING|PROD> get deployments,pods,svc,ingress

cd scripts/ ./createServicePrincipal.sh tti-maps-k8s-sp

Module 1 creating a cluster

1
2
3
4
5
6
minikube start
minikube dashboard
kubectl version
kubectl cluster-info
kubectl get nodes,pods
kubectl get nodes --help

Module 2 deploy an app

Create a deployment using kubectl create deployment

1
2
3
4
5
6
7
8
$ kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1

deployment.apps/kubernetes-bootcamp created
$ kubectl get deployments 
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   1/1     1            1           2m17s

$ kubectl proxy

Module 3 viewing pods & nodes

  • kubectl get - list resources
  • kubectl describe - show detailed information about a resource
  • kubectl logs - print the logs from a container in a pod kubectl logs $POD_NAME
  • kubectl exec - execute a command on a container in a pod. kubectl exec $POD_NAME env
  • kubectl exec -ti $POD_NAME bash to get bash session in pod’s container
  • kubectl exec -ti $POD_NAME bash to get bash session in pod’s container. Or kubectl exec -it $PodName – bash

Module 4 using a service to expose app

  • 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 :. 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 - Exposes the Service using an arbitrary name (specified by externalName in the spec) by returning a CNAME record with the name. No proxy is used. This type requires v1.7 or higher of kube-dns

minikube doesnot support load balancer option yet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
NAME                  TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.96.0.1      <none>        443/TCP          6h8m
kubernetes-bootcamp   NodePort    10.100.1.209   <none>        8080:32324/TCP   33s

kubectl describe services/kubernetes-bootcamp
Name:                     kubernetes-bootcamp
Namespace:                default
Labels:                   app=kubernetes-bootcamp
Annotations:              <none>
Selector:                 app=kubernetes-bootcamp
Type:                     NodePort
IP:                       10.100.1.209
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  32324/TCP
Endpoints:                172.17.0.6:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
NODE_PORT=30825
curl $(minikube ip):$NODE_PORT
kubectl get pods -l run=kubernetes-bootcamp
kubectl get services -l run=kubernetes-bootcamp
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
kubectl label pod $POD_NAME app=v1
kubectl delete service -l run=kubernetes-bootcamp

Module 5 Running multiple instances of app

1
2
3
4
5
6
7
8
kubectl get rs
kubectl scale deployments/kubernetes-bootcamp --replicas=4
kubectl describe deployments/kubernetes-bootcamp
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT
curl $(minikube ip):$NODE_PORT
kubectl scale deployments/kubernetes-bootcamp --replicas=2
kubectl get pods -o wide 

Module 6 Performing a rolling update

By default, the maximum number of Pods that can be unavailable during the update and the maximum number of new Pods that can be created, is one. Both options can be configured to either numbers or percentages (of Pods). In Kubernetes, updates are versioned and any Deployment update can be reverted to a previous (stable) version.

1
2
3
4
5
6
7
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
kubectl rollout status deployments/kubernetes-bootcamp
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v100
kubectl get pods 
NAME                                   READY   STATUS         RESTARTS   AGE
kubernetes-bootcamp-64468f5bc5-47bfn   0/1     ErrImagePull   0          44s
kubectl rollout undo deployments/kubernetes-bootcamp