O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Kubernetes for Developers

Book Description

A developer's field-guide to designing scalable services using Kubernetes

About This Book
  • Develop and run your software using containers within a Kubernetes environment
  • Get hands-on experience of using Kubernetes with DevOps concepts such as continuous integration, benchmark testing, monitoring, and so on
  • Pragmatic example-based approach showing how to use Kubernetes in the development process
Who This Book Is For

If you are a full-stack or back-end software developers interested, curious, or being asked to test as well as run the code you're creating, you can leverage Kubernetes to make that process simpler and consistent regardless of where you deploy. If you're looking for developer focused examples in NodeJS and Python for how to build, test, deploy, and run your code with Kubernetes, this is perfect for you.

What You Will Learn
  • Build your software into containers
  • Deploy and debug software running in containers within Kubernetes
  • Declare and add configuration through Kubernetes
  • Define how your application fits together, using internal and external services
  • Add feedback to your code to help Kubernetes manage your services
  • Monitor and measure your services through integration testing and in production deployments
In Detail

Kubernetes is documented and typically approached from the perspective of someone running software that has already been built. Kubernetes may also be used to enhance the development process, enabling more consistent testing and analysis of code to help developers verify not only its correctness, but also its efficiency. This book introduces key Kubernetes concepts, coupled with examples of how to deploy and use them with a bit of Node.js and Python example code, so that you can quickly replicate and use that knowledge.

You will begin by setting up Kubernetes to help you develop and package your code. We walk you through the setup and installation process before working with Kubernetes in the development environment. We then delve into concepts such as automating your build process, autonomic computing, debugging, and integration testing. This book covers all the concepts required for a developer to work with Kubernetes.

By the end of this book, you will be in a position to use Kubernetes in development ecosystems.

Style and approach

This book will cover examples using NodeJS and Python that walk you through building containers, defining your deployments, deploying, debugging, testing, and generally interacting with your code running on Kubernetes. The examples are focused on common development needs, and include pragmatic advice and the explanations behind that advice.

