Focusing & Motivation Course Booklet
Focusing & Motivation Course Booklet
CONTENTS
∠ WHAT IS KUBERNETES?
∠ KUBERNETES ARCHITECTURE
Kubernetes
∠ GETTING STARTED WITH KUBERNETES
∠ EXAMPLE APPLICATION
∠ AND MORE!
WHAT IS KUBERNETES?
apiVersion: v1
Kubernetes is an open source container orchestration system. It kind: Pod
manages containerized applications across multiple hosts for spec:
containers:
deploying, monitoring, and scaling containers. Originally created
- name: nginx
by Google, in March of 2016 it was donated to the Cloud Native image: nginx
Computing Foundation (CNCF). volumeMounts:
- mountPath: /srv/www
DZON E .COM/ RE FCA RDZ
name: www-data
Kubernetes, or “k8s” or “kube” for short, allows the user to
readOnly: true
declare the desired state of an application using concepts such as - name: git-monitor
“deployments” and “services.” For example, the user may specify image: kubernetes/git-monitor
env:
that they want a deployment with three instances of a Tomcat - name: GIT_REPO
web application running. Kubernetes starts and then continuously value: http://github.com/some/repo.git
volumeMounts:
monitors containers, providing auto-restart, re-scheduling, and - mountPath: /data
replication to ensure the application stays in the desired state. name: www-data
volumes:
Kubernetes is available as a standalone installation or in a variety - name: www-data
emptyDir: {}
of distributions, such as Red Hat OpenShift, Pivotal Container
Service, CoreOS Tectonic, and Canonical Kubernetes. Not only can containers in a pod share the same network
interfaces, but they can also share volumes, as shown in the
KEY KUBERNETES CONCEPTS
Kubernetes resources can be created directly on the command
line but are usually specified using Yet Another Markup Language
(YAML). The available resources and the fields for each resource
may change with new Kubernetes versions, so it’s important to
double-check the API reference for your version to know what’s
available. It’s also important to use the correct “apiVersion” that
matches your version of Kubernetes. This Refcard uses the API
from Kubernetes 1.12, released 27 September 2018.
POD
A Pod is a group of one or more containers. Kubernetes will
schedule all containers for a pod into the same host, with the
same network namespace, so they all have the same IP address
and can access each other using localhost. Here is an example
pod definition:
1
fission.io
example. This example uses the “git-monitor” container to keep a By declaring a service, we can provide a single point of entry for
Git repository updated in /data so the “nginx” container can run all the pods in a deployment. This single point of entry (hostname
a web server to serve the repository files. and IP address) remains valid as pods come and go.
The above example declares both the Persistent Volume Claim run them; and kube-proxy, which manages networking rules so
and a Pod that uses it. This example takes advantage of the ability connections to service IP addresses are correctly routed to pods.
to place multiple YAML documents in the same file using --- as
As shown in the picture, each node can run multiple pods, and
a separator. Of course, in a real example, it would be better to
each pod can include one or more containers. The pod is purely a
separate the Persistent Volume Claim so it can easily be retained
Kubernetes concept; the kubelet configures the container engine
even if the Pod is deleted.
to place multiple containers in the same network namespace so
For more information on the available providers for Kubernetes those containers share an IP address.
Storage Classes, and for multiple examples on configuring persistent
storage, see the DZone Refcard Persistent Container Storage. GETTING STARTED WITH KUBERNETES
SETTING UP KUBERNETES
KUBERNETES ARCHITECTURE There are a variety of ways to set up, configure, and run
Kubernetes uses a client-server architecture, as seen here:
Kubernetes. It can be run in the cloud using providers such
as Amazon Elastic Container Service for Kubernetes, Google
Kubernetes Engine, Azure Kubernetes Service, Packet, Pivotal
Container Service, and others. It can be also run on-premise by
building a cluster from scratch on physical hardware or via virtual
machines. The various options are described in the Kubernetes
setup documentation, where you can find out which solution is
best for you and get step-by-step instructions. The most popular
option for building a multi-host Kubernetes cluster from scratch
is kubeadm, while the recommended way to get started and run a
DZON E .COM/ RE FCA RDZ
A Kubernetes cluster is a set of physical or virtual machines and single-node cluster for development and testing is to use Minikube.
other infrastructure resources that are used to run applications.
However you set up your cluster, you will interact with it using the
The machines that manage the cluster are called Masters, and the
standard Kubernetes command-line client program kubectl.
machines that run the containers are called Nodes.
MINIKUBE
MASTER
Minikube uses virtualization software like VirtualBox, VMware, or
The Master runs services that manage the cluster. The most
important is kube-apiserver, which is the primary service that KVM to run the cluster. Once Minikube is installed, you can use
clients and nodes use to query and modify the resources running the minikube command-line to start a cluster by running the
in the cluster. The API server is assisted by: etcd, a distributed key- following command:
minikube logs
NODE
minikube ssh
A Node is a physical or virtual machine with the necessary
services to run containers. A Kubernetes cluster should have as
You can also open a dashboard view in the browser to see and
many nodes as necessary for all the required pods. Each node
change what is going on in the cluster.
has two Kubernetes services: kubelet, which receives commands
to run containers and uses the container engine (e.g. Docker) to minikube dashboard
kubectl create -f nginx. Create the resources specified The file nginx-deployment.yaml contains the Deployment
yaml in the YAML file. If any speci- definition shown above.
fied resources exist, an error is
returned.
SCALE APPLICATIONS
Deployments can be scaled up and down:
kubectl delete -f nginx. Delete the resources spec-
yml ified in the YAML file. If any kubectl scale --replicas=3 deploy/nginx
resources do not exist, they deployment.extensions/nginx scaled
are ignored.
The Kubernetes controller will then work with the scheduler to
kubectl get pods List all pods in the “default” create or delete pods as needed to achieve the requested number.
namespace. See below for This is reflected in the deployment:
more information on name-
spaces. kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE
kubectl describe pod Show metadata for the “nginx” AGE
nginx pod. The name must match nginx 3 3 3 3
exactly. 3m
kubectl --help You can verify there are three pods by running:
Show the complete list of
available commands. kubectl get po
NAME READY STATUS RESTARTS
AGE
RUN YOUR FIRST CONTAINER nginx-65899c769f-c46xx 1/1 Running 0
Most of the time when using kubectl, we create YAML resource 38s
nginx-65899c769f-j484j 1/1 Running 0
files, so we can configure how we want our application to run.
38s
However, we can create a simple Deployment using kubectl nginx-65899c769f-kp5c7 1/1 Running 0
without using a YAML file: 3m
Note that the original Pod continued to run while two more were Note that we use a PersistentVolumeClaim so we get persistent
added. Of course, we would also want to create a Service to assist data; we’ll assume this has been created already since we want it
in load balancing across these instances; see below for a more to stay around for a long time even as we update the application.
complete example.
Even though there will only be one database instance, we will
DELETE APPLICATIONS create a Service so the IP address will stay the same even if the
Once you are done using the application, you can destroy it with PostgreSQL pod is replaced.
the delete command. kind: Service
apiVersion: v1
kubectl delete deployment nginx
metadata:
deployment.extensions "nginx" deleted
name: postgres-service
spec:
Because Kubernetes monitors pods to achieve the desired selector:
number of replicas, we must delete the Deployment to remove app: postgresql
ports:
the application. Simply stopping the container or deleting the
- protocol: TCP
pod will just cause Kubernetes to create another pod. port: 5432
This example uses a “sidecar” container, kubernetes/git-monitor, Once we’ve created the namespace, we can create resources in it
to keep our application up to date based on a Git repository. using the --namespace (-n) flag, or by specifying the namespace
The sidecar populates a volume which is shared with the in the resource’s metadata:
Node.js container.
apiVersion: v1
kind: Pod
Finally, we create the service that provides the user entrypoint for
metadata:
our application: name: webserver
namespace: development
kind: Service
…
apiVersion: v1
metadata:
name: nodejs-service By using separate namespaces, we can have many pods called
spec: webserver and not have to worry about name collisions.
selector:
app: nodejs
ports: ACCESS CONTROL
- protocol: TCP
Kubernetes supports Role Based Access Control (RBAC).
port: 3000
type: LoadBalancer
Here’s an example that limits developers to read-only access for
Providing services with external IPs so they can be visible from pods in production. First, we create a ClusterRole, a common
outside the cluster is a complex topic because it depends on your set of permissions we can apply to any namespace:
cluster’s environment ( e.g. cloud, virtual machine, or bare metal). kind: ClusterRole
This example uses a LoadBalancer service, which requires some apiVersion: rbac.authorization.k8s.io/v1
external load balancer such as an Amazon Elastic Load Balancer metadata:
name: pod-read-only
DZON E .COM/ RE FCA RDZ
kind: RoleBinding
To see pods in kube-system, we can run:
apiVersion: rbac.authorization.k8s.io/v1
metadata:
$ kubectl get po -n kube-system
name: read-only
NAME READY STATUS
namespace: production
RESTARTS AGE
subjects:
…
- kind: Group
kube-apiserver-minikube 1/1 Running 0
name: developers
17m
apiGroup: rbac.authorization.k8s.io
…
roleRef:
kind: ClusterRole
name: pod-read-only
RESOURCE ISOLATION apiGroup: rbac.authorization.k8s.io
A new namespace can be created from a YAML resource definition:
Alternatively, we can use a ClusterRoleBinding to apply a role
apiVersion: v1
kind: Namespace to a user or group in all namespaces.
metadata:
name: development RESOURCE QUOTAS
labels: By default, pods have unlimited resources. We can apply a quota
name: development
to a namespace:
apiVersion: v1 Note that we can request fractions of a CPU and use varying units
kind: ResourceQuota for memory.
metadata:
name: compute-resources
namespace: sandbox
spec:
hard:
cpu: "5"
memory: 10Gi
apiVersion: v1
kind: Pod
metadata:
name: webserver
namespace: sandbox
spec:
containers:
- image: nginx
name: nginx
resources:
limits:
memory: "128Mi"
cpu: "500m"
DZON E .COM/ RE FCA RDZ
DZone, Inc.
DZone communities deliver over 6 million pages each 150 Preston Executive Dr. Cary, NC 27513
month to more than 3.3 million software developers, 888.678.0399 919.678.0300
architects and decision makers. DZone offers something
Copyright © 2018 DZone, Inc. All rights reserved. No part of this publication
for everyone, including news, tutorials, cheat sheets,
may be reproduced, stored in a retrieval system, or transmitted, in any form
research guides, feature articles, source code and more. or by means electronic, mechanical, photocopying, or otherwise, without
"DZone is a developer’s dream," says PC Magazine. prior written permission of the publisher.
CONTENTS
Advanced
∙ About Kubernetes
∙ Basics
∙ Kubernetes Constructs
∙ Extending Kubernetes
Kubernetes
∙ Kubectl
∙ Managing Nodes
∙ Test Plan
∙ Security and Troubleshooting
∙ Feeding Results to Other Things
∙ Etcd
ABOUT KUBERNETES This research points out how quickly Kubernetes is being adopted
Kubernetes is a distributed cluster technology that manages a and what a strategic role it plays in the global digital landscape.
container-based system in a declarative manner using an API.
Kubernetes differs from the orchestration offered by configuration
Kubernetes is an open-source project governed by the Cloud Native
management solutions in that it provides a declarative API that
Computing Foundation (CNCF) and counts all the largest cloud
collects, stores, and processes events in an eventually consistent
providers and software vendors as its contributors along with a long
manner. A few traits of Kubernetes include:
list of community supporters.
• Abstraction – Kubernetes abstracts the application
There are currently many learning resources to get started with the orchestration from the infrastructure resource and as-a-
fundamentals of Kubernetes, but there is less information on how to service automation. This allows organizations to focus on
manage Kubernetes infrastructure on an ongoing basis. This Refcard the APIs of Kubernetes to manage an application at scale
aims to deliver quickly accessible information for operators using any in a highly available manner instead of the underlying
Kubernetes product. infrastructure resources.
Last year, Gartner predicted that by 2022, more than 75% of global
organizations would be running containerized applications in
production, up from less than 30% at that time. The CNCF 2019
survey, published roughly one year later in 2020, says that more than
that are already in production.
1
Technology
Training
Services
& Support
Explore the
D2iQ Kubernetes Platform
Simplify your
Kubernetes Journey
REFCARD | ADVANCED KUBERNETES
SCHEDULER
Kubernetes relies on a sophisticated algorithm to schedule
Kubernetes objects. The scheduling takes into account filters such
as resources, topology, and volume, and then uses prioritization
settings such as affinity rules to provision pods on particular
Kubernetes worker nodes.
CONTROL PLANE, NODES, AND PERSISTENT STORAGE For more information, see the Kubernetes documentation: https://
Kubernetes’ basic architecture requires a number of components. kubernetes.io/docs/reference/command-line-tools-reference/
kube-scheduler/
API SERVER
The main way to communicate with Kubernetes clusters is through KUBE-PROXY
the Kubernetes API Server. A user, and other Kubernetes components, Each Kubernetes cluster worker node has a network proxy for
sends declarative events to the Kubernetes API Server through connectivity.
HTTPS (port 443). The API Server then processes events and updates
the Kubernetes persistent data store — e.g., etcd. The API Server also For more information, see the Kubernetes documentation: https://
WEB UI (DASHBOARD) Container Runtime CRI is a plugin API that enables Kubernetes to
Kubernetes has an official GUI called Dashboard. That GUI is distinct Interface (CRI) support other container runtimes beyond Docker
and Containerd.
from vendor GUIs that have been designed for specific Kubernetes
derivative products. Note that the Kubernetes Dashboard release Container Network CNI gives users a choice of network overlay that
Interface (CNI) can be used with Kubernetes to add SDN features.
version does not necessarily match the Kubernetes release version.
Container Storage CSI empowers users to support different storage
KUBERNETES CONSTRUCTS Interface (CSI) systems through a driver model.
Namespaces Kubernetes includes a means to segment a single with Kubernetes. A full list of kubectl commands can be found at the
physical cluster into separate logical clusters reference documentation: https://kubernetes.io/docs/reference/
using namespacing.
generated/kubectl/kubectl-commands
Pods Whatever the runtime, Kubernetes fundamentally
manages a logical grouping of one or more MAKING LIFE EASIER
containers called a pod.
Finding Kubernetes command short name:
StatefulSets A Kubernetes controller for managing workloads
that require proper management of state. $ kubectl describe
$ kubectl drain NODE Removes pods from node via graceful To properly evict a node, use:
termination for maintenance
1. Cordon node: $ kubectl cordon $NODENAME
$ kubectl drain NODE Find the names of the objects that
--dry-run=client will be removed 2. Drain node: $ kubectl drain $NODENAME
• Respects PodDisruptionBudgets
$ kubectl drain NODE Remove pods even if they are not
--force=true managed by controller 3. Uncord node: $ kubectl uncordon $NODENAME
TEST WORKLOADS You can also remove pods from the environment.
5. Test specific Kubernetes workloads (using Helm charts) Pull an image onto desktop to docker pull <image>
• Deploy Postgres
• Deploy RabbitMQ Also check that the secret information is correct.
TROUBLESHOOTING COMMAND
SECURITY AND TROUBLESHOOTING
General issue checking $ kubectl describe deployments
TROUBLESHOOTING WITH KUBECTL
The Kubernetes command line kubectl provides many of the Rolling back a $ kubectl rollout undo [Deployment
deployment Name]
resources needed in order to debug clusters.
COMMAND EXAMPLE
Check your $ kubectl auth $ kubectl auth can-i create GENERAL DEBUGGING
permissions can-i deployments --namespace dev
Watch the stdout of a container:
Check $ kubectl auth $ kubectl auth can-i create
permissions can-i [Options] deployments --namespace dev $ kubectl attach bash-rand-77d55b86c7-bntxs
of other users --user=chris
PENDING AND WAITING PODS $ kubectl run busybox --rm -i --tty --restart=Never
--image busybox -- /bin/sh
Pending —> Check Resources
TROUBLESHOOTING COMMAND Copy files into and out of containers (note that this requires a tar
General issue checking $ kubectl describe pod <name of binary in the container):
pending pod>
$ kubectl cp default/bash-rand-77d55b86c7-bntxs:/
Check if pod is using $ kubectl top pod root/rand .
too many resources tar: Removing leading `/' from member names
Increase the verbosity of kubectl output (99 is the most TROUBLESHOOTING WITH JQ
verbose — “get everything”): Kubectl provides an option to output results in JSON format, which
allows for additional parsing and filtering. This can be very beneficial
$ kubectl -v 99 get nodes
when writing scripts or monitoring the state of resources in a
I1211 11:10:28.611959 24842 loader.go:359]
Kubernetes cluster. A popular open-source utility called “jq” makes it
Config loaded from file /home/kensey/bootkube/
easy to parse JSON from the command line.
cluster/auth/kubeconfig
I1211 11:10:28.612482 24842 loader.go:359]
Instead of writing code to parse the JSON objects, the output can
Config loaded from file /home/kensey/bootkube/
be piped into the jq utility to filter out resources that meet certain
cluster/auth/kubeconfig
I1211 11:10:28.614383 24842 loader.go:359] criteria. For example, kubectl can be used to print out all pods in
Config loaded from file /home/kensey/bootkube/ json format, but jq can add value by parsing out only pods with a
cluster/auth/kubeconfig specific start date or container image.
I1211 11:10:28.617867 24842 loader.go:359]
Config loaded from file /home/kensey/bootkube/ Basic usage:
cluster/auth/kubeconfig
[some JSON content] | jq [flags] [filter expression]
I1211 11:10:28.629567 24842 round_trippers.
go:405] GET https://192.168.122.138:6443/api/v1/
nodes?limit=500 200 OK in 11 milliseconds Use the kubectl json format and pipe to the jq utility using a
I1211 11:10:28.630279 24842 get.go:558] no kind simple dot filter to pretty-print the output:
is registered for the type v1beta1.Table in scheme
$ kubectl get nodes -o json | jq '.'
"k8s.io/kubernetes/pkg/api/legacyscheme/scheme.
go:29"
NAME STATUS ROLES AGE VERSION Use the hierarchy to filter the input so only node labels are displayed:
FEEDING RESULTS TO OTHER THINGS Create an etcd backup $ ETCDCTL snapshot save snapshot.db
Using different Kubernetes clusters reduces the potential for • Do not expose the Dashboard to the Internet
vulnerabilities to affect all systems. Also, large clusters with • Ensure the Dashboard ServiceAccount is not open
unrelated services become harder to upgrade, which violates the and accessible to users
first item in this checklist. • Configure the login page and enable RBAC
• Use a private container registry and tagged container • Use jq to process JSON output from the command line: https://
images, keeping tagged images immutable. stedolan.github.io/jq/
CONTENTS
Monitoring
Challenges
∠ Prometheus
Kubernetes ∠ Sensu
∠ Conclusion
∠ References
with more flexibility than any container orchestration platform has ever initiates more than 2 billion container deployments weekly. At one point, it
provided before. Kubernetes, affectionately referred to as Kube or K8s for ran everything through its internally developed platform: Borg.
The fault-tolerant and scalable platform can be deployed within almost with a vast number of machines. Much of the education garnered over the
any infrastructure. Kubernetes will integrate with anything, from public years from developing Borg was distilled into the Kubernetes technology
clouds (Google Cloud, Microsoft Azure, Amazon Web Services, and so on) we see today.
1
MONITORING KUBERNETES
dation (CNCF). The CNCF's purpose is to build sustainable ecosystems As well as being truly container runtime agnostic, Kubernetes' features
while fostering a community around a network of high-quality projects include everything necessary to centrally automate, control, and scale
that orchestrate containers within either a monolithic or a microservices containerized applications. Here are the highlights:
architecture. Kubernetes is one of the highest velocity CNCF projects in the
• Enterprise-scale container deployments
history of open source.
• Rollout management
HOW KUBERNETES WORKS • Built-in service discovery
Kubernetes, at its fundamental level, is a system for coordinating contain- • Self-healing
erized applications across a cluster of machines. It's designed to allow • Autoscaling
developers to fully manage the lifecycle of containerized applications and • Persistent storage management
services using features that deliver predictability, scalability, and high • High-availability features
availability. Simply put, K8s helps developers pilot container clusters, • Interconnected clusters
hence the name, which means helmsman (or pilot) in Greek. It is the most • Resource bin packing
popular option available today because it provides an abstraction to make • Secrets
a cluster of machines act identically to one big machine — a vital feature
when running large-scale environments. These highlighted features are why Kubernetes has become widespread
for leveraging different application architectures from monolithic or
By default, Kubernetes optimizes the use of Docker to run images and batch-driven applications to highly distributed microservice applications.
manage containers. However, K8s can also use other container engines,
such as rkt from CoreOS, for example. Monitoring Kubernetes: The Challenges
Traditionally, servers were treated like pets, with "all hands on deck" being
called to fix one that went down. With containerization, servers are num-
bered as cattle in a herd; when one goes down, it’s simply replaced. Since
its release in 2014, Kubernetes has revolutionized container technology
and become a critical tool for quickly launching applications at scale.
• The capacity and resource utilization of clusters including: as well as the environment.
− Nodes: To ensure the state of all K8s nodes, track the • Container probes: These are diagnostics which actively monitor
CPU, memory, and disk utilization. the health of a container. Each probe results in either one of
three outcomes:
− Deployments/pods: Confirm all implemented pods in a
deployment are healthy.
• Success: The container passed the diagnostic probe.
− Containers: Watch the CPU and memory consumption
• Failure: If the probe determines that a container is no
in relation to configured limits.
longer healthy, the probe will restart it.
• Applications: Track the performance and availability of all • Unknown: The diagnostic failed, so no action is necessary.
applications running in the cluster through request rates,
throughput, error rate, and so on. • Kubelet: The kubelet is a primary node agent that runs on every
node in the cluster and ensures the containers are working. It's
• End-user experience: Supervise mobile application and
also the mechanism through which the master communicates
browser performance to respond to insights and errors.
with the nodes. The kubelet exposes many individual container
Ensure customer satisfaction by following and improving
usage metrics straight from cAdvisor.
load time and availability.
• Supporting infrastructure: As mentioned above, it's vital • cAdvisor: This Kubernetes component is an open-source container
to track Kubernetes clusters running across different plat- resource usage and performance analysis agent that is integrated
forms and multiple vendors. with the kubelet. It is purpose-built for containers, identifying all
containers running in a machine to collect statistics about memory,
• Monitoring the minutiae of operational complexity: All the
network usage, filesystem, and CPU. cAdvisor operates per node,
Kubernetes core elements (i.e., the kubelet, API server, Kube
and also analyzes overall machine usage by assessing the "root"
controller manager, and Kube scheduler) involve numerous
container on the machine. It is simple to use, but also limited:
flags that determine how the cluster operates and performs. The
default values may be fine for smaller clusters at the beginning, • cAdvisor only covers basic resource utilization; it doesn't
but as deployments scale up, it's important to make and monitor have the capacity to analyze how applications are actual-
adjustments. It also becomes crucial to keep track of Kubernetes ly performing.
details such as labels and annotations.
• cAdvisor also doesn't provide any trending, long-term
storage or analysis facilities.
Ironically, monitoring solutions can be responsible for bringing down
clusters by pulling extreme amounts of data from the Kubernetes APIs.
• Kube-state-metrics: Another add-on service that listens to the
Therefore, it is a good idea to determine a key baseline for monitoring, and
Kubernetes API and translates information about Kubernetes
dial it up when necessary to further diagnose issues that require addition-
constructs into metrics. In general, the model interrogates the
al attention. Kubernetes API server and recovers data about the states of
Kubernetes objects.
If a high level of monitoring is justified, it may be mindful to deploy more
masters and nodes (which you'll need to do for HA anyways). Another • Metrics server: The metrics server collects metrics from the
practical technique, especially when it comes to large-scale implementa- Summary API, exposed by the kubelet on each node. It is a clus-
tions, is to implement a lone cluster for storing Kubernetes events. This ter-wide aggregator of resource usage data. As of Kubernetes v1.8,
way the performance of the main instances won't be affected by any it's deployed by default in clusters created by kube-up.sh script as
volatility in event creation and event retrieval for monitoring purposes. a deployment object.
The path of the core monitoring pipeline is as follows: environment. When first installing Prometheus, it's important to define a
few parameters that allow the system to scrape and collect data, starting
1. As cAdvisor is installed by default on all cluster nodes, it collects
with the scrape interval. Prometheus can be configured to provide either
data metrics about those containers and nodes.
real-time monitoring or interval-based analysis of Kubernetes nodes.
2. The kubelet exposes these metrics through the kubelet APIs. (The
default is a one-minute resolution.) By assigning external labels and attaching them to time-series alerts, it's
3. The metrics server identifies all available nodes and requests the also possible to use Prometheus as an alert system for node failures and oth-
kubelet API to report all container and node resource usage. er issues. The rest of the system is defined through scrape configurations.
4. The metrics server then exposes these metrics through the Kuber-
netes aggregation API. THE PROMETHEUS OPERATOR
The difference between controllers and operators often confuses many
Fundamentally, this pipeline outlines a sense of the initial steps necessary users. To deconstruct the two: a Kubernetes Operator is the name of a
to incorporate reasonable monitoring for Kubernetes environments. pattern that involves a Controller adding new objects to the Kubernetes
While we still don't have detailed application monitoring through these API to configure and manage applications such as Prometheus. Put simply,
rudimentary components, we can at least observe and monitor the under- an operator is a domain-specific controller.
lying hosts and Kubernetes nodes, and receive some metrics about any
Kubernetes abstractions. "The Prometheus Operator serves to make running Prometheus on top
of Kubernetes as easy as possible, while preserving Kubernetes-native
Customarily, the average K8s cluster administrator is interested in configuration options." ("Prometheus Operator", 2019)
monitoring from a holistic point of view, while application developers
who build services tend to lean towards monitoring from an app-centric Using the Prometheus Operator allows for easy monitoring of K8s services
point of view. Recently, however, these lines have blurred. Regardless of and deployments. It is viable to run Prometheus using a predetermined
the perspective required, all teams want to minimize the amount of input configuration .yml file, but this can later be automated according to how
required to monitor their systems and manage data collection. We'll now Kubernetes is set up. The Prometheus Operator creates, configures, and
take a look at two viable monitoring options that bring in full visualization manages all monitoring instances atop Kubernetes. When deploying a
and system-level metrics, allowing developers to track, troubleshoot, and new version of an app, K8s creates a new pod (container). Once the pod is
receive alerts about the most crucial parts of K8s clusters. ready, Kube destroys the old one. Prometheus is on constant watch, track-
ing the API; when it detects any differentials, it creates a new Prometheus
Kubernetes itself). You can use queries to manipulate data and generate insights relevant to
your situation. You can also use PromQL to create graphs, visualize data-
Prometheus does more than simply monitor predefined metrics. It is capa- sets, create tables, and generate alerts based on specific parameters.
ble of implementing dimensional data models for truly in-depth analysis,
creating relational insights based on multiple metrics. In other words, devel- Prometheus' web-based console lets you manage all features and
opers and administrators can get a clear view of Ops from different angles. tools available inside Prometheus. You can use regular expressions
and advanced PromQL queries for creating datasets and alerts, and it's
HOW PROMETHEUS WORKS accessible from the outside (when the cloud infrastructure is set up to
Prometheus is implemented as an additional layer into your Kubernetes enable remote access).
BENEFITS OF THE PROMETHEUS APPROACH various sources, Sensu leverages a hybrid of push/pull with its publish/
Simplicity and flexibility are two of Prometheus' strongest suits. When us- subscribe (Pub/Sub) model — because Sensu uses a message bus for com-
ing Prometheus as a monitoring framework on Kubernetes, it's possible to munication, you can publish messages to a specific topic and consumers
implement a highly dimensional data model for monitoring. Prometheus can subscribe to one or more topics. This difference in approach makes
is also available as a Docker image, which means it is easy to set up the Sensu equally as robust, but for different reasons. Both Prometheus and
monitoring framework as part of a containerized cluster. The tool also Sensu allow you to collect the same metrics, although Sensu's service
integrates well with Grafana for increased monitoring visualization. health checking abilities (like the ability to monitor external resources
with proxy requests and entities) can help fill gaps in a purely teleme-
When used with Kubernetes, Prometheus can also collect node, pods,
try-focused approach. A better way forward, therefore, would be to run
and services metrics using the native service discovery configurations of
them in unison.
Kubernetes. There is no need to jump through hoops just to get a com-
prehensive monitoring system up and running; users can jump straight to HOW SENSU WORKS
defining expressions and creating alerts. Sensu uses native collection plugins to collect data for analysis from a
wide range of mainstream utilities (such as StatsD libraries, Prometheus
There is also Prometheus' simple and fluent nature. The scraping capability exporters, Nagios plugins, SNMP traps, CollectD metrics, and so on). It
of Prometheus can be easily integrated with Kube and other compatible differentiates itself from other tools, including Prometheus, by supporting
tools, including Docker and StatsD. In more complex environments, Pro- multi-cloud environments out of the box and providing high availability
metheus works well with kubernetes-service-endpoints and API servers. Even immediately from when users begin configuring the framework.
better, you can use the web GUI to configure alerts and graphs on the fly.
Sensu provides building blocks such as event filters, mutators, and
As with any pure telemetry monitoring solution, however, you miss handlers for operators to use to model workflows and automate them.
out on context with Prometheus' constrained data model. The fact that Via this method, data can be consolidated from external monitoring tools
Prometheus' data collection model defaults to time series presents a with existing operational ones to establish an event-based approach to
double-edged sword. On one hand, you can easily standardize collecting monitoring.(Sensu Go)
data in a particular way, which helps simplify things; on the other, you've
now limited yourself to a constrained data model that might be missing
some vital context.
The pull-based model also gives you limited granularity, as exporters only
provide summarized data that's only scraped periodically. This model
also requires that you punch holes in the firewalls of traditional networks,
and therefore is not suited for monitoring legacy or multi-generational
The Sensu Go open-source project is governed by an MIT license, with
infrastructure. Because Prometheus uses a set of discovery libraries to stay
source code that users are able to download and compile from github.
up to date with the platforms it monitors (like Kube), a degree of lag is in-
com/sensu/sensu-go. Supported (built and tested) packages are available
troduced as resources come and go, which in turn is exaggerated by metric
for download on the Sensu website, and are free to use up to 1,000 enti-
scraping intervals. Finally, Prometheus collection is unauthenticated and
ties. Organizations that require monitoring at scale with enterprise-level
unencrypted — anything that has access to the network can observe your
integrations will need an enterprise license.
telemetry data. (This includes metrics labels — you add labels for context,
but then that context would be out in the open.)
CORE COMPONENTS
For Kubernetes containers, Sensu optimizes its auto-discovery capabilities.
Sensu It's easy to configure monitoring checks and collectors for the Kubernetes
OVERVIEW OF SENSU components, and you can do the same in other containerized environments,
Sensu is a monitoring solution designed for multi-cloud and containerized including Docker. It's also easy to configure a handful of checks for monitor-
infrastructure. One of the benefits that Sensu brings to the table is a com- ing all Kubernetes components and the applications running on them.
posable monitoring event pipeline with an event-driven architecture. The
Sensu agent is a cross-platform event producer, enabling you to execute Similar to Prometheus, Sensu also comes with native support for integration
service checks to monitor system and service health as well as collect and and plugins. It works with logging tools and plays well with Prometheus
analyze unique metrics. Custom workflows enable you to go beyond alert itself. It's possible to use the two frameworks running alongside each other
or incident management (e.g., auto remediation), and monitoring APIs, and have them work together in processing different datasets. For example,
client libraries, and plugins written in any scripting or programming lan- Sensu can collect StatsD metrics and write them to Prometheus.
guage (including Nagios). Such customization offers a range of capabilities.
Sensu can also adapt to its Kubernetes environment. Let's say Sensu is
While Prometheus uses pull architecture for actively scraping data from deployed inside a container, and the decision is made to move the entire
and Sensu will still work in a different environment. With its auto-native name: sensu-prometheus-collector
namespace: default
support, the Sensu agent will run at the new location and be discovered
labels: {}
by the Sensu system. You don't need another integration — it has its own
annotations: {}
auto-discovery mechanism.
spec:
url: https://github.com/sensu/sensu-prometheus-
KUBERNETES MONITORING WITH SENSU
collector/releases/download/1.1.5/sensu-prometheus-
Sensu is a highly scalable monitoring framework for Kubernetes, one that
collector_1.1.5_linux_amd64.tar.gz
will grow alongside deployed apps and cloud environments and provide sha512:
detailed functional monitoring. There is no upper limit to using Sensu, even b183e1262f13f427f8846758b339844812d6a802d5b98ef
in terms of the level of complexity needed for monitoring requirements. 28ba7959290aefdd4a62060bee867a3faffc9bad1427997
c4b50a69e1680b1cda205062b8d8f3be9
Sensu is a well-established, extensible solution with an active community filters:
of expert operators. - entity.system.os == linux
- entity.system.arch == amd64
• JSON spec:
command: sensu-prometheus-collector -exporter-url
The Sensu Prometheus Collector combines the workflow automation http://localhost:8080/metrics
capabilities of Sensu and the power of Prometheus data scraping into a interval: 10
output_metric_format: influxdb_line
Prometheus metric poller. You can develop your own preferences for your
output_metric_handlers:
instrumented code and for when you receive alerts. Sensu will also deliver
- influxdb
the collected metrics to the external time-series database of your choice, publish: true
such as InfluxDB, Graphite, or Prometheus. runtime_assets:
- sensu-prometheus-collector
INSTALL THE SENSU PROMETHEUS COLLECTOR subscriptions:
You can discover, download, and share assets using Bonsai, the Sensu - metrics
asset index. To use the Sensu Prometheus Collector, select the Download
button on the asset page in Bonsai to download the asset definition for sensuctl create -f example-app-metrics.yml
your Sensu backend platform and architecture. Asset definitions tell Sensu
For the most up-to-date asset definition and usage examples, please check
how to download and verify the asset when required by a check, filter,
Bonsai: bonsai.sensu.io/assets/sensu/sensu-prometheus-collector
mutator, or handler.
BENEFITS
Once you've downloaded the asset definition, you can register the asset
The advantages of using Sensu and Prometheus in parallel through the
with Sensu using sensuctl, the command line tool for managing resources
Sensu Prometheus Collector include:
within Sensu, and create your own monitoring workflows.
• As well as monitoring the health of your Kubernetes clusters, the
For example, here's the Prometheus collector asset definition and associ- two will dynamically monitor and collect service health checks
ated sensuctl command for Linux: and metrics for the surrounding infrastructure.
• The ability to achieve more granularity and flexibility to cover the Given how software-dependent we are today, availability is crucial for a
finer aspects of data scraping and analysis with more details. business' survival, and downtime can prove expensive as well as damag-
ing to an organization's reputation. Maintaining visibility into our systems
• Sensu supports and uses standard cryptography for communi-
is therefore essential to overcoming these challenges, and monitoring
cation. Sensu's model allows for a single agent to collect and
system infrastructure and applications has become integral to business
transmit data securely without having to compromise firewalls.
operations. In order to fully reap the rewards that Kubernetes has to offer,
• Being able to easily manage and configure your monitoring setup.
implementing a unified, future-proof monitoring solution (i.e., one that
• The capability to monitor your entire infrastructure from legacy to bridges the gap between past, current, and future technologies) is critical.
cloud-based infrastructure.
References
Conclusion • Prometheus Operator. (2019). Retrieved from coreos.com/opera-
Essentially, leveraging Sensu can allow you to monitor your entire infra-
tors/prometheus/docs/latest/user-guides/getting-started.html
structure — from Kubernetes to bare metal — with increased customiza-
tion and context. Prometheus' robust model is focused on scraping flat • Sensu Go. (n.d.). Sensu Go 5.5. Retrieved from docs.sensu.io/
telemetry data (at truly comprehensive levels), while Sensu offers visibility sensu-go/latest/
into your entire stack, working with industry standard technologies and
formats (like Nagios and StatsD). You can use Sensu to complement
Prometheus' extensive features for additional context from events, health
checks (i.e., not just metrics), more comprehensive capabilities around
processing monitoring data as a workflow, and a wholly secure solution.
Written by Stefan Thorpe, Head of DevOps and Security at Cherre and CTO at Caylent
Stefan is an IT professional with 20+ years management and hands-on experience providing technical and DevOps
solutions to support strategic business objectives.
Devada, Inc.
600 Park Offices Drive
Suite 150
Research Triangle Park, NC
DZone communities deliver over 6 million pages each month 888.678.0399 919.678.0300
to more than 3.3 million software developers, architects, and
decision makers. DZone offers something for everyone, including Copyright © 2019 DZone, Inc. All rights reserved. No part of this pub-
news, tutorials, cheat sheets, research guides, feature articles, lication may be reproduced, stored in a retrieval system, or transmit-
source code, and more. "DZone is a developer’s dream," says PC ted, in any form or by means electronic, mechanical, photocopying,
Magazine. or otherwise, without prior written permission of the publisher.
CONTENTS
∠ Microservices Overview
∠ Orchestration Overview
∠ Key Characteristics of
Microservices on
Microservices With Orchestration
Kubernetes
in an Orchestrated World
∠ Monitoring in a Microservices/
Kubernetes World
Microservices is not a new architecture style. Back in 2014, Martin Having multiple services instead of just one brings increased levels
Fowler and James Lewis published a post defining microservices. of complexity. It adds such questions as: Which service should we
One year later, Kubernetes (the most popular container orchestration update first when it's time to update, especially when there are several
at the moment) was open-sourced by Google. In this Refcard, you services to update simultaneously? There are so many pieces now that
will find an introduction to these technologies, as well as the automating the deployment of each service becomes a requirement.
essential characteristics and design principles of microservices in If there are issues in production, which service is a problem or is
an orchestration world. This Refcard will also dive into monitoring causing a problem? Good observability is needed to spot the issue is
and logging, which are essential tools in distributed systems like an imminent need when working with microservices. It seems that
microservice architectures. with monolith systems, things were simpler. So, is it worth having
the additional complexity of decoupling a monolith into several
Microservices Overview microservices? Sometimes it is, especially when working with systems
Microservices is an architectural style where every piece of a system utilizing Kubernetes in the containers world.
lives separately, as an individual service — rather than condensing all
An advantage of having separate services is that it's possible to
the different parts of a system into a single, larger service, as happens
with traditional monolithic architectures. It is useful to keep in mind work with each microservice independently. Applying an update or
that microservices should not mean as small as possible, but rather patch, or even replacing an entire piece, means not having to notify
should be taken to mean as small as necessary. or change any other parts in the system. This independence provides
flexibility for developing each microservice in the appropriate
1456&78595:;<4= 1456&78;>?5@A?B;>A@= programming language — even different programming languages,
different frameworks, different database engines, or different
!"#$%&'$( !"#$%&'$( operative systems. As long as there's always a way of communicating
between services (like with REST, HTTP, or gRPC), all technology
)"#(*+%&+,%,-#. )"#(*+%&+,%,-#.
decisions will be reasonable.
+/'+0#*% +/'+0#*%
A good approach when working with different technologies and
,(&1'"23+' ,(&1'"23+' platforms is to use containers. Containers simplify the distribution
of a microservice, since the application and all its dependencies are
1
MICROSERVICES ON KUBERNETES
packaged and ready to be used anywhere, as long as the container the state by orchestrating the storage, too; it doesn't matter if it's local
engine is running. But then, isn't it counterproductive having to or external (like a cloud service or a network storage system).
manually instantiate a container in a server cluster every time a
deployment happen? Yes — and that's where orchestration for Key Characteristics of Microservices With
containers comes into play. Orchestration
Based on all the features of Kubernetes, let's take a look at the key
Orchestration Overview characteristics that a microservice should have to integrate better in
Having to supervise the state of each different container manually, an orchestration ecosystem.
then restart it manually every time it goes down, won't scale as the
volume of containers increases. A number of questions arise: Where IDEMPOTENCE
Every time you deploy a microservice, its behavior should always
do we deploy the container? Are there enough resources available
be the same and should always be expected. One of the main
in the cluster of servers for our needs? An orchestrator assumes
reasons to work with containers in microservices is that you can
the burden of working with multiple containers in a microservices
port the application easily from one environment to another. In
architecture (or any containerized applications) in an automated
Kubernetes, the same portability principle applies — you can use
fashion. Other benefits come into play — like deploying with
the same commands to deploy in different environments. When a
minimum or zero downtime, the capability to automatically scale in
pod terminates due to a memory leak and then starts again, the pod
or scale out the containers or the hosts, or even having distinct ways
should continue working because a new deployment will happen.
to separate the work of developers and operations. (More about these
Think of it as if a VM restart has happened, but much faster, with less
topics later.)
overall impact — and it's done automatically by the orchestrator.
to group pods that share the same group of labels. A consumer of a so clone it with git. From now on, we'll be referencing this application.
microservice won't have to keep a list of the pods to consume them; You can skip the step for building the application and deploy it. When
only having the service endpoint will be enough. Kubernetes will take needed, I'll reference portions of code or commands to run.
care of registering new pods and removing those that can't provide a
Let's start by deploying the application in a Kubernetes cluster.
service. Therefore, a microservice only needs to have the endpoints of
You could use the latest version of Docker for Windows or Mac,
its dependencies, ideally as a configuration variable.
or Minikube. Once the cluster is ready to use, run the following
commands:
CONFIGUR ABLE
Remember that one of the benefits of using containers is portability.
kubectl apply -f ./release/kubernetes-manifests.yaml
One way to make a microservice portable, while building the watch kubectl get pods
container only once, is to make use of configuration variables. In
Kubernetes, this is called a configmap. Configmaps allow for Wait a few minutes until all pods are in the "running" state. Then, to get
decoupling of the configuration from the application. The same the IP address of the frontend service, run the following command:
container image with Kubernetes YAML files can be used in different kubectl get service/frontend-external
environments. Kubernetes translates these configurations into
environment variables or as files inside a container. A microservice Visit the application on your browser to confirm that it's working. It's
should always make its configuration pluggable. that easy.
Lastly, more and more recently, there's been a lot of noise for the
Why Orchestration Matters in Microservices
service meshes in the microservices and Kubernetes ecosystem. The
It's true that microservices add more complexity, but when
term "service mesh," per Istio's documentation, is "used to describe
working with platforms like Kubernetes, much of that complexity
the network of microservices that make up such applications and
is orchestrated away. The state of the applications is defined
the interactions between them." In other words, it defines how
through YAML files. When doing a deployment of a microservice into
microservices can communicate. Istio is a popular open-source
Kubernetes, the operations team has to determine how the service
service mesh platform that runs on top of Kubernetes. It provides
will scale horizontally (at the pod or host level), or how to roll out
a uniform way to operate, control, and secure microservices.
changes in small portions (like 5 percent of the traffic first, then the
Kubernetes and Istio apply some design principles that the industry
rest). Kubernetes will watch the state all the time, and when there's
has been advocating for for years, so let's give them a look.
a mismatch, it will try to make the state match in the cluster. This
reconfiguration will happen even if it requires rescheduling pods or
replacing a Kubernetes worker because one was malfunctioning.
Design Principles for Microservices in an
Orchestrated World
Once you have defined the desired state, the aim should be to have In general, when designing software, simplicity matters. Even though
a simple workflow like the one in the following image. What is inside the system may be complex under the hood, its operation and
the files defining the state at any point could become complicated, development should be relatively simple. The aim when designing a
but it will be worth it if every deploy after development delivers the microservice is to be able to reuse code and avoid adding code that
intended (and same) results every time. it doesn't need. Nonetheless, there are certain design principles that
have become a prerequisite to success.
OBSERVABILIT Y
Having a microservice that emits enough information to make it
observable is key, especially when it's time to troubleshoot problems
in a live environment. For that reason, the microservice code
should have instrumentation to generate metrics that will help to
At the command line, using Docker will look like this: understand the error rate failure, or spot latency problems by sending
traces among all microservices involved in a request, then create a
docker build -t company/microservicea:1.0 .
correlation. This can be as simple as including a header to identify
docker push company/microservicea:1.0
a request that is forwarded through the entire application. A typical
kubectl -f ./kubernetes-manifests/
library for collecting and emitting telemetries like metrics and traces
There's a good microservices demo application developed by Google, is OpenCensus. With OpenCensus, it's possible to export metrics
to an external service like Jaeger, Zipkin, Prometheus, and others. application and group them in a pod. Containers in a pod share the
OpenCensus provides libraries for Go, Java, C#, Node.js, C++, Ruby, same network and storage, allowing you to implement a log collector
Erlang/Elixir, Python, Scala, and PHP. (for example) as a sidecar pattern. The application container could
write logs to STDOUT and STDERR, and the collector container will
In the microservices demo, each microservice uses OpenCensus to
read the log data, apply transformations, and send it to a desired
send traces in every request. It's as simple as registering an exporter
centralized location. Log management then becomes transparent to
like this:
the application. If a change needs to happen regarding how to treat
logs, it doesn't interfere with the application.
exporter, err := jaeger.NewExporter(jaeger.Options{
Endpoint: fmt.Sprintf("http://%s", svcAddr),
"13
Process: jaeger.Process{
ServiceName: "productcatalogservice",
}, !""#$%!&$'(
%'(&!$()*
})
#'+
if err != nil { %'##)%&'*
log.Fatal(err)
,-./,012,
}
trace.RegisterExporter(exporter)
AMBASSADOR PATTERN
Another important aspect is that the microservice should provide The ambassador pattern is used as a proxy for the application
endpoints for its health status. In Kubernetes, a liveness probe can be container. It's commonly used for applications that are difficult to
configured to know when a pod should be restarted, perhaps due to a upgrade. An example of this pattern is present when working with
deadlock. And to make sure Kubernetes only sends traffic to healthy Istio. Every time a new pod has to be deployed, Kubernetes injects
pods, there's a readiness probe that can hit an endpoint in the API an Envoy container to route the pod's traffic. This container applies
and confirm that it gets an affirmative answer back. Ideally, these network security policies, circuit breakers, routes, and telemetry. The
endpoints should include logic to verify connections to the database or container application keeps its simplicity, and operators can adapt
run a smoke test for basic functionality. the proxy container as they need to.
Other patterns might be useful, but the ambassador and the sidecar
IMMUTABILIT Y
Once a container has been built, the expectation is that it won't patterns are the most common patterns used in an orchestrated world.
If you want to know more about this topic, Google published a blog Once you successfully collect error logs, you can define alerts and
post with a video explaining how to define the resources a pod will actions, like: "When more than 10% of the request has an error log,
normally need. notify." Initially, you might want to just notify yourself to tune the
threshold and choose a definitive action.
Monitoring in a Microservices/Kubernetes World
In distributed system architectures like microservices, having visibility FAILURE METRICS
from different perspectives will be critical at troubleshooting time. There might be times when errors logs exist even if the request
Many things could happen in a request when there are many parts succeeds. And there are other times when the failure is apparent just
constantly interacting at the same time. The most common method is from looking at the HTTP code in the response. For example, monitor
to write logs to the stdout and stderr streams. for all 5XX error codes, because that means that there has been a
failure in the microservice (like a timeout when calling a database).
For example, a latency problem in the system could exist because
4XX codes will tell you that clients are having problems either because
a microservice is not responding correctly. Maybe Kubernetes is
the request is malformed due to an attack or mistake, or because
restarting the pod too frequently, or perhaps the cluster is out of
there's still a call to a legacy endpoint. When using distributed tracing
capacity and can't schedule any more pods. But for this reason,
like OpenCensus, you can get default metrics for HTTP and gRPC
tools like Istio exist; by injecting a container in every pod, you can
services like latency, request count, and bytes received.
get a pretty good baseline of telemetry. Additionally, when you add
instrumentation with libraries like OpenCensus, you can deeply PERFORMANCE
understand what's happening with and within each service. Latency is a very important metric in microservices. Latency problems
in one service will impact the overall request latency when chaining
All this information will need a storage location, and as a good calls to different microservices. Every call to a microservice should
practice, you might want to have it a centralized location to provide record a trace, which is basically a record of how much time it took
access to anyone in the team — not just for the operations team. to respond. It's possible to add more details to the function level,
including the action, the result, and the pass to the next service. The
CENTR ALIZED LOGGING
You need to have a centralized logging solution due to the volatile hard part is triaging all traces in a request from a client. Usually, a trace
nature of containers and orchestrators. All the information that you ID header has to be sent in every request. If there isn't one, the logging
collect might (i.e. will) get lost. Moreover, a good approach will be to library creates it and it will represent the first trace in a request. As we
have this centralized logging solution isolated from the application saw in a previous section on microservice observability, adding traces
environment, e.g. in a different cluster of servers. You'll reuse this with OpenCensus is simply a matter of including the libraries and
solution for any upcoming system, and it will be easier to triage registering an exporter. Below is a screenshot of a trace in Zipkin.
kubectl logs. This command will print the logs of a container in a stateless, specific workloads can require stateful behavior, such as a
pod; if there's more than one container, then a container name will database. That's also the case when containers in a pod need to share
need to be specified. data between them. A Kubernetes volume solves each of these problems.
Let's say you want the logs from a pod called nginx; you'd use this Volumes in Kubernetes are merely directories that are accessible to
command: the containers in a pod. The container sees this as a directory, but
kubectl logs nginx in reality, it could be a directory stored on the host or on a storage
provider in the cloud.
If you want to get the logs from all the containers, use this:
Let's say, for example, that the Kubernetes cluster lives in Google
kubectl logs nginx --all-containers=true
Cloud Platform (GCP). You first need to create a persistent disk:
Although, I’d recommend you to use cAdvisor to get the logs in a more
gcloud compute disks create --size=100GB --zone=us-
standard way by consuming its API.
central1-a k8s
CONTAINER
With Docker, you have the option to send logs to the stdout and Then, you'd use the persistent disk in a pod by including this:
stderr , too, but you also have the option to send them somewhere
- name: data
else by using logging drivers. You can send logs to syslog, Fluentd,
gcePersistentDisk:
AWS CloudWatch, or hosted services.
pdName: k8s
You can get logs using the Docker command: fsType: ext4
You can also consume logs from cAdvisor for containers instead of And even if there are problems with the centralized logging storage
using the native tools for Docker or Kubernetes. With cAdvisor, you or with the process of collecting data, the capabillity for each service
have both a UI and an API to centralize logging. to continue providing value shouldn't get lost. There are two ways to
collect logs and metrics — one is as a daemonset and a second one is
ORCHESTR ATOR as a sidecar.
Kubernetes also logs data for itself. For containers that are part of
the kube-system namespace (e.g. kube-dns or kube-proxy), you get DAEMONSET
logs in the same way as with containers or pods. If systemd is present, A daemonset is a user “service” that can create a pod to run in
the rest of the logs (like the kubelet and the container runtime) are some or all of the nodes in a cluster. Every time a new node spins
written to journald. If systemd is not present, Kubernetes writes logs up, Kubernetes will create a pod based on what is defined in a
to the /var/log/ directory. If you're using a managed solution from a daemonset. This pod could include a logging agent with the primary
cloud provider, these logs might not be available directly. purpose of exposing logs or pushing them to a centralized logging
location. The approach to access logs from other containers or the
Managing logs in microservices is an important process; this is the
host is by using volumes.
data that will help the team to solve problems in the most effective
way. Logs are also going to be the source of truth needed to replicate a By utilizing a daemonset, you no longer need to change your
scenario that happened in the past. Therefore, data consistency matters application unless you need to directly change or specify a volume to
in this world, especially in microservices distributed architectures. write the logs.
After a container has been terminated, a cleanup process might get $"%&
!"#
rid of the data stored when it was running. Or a node could go down
because it is no longer needed after an auto-scaling action. Reasons
may vary, and that's why microservices shouldn't depend on state —
they should be stateless. !"#
'()*'+",'
-./&*)+01.#23",,0/,
!"#$
%"&
'((
4"2
)"*$+,*-. )"11-5$".
/0+./1"2/
%"&
)-*$.+1,3-&
'((
4"2 4"22,*2
)"*$+,*-. )"11-5$".
/0+./1"2/
Devada, Inc.
600 Park Offices Drive
Suite 150
Research Triangle Park, NC
DZone communities deliver over 6 million pages each
month to more than 3.3 million software developers, 888.678.0399 919.678.0300
architects, and decision makers. DZone offers something for
Copyright © 2019 DZone, Inc. All rights reserved. No part of this publication
everyone, including news, tutorials, cheat sheets, research
may be reproduced, stored in a retrieval system, or transmitted, in any form
guides, feature articles, source code, and more. "DZone is a or by means electronic, mechanical, photocopying, or otherwise, without
developer’s dream," says PC Magazine. prior written permission of the publisher.