" Real coders don't blog, or at least not very often! " A.R.

Tutorial Kubernetes - 2eme partie


Ce tutorial illustre comment démarrer avec Kubernetes (au minimum version 1.2) en mode pas à pas. Il est basé sur l'utilisation de Google Container Engine, mais peut facilement être suivi sur un autre environnement. Il fait suite à la première partie publiée précédemment.


Scaling


check pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mydeploy-2057093778-ns1mu 1/1 Running 0 1h

increase number of pods to 4
$ kubectl scale deployment mydeploy --replicas=4
deployment "mydeploy" scaled


check new number of pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mydeploy-2057093778-aavsb 1/1 Running 0 9s
mydeploy-2057093778-koz91 1/1 Running 0 9s
mydeploy-2057093778-ns1mu 1/1 Running 0 1h
mydeploy-2057093778-sffzz 1/1 Running 0 9s

verify after multiple requests from web browser than more than one container is answering
$ wget -qO- http://104.154.32.162:8080/
server: mydeploy-2057093778-ns1mu
request: 104.154.32.162:8080 /
Hello World from Go! current local time: 20160904 11:52:21
result: 202486500225000000 duration: 677.618103ms

$ wget -qO- http://104.154.32.162:8080/
server: mydeploy-2057093778-aavsb
request: 104.154.32.162:8080 /
Hello World from Go! current local time: 20160904 11:52:57
result: 202486500225000000 duration: 622.089041ms

check in the logs than more than one container is answering

open logs in Cloud Console for Container Engine, cluster knormal, default namespace


inspect deployment file
$ kubectl edit deployment mydeploy
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: 2016-09-04T10:44:03Z
  generation: 3
  labels:
    run: mydeploy
  name: mydeploy
  namespace: default
  resourceVersion: "1543"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/mydeploy
  uid: 7edf25bf-728c-11e6-91e7-42010a8001bb
spec:
  replicas: 4
  selector:
    matchLabels:
      run: mydeploy
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        run: mydeploy
    spec:
      containers:
      - image: gcr.io/xtrav42kubnormal/mykubeapp:1
        imagePullPolicy: IfNotPresent
        name: mydeploy
        ports:
        - containerPort: 8080
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 4
  observedGeneration: 3
  replicas: 4
  updatedReplicas: 4

Kubernetes UI


retrieve Kubernetes master url
$ kubectl cluster-info knormal
Kubernetes master is running at https://23.251.157.118
GLBCDefaultBackend is running at https://23.251.157.118/api/v1/proxy/namespaces/kube-system/services/default-http-backend
Heapster is running at https://23.251.157.118/api/v1/proxy/namespaces/kube-system/services/heapster
KubeDNS is running at https://23.251.157.118/api/v1/proxy/namespaces/kube-system/services/kube-dns
kubernetes-dashboard is running at https://23.251.157.118/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard

retrieve masterAuth/username and masterAuth/password for cluster knormal
$ gcloud container clusters describe knormal
clusterIpv4Cidr: 10.0.0.0/14
createTime: '2016-09-04T10:29:43+00:00'
currentMasterVersion: 1.3.6
currentNodeCount: 3
currentNodeVersion: 1.3.6
endpoint: 23.251.157.118
-- (...)
masterAuth:
password: XXXXXXXXXXXXXXXX
username: admin
-- (...)

open in your browser the Kubernetes UI by appending "/ui" to the Kubernetes master url



Application update


update source code
$ vi app.go
    fmt.Fprintf(w, "request: %s - %s\n",r.Host,r.URL.Path)


rebuild application
$ CGO_ENABLED=0 go build -ldflags '-extldflags "-static"'

build docker image version 2
$ sudo docker build -t gcr.io/$PROJ/mykubeapp:2 .

push image to GCR
$ gcloud docker push gcr.io/$PROJ/mykubeapp:2

check pods and images currently used by pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mydeploy-2057093778-aavsb 1/1 Running 0 5h
mydeploy-2057093778-koz91 1/1 Running 0 5h
mydeploy-2057093778-ns1mu 1/1 Running 0 6h
mydeploy-2057093778-sffzz 1/1 Running 0 5h
$ kubectl describe pods | grep "Image:"
Image: gcr.io/xtrav42kubnormal/mykubeapp:1
Image: gcr.io/xtrav42kubnormal/mykubeapp:1
Image: gcr.io/xtrav42kubnormal/mykubeapp:1
Image: gcr.io/xtrav42kubnormal/mykubeapp:1

update deployment to use new image
$ kubectl set image deployment/mydeploy mydeploy=gcr.io/$PROJ/mykubeapp:2
deployment "mydeploy" image updated

quickly check the pods to see the rolling update happening
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mydeploy-2057093778-koz91 1/1 Terminating 0 5h
mydeploy-2057093778-ns1mu 1/1 Running 0 6h
mydeploy-2057093778-sffzz 1/1 Running 0 5h
mydeploy-2138489491-d0hfg 1/1 Running 0 2s
mydeploy-2138489491-fcpv1 0/1 ContainerCreating 0 1s
mydeploy-2138489491-y4ie0 0/1 ContainerCreating 0 2s

check again
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mydeploy-2138489491-d0hfg 1/1 Running 0 8s
mydeploy-2138489491-fcpv1 1/1 Running 0 7s
mydeploy-2138489491-xn30w 1/1 Running 0 5s
mydeploy-2138489491-y4ie0 1/1 Running 0 8s

check images currently used by pods
$ kubectl describe pods | grep "Image:"
Image: gcr.io/xtrav42kubnormal/mykubeapp:2
Image: gcr.io/xtrav42kubnormal/mykubeapp:2
Image: gcr.io/xtrav42kubnormal/mykubeapp:2
Image: gcr.io/xtrav42kubnormal/mykubeapp:2

check from web browser that the version was updated
$ wget -qO- http://104.154.32.162:8080/
server: mydeploy-2431434819-v8jca
request: 104.154.32.162:8080 - /
Hello World from Go! current local time: 20160904 18:15:13
result: 202486500225000000 duration: 617.491008ms



September 15, 2016
650 words


Categories
Tags
cloud container kubernetes docker rkt GKE GCP

Connect. Socialize.