The Kubernetes Bible

Book description

Get up and running with Kubernetes 1.19 and simplify the way you build, deploy, and maintain scalable distributed systems

Key Features

  • Design and deploy large clusters on various cloud platforms
  • Explore containerized application deployment, debugging, and recovery with the latest Kubernetes version 1.19
  • Become well-versed with advanced Kubernetes topics such as traffic routing or Pod autoscaling and scheduling

Book Description

With its broad adoption across various industries, Kubernetes is helping engineers with the orchestration and automation of container deployments on a large scale, making it the leading container orchestration system and the most popular choice for running containerized applications.

This Kubernetes book starts with an introduction to Kubernetes and containerization, covering the setup of your local development environment and the roles of the most important Kubernetes components. Along with covering the core concepts necessary to make the most of your infrastructure, this book will also help you get acquainted with the fundamentals of Kubernetes. As you advance, you'll learn how to manage Kubernetes clusters on cloud platforms, such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP), and develop and deploy real-world applications in Kubernetes using practical examples. Additionally, you'll get to grips with managing microservices along with best practices.

By the end of this book, you'll be equipped with battle-tested knowledge of advanced Kubernetes topics, such as scheduling of Pods and managing incoming traffic to the cluster, and be ready to work with Kubernetes on cloud platforms.

What you will learn

  • Manage containerized applications with Kubernetes
  • Understand Kubernetes architecture and the responsibilities of each component
  • Set up Kubernetes on Amazon Elastic Kubernetes Service, Google Kubernetes Engine, and Microsoft Azure Kubernetes Service
  • Deploy cloud applications such as Prometheus and Elasticsearch using Helm charts
  • Discover advanced techniques for Pod scheduling and auto-scaling the cluster
  • Understand possible approaches to traffic routing in Kubernetes

Who this book is for

This book is for software developers and DevOps engineers looking to understand how to work with Kubernetes for orchestrating containerized applications and services in the cloud. Prior experience with designing software running in operating system containers, as well as a general background in DevOps best practices, will be helpful. Basic knowledge of Kubernetes, Docker, and leading cloud service providers assist with grasping the concepts covered easily.

