Hands-On Microservices with Kubernetes
eBook - ePub

Hands-On Microservices with Kubernetes

Build, deploy, and manage scalable microservices on Kubernetes

Gigi Sayfan

Share book
  1. 502 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Microservices with Kubernetes

Build, deploy, and manage scalable microservices on Kubernetes

Gigi Sayfan

Book details
Book preview
Table of contents
Citations

About This Book

Enhance your skills in building scalable infrastructure for your cloud-based applications

Key Features

  • Learn to design a scalable architecture by building continuous integration (CI) pipelines with Kubernetes
  • Get an in-depth understanding of role-based access control (RBAC), continuous deployment (CD), and observability
  • Monitor a Kubernetes cluster with Prometheus and Grafana

Book Description

Kubernetes is among the most popular open-source platforms for automating the deployment, scaling, and operations of application containers across clusters of hosts, providing a container-centric infrastructure.

Hands-On Microservices with Kubernetes starts by providing you with in-depth insights into the synergy between Kubernetes and microservices. You will learn how to use Delinkcious, which will serve as a live lab throughout the book to help you understand microservices and Kubernetes concepts in the context of a real-world application. Next, you will get up to speed with setting up a CI/CD pipeline and configuring microservices using Kubernetes ConfigMaps. As you cover later chapters, you will gain hands-on experience in securing microservices, and implementing REST, gRPC APIs, and a Delinkcious data store. In addition to this, you'll explore the Nuclio project, run a serverless task on Kubernetes, and manage and implement data-intensive tests. Toward the concluding chapters, you'll deploy microservices on Kubernetes and learn to maintain a well-monitored system. Finally, you'll discover the importance of service meshes and how to incorporate Istio into the Delinkcious cluster.

By the end of this book, you'll have gained the skills you need to implement microservices on Kubernetes with the help of effective tools and best practices.

What you will learn

  • Understand the synergy between Kubernetes and microservices
  • Create a complete CI/CD pipeline for your microservices on Kubernetes
  • Develop microservices on Kubernetes with the Go kit framework using best practices
  • Manage and monitor your system using Kubernetes and open-source tools
  • Expose your services through REST and gRPC APIs
  • Implement and deploy serverless functions as a service
  • Externalize authentication, authorization and traffic shaping using a service mesh
  • Run a Kubernetes cluster in the cloud on Google Kubernetes Engine

Who this book is for

This book is for developers, DevOps engineers, or anyone who wants to develop large-scale microservice-based systems on top of Kubernetes. If you are looking to use Kubernetes on live production projects or want to migrate existing systems to a modern containerized microservices system, then this book is for you. Coding skills, together with some knowledge of Docker, Kubernetes, and cloud concepts will be useful.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Hands-On Microservices with Kubernetes an online PDF/ePUB?
Yes, you can access Hands-On Microservices with Kubernetes by Gigi Sayfan in PDF and/or ePUB format, as well as other popular books in Computer Science & Systems Architecture. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781789809732
Edition
1

Deploying Microservices

In this chapter, we will deal with two related, yet separate themes: production deployments and development deployments. The concerns, processes, and tools that are used for these two areas are very different. In both cases, the goal is to deploy new software to the cluster, but everything else is different. With production deployments, it's desirable to keep the system stable, be able to have a predictable build and deployment experience, and most importantly, to identify and be able to roll back bad deployments. With development deployments, it's desirable to have isolated deployments for each developer, a fast turnaround, and the ability to avoid cluttering source control or the continuous integration / continuous deployment (CI/CD) system (including image registries) with temporary development versions. Due to this, divergent emphasis is beneficial to isolate production deployments from development deployments.
In this chapter, we will cover the following topics:
  • Kubernetes deployments
  • Deploying to multiple environments
  • Understanding deployment strategies (rolling updates, blue-green deployment, canary deployment)
  • Rolling back deployments
  • Managing versions and upgrades
  • Local development deployments

Technical requirements

In this chapter, we will install many tools, including the following:
  • KO
  • Ksync
  • Draft
  • Skaffold
  • Tilt
There is no need to install them ahead of time.

The code

The code is split between two Git repositories:
  • You can find the code samples here: https://github.com/PacktPublishing/Hands-On-Microservices-with-Kubernetes/tree/master/Chapter11
  • You can find the updated Delinkcious application here: https://github.com/the-gigi/delinkcious/releases/tag/v0.9

Kubernetes deployments

We talked about deployments briefly in Chapter 1, Introduction to Kubernetes for Developers, and we've used Kubernetes deployments in almost every chapter. However, before diving into more sophisticated patterns and strategies, it will be useful to review the basic building blocks and the relationships between Kubernetes deployments, Kubernetes services, and scaling or autoscaling.
Deployments are Kubernetes resources that manage pods via a ReplicaSet. A Kubernetes ReplicaSet is a group of pods that are identified by a common set of labels with a certain number of replicas. The connection between the ReplicaSet to its pods is the ownerReferences field in the pod's metadata. The ReplicaSet controller ensures that the correct number of replicas are always running. If a pod dies for whatever reason, the ReplicaSet controller will schedule a new pod in its place. The following diagram illustrates this relationship:
Deployment and ReplicaSet
We can also observe the ownership chain in the metadata with kubectl. First, let's get the name of the social graph manager pod and find the name of its ReplicaSet owner from the ownerReferences metadata:
$ kubectl get po -l svc=social-graph,app=manager
NAME READY STATUS RESTARTS AGE
social-graph-manager-7d84ffc5f7-bst7w 1/1 Running 53 20d

$ kubectl get po social-graph-manager-7d84ffc5f7-bst7w -o jsonpath="{.metadata.ownerReferences[0]['name']}"
social-graph-manager-7d84ffc5f7

$ kubectl get po social-graph-manager-7d84ffc5f7-bst7w -o jsonpath="{.metadata.ownerReferences[0]['kind']}"
ReplicaSet
Next, we'll get the name of the deployment that owns the ReplicaSet:
$ kubectl get rs social-graph-manager-7d84ffc5f7 -o jsonpath="{.metadata.ownerReferences[0]['name']}"
graph-manager

$ kubectl get rs social-graph-manager-7d84ffc5f7 -o jsonpath="{.metadata.ownerReferences[0]['kind']}"
Deployment
So, if the ReplicaSet controller takes care of managing the number of pods, what does the Deployment object add? The Deployment object encapsulates the concept of a deployment, including a deployment strategy and rollout history. It also provides deployment-oriented operations such as updating a deployment and rolling back a deployment, which we will look at later.

Deploying to multiple environments

In this section, we will create a staging environment for Delinkcious in a new staging namespace. The staging namespace will be a full-fledged copy of the default namespace that will serve as our production environment.
First, let's create the namespace:
$ kubectl create ns staging
namespace/staging created
Then, in Argo CD, we can create a new project called staging:
Argo CD staging project
Now, we need to configure all our services so that Argo CD can sync them to the staging environment. This can be a little tedious to do in the UI now that we have a substantial amount of services. Instead, we will use the Argo CD CLI and a Python 3 program called bootstrap_staging.py to automate the process. The program expects the following:
  • The staging namespace has been created.
  • The Argo CD CLI is installed and in the path.
  • The Argo CD service is available through the localhost on port 8080.
  • The Argo CD admin password is configured as the environment variable.
To expose Argo CD on the localhost at port 80, we can run the following command:
kubectl port-forward -n argocd svc/argocd-server 8080:443
Let's break down the program and understand how it works. This is a good foundation where you can develop your own custom CI/CD solutions by automating CLI tools. The only dependencies are Python's standard library modules: subprocess (allows you to run command-line tools) and os (for accessing environment variables). Here, we only need to run the Argo CD CLI.
The run() function hides all the implementation details and provides a convenient interface where you just need to pass the arguments as a string. The run() function will prepare a proper command list that can be passed to the subprocess module's check_output() function, capture the output, and decode it from bytes to a string:
import os
import subprocess

def run(cmd):
cmd = ('argocd ' + cmd).split()
output = subprocess.check_output(cmd)
return output.decode('utf-8')
The login() function utilizes run(), gets the admin password from the environment, and constructs the proper command string with all the necessary flags so that you can log in as the admin user to Argo CD:
def ...

Table of contents