Getting Started With Kubernetes
Getting Started With Kubernetes
Introduction 2
What Is Kubernetes? 3
Kubernetes Features 4
Installing Kubernetes (For Different OS) 6
Mac OS X Users 6
Windows 7
Kubernetes Fundamentals 9
Containers, Pods, and ReplicaSets 9
Masters, Worker Nodes, and Clusters 11
Services, Ingresses, and Networking 13
Clusters 17
Cluster Configuration Options 17
One Cluster or Many? 18
Deployments 20
Create and Expose a Deployment 20
Scale and Update a Deployment 21
Executing Zero Downtime Deployments 22
Advanced Material 25
References 30
www.caylent.com 2
1
Heptio. (2018). The State of Kubernetes 2018. Seattle, WA: Heptio. Retrieved from https://go.heptio.com/rs/383-ENX-437/images/Heptio_
StateOfK8S_R6.pdf
www.caylent.com 3
What Is
Kubernetes?
Kubernetes was launched into the open-source GitHub stratosphere by Google in 2014 as an
independent solution for managing and orchestrating containers technologies such as Docker and
rkt. The platform is really the third iteration of a container system developed by Google called Borg.
Borg is Google’s internal “cluster manager that runs hundreds of thousands of jobs, from many
thousands of different applications, across a number of clusters each with up to tens of thousands
of machines.” (Verma et al., 20152)
After Borg came Omega, which was never publicly launched, but was used as the test bed for a lot of
innovations that were folded back into Borg and, concurrently, Kubernetes. Kubernetes is the fruition
of lessons learned from all three container management systems over the course of a decade.
Google partnered with Linux in 2015 around the launch time of Kubernetes v1.0 to establish the
Cloud Native Computing Foundation (CNCF) as a true landing pad for the project—essentially, the
C++ Borg rewritten in Go. The CNCF encourages the open source development and
collaboration which surrounds the broad functionality of Kubernetes, making it the extensive and
highly popular project that it is today.
Kubernetes, from the Greek term for “helmsman,” is intended to make steering container
management for multiple nodes as simple as managing containers on a single system. As the
platform is based on Docker containers, it also works perfectly with Node apps—so users can run any
kind of application on it.
2
Verma, A., Pedrosa, L., R. Korupolu, M., Oppenheimer, D., Tune, E., & Wilkes, J. (2015). Large-Scale Cluster Management At Google With Borg.
Retrieved from https://ai.google/research/pubs/pub43438
www.caylent.com 4
Commonly referred to as K8s or Kube, the platform has also started taking over the DevOps scene
in the last couple of years by allowing users to implement best practices for speeding up the
development process through automated testing, deployments, and updates. Working on Kube
allows developers to manage apps and services with almost zero downtime. As well as providing
self-healing capabilities, Kubernetes can also detect and restart services if a process fails inside a
container.
For creating portable and scalable application deployments that can be scheduled, managed, and
maintained easily, it’s easy to see why it’s becoming the go-to technology of choice. Kubernetes
can be used on-premise in a corporate data center, but also integrated with all of the leading public
cloud offerings too. Its cross-functionality and heterogeneous cloud support are why the platform
has rapidly risen to become the standard for container orchestration.
Kubernetes Features
Automated rollouts Using K8s allows users to roll out and roll back new versions/
and rollbacks configurations of an application, without risking any downtime.
www.caylent.com 5
Secrets and It’s also possible to manage the secrets and configuration
configuration details for an application without re-building corresponding
management images. Through Kubernetes secrets, confidential information to
applications can be shared without exposing it to the stack
configuration, much like on GitHub.
www.caylent.com 6
Installing Kubernetes
(For Different OS)
MAC OS X USERS
The two prerequisites for Mac users are that you need to have Homebrew and Homebrew Cask
installed. The latter can be installed after Homebrew by running brew tap caskroom/cask in your
Terminal. Now, follow these steps:
1 Install Docker for Mac first. Docker is the foundation on which we will create, manage, and
run our containers. Installing Docker lets us create containers that will run in Kubernetes
Pods.
2 Install VirtualBox for Mac using Homebrew. Next, in your Terminal, run brew cask install
virtualbox. VirtualBox allows you to run virtual machines on a Mac (like running Windows
inside macOS, except for with a Kubernetes cluster).
3 Now, install kubectl for Mac, the command-line interface tool that allows you to interact
with Kubernetes. In your Terminal, run brew install kubectl.
4 Install Minikube according to the latest GitHub release documentation. At the time of
writing, this uses the following command in Terminal.
Minikube will launch by creating a Kubernetes cluster with a single node. Or you can install
via homebrew with brew cask install minikube.
5 Everything should work! Kick off your Minikube cluster with minikube start—though bear in
mind this may take a few minutes to work. Then type kubectl api-versions. If you get a list of
versions in front of you, everything’s working!
www.caylent.com 7
WINDOWS
1 Launch Windows PowerShell with Admin Privileges ( Right Click -> Run as Administrator ).
As mentioned earlier, kubectl is the command-line interface tool that allows you to interact
with Kubernetes.
3 Your PowerShell should now read the following, enter ‘Y’ to continue running the script:
minikube version
minikube update-check
www.caylent.com 8
6 Now, it’s time to start your new K8s local cluster. Open the PowerShell terminal and run
command:
minikube start
Great! Everything is now installed and it all looks like it’s working. Let’s run through a quick
explanation of the components included in these install steps for both Mac and Windows users:
Homebrew is Mac’s go-to package manager for installations and Homebrew Cask
extends Homebrew with support for quickly installing Mac applications
like Google Chrome, VLC, and, of course, Kubernetes as well as others.
Chocolatey is a package manager like apt-get or yum but solely for Windows. It
was designed to act as a decentralized framework for quickly installing
applications and necessary tools. Chocolatey is built on the NuGet
infrastructure and currently uses PowerShell as its focus for delivering
packages.
Kubernetes Fundamentals
The master node manages the state of a cluster and is essentially the entry point for all
administrative tasks. There are three ways to communicate with the master node:
Via the CLI Via the GUI (Kubernetes Dashboard) Via APIs
To improve fault tolerance, it’s possible to have more than one master node in the cluster in High
Availability (HA) mode. In a multiple master node setup though, only one of them will act as a leader
performing all the operations; the rest of the master nodes would be followers.
Worker nodes are controlled by the master node and may be a VM or physical machine which runs
the applications using pods. Worker nodes are responsible for scheduling pods using the necessary
components to run and connect them (see below). We also connect to worker nodes and not to the
master node/s if accessing applications from the external world
etcd is a distributed key-value store The API server performs all the
which manages the cluster state. All administrative tasks within the
the master nodes connect to it. master node by validating and
processing user/operator REST
commands. Cluster changes are
stored in the distributed key store
The scheduler programs the work (etcd) once executed.
between worker nodes and contains
the resource usage information for
each one. It works according to user/
operator configured constraints. The controller manager instigates
the control/reconciliation loops that
compare the actual cluster state
to the desired cluster state in the
Worker/slave node components (which apiserver and ensure the two match.
execute the application workloads):
Container runtime, as mentioned previously such as Docker or rkt, executes the containers.
You can run each of these components as standard Linux processes, or as Docker ones.
www.caylent.com 13
As pods are ephemeral in nature, any resources like IP addresses allocated to them are not reliable—
as pods can be rescheduled or just die abruptly. To overcome this challenge, Kubernetes offers a
higher-level abstraction known as a service, which groups pods in a logical collection with a policy to
access them via labels and selectors. On configuration, pods will launch with pre-configured labels
(key/value pairs to organize by release, deployment or tier, etc.). For example:
labels:
app: nginx
tier: backend
Users can then use selectors to tell the resource, whatever it may be (e.g., service, deployment, etc.)
to identify pods according to that label. By default, each named service is assigned an IP address,
which is routable only inside the cluster. Services then act as a common access point to pods from
the external world through a connection endpoint to communicate with the appropriate pod and
forwards traffic. The network proxy deamon kube-proxy listens to the API server for every service
endpoint creation/deletion from each worker node, then it sets up route channel accordingly.
By default, Kubernetes isolates pods and the outside world. Connecting with a service in a pod
means opening up a route channel for communication. The collection of routing rules which govern
how external users access services is referred to as ingress. There are three general strategies in
Kubernetes for exposing your application through ingress:
Through nodeport which exposes the application on a port across each of your nodes
Ingress is an integral concept in K8s as it allows simple host or URL based HTTP routing, but it is
always instigated by a third-party proxy. These third-party proxies are known as ingress controllers,
and they’re responsible for reading the ingress resource data and processing that info accordingly.
Different ingress controllers have extended the routing rules in different ways to support alternative
use cases. For an in-depth guide to managing Kubernetes ingresses, read our article here.
Kubernetes networking—as there is no default model, all implementations must work through a
third-party network plug-in (e.g., Project Calico, Weave Net, Flannel, etc.)—is responsible for routing
all internal requests between hosts to the right pod. service, load balancer, or ingress controllers
organize external access (see above). Pods act much the same as VMs or physical hosts with regards
to naming, load balancing, port allocation, and application configuration.
When it comes to networking communication of pods, Kubernetes sets certain conditions and
requirements:
1 All pods can communicate with each other without the need to use network address
translation (NAT)
2 Nodes are also able to communicate with all pods, without the need for NAT
3 Each pod will see itself with the same IP address that other pods see it with
www.caylent.com 15
This leaves us with three networking challenges to overcome in order to take advantage of
Kubernetes:
All the containers within a given service Each pod exists in its own Ethernet
will have the same IP address and namespace. This namespace then needs
port space—as assigned by the pod’s to communicate with other network
assigned network namespace. So, namespaces that are located on the
because all the containers all reside same node. Linux provides a mechanism
within the same namespace, they are able for connecting namespaces using a
to communicate with one another via virtual Ethernet device (VED or ‘veth
localhost. pair’). The VED comprises of a pair of
virtual interfaces.
SUGGESTED TOOLS
Due to its rising popularity and open-source nature, the list of in-built and external tools for
enhancing Kubernetes usage is extensive and far too widespread to cover here. For a glimpse into the
top 50 + tools that Caylent suggest to begin with for improving your work with the platform, check
out our curated list here.
TO GET STARTED
As pods are ephemeral in nature, any resources like IP addresses allocated to them are not reliable—
as pods can be rescheduled or just die abruptly. To overcome this challenge, Kubernetes offers a
higher-level abstraction known as a service, which groups pods in a logical collection with a policy to
access them via labels and selectors. On configuration, pods will launch with pre-configured labels
(key/value pairs to organize by release, deployment or tier, etc.). For example:
MiniKube, as mentioned previously, is the easiest and most recommended way to initiate
an all-in-one Kubernetes cluster locally.
Bootstrap a minimum viable Kubernetes cluster that conforms to best practices with
kubeadm—a first-class citizen of the Kubernetes ecosystem. As well as a set of building
blocks to setup clusters, it is easily extendable to provide more functionality.
www.caylent.com 17
Using KubeSpray means we can install Highly Available Kubernetes clusters on GCE, Azure,
AWS, OpenStack or bare metal machines. The Kubernetes Incubator project tool is based
on Ansible, and it’s available on most Linux distributions.
Kops allows us to create, destroy, upgrade, and maintain highly available, production-grade
Kubernetes clusters from the CLI. It can also provision the machines too.
Clusters
With an all-in-one configuration, both the master and worker components are run on a single node.
This setup is beneficial for development, testing, and training and can be run easily on Minikube. Do
not use this setup in production.
For this installation, there is a single master node that runs a single-node etcd instance and is
connected to multiple worker nodes.
www.caylent.com 18
In this high-availability setup, there are multiple master nodes but only a single-node etcd instance.
Multiple worker nodes are connected to the multiple master nodes. One master will be the leader.
In this installation, etcd is configured outside the Kubernetes cluster in a clustered mode with many
master and worker nodes connected. This is considered the most sophisticated and recommended
production setup.
By default, when you create a cluster, the master and its nodes are launched in a single compute
or availability zone that you pre-configure. It’s possible to improve the availability and resilience of
your clusters by establishing regional clusters. A regional cluster supplies a single static endpoint
for the whole cluster and distributes your cluster’s pods across multiple zones of a given region. It’s
your choice whether a cluster is zonal or regional when you create it. It’s important to note too that
existing zonal cluster can’t be converted to regional or vice versa.
www.caylent.com 19
When it comes to the question of how many clusters though, there are a number of considerations
to take into account. The Kubernetes documentation offers the following advice:
“The selection of the number of Kubernetes clusters may be a relatively static choice, only revisited
occasionally. By contrast, the number of nodes in a cluster and the number of pods in a service may
change frequently according to load and growth.”
One of the biggest considerations needs to be the impact on internal and external customers when
Kubernetes is running in production. For example, an enterprise environment may require a multi-
tenant cluster for distinct teams within the organization to run effectively. A multi-tenant cluster can
be divided across multiple users and/or workloads—these are known as tenants. Operating multiple
clusters can assist to:
Establish maintenance
Separate tenants Improve high
lifecycles to match
and workloads availability
particular workloads
While it is feasible to run multiple clusters per availability zones, it is advisable to run fewer clusters
with more VMs per availability zone. Choosing fewer clusters per availability zone can help with the
following:
Regional clusters, however, replicate cluster masters and nodes across more than one zone within in
a single region. Choosing this option can help with:
Deployments
If you want to revise a deployment, describes the state that you want in a ReplicaSet. Then during a
rollout, the deployment controller will adapt the current state to match the described state that you
want at a controlled rate. All deployment revisions can also be rolled back and scaled too.
www.caylent.com 21
Use the CLI Kubectl terminal to create and manage a deployment. To create a deployment, specify
the application’s container image and the number of replicas that you want to run. Run your first app
with the kubectl run command to create a new deployment. Here’s an example of the output
which creates a ReplicaSet to bring up 3 pods.
apiVersion: v1
kind: Deployment
metadata:
name: example-deployment # Name of our deployment
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
labels:
app: nginx
status: serving
spec:
containers:
- image: nginx:1.9.7
name: nginx
ports:
- containerPort: 80
Before anything else, first check to see if the deployment was created successfully by running the
kubectl rollout status and kubectl get deployment command. The first will show if it failed or not
and the second will indicate how many replicas are available, have been updated, and how many are
open to end users.
deployment.apps/nginx-deployment scaled
If you have changed your mind about the image number and want to update the nginx Pods to use
the nginx:1.9.1 image instead of the nginx:1.7.9 image. use the kubectl command --record as
follows:
Remove any downtime from your production environment so that users don't feel let down when they
need your app the most. To do that, simply run a readiness probe. This is essentially a check that
Kubernetes implements to ensure that your pod is ready to send traffic to it. If it’s not ready, then
Kubernetes won’t use that pod. Easy!
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5
successThreshold: 1
www.caylent.com 23
This tells Kubernetes to send an http get request down the path every five seconds. If the
readinessprobe is successful, then Kube will mark the pod as ready and start sending traffic to it.
Another strategy for avoiding downtime is the rolling update technique that looks like this:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
If you combine these two strategies, your deployment.yaml should look something like this in the end:
strategy:
apiVersion: v1
kind: ReplicationController
metadata:
name: webserver-rc
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
template:
metadata:
labels:
app: webserver
status: serving
spec:
containers:
- image: nginx:1.9.7
name: nginx
ports:
- containerPort: 80
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5
successThreshold: 1
www.caylent.com 24
Summary
Congratulations! Over the course of this guide you’ve:
Gotten to grips with the fundamental architecture and components of Kubernetes, including:
Discovered the best tools to use to optimise your workload with the platform
Launched, scaled, updated, and minimized the downtime on your first Kubernetes
deployment
Advanced Material
An application’s underlying architecture is one of the most crucial factors to consider before running
a new application in production. The terms often discussed in this context is working out if the app
will be ‘stateless’ or ‘stateful.’ Both types have their own advantages and disadvantages.
One of the major factors for choosing Kubernetes in container orchestration is the capability to
harmonize stateful and stateless apps together. It’s possible to take advantage of exactly the same
compute, storage, and network resources across web and API servers, database, message queue,
cache, and file stores at the same time. The second reason is that Kubernetes provides consistent
and declarative provisioning across all environments (testing, staging, production, etc.).
DEFINITION
A stateless application is one which requires no persistent storage. Your cluster is only responsible for
hosting the code and other static content. That’s it. No need to change databases and there are no
writes or leftover files when pods are deleted.
Alternatively, a stateful application looks after several other parameters in a cluster. Dynamic
databases, for example, which even when the app is deleted or goes offline must persist on the disk.
When deployed in a cloud, stateful applications pose a challenge as they can be hard to scale in a
distributed, virtualized environment. Also, limiting data stores to specific locations is no simple task
in an environment where VMs themselves are stateless. Which means that stateful apps can become
problematic in the cloud. On Kubernetes this can raise several issues.
www.caylent.com 26
Ordered operations which are covered in the sequencing needed to deploy an app. First, run
the set of operations that brings up the metadata tier, then run the second set of operations
that brings up the actual data tier.
Stable network IDs instead of IP addresses (which aren’t reliable as mentioned earlier).
Persistent volumes so that pods can access data at all times even if the data moves from
one node to another or if a pod restarts.
STATEFULSET BASICS
Bypass these challenges by using StatefulSets in Kubernetes. These are essentially workload API
objects for managing stateful applications. StatefulSets acts in the same manner as a controller. The
controller initiates the required updates to move from the current state to reach the defined desired
state in a StatefulSet object.
The StatefulSet component that indicates how many container replicas need to be launched
in unique pods.
Ordered pod creation with ordinal index: For a StatefulSet with n replicas, each pod will be
appointed an integer ordinal, from 0 up through n-1, which is individual over the Set.
A stable and unique hostname and network identity for pods across restarts so when re-
spawning a pod will be treated by the cluster as a new member.
As mentioned previously, we rely on YAML files to configure Kubernetes clusters. The YAML files are
there to describe everything from the way Pods need to be configured to how load balancing is done
by the cluster. Unfortunately, setting up a new Kubernetes cluster means creating a .yaml file for that
cluster every time. For many, this means copying and pasting configurations from another cluster
and making manual adjustments to it.
This iterative process is exactly what Helm can help you with. Rather than having to configure a
YAML file manually, you can simply call a Helm Chart—a Chart is the term for Helm’s package
configuration—and let Helm do the rest for you. Helm is a custom built Kubernetes package manager
similar to NPM in NodeJS and Maven in Java.
www.caylent.com 28
Helm Charts act as the template for Kubernetes clusters configurations. The template itself can be
customized to mimic the setup parameters of the particular cluster. There is no need to make manual
adjustments to variables such as host name or provisions since Helm will take the correct variables
and make the necessary adjustments.
Helm lends itself perfectly to the deployment of cloud-native apps. It completely reduces
deployment complexity and lets developers focus more on other more important things. On top of
that, Helm Charts also introduce standardized and reusable templates to the Kubernetes.
The great news is that there are plenty of Helm Charts that you can use straight out of the box on
GitHub. Do you need to add a MongoDB database to your setup? There’s a Chart for that. Want to
deploy a testing environment for your app? You can create a Chart to simplify that process too. Even
the most complex Kubernetes setup can be packaged into its own Helm Chart for iteration.
Pre-configured Helm Charts are handy for setting up Kubernetes clusters quickly, but you can also
create your own Charts. Use Helm to help with deploying test environments and distributing pre-
release versions of your application.
But why stop at pre-release versions? Helm Charts can further be used as a delivery method for
production apps. You can, for instance, install WordPress on an empty cluster by running the correct
Helm Chart for it; everything will be automated, and you will swiftly have a WordPress site up and
running in seconds.
Since you can use values to define variables, Helm Charts can also be used for deploying the app
onto different clusters or setups. Each variable can be adjusted on the fly as the Helm Chart is
processed, allowing for greater flexibility in general.
www.caylent.com 29
In order to upgrade Charts, simply make the necessary adaptations to your values.yaml
then update your Chart version number. Now push the changes to the repository, package the Helm
Chart, and double check the Chart Repository. Run sudo helm upgrade [new chart name] and supply
the release name and the chart name you want to upgrade.
To rollback a Chart to a previous revision, you only need to provide the release name and the revision
number that you want to rollback to. Execute the command sudo helm rollback [new chart name] 1.
For a comprehensive list of tools to make working with Helm that much simpler, don’t forget to check
out our article on 15+ Useful Helm Charts Tools here.
www.caylent.com 30
References
Heptio. (2018). The State of Kubernetes 2018. Seattle, WA: Heptio. Retrieved from https://
go.heptio.com/rs/383-ENX-437/images/Heptio_StateOfK8S_R6.pdf
Verma, A., Pedrosa, L., R. Korupolu, M., Oppenheimer, D., Tune, E., & Wilkes, J. (2015). Large-Scale
Cluster Management At Google With Borg. Retrieved from https://ai.google/research/pubs/
pub43438