Table of contents

  1. THE KUBERNETES BIBLE
  2. Contributors
  3. About the authors
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share Your Thoughts
  6. Section 1: Introducing Kubernetes
  7. Chapter 1: Kubernetes Fundamentals
    1. Understanding monoliths and microservices
      1. Understanding the growth of the internet since the late 1990s
      2. Understanding the need for more frequent software releases
      3. Understanding the organizational shift to agile methodologies
      4. Understanding the shift from on-premises to the cloud
      5. Understanding why the cloud is well suited for scalability
      6. Exploring the monolithic architecture
      7. Exploring the microservices architecture
      8. Choosing between monolithic and microservices architectures
    2. Understanding containers and Docker
      1. Understanding why Docker is good for microservices
      2. Understanding the benefit of Docker container isolation
    3. How can Kubernetes help you to manage your Docker containers?
      1. Understanding that Kubernetes is meant to use Docker in production
    4. Exploring the problems that Kubernetes solves
      1. Ensuring high availability
      2. Release management and container deployment
      3. Autoscaling containers
      4. When and where is Kubernetes not the solution?
    5. Understanding the history of Kubernetes
      1. Understanding how and where Kubernetes started
      2. Who manages Kubernetes today?
      3. Where is Kubernetes today?
    6. Summary
  8. Chapter 2: Kubernetes Architecture – From Docker Images to Running Pods
    1. Understanding the difference between the master and worker nodes
    2. The kube-apiserver component
      1. The role of kube-apiserver
      2. How do you install kube-apiserver?
      3. Where do you install kube-apiserver?
    3. Exploring the kubectl command-line tool and YAML syntax
      1. The role of kubectl
      2. How does kubectl work?
      3. The YAML syntax
      4. kubectl should be installed on any machine that needs to interact with the cluster
    4. The Etcd datastore
      1. The role of the Etcd datastore
      2. Where do you install Etcd?
    5. The Kubelet and worker node components
      1. The Kubelet agent
      2. The kube-proxy component
    6. The kube-scheduler component
      1. The role of the kube-scheduler component
      2. Where do you install kube-scheduler?
    7. The kube-controller-manager component
      1. The role of the kube-controller-manager component
      2. Where do you install kube-controller-manager?
    8. How to make Kubernetes highly available
      1. The single-node cluster
      2. The single-master cluster
      3. The multi-master multi-node cluster
    9. Summary
  9. Chapter 3: Installing Your First Kubernetes Cluster
    1. Technical requirements
    2. Installing a single-node cluster with Minikube
      1. Launching a single-node Kubernetes cluster using Minikube
      2. Stopping and deleting the local Minikube cluster
    3. Launching a multi-node Kubernetes cluster with Kind
      1. Installing Kind onto your local system
      2. Stopping and deleting the local Kind cluster
    4. Installing a Kubernetes cluster using Google GKE
      1. Launching a multi-node Kubernetes cluster on Google GKE
      2. Stopping and deleting a Kubernetes cluster on Google GKE
    5. Installing a Kubernetes cluster using Amazon EKS
      1. Launching a multi-node Kubernetes cluster on Amazon EKS
      2. Deleting the Kubernetes cluster on Amazon EKS
    6. Installing a Kubernetes cluster using Azure AKS
      1. Launching a multi-node Kubernetes cluster on Azure AKS
      2. Stopping and deleting a Kubernetes cluster on Azure AKS
    7. Summary
  10. Section 2: Diving into Kubernetes Core Concepts
  11. Chapter 4: Running Your Docker Containers
    1. Technical requirements
    2. Let's explain the notion of Pods
      1. Each Pod gets an IP address
      2. How you should design your Pods
    3. Launching your first Pods
      1. Creating a Pod with imperative syntax
      2. Creating a Pod with declarative syntax
      3. Reading the Pod's information and metadata
      4. Listing the objects in JSON or YAML
      5. Backing up your resource using the list operation
      6. Getting more information from the list operation
      7. Accessing a Pod from the outside world
      8. Entering a container inside a Pod
      9. Deleting a Pod
    4. Labeling and annotating the Pods
      1. What are labels and why do we need them?
      2. What are annotations and how do they differ from labels?
      3. Adding a label
      4. Listing labels attached to a Pod
      5. Adding or updating a label to/of a running Pod
      6. Deleting a label attached to a running Pod
      7. Adding an annotation
    5. Launching your first job
      1. What are jobs?
      2. Creating a job with restartPolicy
      3. Understanding the job's backoffLimit
      4. Running a task multiple times using completions
      5. Running a task multiple times in parallel
      6. Terminating a job after a specific amount of time
      7. What happens if a job succeeds?
      8. Deleting a job
    6. Launching your first Cronjob
      1. What are Cronjobs?
      2. Creating your first Cronjob
      3. Understanding the schedule
      4. Understanding the role of the jobTemplate section
      5. Controlling the Cronjob execution deadline
      6. Managing the history limits of jobs
      7. Creating a Cronjob
      8. Deleting a Cronjob
    7. Summary
  12. Chapter 5: Using Multi-Container Pods and Design Patterns
    1. Technical requirements
    2. Understanding what multi-container Pods are
      1. Concrete scenarios where you need multi-container Pods
      2. When not to create a multi-container Pod
      3. Creating a Pod made up of two containers
      4. What happens when Kubernetes fails to launch one container in a Pod?
      5. Deleting a multi-container Pod
      6. Understanding the Pod deletion grace period
      7. Accessing a specific container inside a multi-container Pod
      8. Running commands in containers
      9. Overriding the default commands run by your containers
      10. Introducing initContainers
      11. Accessing the logs of a specific container
    3. Sharing volumes between containers in the same Pod
      1. What are Kubernetes volumes?
      2. Creating and mounting an emptyDir volume
      3. Creating and mounting a hostPath volume
    4. The ambassador design pattern
      1. What is the ambassador design pattern?
      2. A simple example of an ambassador multi-container Pod
    5. The sidecar design pattern
      1. What is the sidecar design pattern?
      2. A simple example of a sidecar multi-container Pod
    6. The adapter design pattern
      1. What is the adapter design pattern?
      2. A simple example of an adapter multi-container Pod
    7. Summary
  13. Chapter 6: Configuring Your Pods Using ConfigMaps and Secrets
    1. Technical requirements
    2. Understanding what ConfigMaps and Secrets are
      1. Decoupling your application and your configuration
      2. Understanding how Pods consume ConfigMaps and Secrets
    3. Configuring your Pods using ConfigMaps
      1. Listing ConfigMaps
      2. Creating a ConfigMap
      3. Creating a ConfigMap from literal values
      4. Storing entire configuration files in a ConfigMap
      5. Creating a ConfigMap from an env file
      6. Reading values inside a ConfigMap
      7. Linking ConfigMaps as environment variables
      8. Mounting a ConfigMap as a volume mount
      9. Deleting a ConfigMap
      10. Updating a ConfigMap
    4. Managing sensitive configuration with the Secret object
      1. Listing Secrets
      2. Creating a Secret imperatively with --from-literal
      3. Creating a Secret declaratively with a YAML file
      4. Creating a Secret with content from a file
      5. Reading a Secret
      6. Consuming a Secret as an environment variable
      7. Consuming a Secret as a volume mount
      8. Deleting a Secret
      9. Updating a Secret
    5. Summary
  14. Chapter 7: Exposing Your Pods with Services
    1. Technical requirements
    2. Why would you want to expose your Pods?
      1. Understanding Pod IP assignment
      2. Understanding Pod IP assignment is dynamic
      3. Never hardcode a pod's IP addresses in your application code
      4. Understanding how services route traffic to Pods
      5. Understanding round-robin load balancing in Kubernetes
      6. Understanding how to call a service in Kubernetes
      7. Understanding how DNS names are generated for services
      8. How services get a list of the Pods they service traffic to
      9. Using the dnsutils Docker image to debug your services
      10. Why you shouldn't use the --expose flag
      11. Understanding how DNS names are generated for services
      12. Understanding the different types of services
    3. The NodePort service
      1. Why do you need NodePort services?
      2. Creating two containous/whoami Pods
      3. Understanding NodePort YAML definition
      4. Making sure NodePort works as expected
      5. Is this setup production-ready?
      6. Listing NodePort services
      7. Adding more Pods to NodePort services
      8. Describing NodePort services
      9. Deleting NodePort services
      10. NodePort or kubectl port-forward?
    4. The ClusterIP service
      1. Why do you need ClusterIP services?
      2. How do I know if I need NodePort or ClusterIP services to expose my Pods?
      3. Listing ClusterIP services
      4. Creating ClusterIP services using the imperative way
      5. Describing ClusterIP services
      6. Creating ClusterIP services using the declarative way
      7. Deleting ClusterIP services
      8. Understanding headless services
    5. The LoadBalancer service
      1. Explaining the LoadBalancer services
      2. Should I use the LoadBalancer service type?
    6. Implementing ReadinessProbe
      1. Why do you need ReadinessProbe?
      2. Implementing ReadinessProbe
      3. What is LivenessProbe and why do you need it?
      4. Implementing LivenessProbe
      5. Using ReadinessProbe and LivenessProbe together
    7. Securing your Pods using the NetworkPolicy object
      1. Why do you need NetworkPolicy?
      2. Understanding Pods are not isolated by default
      3. Configuring NetworkPolicy with labels and selectors
    8. Summary
  15. Chapter 8: Managing Namespaces in Kubernetes
    1. Technical requirements
    2. Introduction to Kubernetes namespaces
      1. Why do you need namespaces?
      2. How namespaces are used to split resources into chunks
      3. Understanding default namespaces
    3. How namespaces impact your resources and services
      1. Listing namespaces inside your cluster
      2. Retrieving the data of a specific namespace
      3. Creating a namespace using imperative syntax
      4. Creating a namespace using declarative syntax
      5. Deleting a namespace
      6. Creating a resource inside a namespace with the -n option
      7. Listing resources inside a specific namespace
      8. Listing all the resources inside a specific namespace
      9. Understanding that names are scoped to a namespace
      10. Understanding that not all resources are in a namespace
      11. Resolving a service using namespaces
      12. Switching between namespaces with kubectl
      13. Displaying the current namespace with kubectl
    4. Configuring ResourceQuota and Limit at the namespace level
      1. Understanding why you should set ResourceQuota
      2. Understanding how Pods consume these resources
      3. Understanding how Pods can require computing resources
      4. Understanding how you can limit resource consumption
      5. Understanding why you need ResourceQuota
      6. Creating a ResourceQuota
    5. Listing ResourceQuota
    6. Deleting ResourceQuota
    7. Introducing LimitRange
    8. Listing LimitRange
    9. Deleting LimitRange
    10. Summary
  16. Chapter 9: Persistent Storage in Kubernetes
    1. Technical requirements
    2. Why you would want to use PersistentVolume
      1. Introducing PersistentVolumes
      2. Introducing PersistentVolume types
      3. The benefits brought by PersistentVolume
      4. Introducing access modes
      5. Understanding that not all access modes are available to all PersistentVolume types
      6. Creating our first PersistentVolume
      7. How does Kubernetes PersistentVolumes handle cloud-based storage?
      8. Amazon EBS PersistentVolume YAML
      9. GCE PersistentDisk PersistentVolume YAML
      10. NFS PersistentVolume YAML
      11. Can Kubernetes handle the provisioning or creation of the resource itself?
    3. Understanding how to mount a PersistentVolume to your Pod claims
      1. Introducing PersistentVolumeClaim
      2. Splitting storage creation and storage consumption
      3. The summarized PersistentVolume workflow
      4. Creating a Pod with a PersistentVolumeClaim object
    4. Understanding the life cycle of a PersistentVolume object in Kubernetes
      1. Understanding that PersistentVolume objects are not bound to namespaces
      2. Reclaiming a PersistentVolume object
      3. Updating a reclaim policy
      4. Understanding PersistentVolume and PersistentVolumeClaims statuses
    5. Static and dynamic PersistentVolume provisioning
      1. Static versus dynamic provisioning
      2. Introducing dynamic provisioning
      3. Introducing StorageClasses
      4. Understanding the role of PersistentVolumeClaim for dynamic storage provisioning
    6. Summary
  17. Section 3: Using Managed Pods with Controllers
  18. Chapter 10: Running Production-Grade Kubernetes Workloads
    1. Technical requirements
    2. Ensuring HA and FT on Kubernetes
      1. High Availability
      2. Fault Tolerance
      3. HA and FT for Kubernetes applications
    3. What is ReplicationController?
      1. Creating a ReplicationController object
      2. Testing the behavior of ReplicationController
      3. Scaling ReplicationController
      4. Deleting ReplicationController
    4. What is ReplicaSet and how does it differ from ReplicationController?
      1. Creating a ReplicaSet object
      2. Testing the behavior of ReplicaSet
      3. Scaling ReplicaSet
      4. Using Pod liveness probes together with ReplicaSet
      5. Deleting a ReplicaSet object
    5. Summary
    6. Further reading
  19. Chapter 11: Deployment – Deploying Stateless Applications
    1. Technical requirements
    2. Introducing the Deployment object
      1. Creating a Deployment object
      2. Exposing Deployment Pods using Service objects
      3. Scaling a Deployment object
      4. Deleting a Deployment object
    3. How does a Deployment object manage revisions and version rollout?
      1. Updating a Deployment object
      2. Rolling back a Deployment object
    4. Deployment object best practices
      1. Use declarative object management for Deployments
      2. Do not use the Recreate strategy for production workloads
      3. Do not create Pods that match an existing Deployment label selector
      4. Carefully set up your container probes
      5. Use meaningful and semantic image tags
      6. Migrating from older versions of Kubernetes
    5. Summary
    6. Further reading
  20. Chapter 12: StatefulSet – Deploying Stateful Applications
    1. Technical requirements
    2. Introducing the StatefulSet object
      1. Managing state in containers
      2. Managing state in Kubernetes Pods
      3. StatefulSet and how it differs from a Deployment object
    3. Managing StatefulSet
      1. Creating a StatefulSet
      2. Using the headless Service and stable network identities
      3. State persistence
      4. Scaling StatefulSet
      5. Deleting a StatefulSet
    4. Releasing a new version of an app deployed as a StatefulSet
      1. Updating StatefulSet
      2. Rolling back StatefulSet
    5. StatefulSet best practices
      1. Use declarative object management for StatefulSets
      2. Do not use the TerminationGracePeriodSeconds Pod with a 0 value for StatefulSets
      3. Scale down StatefulSets before deleting
      4. Ensure state compatibility during StatefulSet rollbacks
      5. Do not create Pods that match an existing StatefulSet label selector
    6. Summary
    7. Further reading
  21. Chapter 13: DaemonSet – Maintaining Pod Singletons on Nodes
    1. Technical requirements
    2. Introducing the DaemonSet object
    3. Creating and managing DaemonSets
      1. Creating a DaemonSet
      2. Modifying a DaemonSet
      3. Deleting a DaemonSet
    4. Common use cases for DaemonSets
    5. Alternatives to DaemonSets
    6. Summary
    7. Further reading
  22. Section 4: Deploying Kubernetes on the Cloud
  23. Chapter 14: Kubernetes Clusters on Google Kubernetes Engine
    1. Technical requirements
    2. What are GCP and GKE?
      1. Google Cloud Platform
      2. Google Kubernetes Engine
    3. Preparing your environment
      1. Signing up for a GCP account
      2. Creating a project
      3. Installing the GCP command-line interface
    4. Launching your first GKE cluster
    5. Deploying a workload and interacting with your cluster
      1. Configuring your local client
      2. Launching an example workload
      3. Exploring Google Cloud Console
      4. Deleting your cluster
    6. More about cluster nodes
    7. Summary
    8. Further reading
  24. Chapter 15: Launching a Kubernetes Cluster on Amazon Web Services with Amazon Elastic Kubernetes Service
    1. Technical requirements
    2. What are AWS and Amazon EKS?
      1. AWS
      2. Amazon EKS
    3. Preparing your local environment
      1. Signing up for an AWS account
      2. Installing the AWS command-line interface
      3. Installing eksctl, the official CLI for Amazon EKS
    4. Launching your Amazon EKS cluster
    5. Deploying a workload and interacting with your cluster
      1. Deploying the workload
      2. Exploring the AWS console
    6. Deleting your Amazon EKS cluster
    7. Summary
    8. Further reading
  25. Chapter 16: Kubernetes Clusters on Microsoft Azure with Azure Kubernetes Service
    1. Technical requirements
    2. What are Microsoft Azure and AKS?
      1. Microsoft Azure
      2. AKS
    3. Preparing your local environment
      1. The Azure CLI
    4. Launching your AKS cluster
    5. Deploying a workload and interacting with your cluster
      1. Launching the workload
      2. Exploring the Azure portal
    6. Deleting your AKS cluster
    7. Summary
    8. Further reading
  26. Section 5: Advanced Kubernetes
  27. Chapter 17: Working with Helm Charts
    1. Technical requirements
    2. Understanding Helm
    3. Releasing software to Kubernetes using Helm
      1. Installing Helm on Ubuntu
      2. Installing Helm on Windows
      3. Installing Helm on macOS
      4. Deploying an example chart
    4. Helm chart anatomy
    5. Installing popular solutions using Helm charts
      1. Kubernetes Dashboard
      2. Elasticsearch with Kibana
      3. Prometheus with Grafana
    6. Summary
    7. Further reading
  28. Chapter 18: Authentication and Authorization on Kubernetes
    1. Technical requirements
    2. Authentication and user management
      1. Static token files
      2. ServiceAccount tokens
      3. X.509 client certificates
      4. OpenID Connect tokens
      5. Other methods
    3. Authorization and introduction to RBAC
      1. RBAC mode in Kubernetes
    4. Azure Kubernetes Service and Azure Active Directory integration
      1. Prerequisites
      2. Deploying a managed AKS cluster with AAD and Azure RBAC integration
      3. Accessing the AKS cluster with AAD integration enabled
      4. Using Azure RBAC for an AKS cluster
    5. Summary
    6. Further reading
  29. Chapter 19: Advanced Techniques for Scheduling Pods
    1. Technical requirements
    2. Refresher – What is kube-scheduler?
    3. Managing Node affinity
      1. Pod Node name
      2. Pod Node selector
      3. Node affinity configuration for Pods
    4. Using Node taints and tolerations
    5. Scheduling policies
    6. Summary
    7. Further reading
  30. Chapter 20: Autoscaling Kubernetes Pods and Nodes
    1. Technical requirements
    2. Pod resource requests and limits
    3. Autoscaling Pods vertically using a Vertical Pod Autoscaler
      1. Enabling a VPA in GKE
      2. Enabling a VPA for other Kubernetes clusters
      3. Using a VPA
    4. Autoscaling Pods horizontally using a Horizontal Pod Autoscaler
      1. Using an HPA
    5. Autoscaling Kubernetes Nodes using a Cluster Autoscaler
      1. Enabling the cluster autoscaler in GKE
      2. Enabling the cluster autoscaler in the Amazon Elastic Kubernetes Service
      3. Enabling the cluster autoscaler in the Azure Kubernetes Service
      4. Using the cluster autoscaler
    6. Summary
    7. Further reading
  31. Chapter 21: Advanced Traffic Routing with Ingress
    1. Technical requirements
    2. Refresher: Kubernetes services
      1. The ClusterIP Service
      2. NodePort service
      3. The LoadBalancer service
    3. Introducing the Ingress object
    4. Using nginx as an Ingress Controller
    5. Azure Application Gateway Ingress Controller for AKS
    6. Summary
    7. Further reading
    8. Why subscribe?
  32. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share Your Thoughts

Product information

  • Title: The Kubernetes Bible
  • Author(s): Nassim Kebbani, Piotr Tylenda, Russ McKendrick
  • Release date: February 2022
  • Publisher(s): Packt Publishing
  • ISBN: 9781838827694