title
stringlengths
3
46
content
stringlengths
0
1.6k
20:401
After that, the following form will appear:
20:402
20:403
Figure 20.4: Creating a Kubernetes cluster
20:404
It is worth mentioning that you can get help simply by hovering over any with the mouse.
20:405
As usual, you are required to specify a subscription, resource group, and region. Then, you can choose a unique name (Kubernetes cluster name) and the version of Kubernetes you would like to use. For computational power, you are asked to select a machine template for each node (Node size) and the number of nodes. While for an actual application, it is recommended to select at least three nodes, let’s select just two nodes for our exercise in order to save our free Azure credit.
20:406
Moreover, the default virtual machine should also be set to a cheap one, so click Change size and select DS2 v2. Finally, set Scale method to Manual to prevent the number of nodes from being automatically changed, which might quickly burn through your free Azure credit.
20:407
The Availability zones setting allows you to spread your nodes across several geographic zones for better fault tolerance. The default is three zones. Please change it to two zones since we have just two nodes.
20:408
After implementing the preceding changes, you should see the following settings:
20:409
20:410
Figure 20.5: Chosen settings
20:411
Now you can create your cluster by clicking the Review + create button. A review page should appear. Confirm and create the cluster.
20:412
If you click Next instead of Review + create, you can also define other node types, and then you can provide security information, namely, a service principal, and specify whether you wish to enable role-based access control. In Azure, service principals are accounts that are associated with services you may use to define resource access policies. You may also change the default network settings and other settings.
20:413
Deployment may take a little while (10-20 minutes). After that time, you will have your first Kubernetes cluster! At the end of the chapter, when the cluster is no longer required, please don’t forget to delete it in order to avoid wasting your free Azure credit.
20:414
In the next subsection, you will learn how to install and use minikube, a single-node Kubernetes simulator, on your local machine.
20:415
Using minikube
20:416
The easiest way to install minikube is the usage of the Windows installer you can find in the official installation page: https://minikube.sigs.k8s.io/docs/start/.
20:417
During the installation you will be prompted on the kind of virtualization tool to use. If you already installed Docker Desktop and WSL, please specify Docker.
20:418
If you have a different operating system, please follow the default choices, instead.
20:419
The installation of Docker Desktop is explained in the technical requirements of Chapter 11, Applying a Microservice Architecture to Your Enterprise Application. Please note that both WSL and Docker Desktop must be installed and Docker must be configured to use Linux containers by default.
20:420
Once you have minikube installed, you must add its binary to your computer PATH. The easiest way to do it is to open a PowerShell console and run the following command:
20:421
$oldPath=[Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::Machine)
20:422
if ($oldPath.Split(';') -inotcontains 'C:minikube'){ '
20:423
[Environment]::SetEnvironmentVariable('Path', $('{0};C:minikube' -f $oldPath), [EnvironmentVariableTarget]::Machine) '
20:424
}
20:425
20:426
Once installed, your cluster can be run with:
20:427
minikube start
20:428
20:429
When you have finished working with the cluster, it can be stopped with:
20:430
minikube stop
20:431
20:432
In the next subsection, you will learn how to interact with your minikube instance or Azure cluster through Kubernetes’ official client, kubectl.
20:433
Using kubectl
20:434
Once you have created your Azure Kubernetes cluster, you can interact with it via the Azure Cloud Shell. Click on the console icon in the top right of your Azure portal page. The following screenshot shows the Azure Shell icon:
20:435
20:436
Figure 20.6: Azure Shell icon
20:437
When prompted, select the Bash Shell. Then you will be prompted to create a storage account, so confirm and create it.
20:438
We will use this shell to interact with our cluster. At the top of the shell there is a file icon that we will use to upload our .yaml files:
20:439
20:440
Figure 20.7: How to upload files in Azure Cloud Shell
20:441
It is also possible to download a client called Azure CLI and to install it on your local machine (see https://docs.microsoft.com/en-US/cli/azure/install-azure-cli), but, in this case, you also need to install all the tools needed to interact with the Kubernetes cluster (kubectl and Helm) that are pre-installed in Azure Cloud Shell.
20:442
Once you’ve created a Kubernetes cluster, you can interact with it through the kubectl command-line tool. kubectl is integrated into Azure Cloud Shell, so you just need to activate your cluster credentials to use it. You can do this with the following Azure Cloud Shell command:
20:443
az aks get-credentials --resource-group <resource group> --name <cluster name>
20:444
20:445
The preceding command stores the credentials that were automatically created to enable your interaction with the cluster in a /.kube/config configuration file. From now on, you can issue your kubectl commands with no further authentication.
20:446
If, instead, you need to interact with your local minikube cluster, you need a local installation of kubectl, but minikube installs it automatically for you.
20:447
In order to use the automatically-installed kubectl, all kubectl commands must be preceded by the minikube command and kubectl must be followed by --. Thus, for instance, if you wanted to run the following command:
20:448
kubectl get all
20:449
20:450
Then you would have to write the following:
20:451
minkube kubectl -- get all
20:452
20:453
In the remainder of the chapter, we will write commands that work on actual Kubernetes clusters such as Azure Kubernetes. Therefore, when using minikube, remember to replace kubectl with minikube kubectl -- in your commands.
20:454
If you issue the kubectl get nodes command, you get a list of all your Kubernetes nodes. In general, kubectl get <object type> lists all objects of a given type. You can use it with nodes, pods, statefulset, and so on. kubectl get all shows a list of all the objects created in your cluster. If you also add the name of a specific object, you will get information on just that specific object, as shown here:
20:455
kubectl get <object type><object name>
20:456
20:457
If you add the --watch option, the object list will be continuously updated, so you can see the state of all the selected objects changing over time. You can leave this watch state by hitting Ctrl + C.
20:458
The following command shows a detailed report on a specific object:
20:459
kubectl describe <object name>
20:460
20:461
All objects described in a .yaml file, say myClusterConfiguration.yaml, can be created with the following command:
20:462
kubectl create -f myClusterConfiguration.yaml
20:463
20:464
Then, if you modify the .yaml file, you can reflect all the modifications in your cluster with the apply command, as shown here:
20:465
kubectl apply -f myClusterConfiguration.yaml
20:466
20:467
apply does the same job as create but, if the resource already exists, apply overrides it, while create exits with an error message.
20:468
You can destroy all objects that were created with a .yaml file by passing the same file to the delete command, as shown here:
20:469
kubectl delete -f myClusterConfiguration.yaml
20:470
20:471
The delete command can also be passed an object type and a list of names of objects of that type to destroy, as shown in the following example:
20:472
kubectl delete deployment deployment1 deployment2...
20:473
20:474
The preceding kubectl commands should suffice for most of your practical needs. For more details, the Further reading section contains a link to the official documentation.
20:475
In the next subsection, we will use kubectl create to install a simple demo application.
20:476
Deploying the demo Guestbook application
20:477
The Guestbook application is a demo application used in the examples in the official Kubernetes documentation. We will use it as an example of a Kubernetes application since its Docker images are available in the public Docker repository, so we don’t need to write software.
20:478
The Guestbook application stores the opinions of customers who visit a hotel or a restaurant.
20:479
It is composed of a UI, and an in-memory database, based on Redis. Moreover, updates are sent to the master copy of the Redis database, which is automatically replicated in N read-only Redis replicas.
20:480
20:481
Figure 20.8: Architecture of the Guestbook application
20:482
The UI application can be deployed in Kubernetes as a Deployment, since it is memoryless.
20:483
The Redis master store is deployed as a single pod Deployment. We can’t implement it with an N-pods Deployment since we need sharding for parallelizing updates. However, we might have used a StatefulSet assigning a different data shard to each different master Pod. However, since this is your first Kubernetes exercise and since write operations should not be predominant, a single master database should suffice in the practical case of a single restaurant/hotel.
20:484
Since all slave copies contain the same data and consequently are undistinguishable, they can be implemented with a Deployment, too.
20:485
The whole application is composed of three .yaml files that you can find in the GitHub repository associated with this book (https://github.com/PacktPublishing/Software-Architecture-with-C-Sharp-12-and-.NET-8-4E).
20:486
Here is the code for the master storage based on Redis that is contained in the redis-master.yaml file:
20:487
apiVersion: apps/v1
20:488
kind: Deployment
20:489
metadata:
20:490
name: redis-master
20:491
labels:
20:492
app: redis
20:493
spec:
20:494
selector:
20:495
matchLabels:
20:496
app: redis
20:497
role: master
20:498
tier: backend
20:499
replicas: 1
20:500
template: