title
stringlengths
3
46
content
stringlengths
0
1.6k
20:1
Kubernetes
20:2
This chapter is dedicated to describing the Kubernetes container orchestrator and its implementation in Azure, called Azure Kubernetes Service (AKS). We discussed the importance and the tasks handled by orchestrators in the Which tools are needed to manage microservices? section of Chapter 11, Applying a Microservice Architecture to Your Enterprise Application. Here, it is worth recalling just that Kubernetes is the de facto standard for orchestrators.
20:3
We will show also how to install and use minikube on your local machine, which is a one-node Kubernetes simulator you can use to try out all of the examples in this chapter, and also to test your own applications. Simulators are useful both to avoid wasting too much money on an actual cloud-based Kubernetes cluster, and to provide a different Kubernetes cluster to each developer.
20:4
This chapter explains the fundamental Kubernetes concepts and then focuses on how to interact with a Kubernetes cluster and how to deploy a Kubernetes application. All concepts are put into practice with simple examples. We recommend reading Chapter 11, Applying a Microservice Architecture to Your Enterprise Application, before reading this chapter, since we will use concepts explained in previous chapters.
20:5
More specifically, in this chapter, we will cover the following topics:
20:6
20:7
Kubernetes basics
20:8
Interacting with Azure Kubernetes clusters
20:9
Advanced Kubernetes concepts
20:10
20:11
By the end of this chapter, you will have learned how to implement and deploy a complete solution using Azure Kubernetes Service.
20:12
Technical requirements
20:13
In this chapter, you will require the following:
20:14
20:15
Visual Studio 2022 free Community Edition or better, with all the database tools installed, or any other .yaml file editor, such as Visual Studio Code.
20:16
A free Azure account. The Creating an Azure account section in Chapter 1, Understanding the Importance of Software Architecture, explains how to create one.
20:17
An optional minikube installation. Installation instructions will be given in the Using minikube section of this chapter.
20:18
20:19
The code for this chapter is available at https://github.com/PacktPublishing/Software-Architecture-with-C-Sharp-12-and-.NET-8-4E.
20:20
Kubernetes basics
20:21
Kubernetes is an advanced open source software for managing distributed applications running on a computer network. Kubernetes can be used on your private machine’s cluster, or you can use hardware-scalable Kubernetes offerings from all main cloud providers. This kind of software is called an orchestrator since it dynamically allocates microservices to the available hardware resources in order to maximize performance. Moreover, orchestrators like Kubernetes provide stable virtual addresses to microservices that they move around from one machine to another, thus changing their physical addresses. At the time of writing, Kubernetes is the most widespread orchestrator and the de facto standard for cluster orchestration that can be used with a wide ecosystem of tools and applications. While not being tied to specific languages or frameworks, Kubernetes is a fundamental tool for managing hardware resources and communications in .NET distributed applications based on microservices. This section introduces the basic Kubernetes concepts and entities.
20:22
A Kubernetes cluster is a cluster of virtual machines running the Kubernetes orchestrator.
20:23
20:24
Figure 20.1: Computer network equipped with Kubernetes
20:25
Generally, Kubernetes is installed on specific machines referred to as master nodes, while all other computers simply run an interface software that connects with the software running on the master nodes.
20:26
The virtual machines composing the cluster are called nodes. The smallest software unit we can deploy on Kubernetes is not a single application, but an aggregate of containerized applications called Pod. While Kubernetes supports various types of containers, the most commonly used container type is Docker, which we analyzed in Chapter 11, Applying a Microservice Architecture to Your Enterprise Application, so we will confine our discussion here to Docker. Pods are aggregates of Docker images, each containing one of your .NET microservices or microservices implemented with other technologies.
20:27
More specifically, Pods are sets of Docker images constrained to be placed together on the same node during the overall life of the application. They can be moved to other nodes, but they must be moved together. This means that they can easily communicate through localhost ports. Communication between different Pods, however, is more complex since the IP addresses of Pods are ephemeral resources because Pods have no fixed node where they run, but rather are moved from one node to another by the orchestrator. Moreover, Pods may be replicated to increase performance, so, in general, it makes no sense to address a message to a specific Pod; instead, we address it to any of the identical replicas of the same Pod.
20:28
Cluster nodes and Pods are managed by master nodes that communicate with cluster administrators through an API server, as shown in the following diagram:
20:29
20:30
Figure 20.2: Kubernetes cluster
20:31
The scheduler allocates Pods to nodes according to the administrator constraints, while the controller manager groups several daemons that monitor the cluster’s actual state and try to move it toward the desired state declared through the API server. There are controllers for several Kubernetes resources, from Pod replicas to communication facilities. In fact, each resource has some target objectives to be maintained while the application runs, and the controller verifies these objectives are actually achieved, possibly triggering corrective actions if not, such as moving some Pods running too slowly to less crowded nodes.
20:32
The kubelet manages the interaction of each non-master node with the master nodes.
20:33
In Kubernetes, communication between Pods is handled by resources called Services that are assigned virtual addresses by the Kubernetes infrastructure and that forward their communications to sets of identical Pods. In short, Services are Kubernetes’ way of assigning consistent virtual addresses to sets of Pod replicas.
20:34
All Kubernetes entities may be assigned name-value pairs called labels that are used to reference them through a pattern-matching mechanism. More specifically, Selectors select Kubernetes entities by listing labels they must have.
20:35
Thus, for instance, all Pods that receive traffic from the same Service are selected by specifying labels that they must have in the Service definition.
20:36
The way a Service routes its traffic to all connected Pods depends on the way Pods are organized. Stateless Pods are organized in so-called ReplicaSets. ReplicaSets have a unique virtual address assigned to the whole group and traffic is split equally among all Pods of the group.
20:37
Stateful Kubernetes Pod replicas are organized into so-called StatefulSets. StatefulSets use sharding to split the traffic between all their Pods. For this reason, Kubernetes Services assign a different name to each Pod of the StatefulSet they are connected to. These names look like the following: basename-0.<base URL>, basename-1.<base URL>, ..., basename-n.<base URL>. This way, message sharding is easily accomplished as follows:
20:38
20:39
Each time a message must be sent to a StatefulSet composed of N replicas, you compute a hash between 0 and N-1, say X.
20:40
Add the postfix X to a base name to get a cluster address, such as basename-x.<base URL>.
20:41
Send the message to the basename-x.<base URL> cluster address.
20:42
20:43
Kubernetes has no predefined storing facilities, and you can’t use node disk storage since Pods are moved between the available nodes, so long-term storage must be provided with sharded cloud databases or with other kinds of cloud storage. While each Pod in a StatefulSet can access a sharded cloud database with the usual connection string technique, Kubernetes offers a technique to abstract disk-like cloud storage provided by the external Kubernetes cluster environment. We will describe this storage in the Advanced Kubernetes concepts section.
20:44
All Kubernetes entities mentioned in this short introduction can be defined in a .yaml file, which, once deployed to a Kubernetes cluster, causes the creation of all entities defined in the file. The subsection that follows describes .yaml files, while the other subsections thereafter describe in detail all the basic Kubernetes objects mentioned so far, and explain how to define them in a .yaml file. Other Kubernetes objects will be described throughout the chapter.
20:45
.yaml files
20:46
The desired configuration of a cluster and the structure of Kubernetes objects are described by the developer with a language called YAML, and are packaged in files with a .yaml extension.
20:47
.yaml files, like JSON files, can be used to describe nested objects and collections in a human-readable way, but they do it with a different syntax. You have objects and lists, but object properties are not surrounded by {}, and lists are not surrounded by []. Instead, nested objects are declared by simply indenting their content with spaces. The number of spaces can be freely chosen, but once they’ve been chosen, they must be used consistently.
20:48
List items can be distinguished from object properties by preceding them with a hyphen (-).
20:49
Here is an example involving nested objects and collections:
20:50
Name: John
20:51
Surname: Smith
20:52
Spouse:
20:53
Name: Mary
20:54
Surname: Smith
20:55
Addresses:
20:56
- Type: home
20:57
Country: England
20:58
Town: London
20:59
Street: My home street
20:60
- Type: office
20:61
Country: England
20:62
Town: London
20:63
Street: My home street
20:64
20:65
The preceding Person object has a Spouse nested object and a nested collection of addresses.
20:66
The same example in JSON would be:
20:67
{
20:68
Name: John
20:69
Surname: Smith
20:70
Spouse:
20:71
{
20:72
Name: Mary
20:73
Surname: Smith
20:74
}
20:75
Addresses:
20:76
[
20:77
{
20:78
Type: home
20:79
Country: England
20:80
Town: London
20:81
Street: My home street
20:82
},
20:83
{
20:84
Type: office
20:85
Country: England
20:86
Town: London
20:87
Street: My home street
20:88
}
20:89
]
20:90
}
20:91
20:92
As you can see, the syntax is more readable, since it avoids the overhead of parentheses.
20:93
.yaml files can contain several sections, each defining a different entity, that are separated by a line containing the --- string. Comments are preceded by a # symbol, which must be repeated on each comment line.
20:94
Each section starts with the declaration of the Kubernetes API group and version. In fact, not all objects belong to the same API group. For objects that belong to the core API group, we can specify just the API version, as in the following example:
20:95
apiVersion: v1
20:96
20:97
While objects belonging to different API groups must also specify the API name, as in the following example:
20:98
apiVersion: apps/v1
20:99
20:100
In the next subsection, we analyze ReplicaSets and the Deployments that are built on top of them.
README.md exists but content is empty. Use the Edit dataset card button to edit it.
Downloads last month
4
Edit dataset card