Getting started with Kubernetes

Getting started with Kubernetes

Kubernetes is a very powerful container orchestration platform that is quickly gaining traction and gives you lots of benefits in deploying, running and scaling your microservice web application. But it has also a steep learning curve. In this talk I will introduce you to Kubernetes, why you would want to use it and all the tooling around Kubernetes with the help of practical examples.

Ded87c77266697ee6981c2277bb97633?s=128

Bastian Hofmann

February 16, 2018
Tweet

Transcript

  1. 2.
  2. 3.
  3. 4.
  4. 11.
  5. 12.

    AWS

  6. 13.
  7. 17.
  8. 25.
  9. 34.
  10. 37.

    Image • A docker image built from a Dockerfile that

    contains everything a service needs to run
  11. 38.

    • A container runs a docker image. • Only 1

    process can run inside of a container Container
  12. 39.

    Pod • A group of 1 or more containers •

    Same port space • Ports are not accessible from outside of the pod
  13. 46.

    Volumes • Volumes can be mounted into a container to

    access a ConfigMap, Secret or a folder on the host
  14. 48.
  15. 50.

    PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application

    POD ReplicaSet: 2 instances PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application POD
  16. 51.

    PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER ReplicaSet: 2

    instances PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER CONFIG WEB :80 PHP Application POD PHP Application POD
  17. 52.

    PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER ReplicaSet: 2

    instances PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER CONFIG WEB :80 https://php-app.k8s.foo.com:443/ PHP Application POD PHP Application POD
  18. 54.
  19. 55.
  20. 57.

    NAME READY STATUS RESTARTS AGE kubernetes-dashboard-5b5bf59977-t9xb9 1/1 Running 2 9d

    nginx-ingress-controller-5549f5597c-97kcw 0/1 Running 2 9d nginx-ingress-default-backend-564d9d9477-tmnnr 1/1 Running 4 9d mysql-556c9b5bcb-5jdrt 1/1 Running 1 8d symfony-demo-5b75f5fc6-c7wr9 1/1 Running 0 8d symfony-demo-5b75f5fc6-jg8n4 1/1 Running 23 8d
  21. 58.
  22. 59.

    $ kubectl proxy --port=8080 $ curl http://localhost:8080/api/v1/namespaces/default/pods { "kind": "PodList",

    "apiVersion": "v1", "metadata": { "selfLink": "/api/v1/namespaces/default/pods", "resourceVersion": "336834" }, "items": [ { "metadata": { "name": "kubernetes-dashboard-5b5bf59977-t9xb9", "generateName": "kubernetes-dashboard-5b5bf59977-", …
  23. 61.
  24. 68.
  25. 81.

    PHP

  26. 88.

    FROM php:7.2-apache WORKDIR /var/www/html # install packages RUN apt-get update

    -y && \ apt-get install -y --no-install-recommends \ curl git openssl \ less vim wget unzip rsync git mysql-client \ libcurl4-openssl-dev libfreetype6 libjpeg62-turbo libpng-dev libjpeg-dev libxml2-dev libxpm4 \ libicu-dev coreutils openssh-client libsqlite3-dev && \ apt-get clean && \ apt-get autoremove -y && \ rm -rf /var/lib/apt/lists/*
  27. 89.

    # install php extensions RUN docker-php-ext-configure gd --with-jpeg-dir=/usr/ local/ &&

    \ docker-php-ext-install -j$(nproc) iconv intl pdo_sqlite curl json xml mbstring zip bcmath soap pdo_mysql gd # apache config RUN /usr/sbin/a2enmod rewrite && /usr/sbin/a2enmod headers && /usr/sbin/a2enmod expires COPY ./container/apache.conf /etc/apache2/sites- available/000-default.conf
  28. 90.

    ENV COMPOSER_HOME /tmp ENV COMPOSER_VERSION 1.6.3 RUN curl -s -f

    -L -o /tmp/installer.php https:// raw.githubusercontent.com/composer/getcomposer.org/ b107d959a5924af895807021fcef4ffec5a76aa9/web/installer \ && php -r " \ \$signature = '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fd fdd586475ca9813a858088ffbc1f233e9b180f061'; \ \$hash = hash('SHA384', file_get_contents('/tmp/ installer.php')); \ if (!hash_equals(\$signature, \$hash)) { \ unlink('/tmp/installer.php'); \ echo 'Integrity check failed, installer is either corrupt or worse.' . PHP_EOL; \ exit(1); \ }" \ && php /tmp/installer.php --no-ansi --install-dir=/usr/ bin --filename=composer --version=${COMPOSER_VERSION} \ && composer --ansi --version --no-interaction \ && rm -rf /tmp/* /tmp/.htaccess
  29. 91.

    COPY composer.* /var/www/html/ RUN composer install COPY --from=0 /var/www/html/node_modules/ /var/www/html/

    node_modules/ COPY . /var/www/html/ RUN chown -R www-data:www-data /var/www/html && composer dump-autoload
  30. 100.

    kind: Deployment apiVersion: extensions/v1beta1 metadata: name: symfony-demo spec: revisionHistoryLimit: 3

    template: metadata: labels: app: symfony-demo spec: containers: - name: symfony-demo image: symfony-demo:1.0.0 imagePullPolicy: Never ports: - containerPort: 80
  31. 101.

    containers: - name: symfony-demo image: symfony-demo:latest imagePullPolicy: Never ports: -

    containerPort: 80 livenessProbe: httpGet: path: / port: 80 timeoutSeconds: 1 initialDelaySeconds: 10 readinessProbe: httpGet: path: / port: 80 timeoutSeconds: 1
  32. 103.

    •Setting environment variables •Mounting volumes •Requesting resources •Defining upgrade strategies

    •Defining command •Configure networking •Configure affinities •LifeCycle events •…
  33. 104.
  34. 105.

    kind: Service apiVersion: v1 metadata: name: symfony-demo spec: ports: -

    name: http port: 80 targetPort: 80 protocol: TCP selector: app: symfony-demo
  35. 106.
  36. 107.

    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
  37. 110.
  38. 112.

    kind: Deployment apiVersion: extensions/v1beta1 metadata: name: symfony-demo spec: revisionHistoryLimit: 3

    template: metadata: labels: app: symfony-demo spec: containers: - name: symfony-demo image: symfony-demo:1.1.0 imagePullPolicy: Never ports: - containerPort: 80
  39. 117.
  40. 122.

  41. 126.

    apiVersion: extensions/v1beta1 kind: DaemonSet metadata: name: kubernetes-ingress-nginx labels: k8s-app: kubernetes-ingress-nginx

    spec: updateStrategy: type: RollingUpdate rollingUpdate: maxUnavailable: 1 minReadySeconds: 5 template: …
  42. 127.
  43. 129.

    apiVersion: batch/v1beta1 kind: CronJob metadata: name: cron-job spec: schedule: "*/1

    * * * *" jobTemplate: spec: template: spec: containers: - name: cron-job image: your-cron-job restartPolicy: OnFailure
  44. 137.

    kind: Service apiVersion: v1 metadata: name: symfony-demo spec: ports: -

    name: http port: 80 targetPort: 80 protocol: TCP selector: app: symfony-demo
  45. 139.

    $ kubectl get service symfony-demo NAME TYPE CLUSTER-IP PORT(S) AGE

    symfony-demo ClusterIP 10.106.119.24 80/TCP 6d
  46. 143.

    DNS

  47. 151.
  48. 152.
  49. 155.

    PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application

    POD NODEJS LINKERD STATSD Other service POD NODEJS LINKERD STATSD Other service POD
  50. 156.

    PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application

    POD NODEJS LINKERD STATSD Other service POD NODEJS LINKERD STATSD Other service POD
  51. 157.

    PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application

    POD NODEJS LINKERD STATSD Other service POD NODEJS LINKERD STATSD Other service POD
  52. 158.
  53. 162.
  54. 163.
  55. 165.
  56. 170.
  57. 171.
  58. 172.
  59. 173.

  60. 174.
  61. 175.

    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
  62. 177.
  63. 178.
  64. 180.

    apiVersion: v1 kind: Pod metadata: name: test-pd spec: containers: -

    image: k8s.gcr.io/test-webserver name: test-container volumeMounts: - mountPath: /cache name: cache-volume volumes: - name: cache-volume emptyDir: {}
  65. 188.
  66. 192.

    spec: containers: - name: test-container image: k8s.gcr.io/busybox command: [ "/bin/sh",

    "-c", "env" ] env: - name: SPECIAL_KEY valueFrom: configMapKeyRef: name: special-config key: special-key
  67. 193.

    spec: containers: - name: test-container image: k8s.gcr.io/busybox command: [ "/bin/sh",

    "-c", "env" ] envFrom: - configMapRef: name: special-config
  68. 195.

    spec: containers: - name: test-container image: k8s.gcr.io/busybox command: [ "/bin/sh",

    "-c", "ls /etc/config/" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: special-config
  69. 197.
  70. 203.
  71. 208.

    PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application

    POD INIT The only image that contains the source code
  72. 210.
  73. 211.

    PHP-FPM NGINX LINKERD STATSD MEM CACHED MONGO ROUTER PHP Application

    POD INIT emptyDir Volume with source Mounts and uses
  74. 213.
  75. 214.
  76. 217.
  77. 218.
  78. 220.
  79. 221.
  80. 223.
  81. 231.
  82. 233.
  83. 237.
  84. 243.
  85. 246.
  86. 247.
  87. 248.
  88. 254.