Slide 1

Slide 1 text

Kubernetes Faster and Easier

Slide 2

Slide 2 text

2 @saturnism @gcpcloud Ray Tsang Developer Advocate Google Cloud Platform Java Champion Spring Cloud GCP spring.io/projects/spring-cloud-gcp gcplab.me/spring @saturnism | saturnism.me

Slide 3

Slide 3 text

3 @saturnism @gcpcloud Ray Tsang Traveler Photographer flickr.com/saturnism

Slide 4

Slide 4 text

4 @saturnism @gcpcloud https://www.youtube.com/watch?v=Bcs-inRnLDc

Slide 5

Slide 5 text

5 @saturnism @gcpcloud 5 Your App

Slide 6

Slide 6 text

6 @saturnism @gcpcloud Don't start with Kubernetes

Slide 7

Slide 7 text

7 @saturnism @gcpcloud It all starts with your application Twelve-Factor App 12factor.net

Slide 8

Slide 8 text

8 @saturnism @gcpcloud Test, Test, Test Local Mock, Wiremock, Contract TestContainers

Slide 9

Slide 9 text

9 @saturnism @gcpcloud 9 Containers

Slide 10

Slide 10 text

10 @saturnism @gcpcloud Power → Responsibility Runtime Environments may be Your Responsibility Now!

Slide 11

Slide 11 text

11 @saturnism @gcpcloud Choose a JDK Container Aware OpenJDK 8u192 or above

Slide 12

Slide 12 text

12 @saturnism @gcpcloud OOMKilled Cloud Foundry Buildpack Memory Calculator https://github.com/cloudfoundry/java-buildpack-memory-calculator

Slide 13

Slide 13 text

13 @saturnism @gcpcloud Native Memory Tracking -XX:NativeMemoryTracking=summary -XX:+PrintNMTStatistics (Doesn't work when set via JAVA_TOOL_OPTIONS - must be part of the argument)

Slide 14

Slide 14 text

14 @saturnism @gcpcloud Container Best Practices saturnism.me/talk/docker-tips-and-tricks/ What's in that image? Don't run as root Multi-stage build Create small image Fat JAR to Thin JAR Layering Build cache Pin versions Reduce layer size ...

Slide 15

Slide 15 text

15 @saturnism @gcpcloud Just Jib It

Slide 16

Slide 16 text

16 @saturnism @gcpcloud Don't write to container filesystem! Those logs!

Slide 17

Slide 17 text

17 @saturnism @gcpcloud Configuration via external sources Environmental variable Command line arguments

Slide 18

Slide 18 text

18 @saturnism @gcpcloud 18 Finally, Kubernetes!

Slide 19

Slide 19 text

19 @saturnism @gcpcloud Local Kubernetes Linux - consider k3s, k3d, kind, … Mac - Docker for Desktop, Minikube

Slide 20

Slide 20 text

20 @saturnism @gcpcloud Keep Base Deployment Simple kubectl create deployment myservice --image=... --dry-run -oyaml > k8s/deployment.yaml kubectl create svc clusterip myservice --tcp=8080:8080 --dry-run -oyaml/service.yaml

Slide 21

Slide 21 text

21 @saturnism @gcpcloud Continuous Development skaffold

Slide 22

Slide 22 text

22 @saturnism @gcpcloud Resource Limits If you don't' set it, your app may use all the memory... Set it at namespace level, or for individual deployments

Slide 23

Slide 23 text

23 @saturnism @gcpcloud Environments Kustomize

Slide 24

Slide 24 text

24 @saturnism @gcpcloud Liveness Probe Readiness Probe Signals Lifecycle Hooks

Slide 25

Slide 25 text

25 @saturnism @gcpcloud Liveness Probe → Restarts Readiness Probe → Remove from Service Signals → Shutdown or Killed Lifecycle Hooks → PreStart, PreStop

Slide 26

Slide 26 text

26 @saturnism @gcpcloud When to use? Failure Means... Practices Example Liveness Probe If application is alive. Application will be restarted, and that a restart will help recover. Runs on serving port of the application, e.g., 8080. Don't check dependency. E.g., don't check dependent database connection, etc. A simple /alive URL that returns 200. Readiness Probe Ready to serve requests. Take the pod instance out of load balancer. Flip to ready when application has done all the initializations (cache preloaded). Upon SIGTERM, flip readiness to false. See Graceful Shutdown. /actuator/health on the management port.

Slide 27

Slide 27 text

27 @saturnism @gcpcloud Anatomy of a Graceful Shutdown 1. Receive SIGTERM or PreStop Lifecycle Hook 2. Fail Readiness Probe 3. Receive requests until Kubernetes detects readiness probe failure 4. Kubernetes removes pod endpoint from Service 5. Finish serving in-flight requests 6. Shutdown

Slide 28

Slide 28 text

28 @saturnism @gcpcloud Production is HARD Pod Security Policy / Pod Security Context Expect your app to not work in production environment with hardened security Try this early and fix issues

Slide 29

Slide 29 text

29 @saturnism @gcpcloud 29 Thanks! spring.io/projects/spring-cloud-gcp cloud.google.com/java Come to the Google Cloud Platform Booth! @saturnism | saturnism.me