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 Cookbook, 2nd Edition

Book Description

Learn how to automate and manage your containers and reduce the overall operation burden on your system.

About This Book
  • Use containers to manage, scale and orchestrate apps in your organization
  • Transform the latest concept of Kubernetes 1.10 into examples
  • Expert techniques for orchestrating containers effectively
Who This Book Is For

This book is for system administrators, developers, DevOps engineers, or any stakeholder who wants to understand how Kubernetes works using a recipe-based approach. Basic knowledge of Kubernetes and Containers is required.

What You Will Learn
  • Build your own container cluster
  • Deploy and manage highly scalable, containerized applications with Kubernetes
  • Build high-availability Kubernetes clusters
  • Build a continuous delivery pipeline for your application
  • Track metrics and logs for every container running in your cluster
  • Streamline the way you deploy and manage your applications with large-scale container orchestration
In Detail

Kubernetes is an open source orchestration platform to manage containers in a cluster environment. With Kubernetes, you can configure and deploy containerized applications easily. This book gives you a quick brush up on how Kubernetes works with containers, and an overview of main Kubernetes concepts, such as Pods, Deployments, Services and etc.

This book explains how to create Kubernetes clusters and run applications with proper authentication and authorization configurations. With real-world recipes, you'll learn how to create high availability Kubernetes clusters on AWS, GCP and in on-premise datacenters with proper logging and monitoring setup. You'll also learn some useful tips about how to build a continuous delivery pipeline for your application. Upon completion of this book, you will be able to use Kubernetes in production and will have a better understanding of how to manage containers using Kubernetes.

Style and approach

