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 full-size slide

  2. Container orchestration platform

    View full-size slide

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

    View full-size slide

  4. Very Powerful

    View full-size slide

  5. Large community

    View full-size slide

  6. Lot’s of large company backers

    View full-size slide

  7. No vendor lock in

    View full-size slide

  8. Included in Docker Desktop Clients

    View full-size slide

  9. Cloud Providers

    View full-size slide

  10. Google Cloud Platform

    View full-size slide

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

    View full-size slide

  12. Managed Kubernetes

    View full-size slide

  13. SysEleven MetaKube

    View full-size slide

  14. Easy upgrades

    View full-size slide

  15. Easy scaling

    View full-size slide

  16. Load Balancing

    View full-size slide

  17. Distributed Persistent Storage

    View full-size slide

  18. Premium support

    View full-size slide

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

    View full-size slide

  20. German company
    with German datacenters

    View full-size slide

  21. You can focus on what is important

    View full-size slide

  22. But this talk is about
    how to use Kubernetes

    View full-size slide

  23. Not only for production workloads

    View full-size slide

  24. But in your development workflows

    View full-size slide

  25. Kubernetes has standardized apis

    View full-size slide

  26. More and more integrations

    View full-size slide

  27. Introduction to Kubernetes

    View full-size slide

  28. Deployment of a simple application

    View full-size slide

  29. Deployment of a micro-service
    application

    View full-size slide

  30. Some tools for development with
    Kubernetes

    View full-size slide

  31. Why containers?

    View full-size slide

  32. Services run in isolation

    View full-size slide

  33. Everything needed to run a service in
    one image

    View full-size slide

  34. Decouple
    Ops and Dev

    View full-size slide

  35. Make things …

    View full-size slide

  36. Easier to deploy

    View full-size slide

  37. Easier to upgrade system
    dependencies

    View full-size slide

  38. Easier to scale

    View full-size slide

  39. Easier to develop

    View full-size slide

  40. Kubernetes helps you deploying
    containers

    View full-size slide

  41. Kubernetes helps you running
    containers

    View full-size slide

  42. Kubernetes helps you scaling
    containers

    View full-size slide

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

    View full-size slide

  44. Kubernetes Cluster

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  49. • Manages updates and
    rollbacks of replica sets
    Deployment

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  53. Everything is a resource

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  56. 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 full-size slide

  57. $ kubectl create -f deployment.yaml

    View full-size slide

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

    View full-size slide

  59. $ 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 full-size slide

  60. $ kubectl delete deployment symfony-demo

    View full-size slide

  61. Practical example

    View full-size slide

  62. Preparations

    View full-size slide

  63. We need a cluster

    View full-size slide

  64. Let’s deploy the symfony demo app

    View full-size slide

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

    View full-size slide

  66. Copy our code

    View full-size slide

  67. Build the project

    View full-size slide

  68. Composer install

    View full-size slide

  69. yarn install

    View full-size slide

  70. yarn run build

    View full-size slide

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

    View full-size slide

  72. Build the image

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  75. Resources are defined in YAML or
    JSON

    View full-size slide

  76. 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 full-size slide

  77. 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 full-size slide

  78. Many more options configurable

    View full-size slide

  79. 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 full-size slide

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

    View full-size slide

  81. 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 full-size slide

  82. Creating everything

    View full-size slide

  83. kubectl apply -f deployment/webapp.yaml

    View full-size slide

  84. Rolling Deployments

    View full-size slide

  85. 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 full-size slide

  86. kubectl apply -f deployment/webapp.yaml

    View full-size slide

  87. Writing this YAML files is tedious

    View full-size slide

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

    View full-size slide

  89. Per Development team

    View full-size slide

  90. Per developer

    View full-size slide

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

    View full-size slide

  92. "Templating"

    View full-size slide

  93. Great tools because of standardized
    Kubernetes API

    View full-size slide

  94. Allows to install applications

    View full-size slide

  95. So called "charts"

    View full-size slide

  96. Writing your own charts if fairly easy

    View full-size slide

  97. Charts can depend on other charts

    View full-size slide

  98. Multiple deployments of one chart
    possible

    View full-size slide

  99. Different namespaces

    View full-size slide

  100. Different release names

    View full-size slide

  101. Configuration over values

    View full-size slide

  102. Different versions

    View full-size slide

  103. Different ingress urls

    View full-size slide

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

    View full-size slide

  105. Make a code change

    View full-size slide

  106. Build docker image

    View full-size slide

  107. Push docker image

    View full-size slide

  108. Run helm install/upgrade with new
    image version

    View full-size slide

  109. Can this be quicker?

    View full-size slide

  110. Similar templating to helm

    View full-size slide

  111. Services can depend on other
    services

    View full-size slide

  112. $ forge deploy

    View full-size slide

  113. Supports different profiles

    View full-size slide

  114. $ forge --profile staging deploy

    View full-size slide

  115. $ forge --profile bastian deploy

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  118. You can use the branch name in
    templates

    View full-size slide

  119. Demo application

    View full-size slide

  120. web
    quote-svc
    hello-svc

    View full-size slide

  121. Not all services have an ingress

    View full-size slide

  122. Accessing Kubernetes from the
    outside

    View full-size slide

  123. web
    quote-svc
    hello-svc

    View full-size slide

  124. Getting a shell in a running container

    View full-size slide

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

    View full-size slide

  126. Port forwarding through kubectl

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  130. Takes some time

    View full-size slide

  131. What about step debugging?

    View full-size slide

  132. Of course you can run everything
    locally

    View full-size slide

  133. But you develop only on one service

    View full-size slide

  134. There may be lots of services

    View full-size slide

  135. Telepresence

    View full-size slide

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

    View full-size slide

  137. $ 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 full-size slide

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

    View full-size slide

  139. ... or a locally running docker
    container

    View full-size slide

  140. $ 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 full-size slide

  141. Great tooling because of common
    APIs

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide