Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Creating a fast Kubernetes Development Workflow

Creating a fast Kubernetes Development Workflow

Kubernetes is very powerful in orchestrating where your services are running, but building docker images, writing and updating all the necessary YAML files and applying them to your cluster can still become an annoying maintenance overhead.
In this talk you will learn how to create a fast, low friction development workflow with Kubernetes and tools like Telepresence and Forge which enables you to quickly and safely build, test and deploy your services.

Bastian Hofmann

October 19, 2018
Tweet

More Decks by Bastian Hofmann

Other Decks in Programming

Transcript

  1. @BastianHofmann
    Creating a fast
    Kubernetes Development Workflow
    Bastian Hofmann

    View Slide

  2. View Slide

  3. Container orchestration platform

    View Slide

  4. Deploy, run and scale your services
    in isolated containers

    View Slide

  5. Very Powerful

    View Slide

  6. Large community

    View Slide

  7. Lot’s of large company backers

    View Slide

  8. No vendor lock in

    View Slide

  9. Runs on

    View Slide

  10. Your laptop

    View Slide

  11. Included in Docker Desktop Clients

    View Slide

  12. Bare metal

    View Slide

  13. Cloud Providers

    View Slide

  14. AWS

    View Slide

  15. Azure

    View Slide

  16. Google Cloud Platform

    View Slide

  17. And if you don't want to install and
    maintain Kubernetes yourself

    View Slide

  18. Managed Kubernetes

    View Slide

  19. Google GKE

    View Slide

  20. Amazon EKS

    View Slide

  21. SysEleven MetaKube

    View Slide

  22. Easy upgrades

    View Slide

  23. Easy scaling

    View Slide

  24. Load Balancing

    View Slide

  25. Distributed Persistent Storage

    View Slide

  26. Backups

    View Slide

  27. Premium support

    View Slide

  28. We monitor you cluster, ensure it's
    working and tell you if something is
    wrong

    View Slide

  29. German company
    with German datacenters

    View Slide

  30. You can focus on what is important

    View Slide

  31. But this talk is about
    how to use Kubernetes

    View Slide

  32. Not only for production workloads

    View Slide

  33. But in your development workflows

    View Slide

  34. Kubernetes has standardized apis

    View Slide

  35. More and more integrations

    View Slide

  36. Great tools

    View Slide

  37. Agenda

    View Slide

  38. Introduction to Kubernetes

    View Slide

  39. Deployment of a simple application

    View Slide

  40. Deployment of a micro-service
    application

    View Slide

  41. Some tools for development with
    Kubernetes

    View Slide

  42. But first

    View Slide

  43. Why containers?

    View Slide

  44. Services run in isolation

    View Slide

  45. Everything needed to run a service in
    one image

    View Slide

  46. Decouple
    Ops and Dev

    View Slide

  47. Make things …

    View Slide

  48. Easier to deploy

    View Slide

  49. Easier to upgrade system
    dependencies

    View Slide

  50. Easier to scale

    View Slide

  51. Easier to develop

    View Slide

  52. Kubernetes helps you deploying
    containers

    View Slide

  53. Kubernetes helps you running
    containers

    View Slide

  54. Kubernetes helps you scaling
    containers

    View Slide

  55. Let’s define some core concepts and
    terminology first

    View Slide

  56. Kubernetes Cluster

    View Slide

  57. • A docker image built from
    a Dockerfile that contains
    everything a service needs
    to run
    Image

    View Slide

  58. • A container runs a docker
    image.
    • Only 1 process can run
    inside of a container
    Container

    View Slide

  59. • A group of 1 or more
    containers
    • Same port space
    • Ports are not accessible
    from outside of the pod
    Pod

    View Slide

  60. • Defines and manages how
    many instances of a pod
    should run
    Replica Set

    View Slide

  61. • Manages updates and
    rollbacks of replica sets
    Deployment

    View Slide

  62. • Makes a port of a pod
    accessible to other pods
    Service

    View Slide

  63. • Makes a service
    accessible to the outside
    of Kubernetes
    Ingress

    View Slide

  64. Volumes, ConfigMaps, Secrets,
    PersistentVolumeClaims, CronJobs,
    StatefulSets, ...

    View Slide

  65. Everything is a resource

    View Slide

  66. You interact with Kubernetes by
    creating, receiving, updating and
    deleting resources

    View Slide

  67. Kubernetes has controllers to listen
    on these interactions and get the
    cluster in the desired state

    View Slide

  68. kind: Deployment
    apiVersion: extensions/v1beta1
    metadata:
    name: symfony-demo
    spec:
    template:
    spec:
    containers:
    - name: symfony-demo
    image: symfony-demo:1.1.0
    ports:
    - containerPort: 80

    View Slide

  69. $ kubectl create -f deployment.yaml

    View Slide

  70. $ kubectl get deployments
    NAME DESIRED CURRENT UP-TO-DATE AVAILABLE
    AGE
    symfony-demo 1 1 1 1
    21h

    View Slide

  71. $ kubectl get deployment symfony-demo -o yaml
    apiVersion: extensions/v1beta1
    kind: Deployment
    metadata:
    annotations:
    ...
    spec:
    ...
    template:
    ...
    spec:
    containers:
    - name: symfony-demo
    image: symfony-demo:1.1.0

    View Slide

  72. $ kubectl delete deployment symfony-demo

    View Slide

  73. Practical example

    View Slide

  74. Preparations

    View Slide

  75. We need a cluster

    View Slide

  76. View Slide

  77. View Slide

  78. View Slide

  79. Let’s deploy the symfony demo app

    View Slide

  80. https:/
    /github.com/symfony/demo

    View Slide

  81. Demo

    View Slide

  82. Dockerfile

    View Slide

  83. Copy our code

    View Slide

  84. Build the project

    View Slide

  85. Composer install

    View Slide

  86. yarn install

    View Slide

  87. yarn run build

    View Slide

  88. https:/
    /docs.docker.com/develop/develop-
    images/multistage-build/

    View Slide

  89. Build the image

    View Slide

  90. docker build -t symfony-demo:2.0.0 .

    View Slide

  91. Now we have to tell Kubernetes
    what to do with the image

    View Slide

  92. Resources are defined in YAML or
    JSON

    View Slide

  93. Deployment

    View Slide

  94. kind: Deployment
    apiVersion: extensions/v1beta1
    metadata:
    name: symfony-demo
    spec:
    template:
    metadata:
    labels:
    app: symfony-demo
    spec:
    containers:
    - name: symfony-demo
    image: symfony-demo:1.0.0
    ports:

    View Slide

  95. containers:
    - name: symfony-demo
    image: symfony-demo:1.0.0
    ports:
    - containerPort: 80
    livenessProbe:
    httpGet:
    path: /
    port: 80
    timeoutSeconds: 1
    initialDelaySeconds: 10
    readinessProbe:
    httpGet:
    path: /

    View Slide

  96. Many more options configurable

    View Slide

  97. Many more options
    • Setting environment variables
    • Mounting volumes
    • Requesting resources
    • Defining upgrade strategies
    • Defining command
    • Configure networking
    • Configure the scheduler
    • Listen on lifecycle events
    • Configure system capabilities for the container
    • …

    View Slide

  98. Service

    View Slide

  99. kind: Service
    apiVersion: v1
    metadata:
    name: symfony-demo
    spec:
    ports:
    -
    name: http
    port: 80
    targetPort: 80
    protocol: TCP
    selector:
    app: symfony-demo

    View Slide

  100. Ingress

    View Slide

  101. kind: Ingress
    apiVersion: extensions/v1beta1
    metadata:
    name: symfony-demo
    spec:
    rules:
    - host: symfony-demo.local.k8s
    http:
    paths:
    - path: /
    backend:
    serviceName: symfony-demo
    servicePort: 80

    View Slide

  102. Creating everything

    View Slide

  103. kubectl apply -f deployment/webapp.yaml

    View Slide

  104. View Slide

  105. Rolling Deployments

    View Slide

  106. kind: Deployment
    apiVersion: extensions/v1beta1
    metadata:
    name: symfony-demo
    spec:
    template:
    spec:
    containers:
    - name: symfony-demo
    image: symfony-demo:1.1.0
    ports:
    - containerPort: 80

    View Slide

  107. kubectl apply -f deployment/webapp.yaml

    View Slide

  108. Writing this YAML files is tedious

    View Slide

  109. YAML files are tied to a specific
    version and a specific environment

    View Slide

  110. Production

    View Slide

  111. Staging

    View Slide

  112. Development

    View Slide

  113. Per Development team

    View Slide

  114. Per branch

    View Slide

  115. Per developer

    View Slide

  116. Built-in

    View Slide

  117. Namespaces

    View Slide

  118. Still we'd need to maintain multiple
    very similar YAML files with slightly
    different versions and configuration.

    View Slide

  119. "Templating"

    View Slide

  120. Great tools because of standardized
    Kubernetes API

    View Slide

  121. Helm

    View Slide

  122. View Slide

  123. Allows to install applications

    View Slide

  124. So called "charts"

    View Slide

  125. Writing your own charts if fairly easy

    View Slide

  126. Charts can depend on other charts

    View Slide

  127. Multiple deployments of one chart
    possible

    View Slide

  128. Different namespaces

    View Slide

  129. Different release names

    View Slide

  130. Configuration over values

    View Slide

  131. View Slide

  132. Different versions

    View Slide

  133. Different ingress urls

    View Slide

  134. $ helm install stable/wordpress --namespace bastian --name
    my-wordpress --values dev.yaml --values bastian.yaml

    View Slide

  135. Still:

    View Slide

  136. Make a code change

    View Slide

  137. Build docker image

    View Slide

  138. Push docker image

    View Slide

  139. Run helm install/upgrade with new
    image version

    View Slide

  140. Can this be quicker?

    View Slide

  141. Forge

    View Slide

  142. View Slide

  143. Similar templating to helm

    View Slide

  144. Services can depend on other
    services

    View Slide

  145. $ forge deploy

    View Slide

  146. Supports different profiles

    View Slide

  147. $ forge --profile staging deploy

    View Slide

  148. $ forge --profile bastian deploy

    View Slide

  149. Different profiles can deploy to
    different namespaces with different
    ingress hostnames

    View Slide

  150. Default profile can be dependent on
    the branch you are building from

    View Slide

  151. You can use the branch name in
    templates

    View Slide

  152. Demo application

    View Slide

  153. web
    quote-svc
    hello-svc

    View Slide

  154. Not all services have an ingress

    View Slide

  155. Accessing Kubernetes from the
    outside

    View Slide

  156. web
    quote-svc
    hello-svc

    View Slide

  157. Getting a shell in a running container

    View Slide

  158. $ kubectl exec $POD_NAME -i -t -- /bin/bash

    View Slide

  159. Port forwarding through kubectl

    View Slide

  160. $ kubectl port-forward pod/$POD_NAME 8080:80

    View Slide

  161. $ kubectl port-forward service/$SERVICE_NAME 8080:80

    View Slide

  162. Still, if you make a code change you
    have to commit, push, build, deploy

    View Slide

  163. Takes some time

    View Slide

  164. What about step debugging?

    View Slide

  165. Of course you can run everything
    locally

    View Slide

  166. But you develop only on one service

    View Slide

  167. There may be lots of services

    View Slide

  168. Telepresence

    View Slide

  169. View Slide

  170. Creates a two-way proxy between
    the Kubernetes cluster and you

    View Slide

  171. $ telepresence
    T: Starting proxy with method 'vpn-tcp'...
    @fhgbvx65xg|bash-3.2$ curl http://quote-svc/quote | jq '.'
    [
    {
    "ID": 503,
    "title": "stefan sagmeister",
    "content": "...\n",
    "link": "https://quotesondesign.com/stefan-
    sagmeister-2/"
    }
    ]

    View Slide

  172. Swap a running deployment in the
    cluster with a local process

    View Slide

  173. ... or a locally running docker
    container

    View Slide

  174. $ telepresence --swap-deployment quote-svc --namespace
    dev-flow-demo --expose 3000 --run npm run debug
    T: Starting proxy with method 'vpn-tcp',...
    T: Forwarding remote port 3000 to local port 3000....
    > [email protected] debug /Users/bhofmann/forge_test/quote-
    svc
    > nodemon --inspect quote-svc.js
    [nodemon] watching: *.*
    [nodemon] starting `node --inspect quote-svc.js`
    Debugger listening on ws://127.0.0.1:9229/83aa27ac-
    d879-4b50-a228-440354cca791
    quote svc listening on port 3000!

    View Slide

  175. Demo

    View Slide

  176. Summary

    View Slide

  177. Powerful

    View Slide

  178. Helpful

    View Slide

  179. Great tooling because of common
    APIs

    View Slide

  180. Especially great if you have multiple
    services and don't want to run
    everything locally

    View Slide

  181. http:/
    /speakerdeck.com/
    u/bastianhofmann

    View Slide

  182. [email protected]
    https:/
    /twitter.com/BastianHofmann

    View Slide