search by tags

for the user

adventures into the land of the command line

kubernetes minikube bootcamp

buh-bibliography

create a cluster

$ minikube version
$ minikube start
$ minikube dashboard

$ kubectl version
$ kubectl cluster-info

Kubernetes master is running at https://192.168.99.100:8443

$ kubectl get nodes
NAME       STATUS    ROLES     AGE       VERSION
minikube   Ready         14d       v1.8.0

deploy an app

$ kubectl run kubernetes-bootcamp --image=docker.io/jocatalin/kubernetes-bootcamp:v1 --port=8080
$ kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   1         1         1            1           13s

$ kubectl proxy
$ curl http://localhost:8001/version

explore your app

$ kubectl get pods
NAME                                   READY     STATUS    RESTARTS   AGE
kubernetes-bootcamp-6c74779b45-npljq   1/1       Running   0          8m

$ kubectl describe pods

Name:           kubernetes-bootcamp-6c74779b45-npljq
Namespace:      default
Node:           minikube/192.168.99.100
Start Time:     Wed, 13 Dec 2017 16:44:29 +0100
Labels:         pod-template-hash=2730335601
                run=kubernetes-bootcamp
Annotations:    kubernetes.io/created-by={"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicaSet","namespace":"default","name":"kubernetes-bootcamp-6c74779b45","uid":"8175a547-e01c-11e7-b468-0...
Status:         Running
IP:             172.17.0.4
Created By:     ReplicaSet/kubernetes-bootcamp-6c74779b45
Controlled By:  ReplicaSet/kubernetes-bootcamp-6c74779b45
Containers:
  kubernetes-bootcamp:
    Container ID:   docker://1e1d86497d7264488c292b5c204b8e2e0e994cced4c3fb5e2bd9582d80b8ed11
    Image:          docker.io/jocatalin/kubernetes-bootcamp:v1
    Image ID:       docker-pullable://jocatalin/[email protected]:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
    Port:           8080/TCP
    State:          Running
      Started:      Wed, 13 Dec 2017 16:44:50 +0100
    Ready:          True
    Restart Count:  0
    Environment:    
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-mbdz7 (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          True
  PodScheduled   True
Volumes:
  default-token-mbdz7:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-mbdz7
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  
Tolerations:     
Events:
  Type    Reason                 Age   From               Message
  ----    ------                 ----  ----               -------
  Normal  Scheduled              9m    default-scheduler  Successfully assigned kubernetes-bootcamp-6c74779b45-npljq to minikube
  Normal  SuccessfulMountVolume  9m    kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-mbdz7"
  Normal  Pulling                9m    kubelet, minikube  pulling image "docker.io/jocatalin/kubernetes-bootcamp:v1"
  Normal  Pulled                 8m    kubelet, minikube  Successfully pulled image "docker.io/jocatalin/kubernetes-bootcamp:v1"
  Normal  Created                8m    kubelet, minikube  Created container
  Normal  Started                8m    kubelet, minikube  Started container

get pod name

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

Name of the Pod: kubernetes-bootcamp-6c74779b45-npljq

get from http api

http://localhost:8001/api/v1/proxy/namespaces/default/pods/kubernetes-bootcamp-6c74779b45-npljq/

-> Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c74779b45-npljq | v=1

view logs

$ kubectl logs kubernetes-bootcamp-6c74779b45-npljq
Kubernetes Bootcamp App Started At: 2017-12-13T15:44:50.293Z | Running On:  kubernetes-bootcamp-6c74779b45-npljq

Running On: kubernetes-bootcamp-6c74779b45-npljq | Total Requests: 1 | App Uptime: 736.711 seconds | Log Time: 2017-12-13T15:57:07.005Z

execute commands on the container

$ kubectl exec $POD_NAME env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-6c74779b45-npljq
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.0.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.0.0.1:443
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.0.0.1
KUBERNETES_SERVICE_HOST=10.0.0.1
KUBERNETES_SERVICE_PORT=443
NPM_CONFIG_LOGLEVEL=info
NODE_VERSION=6.3.1
HOME=/root

start a bash session in the pod’s container

$ kubectl exec -ti $POD_NAME bash
$ [email protected]:/# curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c74779b45-npljq | v=1

expose your app publicly

$ kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1             443/TCP   14d

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

$ kubectl get services
NAME                  TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.0.0.1             443/TCP          14d
kubernetes-bootcamp   NodePort    10.0.0.103           8080:31271/TCP   5s

$ kubectl describe services/kubernetes-bootcamp
Name:                     kubernetes-bootcamp
Namespace:                default
Labels:                   run=kubernetes-bootcamp
Annotations:              
Selector:                 run=kubernetes-bootcamp
Type:                     NodePort
IP:                       10.0.0.103
Port:                       8080/TCP
TargetPort:               8080/TCP
NodePort:                   31271/TCP
Endpoints:                172.17.0.4:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   

$ kubectl describe node minikube | grep InternalIP
  InternalIP:  192.168.99.100

$ curl 192.168.99.100:31271
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c74779b45-npljq | v=1

using labels

$ kubectl describe deployment | grep Labels
Labels:                 run=kubernetes-bootcamp

$ kubectl get pods -l run=kubernetes-bootcamp
NAME                                   READY     STATUS    RESTARTS   AGE
kubernetes-bootcamp-6c74779b45-npljq   1/1       Running   0          36m

$ kubectl get services -l run=kubernetes-bootcamp
NAME                  TYPE       CLUSTER-IP   EXTERNAL-IP   PORT(S)          AGE
kubernetes-bootcamp   NodePort   10.0.0.103           8080:31271/TCP   7m

$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"
"}}{{end}}'); \
$ echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-6c74779b45-npljq

$ kubectl label pod $POD_NAME app=v1
pod "kubernetes-bootcamp-6c74779b45-npljq" labeled

$ kubectl describe pods | grep Labels
Labels:         app=v1

$ kubectl get pods -l app=v1
NAME                                   READY     STATUS    RESTARTS   AGE
kubernetes-bootcamp-6c74779b45-npljq   1/1       Running   0          37m

delete a service

$ kubectl delete service -l run=kubernetes-bootcamp
service "kubernetes-bootcamp" deleted

$ kubectl get services
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1             443/TCP   14d

$ curl 192.168.99.100:31271
curl: (7) Failed to connect to 192.168.99.100 port 31271: Connection refused

$ kubectl exec -ti $POD_NAME curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c74779b45-npljq | v=1

scaling up

$ kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   1         1         1            1           45m

$ kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment "kubernetes-bootcamp" scaled

$ kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   4         4         4            1           45m

$ kubectl get pods -o wide
NAME                                   READY     STATUS    RESTARTS   AGE       IP           NODE
kubernetes-bootcamp-6c74779b45-5fd9g   1/1       Running   0          18s       172.17.0.6   minikube
kubernetes-bootcamp-6c74779b45-kbzrv   1/1       Running   0          18s       172.17.0.7   minikube
kubernetes-bootcamp-6c74779b45-npljq   1/1       Running   0          46m       172.17.0.4   minikube
kubernetes-bootcamp-6c74779b45-r67bz   1/1       Running   0          18s       172.17.0.5   minikube

$ kubectl describe deployments/kubernetes-bootcamp | grep Replicas
Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
  Available      True    MinimumReplicasAvailable

load balancing

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

$ kubectl describe services/kubernetes-bootcamp | grep Endpoints
Endpoints:                172.17.0.4:8080,172.17.0.5:8080,172.17.0.6:8080 + 1 more...

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

$ curl 192.168.99.100:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c74779b45-5fd9g | v=1
$ curl 192.168.99.100:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c74779b45-5fd9g | v=1
$ curl 192.168.99.100:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c74779b45-npljq | v=1
$ curl 192.168.99.100:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c74779b45-5fd9g | v=1
$ curl 192.168.99.100:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c74779b45-npljq | v=1
$ curl 192.168.99.100:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c74779b45-npljq | v=1

scaling down

$ kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment "kubernetes-bootcamp" scaled

$ kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   2         2         2            2           52m

$ kubectl get pods -o wide
NAME                                   READY     STATUS        RESTARTS   AGE       IP           NODE
kubernetes-bootcamp-6c74779b45-5fd9g   1/1       Running       0          6m        172.17.0.6   minikube
kubernetes-bootcamp-6c74779b45-kbzrv   1/1       Terminating   0          6m        172.17.0.7   minikube
kubernetes-bootcamp-6c74779b45-npljq   1/1       Running       0          52m       172.17.0.4   minikube
kubernetes-bootcamp-6c74779b45-r67bz   1/1       Terminating   0          6m        172.17.0.5   minikube

update your app

$ kubectl get pods -o wide
NAME                                   READY     STATUS    RESTARTS   AGE       IP           NODE
kubernetes-bootcamp-6c74779b45-5fd9g   1/1       Running   0          12m       172.17.0.6   minikube
kubernetes-bootcamp-6c74779b45-cvzqf   1/1       Running   0          3s        172.17.0.7   minikube
kubernetes-bootcamp-6c74779b45-npljq   1/1       Running   0          58m       172.17.0.4   minikube
kubernetes-bootcamp-6c74779b45-xwxk4   1/1       Running   0          3s        172.17.0.5   minikube

$ kubectl describe pods | grep Image:
    Image:          jocatalin/kubernetes-bootcamp:v1
    Image:          jocatalin/kubernetes-bootcamp:v1
    Image:          jocatalin/kubernetes-bootcamp:v1
    Image:          jocatalin/kubernetes-bootcamp:v1

$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment "kubernetes-bootcamp" image updated

$ kubectl get pods -o wide
NAME                                   READY     STATUS        RESTARTS   AGE       IP            NODE
kubernetes-bootcamp-5b44b7696c-6zk6v   1/1       Running       0          16s       172.17.0.11   minikube
kubernetes-bootcamp-5b44b7696c-cbv8d   1/1       Running       0          24s       172.17.0.8    minikube
kubernetes-bootcamp-5b44b7696c-cr68x   1/1       Running       0          23s       172.17.0.9    minikube
kubernetes-bootcamp-5b44b7696c-pn8lv   1/1       Running       0          19s       172.17.0.10   minikube
kubernetes-bootcamp-6c74779b45-5fd9g   1/1       Terminating   0          13m       172.17.0.6    minikube
kubernetes-bootcamp-6c74779b45-cvzqf   1/1       Terminating   0          1m        172.17.0.7    minikube
kubernetes-bootcamp-6c74779b45-npljq   1/1       Terminating   0          59m       172.17.0.4    minikube
kubernetes-bootcamp-6c74779b45-xwxk4   1/1       Terminating   0          1m        172.17.0.5    minikube

$ kubectl describe pods | grep Image:
    Image:          jocatalin/kubernetes-bootcamp:v2
    Image:          jocatalin/kubernetes-bootcamp:v2
    Image:          jocatalin/kubernetes-bootcamp:v2
    Image:          jocatalin/kubernetes-bootcamp:v2

verify the update

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

$ curl 192.168.99.100:$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b44b7696c-cbv8d | v=2

$ kubectl rollout status deployments/kubernetes-bootcamp
deployment "kubernetes-bootcamp" successfully rolled out

rollback a failed update

$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v10
deployment "kubernetes-bootcamp" image updated

$ kubectl get deployments
NAME                  DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   4         5         2            3           1h

$ kubectl get pods
NAME                                   READY     STATUS         RESTARTS   AGE
kubernetes-bootcamp-5b44b7696c-6zk6v   1/1       Terminating    0          4m
kubernetes-bootcamp-5b44b7696c-cbv8d   1/1       Running        0          5m
kubernetes-bootcamp-5b44b7696c-cr68x   1/1       Running        0          5m
kubernetes-bootcamp-5b44b7696c-pn8lv   1/1       Running        0          4m
kubernetes-bootcamp-7fddcd67d6-26pf2   0/1       ErrImagePull   0          8s
kubernetes-bootcamp-7fddcd67d6-8bwrz   0/1       ErrImagePull   0          8s

$ kubectl describe pods
.
.
.
Events:
  Type     Reason                 Age                From               Message
  ----     ------                 ----               ----               -------
  Normal   Scheduled              16s                default-scheduler  Successfully assigned kubernetes-bootcamp-7fddcd67d6-8bwrz to minikube
  Normal   SuccessfulMountVolume  16s                kubelet, minikube  MountVolume.SetUp succeeded for volume "default-token-mbdz7"
  Normal   Pulling                14s                kubelet, minikube  pulling image "jocatalin/kubernetes-bootcamp:v10"
  Warning  Failed                 13s                kubelet, minikube  Failed to pull image "jocatalin/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: manifest for jocatalin/kubernetes-bootcamp:v10 not found
  Warning  FailedSync             12s (x2 over 13s)  kubelet, minikube  Error syncing pod
  Normal   BackOff                12s                kubelet, minikube  Back-off pulling image "jocatalin/kubernetes-bootcamp:v10"

$ kubectl rollout undo deployments/kubernetes-bootcamp
deployment "kubernetes-bootcamp" rolled back

$ kubectl get pods
NAME                                   READY     STATUS    RESTARTS   AGE
kubernetes-bootcamp-5b44b7696c-b5xd5   1/1       Running   0          25s
kubernetes-bootcamp-5b44b7696c-cbv8d   1/1       Running   0          6m
kubernetes-bootcamp-5b44b7696c-cr68x   1/1       Running   0          6m
kubernetes-bootcamp-5b44b7696c-pn8lv   1/1       Running   0          5m