title
stringlengths
3
46
content
stringlengths
0
1.6k
20:301
In Azure Kubernetes Service (AKS), you must also specify the resource group where the IP address was allocated in an annotation:
20:302
apiVersion: v1
20:303
kind: Service
20:304
metadata:
20:305
annotations:
20:306
service.beta.kubernetes.io/azure-load-balancer-resource-group: <IP resource group name>
20:307
name: my-service-name
20:308
...
20:309
20:310
In AKS, you can remain with a dynamic IP address, but you can get a public static domain name of the type <my-service-label>.<location>.cloudapp.azure.com, where <location> is the geographic label you have chosen for your resources. <my-service-label> is a label that you have verified that makes the previous domain name unique. The chosen label must be declared in an annotation of your service, as shown here:
20:311
apiVersion: v1
20:312
kind: Service
20:313
metadata:
20:314
annotations:
20:315
service.beta.kubernetes.io/azure-dns-label-name: <my-service-label>
20:316
name: my-service-name
20:317
...
20:318
20:319
StatefulSets don鈥檛 need any load balancing since each Pod instance has its own identity, but do require a unique URL address for each Pod instance. This unique URL is provided by the so-called headless Services. Headless Services are defined like ClusterIP services, the only difference being that they have their spec->clusterIP property set to none:
20:320
...
20:321
spec:
20:322
clusterIP: none
20:323
selector:
20:324
...
20:325
20:326
All StatefulSets handled by a headless Service must place the Service name in their spec-> serviceName property, as already stated in the StatefulSets subsection.
20:327
The unique name provided by a headless Service to all StatefulSets Pod instances it handles is <unique pod name>.<service name>.<namespace>.svc.cluster.local.
20:328
Services only understand low-level protocols, such as TCP/IP, but most web applications are situated on the more sophisticated HTTP protocol. That鈥檚 why Kubernetes offers higher-level entities called Ingresses that are built on top of services.
20:329
Ingresses are fundamental in the implementation of all web-based applications that need support for HTTP. Moreover, since at the moment, a substantial amount of applications are web applications, ingresses are a must for all microservices applications. In particular, they are needed by all microservices based on ASP.NET Core, which we will discuss in the remainder of the book.
20:330
The following subsection describes these and explains how to expose a set of Pods through a level 7 protocol load balancer, which can be used to get access to typical HTTP services, instead of through a LoadBalancer Service.
20:331
Ingresses
20:332
Ingresses were conceived to enable each application running in a Kubernetes cluster to expose an HTTP-based interface. This is a fundamental requirement for any orchestrator, since nowadays all microservices applications are web applications that interact with their clients through HTTP-based protocols. Moreover, Ingresses must be very efficient since all communications with the Kubernetes cluster will pass through them.
20:333
Accordingly, ingresses offer all of the typical services offered by an advanced and efficient web server. They provide the following services:
20:334
20:335
HTTPS termination. They accept HTTPS connections and route them in HTTP format to any service in the cluster.
20:336
Name-based virtual hosting. They associate several domain names with the same IP address and route each domain, or <domain>/<path prefix>, to a different cluster Service.
20:337
Load balancing.
20:338
20:339
20:340
An ingress is the usual communication choice for Deployments and ReplicaSets that do communicate outside of their Kubernetes cluster and need advanced HTTP features.
20:341
20:342
Since rewriting all functionalities of an advanced web server from scratch would be substantially impossible, Ingresses rely on existing web servers to offer their services. More specifically, Kubernetes offers the possibility to add an interface module called Ingress Controllers to connect each Kubernetes cluster with an existing web server, such as NGINX and Apache.
20:343
Ingress Controllers are custom Kubernetes objects that must be installed in the cluster. They handle the interface between Kubernetes and the pre-existing web server software, which can be either an external web server or a web server that is part of the Ingress Controller installation.
20:344
We will describe the installation of an Ingress Controller based on the NGINX web server software in the Advanced Kubernetes concepts section, as an example of the use of Helm. However, there are Ingress Controllers for all main web servers. The Further reading section contains information on how to install also an Ingress Controller that interfaces an external Azure Application Gateway.
20:345
HTTPS termination and name-based virtual hosting (see the explanation of these terms at the beginning of this subsection) can be configured in the Ingress definition in a way that is independent of the chosen Ingress Controller, while the way load balancing is achieved depends on the specific Ingress Controller chosen and on its configuration. Some Ingress Controller configuration data can be passed in the metadata-> annotations field of the Ingress definition.
20:346
Name-based virtual hosting is defined in the spec-> rules section of the Ingress definition:
20:347
...
20:348
spec:
20:349
...
20:350
rules:
20:351
- host: *.mydomain.com
20:352
http:
20:353
paths:
20:354
- path: /
20:355
pathType: Prefix
20:356
backend:
20:357
service:
20:358
name: my-service-name
20:359
port:
20:360
number: 80
20:361
- host: my-subdomain.anotherdomain.com
20:362
...
20:363
20:364
Each rule specifies an optional hostname that can contain the * wildcard. If no hostname is provided, the rule matches all hostnames. For each rule, we can specify several paths, each redirected to a different service/port pair, where the service is referenced through its name. The way the match with each path is carried out depends on the value of pathType; if this value is Prefix, the specified path must be a prefix of any matching path. Otherwise, if this value is Exact, the match must be exact. Matches are case-sensitive.
20:365
HTTPS termination on a specific hostname is specified by associating it with a certificate encoded in a Kubernetes secret:
20:366
...
20:367
spec:
20:368
...
20:369
tls:
20:370
- hosts:
20:371
- www.mydomain.com
20:372
secretName: my-certificate1
20:373
- my-subdomain.anotherdomain.com
20:374
secretName: my-certificate2
20:375
...
20:376
20:377
HTTPS certificates can be obtained free of charge at https://letsencrypt.org/. The procedure is explained on the website, but basically, as with all certificate authorities, you provide a key and they return the certificate based on that key. It is also possible to install a certificate manager that takes care of automatically installing and renewing the certificate. The way a key/certificate pair is encoded in a Kubernetes secret string is detailed in the Advanced Kubernetes concepts section.
20:378
The whole Ingress definition is as follows:
20:379
apiVersion: networking.k8s.io/v1
20:380
kind: Ingress
20:381
metadata:
20:382
name: my-example-ingress
20:383
namespace: my-namespace
20:384
spec:
20:385
tls:
20:386
...
20:387
rules:
20:388
...
20:389
20:390
Here, the namespace is optional, and if not specified, is assumed to be default.
20:391
In the next section, we will put into practice some of the concepts explained here by defining an Azure Kubernetes cluster and deploying a simple application.
20:392
Interacting with Kubernetes clusters
20:393
In this section, we will explain both how to create an Azure Kubernetes cluster, and how to install minikube, a Kubernetes simulator, on your local machine. All examples can be run on both Azure Kubernetes and your local minikube instance.
20:394
Creating an Azure Kubernetes cluster
20:395
To create an AKS cluster, do the following:
20:396
20:397
Type AKS into the Azure search box.
20:398
Select Kubernetes services.
20:399
Then click the Create button.
20:400