Downloading the example code for this book You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Kubernetes for Developers
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. Setting Up Kubernetes for Development
    1. What you need for development
      1. Optional tools
    2. Getting a local cluster up and running
    3. Resetting and restarting your cluster
    4. Looking at what's built-in and included with Minikube
    5. Verifying Docker
    6. Clearing and cleaning Docker images
    7. Kubernetes concept – container
    8. Kubernetes resource – Pod
      1. Namespaces
      2. Writing your code for Pods and Containers
    9. Kubernetes resource – Node
      1. Networks
      2. Controllers
    10. Kubernetes resource – ReplicaSet
    11. Kubernetes resource – Deployment
      1. Representing Kubernetes resources
    12. Summary
  7. Packaging Your Code to Run in Kubernetes
    1. Container images
      1. Container registries
    2. Making your first container
      1. Dockerfile commands
    3. Example – Python/Flask container image
      1. Building the container
      2. Running your container
        1. Pod name
        2. Port forwarding
        3. Proxy
        4. How did the proxy know to connect to port 5000 on the container?
      3. Getting logs from your application
    4. Example – Node.js/Express container image
      1. Building the container
      2. Running your container
        1. Port forwarding
        2. Proxy
      3. Getting logs from your application
    5. Tagging your container images
    6. Summary
  8. Interacting with Your Code in Kubernetes
    1. Practical notes for writing software to run in a container
      1. Getting options for your executable code
      2. Practical notes for building container images
      3. Sending output from your program
    2. Logs
      1. Pods with more than one container
      2. Streaming the logs
      3. Previous logs
      4. Timestamps
    3. More debugging techniques
      1. Interactive deployment of an image
      2. Attaching to a running Pod
      3. Running a second process in a container
    4. Kubernetes concepts – labels
      1. Organization of labels
    5. Kubernetes concepts – selectors
      1. Viewing labels
      2. Listing resources with labels using kubectl
      3. Automatic labels and selectors
    6. Kubernetes resources – service
      1. Defining a service resource
      2. Endpoints
      3. Service type – ExternalName
      4. Headless service
    7. Discovering services from within your Pod
      1. DNS for services
    8. Exposing services outside the cluster
      1. Service type – LoadBalancer
      2. Service type – NodePort
      3. Minikube service
    9. Example service – Redis
      1. Finding the Redis service
      2. Using Redis from Python
      3. Updating the Flask deployment
    10. Deployments and rollouts
      1. Rollout history
      2. Rollout undo
      3. Updating with the kubectl set command
    11. Summary
  9. Declarative Infrastructure
    1. Imperative versus declarative commands
      1. A wall of YAML
      2. Creating a simple deployment
    2. Declaring your first application
      1. ImagePullPolicy
      2. Audit trail
    3. Kubernetes resource – Annotations
      1. Exposing labels and annotations in Pods
    4. Kubernetes resource – ConfigMap
      1. Creating a ConfigMap
      2. Managing ConfigMaps
      3. Exposing the configuration into your container images
        1. Environment variables
        2. Exposing ConfigMap as files inside the container
      4. Dependencies on ConfigMaps
    5. Kubernetes resource – Secrets
      1. Exposing Secrets into a container
      2. Secrets and security – how secret are the secrets?
    6. Example – Python/Flask deployment with ConfigMap
      1. SIDEBAR – JSONPATH
      2. Using the ConfigMap within Python/Flask
    7. Summary
  10. Pod and Container Lifecycles
    1. Pod lifecycle
      1. Container lifecycle
      2. Deployments, ReplicaSets, and Pods
      3. Getting a snapshot of the current state
    2. Probes 
      1. Liveness probe
      2. Readiness probe
      3. Adding a probe to our Python example
        1. Running the Python probes example
      4. Adding a probe to our Node.js example
    3. Container lifecycle hooks
    4. Initialization containers
      1. Quick interactive testing
    5. Handling a graceful shutdown
      1. SIGTERM in Python
      2. SIGTERM in Node.js
    6. Summary
  11. Background Processing in Kubernetes
    1. Job
    2. CronJob
    3. A worker queue example with Python and Celery
      1. Celery worker example
        1. RabbitMQ and configuration
        2. Celery worker
    4. Persistence with Kubernetes
      1. Volumes
      2. PersistentVolume and PersistentVolumeClaim
    5. Stateful Sets
      1. A Node.js example using Stateful Set
    6. Custom Resource Definition
    7. Summary
  12. Monitoring and Metrics
    1. Built-in metrics with Kubernetes
    2. Kubernetes concept – Quality of Service
      1. Choosing requests and limits for your containers
    3. Capturing metrics with Prometheus
      1. Installing Helm
      2. Installing Prometheus using Helm
      3. Viewing metrics with Prometheus 
    4. Installing Grafana
    5. Using Prometheus to view application metrics
      1. Flask metrics with Prometheus
      2. Node.js metrics with Prometheus
      3. Service signals in Prometheus
    6. Summary
  13. Logging and Tracing
    1. A Kubernetes concept – DaemonSet
    2. Installing and using Elasticsearch, Fluentd, and Kibana
      1. Log aggregation with EFK
    3. Viewing logs using Kibana
      1. Filtering by app
      2. Lucene query language
      3. Running Kibana in production
    4. Distributed tracing with Jaeger
      1. Spans and traces
      2. Architecture of Jaeger distributed tracing
      3. Trying out Jaeger
    5. Example – adding tracing to your application
      1. Adding a tracing collector to your pod
      2. Add the libraries and code to generate traces
      3. Considerations for adding tracing
    6. Summary
  14. Integration Testing
    1. Testing strategies using Kubernetes
      1. Reviewing resources needed for testing
      2. Patterns of using Kubernetes with testing
        1. Tests local and system-under-test in Kubernetes
        2. Tests local and system-under-test in Kubernetes namespaces
        3. Tests in Kubernetes and system-under-test in Kubernetes namespaces
    2. Simple validation with Bats
    3. Example – integration testing with Python
      1. PyTest and pytest-dependency
      2. PyTest fixtures and the python-kubernetes client
      3. Waiting for state changes
      4. Accessing the deployment
    4. Example – integration testing with Node.js
      1. Node.js tests and dependencies with mocha and chai
      2. Validating the cluster health
      3. Deploying with kubectl
      4. Waiting for the pods to become available
      5. Interacting with the deployment
    5. Continuous integration with Kubernetes
      1. Example – using Minikube with Travis.CI
        1. Next steps
      2. Example – using Jenkins and the Kubernetes plugin
        1. Installing Jenkins using Helm
        2. Accessing Jenkins
        3. Updating Jenkins
        4. Example pipeline
        5. Next steps with pipelines
    6. Summary
  15. Troubleshooting Common Problems and Next Steps
    1. Common errors and how to resolve them
      1. Error validating data
        1. Navigating the documentation
      2. ErrImagePull
      3. CrashLoopBackOff
        1. Starting and inspecting the image
        2. Adding your own setup to the container 
      4. No endpoints available for service
      5. Stuck in PodInitializing
        1. Missing resources
    2. Emerging projects for developers
      1. Linters
      2. Helm
      3. ksonnet
      4. Brigade
      5. skaffold
      6. img
      7. Draft
      8. ksync
      9. Telepresence
    3. Interacting with the Kubernetes project
      1. Slack
      2. YouTube
      3. Stack Overflow
      4. Mailing lists and forums
    4. Summary
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think