developing kubernetes services
play

Developing Kubernetes Services at Airbnb Scale @MELANIECEBULA What - PowerPoint PPT Presentation

@MELANIECEBULA / MARCH 2019 / CON LONDON Developing Kubernetes Services at Airbnb Scale @MELANIECEBULA What is kubernetes? @MELANIECEBULA Who am I? A BRIEF HISTORY @MELANIECEBULA Why Microservices? 4000000 3000000 2000000 MONOLITH


  1. @MELANIECEBULA kubernetes config files Production Canary Dev Deployment Deployment Deployment kubectl apply kubernetes Production Canary Dev kubernetes cluster ConfigMap ConfigMap ConfigMap verbose Dev repetitive by Production Canary Service Service namespace Service P

  2. @MELANIECEBULA k tool KUBECTL WRAPPER Production Canary Dev Deployment Deployment Deployment kubectl apply Production Canary Dev kubernetes cluster ConfigMap ConfigMap ConfigMap calls kubectl commands (incl. Dev Production Canary plugins) Service Service Service

  3. @MELANIECEBULA k tool OPINIONATED KUBECTL

  4. @MELANIECEBULA • Runs in the project home directory: 
 $ cd /path/to/bonk $ k status • Environment variables for arguments: k tool standardized $ k status ENV=staging USES ENV VARS namespaces! • Prints the command that it will execute: 
 $ k status ENV=staging kubectl get pods --namespace=bonk-staging

  5. @MELANIECEBULA • k generate generates kubernetes fj les • k build performs project build, docker build and k tool docker push with tags • k deploy creates namespace, applies/replaces SIMPLIFIES BUILDS AND DEPLOYS kubernetes fj les, sleeps and checks deployment status • can chain commands; ex: k all

  6. @MELANIECEBULA • defaults to random pod, main container: $ k ssh ENV=staging k tool • specify particular pod, speci fj c container: A DEBUGGING TOOL $ k logs ENV=staging POD=… CONTAINER=bonk • automates debugging with k diagnose ENV=staging

  7. @MELANIECEBULA • defaults to random pod, main container: call kubectl diagnose $ k ssh ENV=staging k tool • specify particular pod, speci fj c container: A DEBUGGING TOOL $ k logs ENV=staging POD=… CONTAINER=bonk • automates debugging with k diagnose ENV=staging

  8. @MELANIECEBULA What are kubectl plugins?

  9. @MELANIECEBULA What are kubectl plugins?

  10. @MELANIECEBULA k diagnose SETUP deploy bonk service with failing command new pod in CrashLoopBackoff

  11. @MELANIECEBULA k diagnose MANUALLY 1. use “get pods - o=yaml” and look for problems 2. grab logs for unready container

  12. @MELANIECEBULA k diagnose MANUALLY 3. get k8s events related to this pod

  13. @MELANIECEBULA kubectl podevents KUBECTL PLUGIN kubectl podevents plugin

  14. @MELANIECEBULA // defines CLI command and flags kubectl diagnose var Namespace string USES COBRA GO CLI var rootCmd = &cobra.Command{ Use: “kubectl diagnose —namespace<namespace>" Short: “diagnoses a namespace with pods in CrashLoopBackOff” Run: func(cmd *cobra.Command, arg[]string) { // Fill in with program logic } } func Execute() { rootCmd.Flags().StringVarP(&Namespace, "namespace", "n", “”) rootCmd.MarkFlagRequired("namespace") if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) }

  15. @MELANIECEBULA // get pods (assume Namespace is defined) kubectl diagnose kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube","config") USES K8S CLIENT-GO config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { … } clientset, err := kubernetes.NewForConfig(config) if err != nil { … } pods, err := uses k8s client-go clientset.CoreV1().Pods(Namespace).List(metav1.ListOptions{}) and Namespace fmt.Printf("There are %d pods in the namespace %s\n", param to get pods len(pods.Items), Namespace) for _, pod := range pod.Items { podName := pod.Name for _, c := range pod.Status.ContainerStatuses { if c.Ready != true { // print c.LastTerminatedState and c.State } }

  16. @MELANIECEBULA // get pods (assume Namespace is defined) kubectl diagnose kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube","config") USES K8S CLIENT-GO config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) if err != nil { … } clientset, err := kubernetes.NewForConfig(config) if err != nil { … } pods, err := clientset.CoreV1().Pods(Namespace).List(metav1.ListOptions{}) fmt.Printf("There are %d pods in the namespace %s\n", len(pods.Items), Namespace) for _, pod := range pod.Items { podName := pod.Name for _, c := range pod.Status.ContainerStatuses { if c.Ready != true { prints info for all unready containers // print c.LastTerminatedState and c.State } }

  17. @MELANIECEBULA // get pod events for namespace and pod kubectl diagnose cmd := exec.Command("kubectl", "podevents", Namespace, podName) USES OS/EXEC (WHEN LAZY) var out bytes.Buffer podevents kubectl var stderr bytes.Buffer plugin cmd.Stdout = &out cmd.Stderr = &stderr err := cmd.Run() if err != nil { fmt.Println(fmt.Sprint(err) + ": " + stderr.String()) log.Fatal(err) } else { fmt.Println("Events: \n" + out.String()) } } // also grab logs cmd = exec.Command("kubectl", "logs", podname, fmt.Sprintf("-- namespace=%s", Namespace), "-c", "bonk")

  18. @MELANIECEBULA kubectl diagnose GO KUBECTL PLUGIN

  19. @MELANIECEBULA kubectl diagnose GO KUBECTL PLUGIN 1. unready container info

  20. @MELANIECEBULA kubectl diagnose GO KUBECTL PLUGIN 1. unready container info 2. kubectl podevents

  21. @MELANIECEBULA kubectl diagnose GO KUBECTL PLUGIN 1. unready container info 2. kubectl podevents 3. pod logs for unready containers

  22. Takeaways • Create an opinionated kubectl wrapper • Automate common k8s workflows with kubectl plugins

  23. CI/CD

  24. Each step in our CI /CD jobs are RUN steps in a build Dockerfile

  25. runs k commands

  26. DEPLOY PROCESS

  27. @MELANIECEBULA A single deploy process for every change Develop Merge Deploy Write code and config Open a PR and merge Deploy all code and under your project your code to master config changes

  28. @MELANIECEBULA A single deploy process for every change Project Deployment AWS Storage ownership kubectl kubectl apply apply Service kubernetes cluster ConfigMap Alerts Docs Discovery API Gateway Service Dashboards Secrets Routes

  29. @MELANIECEBULA How do we apply k8s configuration? • kubectl apply all files Deployment • in some cases where apply “kubectl fails, replace files without apply” force kubernetes cluster • always restart pods on ConfigMap deploy to pick up changes • return atomic success or failure state by sleeping and checking status Service

  30. @MELANIECEBULA How do you always restart pods on deploy? We add a date label Deployment to the pod spec, which convinces k8s kubectl kubectl to relaunch all pods apply apply kubernetes cluster ConfigMap Service

  31. @MELANIECEBULA How do we apply custom configuration?

  32. @MELANIECEBULA How do we apply custom configuration? kubectl kubectl apply apply kubernetes cluster aws.yml AWS AWS AWS CRD Controller webhook

  33. @MELANIECEBULA How do we apply custom configuration? 1. Create a custom kubectl kubectl apply apply resource definition for aws.yml kubernetes cluster aws.yml AWS AWS AWS CRD Controller webhook

  34. @MELANIECEBULA How do we apply custom configuration? 2. Create a controller kubectl kubectl that calls a web hook apply apply when aws.yml is kubernetes cluster applied aws.yml AWS AWS AWS CRD Controller webhook

  35. @MELANIECEBULA How do we apply custom configuration? 3. Create a web hook kubectl kubectl that updates a apply apply custom resource kubernetes cluster aws.yml AWS AWS AWS CRD Controller webhook

  36. @MELANIECEBULA How do we apply custom configuration? 4. AWS lambda exposes web hook to be called AWS AWS AWS lambda AWS CRD Controller webhook

  37. Takeaways • Code and configuration should be deployed with the same process • Use custom resources and custom controllers to integrate k8s with your infra

  38. VALIDATION

  39. @MELANIECEBULA • enforce best practices • at build time with validation scripts Configuration • at deploy time with admission controller SHOULD BE VALIDATED

  40. @MELANIECEBULA How do we validate configuration at build time?

  41. @MELANIECEBULA How do we validate configuration at build time? project.yml validation script global job dispatcher kube project docs validation validation script build build jobs bonk CI jobs aws .yml validation script global jobs repo

  42. @MELANIECEBULA How do we validate configuration at build time? project.yml validation script global job dispatcher kube project docs validation validation script build build jobs 1. Define global job in bonk CI jobs aws .yml global jobs repo validation script global jobs repo

  43. @MELANIECEBULA How do we validate configuration at build time? project.yml validation script global job dispatcher kube project docs validation validation script build build jobs 2. job dispatcher always dispatches bonk CI jobs aws .yml global jobs to validation script projects global jobs repo

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend