Professional Documents
Culture Documents
The Gorilla Guide To Kubernetes in The Enterprise PDF
The Gorilla Guide To Kubernetes in The Enterprise PDF
The
Gorilla
Guide to... ®
Kubernetes in the
Enterprise
Joep Piscaer
I N S I D E T H E G U I D E:
• Why the Industry Has Embraced Containers
• Keys to Smoothing Your Kubernetes On-ramp
• Managed Kubernetes Provides the Optimal Experience
www.actualtechmedia.com
THE GORILLA GUIDE TO...
Kubernetes in the
Enterprise
AUTHOR
Joep Piscaer
E D ITO R
Keith Ward, ActualTech Media
L AYO U T A N D D E S I G N
Olivia Thomson, ActualTech Media
All rights reserved. This book or any portion thereof may not be reproduced or used
in any manner whatsoever without the express written permission of the publisher
except for the use of brief quotations in a book review.
Printed in the United States of America.
ACT UA LT E C H M E D I A
Okatie Village Ste 103-157
Bluffton, SC 29909
www.actualtechmedia.com
ENTERING THE JUNGLE
iii
Chapter 6: Top Use Cases 49
Simple Deployment of Stateless Applications 49
Deploy Stateful Data Services 52
CI/CD Platform with Kubernetes 54
iv
CALLOUTS USED IN THIS BOOK
v
ICONS USED IN THIS BOOK
D E F I N IT I O N
Defines a word, phrase, or concept.
KNOWLEDGE CHECK
Tests your knowledge of what you’ve read.
PAY AT T E NT I O N
We want to make sure you see this!
GPS
We’ll help you navigate your knowledge to the
right place.
WATC H O U T!
Make sure you read this so you don’t make a crit-
ical error!
vi
CHAPTER 1
Drawbacks of this approach are the inherent size and volume of VMs.
Each OS is many gigabytes in size, which not only requires storage
space, but also increases the memory footprint. See Figure 1.
Infrastructure Infrastructure
New versions of the OS, its components, and other software inside the
VM are layered on top of the older version. Because of this, each in-
place upgrade creates potential version conflicts, stability problems,
and ballooning of uncleaned recent versions on disk. Maintaining this
ever-increasing complexity is a major operational pain point, and
often leads to downtime.
Containers
First and foremost, containers package up only the parts of the applica-
tion unique to that container, like the business logic. Containers share
the underlying OS and often common libraries, frameworks, or other
pieces of middleware. This results in much lighter packages (contain-
ers are usually megabytes, instead of the gigabytes that are typical
with VMs), and are clearly decoupled from the layer cake underneath.
The same goes for a new version of the application inside the contain-
er: instead of updating the container, a new container with the new
version is deployed, and traffic is diverted to that new container. The
old one is killed as soon as the new container is operating correctly.
Cloud-native Services
While containers are a great fit for custom business logic and code,
many of the moving parts of an application stack are standard and
common components. Instead of re-inventing the wheel, using
commercially available and/or open source software for those com-
ponents makes sense. Other than a few niche and extreme use cases,
why build your own database engine, caching layer or web server?
This definition puts the focus on more than just a set of technological
tools. It encompasses business outcomes like scalability, dynamic
behavior, and resiliency; standards regarding certain patterns of
methodology and design like immutability and frequent changes;
and a focus on operational excellence with abilities like decoupling,
observability, and automation.
1. Fluentd. This is a unified logging tool that helps users better un-
derstand what’s happening in their environments by providing a
unified layer for collecting, filtering, and routing log data.
Of course, there are numerous software projects not part of the CNCF
that fit into the ecosystem very well. Examples include Istio, the
popular service mesh, and Terraform, the composable infrastructure
automation tool.
Developer Agility
We’ve seen that containers unlock the full benefits of agile software
development and operations. Creating smaller, portable container
images that contain only the application increases developer velocity
and the speed through the pipeline into production, which massively
reduces the inertia of each release.
Cost Management
Similar to the move from physical to virtual servers, moving to con-
tainers optimizes resource usage. This lowers the cost of each applica-
tion, as it runs more efficiently. As discussed before, a major difference
between a VM and a container is its relative size: a container is mag-
nitudes smaller than a VM. This makes it nimbler and more flexible,
especially from a cost perspective. This allows the container to run
where it’s cheaper, an important consideration in ephemeral compute
instances where the application is non-production or resilient itself.
There are some solutions for cost control, like CloudHealth, CoreOS
Operator Framework, and Platform9’s Arbitrage that help assign
cost across the multitude of layers in Kubernetes and the underlying
public cloud or on-premises platform.
Only after implementation does cost control start to matter. The tan-
gible benefits of the system have started to manifest in day-to-day
operations; after that, the downsides, including cost sprawl, need to
be reined in, but only after it’s proven successful.
And here lies the true cost/benefit analysis: it’s not just about controlling
infrastructure costs, but developer costs, too: how much quicker can
1
https://kubernetes.io/docs/tasks/run-application/horizontal-pod-autoscale/
Cloud
Node 1 Provider
kubectl Network Edge
Pods
etcd
kubelet
scheduler Node 1
Pods
Container Runtime
kubelet
System Services
Containers
cloud-controller manager
kube-controller-manager
kube-apiserver
kube-scheduler
kube-proxy
Additional Services
etcd
Container Runtime
kubelet
System Services
Control Plane
The control plane is the system that maintains a record of all
Kubernetes objects. It continuously manages object states, respond-
ing to changes in the cluster; it also works to make the actual state of
system objects match the desired state.
Cluster Nodes
Cluster nodes are machines that run containers and are managed by
the master nodes (Figure 5). The Kubelet is the primary and most
important controller in Kubernetes. It’s responsible for driving the
container execution layer, typically Docker.
Containers
pod pod
kube-proxy
Container Runtime
kubelet
System Services
Pods are ephemeral, with a limited lifespan. When scaling back down
or upgrading to a new version, for instance, pods eventually die. Pods
can do horizontal autoscaling (i.e., grow or shrink the number of
instances), and perform rolling updates and canary deployments.
Pod
Container Container
External
Volume
Pod Network
By default, services are only reachable inside the cluster using the
clusterIP service type. Other service types do allow external access;
the LoadBalancer type is the most common in cloud deployments. It
will spin up a load balancer per service on the cloud environment,
which can be expensive. With many services, it can also become
very complex.
Networking
Kubernetes has a distinctive networking model for cluster-wide, pod-
to-pod networking. In most cases, the Container Network Interface
(CNI) uses a simple overlay network (like Flannel) to obscure the
underlying network from the pod by using traffic encapsulation (like
VXLAN); it can also use a fully-routed solution like Calico. In both
Persistent Storage
Kubernetes uses the concept of volumes. At its core, a volume is just a
directory, possibly with some data in it, which is accessible to a pod.
How that directory comes to be, the medium that backs it, and its
contents are determined by the particular volume type used.
There are many options for mounting both file and block storage to
a pod. The most common ones are public cloud storage services, like
AWS EBS and gcePersistentDisk, or types that hook into a physi-
cal storage infrastructure, like CephFS, Fibre Channel, iSCSI, NFS,
Flocker or glusterFS.
There are a few special kinds, like configMap and Secrets, used for
injecting information stored within Kubernetes into the pod or emp-
tyDir, commonly used as scratch space.
Pod 2
objects linked to the backing storage provider that make these re-
sources available for consumption.
Labels: Labels:
app=nginx app=mysql
env=prod env=dev
Labels:
app=nginx
env=prod
Service
app=nginx
env=prod
Host B
Labels:
app=nginx
env=prod
Labels: Labels:
app=mysql app=nginx
env=prod env=dev
service.namespace.svc.cluster.local
10.32.0.125.namespace.pod.cluster.local
There are four different service types, each with different behaviors.
Labels are often used to describe release state (stable, canary), en-
vironment (development, testing, production), app tier (frontend,
backend) or customer identification. Selectors use labels to filter or
select objects, and are used throughout Kubernetes. This prevents
objects from being hard linked.
Deploying Kubernetes
Deploying a Kubernetes cluster from scratch can be a daunting task.
It requires knowledge of its core concepts, the ability to make archi-
tecture choices, and expertise on the deployment tools and knowledge
of the underlying infrastructure, be it on-premises or in the cloud.
Big clusters put a higher burden on the master nodes, and they need to
be sized appropriately. It’s recommended to run at least three nodes
for etcd, which allows a single node failure. While it may be tempting
to run etcd on the same nodes as the Kubernetes master nodes, it’s
recommended to create and run the etcd as a separate cluster.
Adding more nodes will protect against multiple node failures simul-
taneously (5 nodes/2 failures and 7 nodes/4 failures), but each node
added can decrease Kubernetes’ performance. For master nodes,
running two protects against failure of any one node.
For both the etcd cluster and Kubernetes master nodes, designing for
availability across multiple physical locations (such as Availability
Zones in AWS) protects the Kubernetes environment against physical
and geographical failure scenarios.
On-Premises Implementations
Many on-premises environments are repurposed to integrate with
Kubernetes (like creating clusters on top of VMs). In some cases, a
new infrastructure is created for the cluster. In any case, integrating
servers, storage and networking into a smoothly-running environ-
ment is still highly-skilled work.
Public Cloud
This is why many, for their first foray into Kubernetes, spin up
clusters in public cloud environments. Kubernetes deployment tools
integrate natively with public cloud environments, and are able to spin
up the required compute instances, as well as configure storage and
networking services for day-to-day operations.
For cloud instances, it’s critically important to select the right instance
type. While some instance types are explicitly a bad idea (for example,
VMs with partial physical CPUs assigned or with CPU oversubscrip-
tion), others might be too expensive. An advantage of public clouds is
their consumption-based billing, which provides the opportunity to
re-evaluate consumption periodically.
Networking Concerns
The slightly-different-than-usual networking model of Kubernetes
requires some planning. The most basic networking pieces are the
addresses for the nodes and public-facing Kubernetes parts. These
are part of the regular, existing network. Kubernetes allocates an IP
D eploying K ubernetes
33
block for pods, as well as a block for services. Of course, these ranges
should not collide with existing ranges on the physical network.
Lifecycle Management
There are various tools to manage the lifecycle of a Kubernetes clus-
ter. Broadly speaking, there are tools for the deployment and lifecycle
management of clusters, and there are tools for interacting with a
cluster for day-to-day operations.
Kubeadm
Kubeadm is the default way of bootstrapping a best-practice-com-
pliant cluster on existing infrastructure. Add-ons and networking
setup are both out of scope for Kubeadm, as well as provisioning the
underlying infrastructure.
Kubespray
Kubespray takes the configuration management approach, based
on Ansible playbooks. This is ideal for those already using Ansible
and who are comfortable with configuration management tooling.
Kubespray uses kubeadm under the hood.
MiniKube
MiniKube is one of the more popular ways of trying out Kubernetes
locally. The tool is a good starting point for taking first steps with
Kubernetes. It launches a single-node cluster inside a VM on your
local laptop. It runs on Windows, Linux and MacOS, and has a
dashboard.
D eploying K ubernetes
34
Kops
Kops allows you to control the full Kubernetes cluster lifecycle, from
infrastructure provisioning to cluster deletion. It’s mainly for de-
ploying on AWS, but support for GCE and VMware vSphere is coming.
Various cloud vendors use their own proprietary tools for deploying
and managing cluster lifecycle. These are delivered as part of the
managed Kubernetes service, and usually not surfaced up to the user.
• Service discovery
While Kube-DNS is still the default, CoreDNS will replace it,
starting with version 1.13, to do service discovery.
• User interface
The Kubernetes dashboard is an addon.
While the name addon suggests some of these are optional, in reality
many are required for a production-grade Kubernetes environment.
Choosing the most suitable network provider, like Flannel or Calico,
is crucial for integrating the cluster into the existing environment, be
it on-premises or in the cloud.
D eploying K ubernetes
35
Helm is used to find and install popular software packages and man-
age the lifecycle of those applications. It’s somewhat comparable to a
Docker Registry, only Helm charts might contain multiple containers
and Kubernetes-specific information, like pod specifications. Helm
includes a default repository for many software packages:
There are unique Charts available for things like the games Minecraft
and Factorio, the smart home automation system Home Assistant,
and Ubiquiti’s wireless access point controller, UniFi SDN. Helm
makes the life of application developers easier by eliminating the
toil of finding, installing and managing the lifecycle of many popular
software packages.
D eploying K ubernetes
36
CHAPTER 4
Putting Kubernetes
To Work
Giving Users Access
Kubernetes uses Role-based Access Control (RBAC) to regulate user
access to its resources by assigning roles to users (Figure 9). While
it’s possible to let all users log in using full administrator credentials,
most organizations will want to limit who has full access for security,
compliance and risk management reasons.
Pod AutoScaler
list
Service
Developer Developer get
Deployment
Secrets
Ingress create
PV
ReplicaSets watch
Administrator Administrator Namespace
DaemonSet
delete
CronJob
Job
Nodes patch
OS Process Process in Pod PVC
Many organizations take this one step further and lock down ac-
cess even more, so only tooling in their CI/CD pipeline can access
Kubernetes, via service accounts. This locks out real, actual humans, as
they’re expected to interact with Kubernetes clusters only indirectly.
https://github.com/kubernetes/dashboard
2
+ = Observability +
Visualization Tracing
+
Persistent Storage
Managing storage in production is traditionally one of the most
complex and time-consuming administrative tasks. Kubernetes
simplifies this by separating supply and demand.
Managed Kubernetes
Solutions
Running Kubernetes is still a lot of work, and requires deep domain
expertise. More importantly, it takes time to design, implement and
get into production. While the benefits of Kubernetes are massive,
managing Kubernetes isn’t just set-it-and-forget-it.
Every business must give thought to the buy vs. rent paradigm. It
comes down to evaluating cost and risk, and judging whether they’re
outweighed by the advantages of a particular solution.
And really, what is the business problem you’re trying to solve with
Kubernetes? It probably has to do with developer velocity, creating
flow and reducing the work in progress. Having a sub-optimal plat-
form will hurt those goals.
This is the true value of a managed service: making sure the ser-
vice delivered is top-notch. The service provider makes sure their
Kubernetes environment is highly available, resilient, flexible, up-
to-date, secure, and efficient. Their job is to remove the toil and
hassles of Kubernetes.
Focus on Moving the Business Forward
While installing and managing Kubernetes doesn’t move the busi-
ness forward, quickly deploying new applications and versions to
customers does. To developers – Kubernetes’ end users – platform
availability is the key. They don’t care who builds it or how it gets
there: they just want to get their hands on it, and for it to work well.
A large part of that work consists of jobs like node lifecycle manage-
ment, deploying new hosts, and making sure the hosts are kept up
to date with the latest OS patches and container runtime versions.
Because for many organizations, it’s not just Kubernetes. Efforts usu-
ally also include Jenkins and other CI/CD tools, a suite of observability
tools for logging, metrics, tracing and dashboards, various databases
and key/value stores, pub/sub message queueing systems, and more.
Setting all these up before a developer can start using the ecosystem
simply takes too much time, and can overwhelm an IT department.
• Deployment time
Deploying a Kubernetes Control Plane is a piece of cake for the
MSP. Plugging in your various environments should be easy,
quick and frictionless.
The downside is paying those public cloud bills. Even though public
cloud is great, not everything must, or can, run in that environment
for financial or data governance reasons. This means that the man-
aged Kubernetes solution needs to be able to manage nodes across
on-premises and public cloud boundaries.
(Note also that there are many use cases and tutorials beyond the
ones detailed here, available at the Kubernetes website.3)
https://kubernetes.io/docs/tutorials/
3
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
T op U se C ases
50
First, we’d need to add a service, to make the database (running
outside of the Kubernetes cluster for now) available for consump-
tion in pods:
kind: Service
apiVersion: v1
metadata:
name: external-mysql-service
Spec:
type: ClusterIP
ports:
- port: 3306
targetPort: 3306
selector: {}
Since the database doesn’t run on the cluster, we don’t need to add
any pod selectors. Kubernetes doesn’t know where to route the traf-
fic, so we need to create an endpoint to instruct Kubernetes where to
send traffic from this service.
kind: Endpoints
apiVersion: v1
metadata:
name: external-mysql-service
subsets:
- addresses:
- ip: 10.240.0.4
ports:
- port: 3306
Within the WordPress configuration, you can now add the MySQL
database using the metadata name from the example above,
external-mysql-service .
T op U se C ases
51
Deploy Stateful Data Services
In the previous example, we assumed that the MySQL instance ran
outside of Kubernetes. What if we want to put it under Kubernetes’
control and run it as a pod?
The StatefulSet pod type and a PersistentVolume help with this. Let’s
look at an example:
apiVersion: v1
kind: Service
metadata:
name: mysql-on-kubernetes
spec:
ports:
- port: 3306
selector:
app: mysql
clusterIP: None
---
apiVersion: apps/v1 # for versions before 1.9.0 use
apps/v1beta2
kind: Deployment
metadata:
name: mysql-on-kubernetes
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
# Use secret in real usage
- name: MYSQL_ROOT_PASSWORD
value: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
T op U se C ases
52
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
The file defines a volume mount for /var/lib/mysql, and then creates
a PersistentVolumeClaim that looks for a 20GB volume. This claim is
satisfied by any existing volume that meets the requirements, or by
a dynamic provisioner.
kind: PersistentVolume
apiVersion: v1
metadata:
name: mysql-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce
hostPath:
path: “/mnt/data”
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
T op U se C ases
53
Using the same data structure and the same tools, it’s possible to
create services on the platform with persistent storage. While it’s a
little extra work to re-platform data services like databases onto the
Kubernetes platform, it does make sense.
T op U se C ases
54
This setup allows developers to automate their pipeline: each new
code commit on git triggers a container build (which is built using
the Jenkins Agent) and subsequently pushed to Kubernetes to replace
the old version of the app in question for a rolling upgrade. (There’s a
more detailed explanation available as well.4)
https://betsol.com/2018/11/devops-using-jenkins-docker-and-kubernetes/
4
T op U se C ases
55
CHAPTER 7
Platform9 Managed
Kubernetes
Platform9 Managed Kubernetes (PMK) is the industry’s only SaaS-
based, continuously managed Kubernetes service that guarantees
24x7x365 SLA and works across any infrastructure: on-premises
data centers, public clouds, and at the edge.
If you’re not using containers yet, the question is why? The bene-
fits are massive, and an enterprise-grade management system like
Kubernetes makes it easier than ever.
If you’re ready to see what Kubernetes can do for you, visit Platform95
for more information, and start playing in your free sandbox.
https://platform9.com/managed-kubernetes/
5
T he B ig D ecision 62