Best practices for deploying highly available applications on Kubernetes. Part 1

Kubernetes . , . Kubernetes.





, Kubernetes « », . CD- / Kubernetes-. , , Kubernetes- .





1.

- , . ? Kubernetes (Node, Pod, ReplicaSet .) , . . /. , Kubernetes .





, , - Pod' . , — — . , . , .





, , , . , - , ( , — ). , , . , .





, HorizontalPodAutoscaler. , , — HorizontalPodAutoscaler . HorizontalPodAutoscaler .





2.

Deployment' , 75% Pod' + ReplicaSet' Ready



. , 75%, . strategy.rollingUpdate.maxUnavailable



. , 25% Pod', maxUnavailable



. maxUnavailable



.





(RollingUpdate



) : , — . , , strategy.type: Recreate



. Recreate



, . , .





(blue-green, canary .) RollingUpdate, , , . . (. « Kubernetes: rolling, recreate, blue/green, canary, dark (A/B-)» .)





3.

Pod' , . Pod' Deployment' :





      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - podAffinityTerm:
              labelSelector:
                matchLabels:
                  app: testapp
              topologyKey: kubernetes.io/hostname
      
      



preferredDuringScheduling



requiredDuringScheduling



, Pod', , Pod' . , requiredDuringScheduling



, , Pod' .





4.

priorityClassName , Pod' schedule' , , Pod' «» (evicted) , Pod' .





PriorityClass Pod' priorityClassName



. PriorityClass



' :





  • Cluster. Priority > 10000. , kube-apiserver.





  • Daemonsets. Priority: 10000. , Pod' DaemonSet' .





  • Production-high. Priority: 9000. Stateful-.





  • Production-medium. Priority: 8000. Stateless-.





  • Production-low. Priority: 7000. .





  • Default. Priority: 0. production.





evict' .





5.

, STOPSIGNAL



( TERM



). graceful shutdown, , Kubernetes.





, nginx, preStop- :





lifecycle:
  preStop:
    exec:
      command:
      - /bin/sh
      - -ec
      - |
        sleep 3
        nginx -s quit
      
      



  1. sleep 3



    race conditions, endpoint. 





  2. nginx -s quit



    nginx. nginx , . . STOPSIGNAL: SIGQUIT



    .





( graceful shutdown nginx PHP-FPM .)





STOPSIGNAL



, . , STOPSIGNAL



. , , , preStop-, , STOPSIGNAL



, .





, , — terminationGracePeriodSeconds



. , . (30 ), KILL



. , , preStop- / STOPSIGNAL



30 , terminationGracePeriodSeconds



. , , - ( ).





, preStop- , . . STOPSIGNAL



, preStop- . , terminationGracePeriodSeconds



preStop-. , , , KILL



, terminationGracePeriodSeconds



.





, , ( , --timeout



Sidekiq). , , , terminationGracePeriodSeconds



.





6.

resources.requests



Pod' , Pod . , Pod schedule' , (. . non-requested) , (requests) Pod'. resources.limits



Pod', , requests. , , , Pod' . . Pod' QoS class: , , Pod' (evicted) .





, CPU, . , / , CPU-, Linux 5.4 ( EL7/CentOS7 3.10.0-1062.8.1.el7).





( , requests limits, QoS- Kubernetes, .)





: , Redis, , , «» . , . , , KILL. / , , . Kubernetes, limits.memory



Pod'.





Redis, :





maxmemory 500mb   #     500 ...
maxmemory-policy allkeys-lru   # ...Redis    
      
      



Sidekiq Sidekiq worker killer:





require 'sidekiq/worker_killer'
Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
    #   Sidekiq      500 
    chain.add Sidekiq::WorkerKiller, max_rss: 500
  end
end
      
      



, limits.memory



, .





VerticalPodAutoscaler .





7.

Kubernetes (healthcheck') , , (readiness) (liveness). Deployment' Pod' .





: timeoutSeconds. — . readinessProbe livenessProbe. timeoutSeconds



, Pod' ( Pod' Service) Pod' (readiness), , , (liveness).





7.1 Liveness probe

