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

Kubernetes Best Practices

Kubernetes Best Practices

Kubernetes and friends are powerful tools that can really simplify your operations. However, there are many gotchas and common pitfalls that can ruin your experience. I’ll share some best practices around building and deploying your containers that will let you run more stably, efficiently, and securely.

Sandeep Dinesh

July 14, 2017
Tweet

More Decks by Sandeep Dinesh

Other Decks in Technology

Transcript

  1. 8 Google Cloud Platform Overhead Node.js App Your App →

    5MB Your App’s Dependencies → 95MB Total App Size → 100MB Docker Base Images: node:8 → 667MB node:8-wheezy → 521MB node:8-slim → 225MB node:8-alpine → 63.7MB scratch → ~50MB
  2. 9 Google Cloud Platform Overhead Node.js App Your App →

    5MB Your App’s Dependencies → 95MB Total App Size → 100MB Docker Base Images: node:8 → 667MB node:8-wheezy → 521MB node:8-slim → 225MB node:8-alpine → 63.7MB scratch → ~50MB ← 6.6x App Size!!
  3. 10 Google Cloud Platform Overhead Node.js App Your App →

    5MB Your App’s Dependencies → 95MB Total App Size → 100MB Docker Base Images: node:8 → 667MB node:8-wheezy → 521MB node:8-slim → 225MB node:8-alpine → 63.7MB scratch → ~50MB ← 6.6x App Size!! ← 13.3x “min” overhead!!
  4. 11 Google Cloud Platform Overhead Node.js App Your App →

    5MB Your App’s Dependencies → 95MB Total App Size → 100MB Docker Base Images: node:8 → 667MB node:8-wheezy → 521MB node:8-slim → 225MB node:8-alpine → 63.7MB scratch → ~50MB ← 6.6x App Size!! ← 13.3x “min” overhead!! Pros: Builds are faster Need less storage Cold starts (image pull) are faster Potentially less attack surface Cons: Less tooling inside container “Non-standard” environment
  5. 13 Google Cloud Platform Code Build Container Compiler Dev Deps

    Unit Tests etc... Build Artifact(s) Runtime Container Runtime Env Debug/Monitor Tooling Binaries Static Files Bundles Transpiled Code
  6. 17 Google Cloud Platform FROM node:alpine RUN apk update &&

    apk add imagemagick RUN groupadd -r nodejs RUN useradd -m -r -g nodejs nodejs USER nodejs ADD package.json package.json RUN npm install ADD index.js index.js CMD npm start Example Dockerfile
  7. 18 Google Cloud Platform Enforce it! apiVersion: v1 kind: Pod

    metadata: name: hello-world spec: containers: # specification of the pod’s containers # ... securityContext: runAsNonRoot: true
  8. 20 Google Cloud Platform apiVersion: v1 kind: Pod metadata: name:

    hello-world spec: containers: # specification of the pod’s containers # ... securityContext: runAsNonRoot: true readOnlyRootFilesystem: true Enforce it!
  9. 25 Google Cloud Platform FROM node:alpine RUN apk update &&

    apk add imagemagick RUN groupadd -r nodejs RUN useradd -m -r -g nodejs nodejs USER nodejs ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.0/dumb-init_1.2.0_amd64 \ /usr/local/bin/dumb-init RUN chmod +x /usr/local/bin/dumb-init ENTRYPOINT ["/usr/bin/dumb-init", "--"] ADD package.json package.json RUN npm install ADD index.js index.js CMD npm start Example Dockerfile
  10. 29 Google Cloud Platform $ kubectl apply -f deployment.yaml --record

    … $ kubectl rollout history deployments my-deployment deployments "ghost-recorded" REVISION CHANGE-CAUSE 1 kubectl apply -f deployment.yaml --record 2 kubectl edit deployments my-deployment 3 kubectl set image deployment/my-deplyoment my-container=app:2.0
  11. 31 Google Cloud Platform apiVersion: extensions/v1beta1 kind: Deployment metadata: name:

    web spec: replicas: 12 template: metadata: labels: name: web color: blue experimental: 'true' Labels
  12. 32 Google Cloud Platform App: Nifty Phase: Dev Role: FE

    App: Nifty Phase: Test Role: FE App: Nifty Phase: Dev Role: BE App: Nifty Phase: Test Role: BE Labels
  13. 33 Google Cloud Platform App: Nifty Phase: Dev Role: FE

    App: Nifty Phase: Test Role: FE App: Nifty Phase: Dev Role: BE App: Nifty Phase: Test Role: BE Labels App = Nifty
  14. 34 Google Cloud Platform App: Nifty Phase: Dev Role: FE

    App: Nifty Phase: Test Role: FE App: Nifty Phase: Dev Role: BE App: Nifty Phase: Test Role: BE Labels Role = BE
  15. 35 Google Cloud Platform App: Nifty Phase: Dev Role: FE

    App: Nifty Phase: Test Role: FE App: Nifty Phase: Dev Role: BE App: Nifty Phase: Test Role: BE Labels Phase = Dev
  16. 36 Google Cloud Platform App: Nifty Phase: Dev Role: FE

    App: Nifty Phase: Test Role: FE App: Nifty Phase: Dev Role: BE App: Nifty Phase: Test Role: BE Labels Phase = Dev Role = FE
  17. 39 Google Cloud Platform Auth, Rate Limiting, etc. Examples App

    Incoming Requests localhost App App Proxy Proxy Proxy
  18. 42 Google Cloud Platform apiVersion: v1 kind: Pod metadata: name:

    awesomeapp-pod labels: app: awesomeapp annotations: pod.beta.kubernetes.io/init-containers : '[ { "name": "init-myapp", "image": "busybox", "command": ["sh", "-c", "until nslookup myapp; do echo waiting for myapp; sleep 2; done;"] }, { "name": "init-mydb", "image": "busybox", "command": ["sh", "-c", "until nslookup mydb; do echo waiting for mydb; sleep 2; done;"] } ]' spec: containers: - name: awesomeapp-container image: busybox command: ['sh', '-c', 'echo The app is running! && sleep 3600' ]
  19. 45 Google Cloud Platform Readiness → Is the app ready

    to start serving traffic? • Won’t be added to a service endpoint until it passes • Required for a “production app” in my opinion Liveness → Is the app still running? • Default is “process is running” • Possible that the process can be running but not working correctly • Good to define, might not be 100% necessary These can sometimes be the same endpoint, but not always Health Checks
  20. 49 Google Cloud Platform Ingress Service 1 Service 2 Service

    3 websocket.mydomain.com mydomain.com /foo /bar
  21. 52 Google Cloud Platform apiVersion: v1 kind: Service metadata: name:

    myservice spec: type: LoadBalancer loadBalancerIP: QQQ.ZZZ.YYY.XXX ports: - port: 80 targetPort: 3000 protocol: TCP selector: name: myapp $ gcloud compute addresses create ingress --global … $ gcloud compute addresses create myservice --region=us-west1 Created … address: QQQ.ZZZ.YYY.XXX … $ apiVersion: extensions/v1beta1 kind: Ingress metadata: name: myingress annotations: kubernetes.io/ingress.global-static-ip-name: "ingress" spec: backend: serviceName: myservice servicePort: 80
  22. 54 Google Cloud Platform External Services kind: Service apiVersion: v1

    metadata: name: mydatabase namespace: prod spec: type: ExternalName externalName : my.database.example.com ports: - port: 12345 kind: Service apiVersion: v1 metadata: name: mydatabase spec: ports: - protocol: TCP port: 80 targetPort: 12345 kind: Endpoints apiVersion: v1 metadata: name: mydatabase subsets: - addresses: - ip: 10.128.0.2 ports: - port: 12345 Hosted Database Database outside cluster but inside network
  23. 72 Google Cloud Platform More Resources • http://blog.kubernetes.io/2016/08/security-best-practices-kubernetes-deployment.html • https://github.com/gravitational/workshop/blob/master/k8sprod.md

    • https://nodesource.com/blog/8-protips-to-start-killing-it-when-dockerizing-node-js/ • https://www.ianlewis.org/en/using-kubernetes-health-checks • https://www.linux.com/learn/rolling-updates-and-rollbacks-using-kubernetes-deployments • https://kubernetes.io/docs/api-reference/v1.6/