This recipe-based book will teach you how to use Kubernetes in production and will help you discover various steps involved in managing your containers using Kubernetes

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 Cookbook Second Edition
  3. Packt Upsell
    1. Why subscribe?
    2. PacktPub.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    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. Download the color images
      3. Conventions used
    4. Sections
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    5. Get in touch
      1. Reviews
  6. Building Your Own Kubernetes Cluster
    1. Introduction
    2. Exploring the Kubernetes architecture
      1. Getting ready
      2. How to do it...
        1. Kubernetes master
        2. API server (kube-apiserver)
        3. Scheduler (kube-scheduler)
        4. Controller manager (kube-controller-manager)
        5. Command-line interface (kubectl)
        6. Kubernetes node
        7. kubelet
        8. Proxy (kube-proxy)
      3. How it works...
        1. etcd
        2. Kubernetes network
      4. See also
    3. Setting up the Kubernetes cluster on macOS by minikube
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Setting up the Kubernetes cluster on Windows by minikube
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    5. Setting up the Kubernetes cluster on Linux via kubeadm
      1. Getting ready
      2. How to do it...
        1. Package installation
          1. Ubuntu
          2. CentOS
        2. System configuration prerequisites
          1. CentOS system settings
        3. Booting up the service
        4. Network configurations for containers
        5. Getting a node involved
      3. How it works...
      4. See also
    6. Setting up the Kubernetes cluster on Linux via Ansible (kubespray)
      1. Getting ready
        1. Installing pip
        2. Installing Ansible
        3. Installing python-netaddr
        4. Setting up ssh public key authentication
      2. How to do it...
        1. Maintaining the Ansible inventory
        2. Running the Ansible ad hoc command to test your environment
        3. Ansible troubleshooting
          1. Need to specify a sudo password
          2. Need to specify different ssh logon user
          3. Need to change ssh port
          4.  Common ansible issue
      3. How it works...
      4. See also
    7. Running your first container in Kubernetes
      1. Getting ready
      2. How to do it...
        1. Running a HTTP server (nginx)
        2. Exposing the port for external access
        3. Stopping the application
      3. How it works…
      4. See also
  7. Walking through Kubernetes Concepts
    1. Introduction
    2. An overview of Kubernetes
    3. Linking Pods and containers
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Managing Pods with ReplicaSets 
      1. Getting ready
      2. How to do it...
        1. Creating a ReplicaSet
        2. Getting the details of a ReplicaSet
        3. Changing the configuration of a ReplicaSet
        4. Deleting a ReplicaSet
      3. How it works...
      4. There's more...
      5. See also
    5. Deployment API
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Using kubectl set to update the container image
        2. Updating the YAML and using kubectl apply
      4. See also
    6. Working with Services
      1. Getting ready
      2. How to do it...
        1. Creating a Service for different resources
          1. Creating a Service for a Pod
          2. Creating a Service for a Deployment with an external IP
          3. Creating a Service for an Endpoint without a selector
          4. Creating a Service for another Service with session affinity
        2. Deleting a Service
      3. How it works...
      4. There's more...
      5. See also
    7. Working with volumes
      1. Getting ready
      2. How to do it...
        1. emptyDir
        2. hostPath
        3. NFS
        4. glusterfs
        5. downwardAPI
        6. gitRepo
      3. There's more...
        1. PersistentVolumes
        2. Using storage classes
          1. gcePersistentDisk
          2. awsElasticBlockStore
      4. See also
    8. Working with Secrets
      1. Getting ready
      2. How to do it...
        1. Creating a Secret
          1. Working with kubectl create command line
          2. From a file
          3. From a directory
          4. From a literal value
          5. Via configuration file
        2. Using Secrets in Pods
          1. By environment variables
          2. By volumes
        3. Deleting a Secret
      3. How it works...
      4. There's more...
        1. Using ConfigMaps
        2. Mounting Secrets and ConfigMap in the same volume
      5. See also
    9. Working with names
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    10. Working with Namespaces
      1. Getting ready
      2. How to do it...
        1. Creating a Namespace
        2. Changing the default Namespace
        3. Deleting a Namespace
      3. How it works…
      4. There's more...
        1. Creating a LimitRange
        2. Deleting a LimitRange
      5. See also
    11. Working with labels and selectors
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Equality-based label selector
        2. Set-based label selector
      4. There's more...
        1. Linking Service to Pods or ReplicaSets using label selectors
        2. Linking Deployment to ReplicaSet using the set-based selector
      5. See also
  8. Playing with Containers
    1. Introduction
    2. Scaling your containers
      1. Getting ready
      2. How to do it...
        1. Scale up and down manually with the kubectl scale command
        2. Horizontal Pod Autoscaler (HPA)
      3. How it works...
      4. There is more…
      5. See also
    3. Updating live containers
      1. Getting ready
      2. How to do it...
        1. Deployment update strategy – rolling-update
        2. Rollback the update
        3. Deployment update strategy – recreate
      3. How it works...
      4. There's more...
      5. See also
    4. Forwarding container ports
      1. Getting ready
      2. How to do it...
        1. Container-to-container communication
        2. Pod-to-Pod communication
          1. Working with NetworkPolicy
        3. Pod-to-Service communication
        4. External-to-internal communication
          1. Working with Ingress
      3. There's more...
      4. See also
    5. Ensuring flexible usage of your containers
      1. Getting ready
      2. How to do it...
        1. Pod as DaemonSets
        2. Running a stateful Pod
      3. How it works...
        1. Pod recovery by DaemonSets
        2. Pod recovery by StatefulSet
      4. There's more...
      5. See also
    6. Submitting Jobs on Kubernetes
      1. Getting ready
      2. How to do it...
        1. Pod as a single Job
        2. Create a repeatable Job
        3. Create a parallel Job
        4. Schedule to run Job using CronJob
      3. How it works...
      4. See also
    7. Working with configuration files
      1. Getting ready
        1. YAML
        2. JSON
      2. How to do it...
      3. How it works...
        1. Pod
        2. Deployment
        3. Service
      4. See also
  9. Building High-Availability Clusters
    1. Introduction
    2. Clustering etcd 
      1. Getting ready
      2. How to do it...
        1. Static mechanism
        2. Discovery  mechanism
        3. kubeadm
        4. kubespray
        5. Kops
      3. See also
    3. Building multiple masters
      1. Getting ready
      2. How to do it...
        1. Setting up the first master
        2. Setting up the other master with existing certifications
        3. Adding nodes in a HA cluster
      3. How it works...
      4. See also
  10. Building Continuous Delivery Pipelines
    1. Introduction
    2. Moving monolithic to microservices
      1. Getting ready
      2. How to do it...
        1. Microservices
        2. Frontend WebUI
      3. How it works...
        1. Microservices
        2. Frontend WebUI
    3. Working with the private Docker registry
      1. Getting ready
        1. Using Kubernetes to run a Docker registry server
        2. Using Amazon elastic container registry
        3. Using Google cloud registry
      2. How to do it...
        1. Launching a private registry server using Kubernetes
          1. Creating a self-signed SSL certificate
          2. Creating HTTP secret
          3. Creating the HTTP basic authentication file
          4. Creating a Kubernetes secret to store security files
          5. Configuring a private registry to load a Kubernetes secret
        2. Create a repository on the AWS elastic container registry
        3. Determining your repository URL on Google container registry
      3. How it works...
        1. Push and pull an image from your private registry
        2. Push and pull an image from Amazon ECR
        3. Push and pull an image from Google cloud registry
          1. Using gcloud to wrap the Docker command
          2. Using the GCP service account to grant a long-lived credential
    4. Integrating with Jenkins
      1. Getting ready
      2. How to do it...
        1. Setting up a custom Jenkins image
        2. Setting up Kubernetes service account and ClusterRole
        3. Launching the Jenkins server via Kubernetes deployment
      3. How it works...
        1. Using Jenkins to build a Docker image
        2. Deploying the latest container image to Kubernetes
  11. Building Kubernetes on AWS
    1. Introduction
    2. Playing with Amazon Web Services
      1. Getting ready
        1. Creating an IAM user
        2. Installing AWS CLI on macOS
        3. Installing AWS CLI on Windows
      2. How to do it...
      3. How it works...
        1. Creating VPC and Subnets
        2. Internet gateway
        3. NAT-GW
        4. Security group
        5. EC2
    3. Setting up Kubernetes with kops
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Working with kops-built AWS cluster
        2. Deleting kops-built AWS cluster
      4. See also
    4. Using AWS as Kubernetes Cloud Provider
      1. Getting ready
      2. How to do it...
        1. Elastic load balancer as LoadBalancer service
        2. Elastic Block Store as StorageClass
      3. There's more...
    5. Managing Kubernetes cluster on AWS by kops
      1. Getting ready
      2. How to do it...
        1. Modifying and resizing instance groups
          1. Updating nodes
          2. Updating masters
        2. Upgrading a cluster
      3. There's more...
      4. See also
  12. Building Kubernetes on GCP
    1. Playing with GCP
      1. Getting ready
        1. Creating a GCP project
        2. Installing Cloud SDK
          1. Installing Cloud SDK on Windows
          2. Installing Cloud SDK on Linux and macOS
        3. Configuring Cloud SDK
      2. How to do it...
        1. Creating a VPC
        2. Creating subnets
        3. Creating firewall rules
        4. Adding your ssh public key to GCP
      3. How it works...
        1. Launching VM instances
    2. Playing with Google Kubernetes Engine
      1. Getting ready
      2. How to do it…
      3. How it works…
      4. See also
    3. Exploring CloudProvider on GKE
      1. Getting ready
      2. How to do it…
        1. StorageClass
        2. Service (LoadBalancer)
        3. Ingress 
      3. There's more…
      4. See also
    4. Managing Kubernetes clusters on GKE
      1. Getting ready
      2. How to do it…
        1. Node pool
        2. Multi-zone and regional clusters
          1. Multi-zone clusters
          2. Regional clusters
        3. Cluster upgrades
      3. See also
  13. Advanced Cluster Administration
    1. Introduction
    2. Advanced settings in kubeconfig
      1. Getting ready
      2. How to do it...
        1. Setting new credentials
        2. Setting new clusters
        3. Setting contexts and changing current-context
        4. Cleaning up kubeconfig
      3. There's more...
      4. See also
    3. Setting resources in nodes
      1. Getting ready
      2. How to do it...
        1. Configuring a BestEffort pod
        2. Configuring a Guaranteed pod
        3. Configuring a Burstable pod
      3. How it works...
      4. See also
    4. Playing with WebUI
      1. Getting ready
      2. How to do it...
        1. Relying on the dashboard created by minikube
        2. Creating a dashboard manually on a system using other booting tools
      3. How it works...
        1. Browsing your resource by dashboard
        2. Deploying resources by dashboard
        3. Removing resources by dashboard
      4. See also
    5. Working with the RESTful API
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. There's more...
      5. See also
    6. Working with Kubernetes DNS
      1. Getting ready
      2. How to do it...
        1. DNS for pod
        2. DNS for Kubernetes Service
        3. DNS for StatefulSet
      3. How it works...
        1. Headless service when pods scale out
      4. See also
    7. Authentication and authorization
      1. Getting ready
      2. How to do it...
        1. Authentication
          1. Service account token authentication
          2. X509 client certs
          3. OpenID connect tokens
        2. Authorization
          1. Role and RoleBinding
          2. ClusterRole and ClusterRoleBinding
          3. Role-based access control (RBAC)
        3. Admission control
          1. NamespaceLifecycle
          2. LimitRanger
          3. ServiceAccount
          4. PersistentVolumeLabel (deprecated from v1.8)
          5. DefaultStorageClass
          6. DefaultTolerationSeconds
          7. ResourceQuota
          8. DenyEscalatingExec
          9. AlwaysPullImages
      3. There's more…
        1. Initializers (alpha)
        2. Webhook admission controllers (beta in v1.9)
      4. See also
  14. Logging and Monitoring
    1. Introduction
    2. Working with EFK
      1. Getting ready
      2. How to do it...
        1. Setting up EFK with minikube
        2. Setting up EFK with kubespray
        3. Setting up EFK with kops
      3. How it works...
      4. There's more...
      5. See also
    3. Working with Google Stackdriver
      1. Getting ready
      2. How to do it...
      3. How it works...
      4. See also
    4. Monitoring master and node
      1. Getting ready
      2. How to do it...
      3. How it works...
        1. Introducing the Grafana dashboard
        2. Creating a new metric to monitor Pod
      4. There's more...
        1. Monitoring your Kubernetes cluster on AWS
        2. Monitoring your Kubernetes cluster on GCP
      5. See also
  15. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think