liveness probe (: « »), . — , livenessProbe , , . deadlock’ , . , (, , ), , «» livenessProbe.





livenessProbe , livenessProbe , liveness-, TCP- ( ). deadlock', , .





, livenessProbe , . : livenessProbe - , livenessProbe , . . ( ) , . , , — (, , ) . , Pod' . - , , , , livenessProbe .





, - livenessProbe, , , . , livenessProbe / . , 11 , , 10 , livenessProbe .





, , livenessProbe .





( liveness probe .)





7.2 Readiness probe

readinessProbe (: « [ ]»), , . : , , , . , , livenessProbe. readinessProbe , livenessProbe, .





readinessProbe , Pod . , . . Pod' - . , readinessProbe , Pod'. , readinessProbe , .





, readinessProbe : , , . readinessProbe, , .





, readinessProbe , . startupProbe, Kubernetes 1.16 Beta 1.18. , Kubernetes < 1.18 readinessProbe, Kubernetes >= 1.18 — startupProbe. readinessProbe Kubernetes >= 1.18, Pod' .





7.3 Startup probe

startupProbe (: « ») , Pod , , / Deployment'. readinessProbe, startupProbe . startupProbe , startupProbe , , Pod' CrashLoopBackOff



. . , , CrashLoopBackOff



, .





, Kubernetes >= 1.18.





failureTreshold



initialDelaySeconds



. , .





8.

readinessProbe. , , Pod , .





initContainers , startupProbe/readinessProbe . readinessProbe, , . initContainers



, , :





      initContainers:
      - name: wait-postgres
        image: postgres:12.1-alpine
        command:
        - sh
        - -ec
        - |
          until (pg_isready -h example.org -p 5432 -U postgres); do
            sleep 1
          done
        resources:
          requests:
            cpu: 50m
            memory: 50Mi
          limits:
            cpu: 50m
            memory: 50Mi
      - name: wait-redis
        image: redis:6.0.10-alpine3.13
        command:
        - sh
        - -ec
        - |
          until (redis-cli -u redis://redis:6379/0 ping); do
            sleep 1
          done
        resources:
          requests:
            cpu: 50m
            memory: 50Mi
          limits:
            cpu: 50m
            memory: 50Mi
      
      



, , Deployment stateless- .





: Kubernetes >= 1.18, Ubuntu/Debian >= 5.4.





apiVersion: apps/v1
kind: Deployment
metadata:
  name: testapp
spec:
  replicas: 10
  selector:
    matchLabels:
      app: testapp
  template:
    metadata:
      labels:
        app: testapp
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - podAffinityTerm:
              labelSelector:
                matchLabels:
                  app: testapp
              topologyKey: kubernetes.io/hostname
      priorityClassName: production-medium
      terminationGracePeriodSeconds: 40
      initContainers:
      - name: wait-postgres
        image: postgres:12.1-alpine
        command:
        - sh
        - -ec
        - |
          until (pg_isready -h example.org -p 5432 -U postgres); do
            sleep 1
          done
        resources:
          requests:
            cpu: 50m
            memory: 50Mi
          limits:
            cpu: 50m
            memory: 50Mi
      containers:
      - name: backend
        image: my-app-image:1.11.1
        command:
        - run
        - app
        - --trigger-graceful-shutdown-if-memory-usage-is-higher-than
        - 450Mi
        - --timeout-seconds-for-graceful-shutdown
        - 35s
        startupProbe:
          httpGet:
            path: /simple-startup-check-no-external-dependencies
            port: 80
          timeoutSeconds: 7
          failureThreshold: 12
        lifecycle:
          preStop:
            exec:
              ["sh", "-ec", "#command to shutdown gracefully if needed"]
        resources:
          requests:
            cpu: 200m
            memory: 500Mi
          limits:
            cpu: 200m
            memory: 500Mi
      
      



, , PodDisruptionBudget



, HorizontalPodAutoscaler



VerticalPodAutoscaler



, . , / .





P.S.

:





  • «10 Kubernetes»;





  • « Kubernetes» ( );





  • «Liveness probes Kubernetes ».








All Articles