Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Getting Started with Kubernetes, - Third Edition

You're reading from  Getting Started with Kubernetes, - Third Edition

Product type Book
Published in Oct 2018
Publisher Packt
ISBN-13 9781788994729
Pages 470 pages
Edition 3rd Edition
Languages
Concepts
Authors (2):
Jonathan Baier Jonathan Baier
Profile icon Jonathan Baier
Jesse White Jesse White
Profile icon Jesse White
View More author details

Table of Contents (23) Chapters

Title Page
Dedication
Packt Upsell
Contributors
Preface
1. Introduction to Kubernetes 2. Building a Foundation with Core Kubernetes Constructs 3. Working with Networking, Load Balancers, and Ingress 4. Implementing Reliable Container-Native Applications 5. Exploring Kubernetes Storage Concepts 6. Application Updates, Gradual Rollouts, and Autoscaling 7. Designing for Continuous Integration and Delivery 8. Monitoring and Logging 9. Operating Systems, Platforms, and Cloud and Local Providers 10. Designing for High Availability and Scalability 11. Kubernetes SIGs, Incubation Projects, and the CNCF 12. Cluster Federation and Multi-Tenancy 13. Cluster Authentication, Authorization, and Container Security 14. Hardening Kubernetes 15. Kubernetes Infrastructure Management 1. Assessments 2. Other Books You May Enjoy Index

Chapter 4. Implementing Reliable Container-Native Applications

This chapter will cover the various types of workloads that Kubernetes supports. We will cover deployments for applications that are regularly updated and long-running. We will also revisit the topics of application updates and gradual rollouts using Deployments. In addition, we will look at jobs used for short-running tasks. We will look at DaemonSets, which allow programs to be run on every node in our Kubernetes cluster. In case you noticed, we won't look into StatefulSets yet in this chapter but we'll investigate them in the next, when we look at store and how K8s helps you manage storage and stateful applications on your cluster.

The following topics will be covered in this chapter:

  • Deployments
  • Application scaling with Deployments
  • Application updates with Deployments
  • Jobs
  • DaemonSets

Technical requirements


You'll need a running Kubernetes cluster like the one we created in the previous chapters. You'll also need access to deploy to that cluster through the kubectl command.

Here's the GitHub repository for this chapter: https://github.com/PacktPublishing/Getting-Started-with-Kubernetes-third-edition/tree/master/Code-files/Chapter04.

 

How Kubernetes manages state


As discussed previously, we know that Kubernetes makes an effort to enforce the desired state of the operator in a given cluster. Deployments give operators the ability to define an end state and the mechanisms to effect change at a controlled rate of stateless services, such as microservices. Since Kubernetes is a control and data plane that manages the metadata, current status, and specification of a set of objects, Deployments provide a deeper level of control for your applications. There are a few archetypal deployment patterns that are available: recreate, rolling update, blue/green via selector, canary via replicas, and A/B via HTTP headers.

Deployments


In the previous chapter, we explored some of the core concepts for application updates using the old rolling-update method. Starting with version 1.2, Kubernetes added the Deployment construct, which improves on the basic mechanisms of rolling-update and ReplicationControllers. As the name suggests, it gives us finer control over the code deployment itself. Deployments allow us to pause and resume application rollouts via declarative definitions and updates to pods and ReplicaSets. Additionally, they keep a history of past deployments and allow the user to easily roll back to previous versions.

Note

It is no longer recommended to use ReplicationControllers. Instead, use a Deployment that configures a ReplicaSet in order to set up application availability for your stateless services or applications. Furthermore, do not directly manage the ReplicaSets that are created by your deployments; only do so through the Deployment API.

Deployment use cases

We'll explore a number of typical...

Jobs


Deployments and ReplicationControllers are a great way to ensure long-running applications are always up and able to tolerate a wide array of infrastructure failures. However, there are some use cases this does not address, specifically short-running, run once tasks, as well as regularly scheduled tasks. In both cases, we need the tasks to run until completion, but then terminate and start again at the next scheduled interval.

To address this type of workload, Kubernetes has added a batch API, which includes the Job type. This type will create 1 to n pods and ensure that they all run to completion with a successful exit. Based on restartPolicy, we can either allow pods to simply fail without retry (restartPolicy: Never) or retry when a pods exits without successful completion (restartPolicy: OnFailure). In this example, we will use the latter technique as shown in the listing longtask.yaml:

apiVersion: batch/v1
kind: Job
metadata:
  name: long-task
spec:
  template:
    metadata:
   ...

DaemonSets


While ReplicationControllers and Deployments are great at making sure that a specific number of application instances are running, they do so in the context of the best fit. This means that the scheduler looks for nodes that meet resource requirements (available CPU, particular storage volumes, and so on) and tries to spread across the nodes and zones.

 

 

 

 

 

 

This works well for creating highly available and fault tolerant applications, but what about cases where we need an agent to run on every single node in the cluster? While the default spread does attempt to use different nodes, it does not guarantee that every node will have a replica and, indeed, will only fill a number of nodes equivalent to the quantity specified in the ReplicationController or Deployment specification.

To ease this burden, Kubernetes introduced DaemonSet, which simply defines a pod to run on every single node in the cluster or a defined subset of those nodes. This can be very useful for a number of production...

Node selection


As mentioned previously, we can schedule DaemonSets to run on a subset of nodes as well. This can be achieved using something called nodeSelectors. These allow us to constrain the nodes a pod runs on, by looking for specific labels and metadata. They simply match key-value pairs on the labels for each node. We can add our own labels or use those that are assigned by default.

 

 

The default labels are listed in the following table:

Summary


Now, you should have a good foundation of the core constructs in Kubernetes. We explored the new Deployment abstraction and how it improves on the basic ReplicationController, allowing for smooth updates and solid integration with services and autoscaling. We also looked at other types of workload in jobs and DaemonSets. You learned how to run short-running or batch tasks, as well as how to run agents on every node in our cluster. Finally, we took a brief look at node selection and how that can be used to filter the nodes in the cluster used for our workloads.

We will build on what you learned in this chapter and look at stateful applications in the next chapter, exploring both critical application components and the data itself.

Questions


  1. Name four use cases for Kubernetes deployments
  2. Which element of a deployment definition tells the deployment which pod to manage?
  3. Which flag do you need to activate in order to see the history of your changes?
  4. Which underlying mechanism (a Kubernetes object, in fact) does a Deployment use in order to update your container images?
  5. What's the name of the technology that lets your pods scale up and down according to CPU load?
  6. Which type of workload should you run for an ephemeral, short-lived task?
  7. What's the purpose of a DaemonSet?
lock icon The rest of the chapter is locked
You have been reading a chapter from
Getting Started with Kubernetes, - Third Edition
Published in: Oct 2018 Publisher: Packt ISBN-13: 9781788994729
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}

Default node labels

Description

kubernetes.io/hostname

This shows the hostname of the underlying instance or machine

beta.kubernetes.io/os

This shows the underlying operating system as a report in the Go language

beta.kubernetes.io/arch

This shows the underlying processor architecture as a report in the Go language

beta.kubernetes.io/instance-type

This is the instance type of the underlying cloud provider (cloud-only) 

failure-domain.beta.kubernetes.io/region

This is the region of the underlying cloud provider (cloud-only) 

failure-domain.beta.kubernetes.io/zone...