Kubernetes-objects-on-Azure
Kubernetes-objects-on-Azure
Microsoft Azure
An introduction to deployment options based on
a simple ASP.NET Core web application
By Mahesh Kshirsagar
Azure Customer Advisory Team (AzureCAT)
January 2018
Kubernetes objects on Microsoft Azure
Contents
Overview ..............................................................................................................................................................4
In this guide ................................................................................................................................................................... 5
Sample code .................................................................................................................................................................. 5
PART 1 GET STARTED WITH KUBERNETES ...........................................................................................6
Set up a local Kubernetes environment ...................................................................................................6
Enable a hypervisor ..................................................................................................................................................... 7
Install kubectl ................................................................................................................................................................ 8
Install Minikube ............................................................................................................................................................ 9
Validate the prerequisite installation ................................................................................................................... 9
Open the Kubernetes dashboard from Minikube ............................................................................. 10
Start a local Kubernetes cluster ................................................................................................................ 11
Troubleshoot deployment..................................................................................................................................... 12
Set up Kubernetes on Azure...................................................................................................................... 12
Create a Kubernetes cluster on Container Service ...................................................................................... 12
Manage a Kubernetes cluster on Azure ........................................................................................................... 14
Open the Kubernetes dashboard for an Azure-based cluster ................................................................ 15
PART 2 WORK WITH THE KUBERNETES DASHBOARD ................................................................. 18
Switch context between a local cluster and an Azure cluster ....................................................... 18
Tour the Kubernetes dashboard .............................................................................................................. 20
Admin ............................................................................................................................................................................ 20
Namespaces ....................................................................................................................................................... 20
Nodes .................................................................................................................................................................... 20
Persistent volume ............................................................................................................................................. 21
Storage classes .................................................................................................................................................. 21
Roles ...................................................................................................................................................................... 21
Workloads .................................................................................................................................................................... 21
Deployments ...................................................................................................................................................... 22
Replica sets ......................................................................................................................................................... 22
Replication controllers .................................................................................................................................... 22
Daemon sets ....................................................................................................................................................... 22
Stateful sets ........................................................................................................................................................ 23
Jobs ........................................................................................................................................................................ 23
Pods ....................................................................................................................................................................... 23
Services and discovery ............................................................................................................................................ 23
Ingresses .............................................................................................................................................................. 24
2
Kubernetes objects on Microsoft Azure
Services ................................................................................................................................................................. 24
Storage and config ................................................................................................................................................... 24
Persistent volume claims ............................................................................................................................... 24
ConfigMap .......................................................................................................................................................... 24
Secrets................................................................................................................................................................... 24
Summary of key Kubernetes resources for workloads .............................................................................. 25
PART 3 DEPLOY THE TIERS ...................................................................................................................... 26
Considerations for SQL Server back-end deployment .................................................................... 26
Create a secret ........................................................................................................................................................... 27
Create a storage class ............................................................................................................................................. 27
Create a persistent volume claim ....................................................................................................................... 27
Create a service ......................................................................................................................................................... 28
Create a stateful set ................................................................................................................................................. 29
Create the database ................................................................................................................................................. 30
Verify data persistence ........................................................................................................................................... 31
Considerations for the ASP.NET Core web front-end deployment ............................................ 33
Install Heapster with InfluxDB and Grafana.................................................................................................... 33
Change the grafana.yaml file ....................................................................................................................... 33
Create services ................................................................................................................................................... 35
Use the Horizontal Pod Autoscaler.................................................................................................................... 36
Create deployment .......................................................................................................................................... 36
Create service ..................................................................................................................................................... 37
Create horizontalPodAutoScaler ................................................................................................................ 38
Troubleshoot issues ......................................................................................................................................... 38
Verify how operations scale.................................................................................................................................. 38
Verify the application .............................................................................................................................................. 41
Verify the database .................................................................................................................................................. 42
Conclusion ........................................................................................................................................................ 42
Authored by Mahesh Kshirsagar. Edited by Ed Price. Reviewed by Brendon Burns, Wasim Bloch, and Kimmo Forss.
© 2018 Microsoft Corporation. This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR
IMPLIED, IN THIS SUMMARY. The names of actual companies and products mentioned herein may be the trademarks of their respective
owners.
3
Kubernetes objects on Microsoft Azure
Overview
More people are turning to Kubernetes as a container orchestrator—a rapidly growing trend
according to search statistics.1 As more enterprises adopt architectures based on containers or
microservices, they also need to adopt a container orchestrator, and they need options for
managing their containerized workloads. Many developers start with Docker, an excellent
ecosystem for learning to work with containers. But Kubernetes pushes the boundaries with its
exhaustive set of options for managing containerized workloads. The sheer number of options
can be overwhelming, especially when you’re getting started.
This paper attempts to demystify Kubernetes by focusing on a real-life scenario in which a basic
tiered application is deployed using pods and controllers. Step by step, using a sample
application, you’ll see how to choose the right Kubernetes objects for running workloads in Azure.
Kubernetes provides multiple deployment objects, each with its unique advantages that are
explained in the context of a sample two-tier workload on Kubernetes. The concepts you learn by
deploying this workload can easily be extended to multitier architecture implementations.
The workload discussed in this paper consists of an application with a web front end running
ASP.NET Core 1.0 (with some ASP.NET SignalR) shown in Figure 1. The back end consists of a SQL
Server container running on Linux.
Figure 1. Use the sample app to see how to use Azure Container Service with Kubernetes.
In this guide
This paper has three parts that follow the typical order you might use to start up and learn
Kubernetes:
• In Part 1, get started with Kubernetes, first by setting up a local development environment,
then a Kubernetes cluster on Azure using Azure Container Service. Learn about deployment
options and process.
• In Part 2, get a high-level overview of key Kubernetes concepts through a tour of the
dashboard, the Kubernetes web interface. Learn how to switch between clusters and work
with the dashboard options.
• In Part 3, walk through a sample deployment and learn how to choose the right deployment
objects. Set up a SQL Server back end and ASP.NET Core web front end.
The content for this paper was originally written as a series of blogs.
Sample code
This guide demonstrates the deployment of a sample application, ContainerDemoApp. You can
copy the source code from GitHub:
Resource URL
5
Kubernetes objects on Microsoft Azure
PART 1
GET STARTED WITH KUBERNETES
In a typical development scenario, developers start on their local computers before deploying to a
remote cluster, most likely in the cloud. Part 1 shows the basics you need to set up a Kubernetes
cluster.
First, you’ll set up the development environment for Kubernetes on your local computer , then
start a local Kubernetes cluster. Next, you’ll get started on Azure and set up a Kubernetes cluster
there.
• Kubectl is the Kubernetes command-line tool used to manage the Kubernetes cluster.
• Minikube is a tool that runs a single-node Kubernetes cluster inside a virtual machine on your
development computer.
6
Kubernetes objects on Microsoft Azure
Enable a hypervisor
This guide uses Hyper-V as the hypervisor. On many Windows 10 versions, Hyper-V is already
installed—for example, on 64-bit versions of Windows Professional, Enterprise, and Education in
Windows 8 and later. It is not available on Windows Home edition.
NOTE: If you’re running something other than Windows 10 on your development platforms,
another hypervisor option is to use VirtualBox, a cross-platform virtualization application. For a
list of hypervisors, see “Install a Hypervisor” on the Minikube page of the Kubernetes
documentation.
3. To set up a virtual switch, type hyper in the Windows Start menu, then select Hyper-V
Manager.
7. Ensure that the Allow management operating system to share this network adapter
checkbox is selected.
7
Kubernetes objects on Microsoft Azure
Install kubectl
In this step, you install kubectl from the Kubernetes site and set it as an environment variable.
1. Go to Install and Set Up kubectl in the Kubernetes documentation and follow the instructions
for Windows to copy the executable (.exe) file to your C: directory.
2. In the Windows Search box, type View advanced system settings and click on the result from
the Control Panel.
3. In the Advanced tab of the System Properties dialog box, click the Environment Variables
button.
4. In the System variables frame of the Environment Variables dialog box, select the row with
Path as a variable name, then select the Edit button.
5. In the Edit System Variable dialog box, click New, then add that path to where you copy and
pasted the kubectl.exe. Click OK.
8
Kubernetes objects on Microsoft Azure
Install Minikube
Like kubectl, you set up Minikube by copying the latest Minikube executable (.exe) file and
making it an environment variable.
1. Go to the kubernetes/minikube GitHub page and download Minikube. For the examples
shown in this guide, the file was downloaded to the same location (C:\) as the kubectl file.
The first time you run this command, no cluster has been set up yet, so errors are generated for
“Server Version.” Ignore these. Figure 2 compares the output from minikube and kubectl.
Next, run the following command to verify the latest version of Kubernetes that is available for
installation:
Minikube get-k8s-versions
Figure 3 shows that the latest Kubernetes version available for installation is V1.7.0.
9
Kubernetes objects on Microsoft Azure
10
Kubernetes objects on Microsoft Azure
NOTE: The screenshots in this document show a Windows 10 setup that includes a Hyper-V
virtual switch.
To verify the Minikube status, run the minikube status command as Figure 7 shows.
11
Kubernetes objects on Microsoft Azure
Now use kubectl to verify status. Run the cluster-info and get nodes commands as Figure 8
shows.
Troubleshoot deployment
If you encounter deployment issues, follow these steps:
1. Ensure that you are running the command prompt in Administrator mode.
4. Delete the .kube and .minikube folders in the HOME path (generally found at
C:\Windows\Users\<your-user-id>).
NOTE: In October 2017, Microsoft announced a managed Kubernetes service called AKS that
offers new deployment options and other benefits. This guide uses Container Service, which has
been available since 2015 and features an Azure–backed SLA. However, in the setup steps
below, you can substitute the equivalent AKS command for the Container Service command.
Other Azure services support containers, including Azure Service Fabric, Azure App Service, and
Azure Container Instances. The focus here is on Container Service because its of support for
Kubernetes.
A common practice is to use the switch --generate-ssh-keys to autogenerate SSH keys, but
many enterprises prefer to have their operations or security teams generate and provide SSH keys
to the development teams. This paper uses the latter approach instead of autogenerating SSH
keys.
12
Kubernetes objects on Microsoft Azure
To support this approach, use the --ssh-key-file switch. For details about the various switches
available when creating a Kubernetes cluster, see the az acs command reference.
NOTE: Due to a current known issue, please compact the SSH public key by completing the
following procedure on Windows 10 using PuTTY.
2. Copy the public key, create a new text file, paste the public key there and save the file. Note
that there are no comments at the beginning or the end of this public key, which you would
generally see if you open it as-is.
3. Use the az acs create command with the full path of this file to create a new Kubernetes
cluster as follows:
az acs create --orchestrator-type=kubernetes --resource-group acs-kuber-rg
--name-acs-kuber-cluster --ssh-key-value="<full-path-name>"
4. After a while, the following message appears, indicating the successful deployment of a
Kubernetes cluster on Azure:
13
Kubernetes objects on Microsoft Azure
When the command completes, run the config current-context command to see if the current
context of kubectl is pointing to the Azure cluster:
kubectl config current-context acs-kuber-cluster-ac-kuber-rg-5dd399
After you confirm that the current context is pointing to the Azure cluster, run the get nodes
command to get more details about the nodes (Figure 9).
These nodes correspond to virtual machines on Azure. As Figure 10 shows, the names match.
14
Kubernetes objects on Microsoft Azure
To identify the various endpoints of the Kubernetes cluster on Azure, run the cluster-info
command (Figure 11).
Figure 12. Getting the IP address for the dashboard with the kubectl proxy command.
When you go to that address, you’ll see a list of all the endpoints of the Azure-based Kubernetes
cluster (Figure 13).
15
Kubernetes objects on Microsoft Azure
To see the dashboard for the Azure-based Kubernetes cluster, add the /ui path to the address—
for example, http://127.0.0.1:8001/ui. The Kubernetes dashboard opens (Figure 14), and you can
now access the cluster through this browser interface.
For details about using the dashboard, see Part 2: Work with the Kubernetes dashboard.
16
Kubernetes objects on Microsoft Azure
Figure 14. Kubernetes dashboard for Azure-based cluster showing the welcome page of an empty cluster.
17
Kubernetes objects on Microsoft Azure
PART 2
WORK WITH THE KUBERNETES
DASHBOARD
Kubernetes gives you a multitude of options, and the dashboard helps you discover them. First,
you need to know how to switch between your local clusters and your clusters on Azure.
Kubernetes calls this switching context.
This section offers a high-level overview of the dashboard and introduces you to common options
for getting information about your clusters.
Kubectl issues commands against a specific cluster using the context. To see which contexts are
available to receive commands, use the config view command as shown in Figure 15.
18
Kubernetes objects on Microsoft Azure
The kubectl config view command lists clusters, configs, and shows the current config.
Use the command kubectl config use-context <name-of-context> to switch between contexts. For
example:
kubectl config use-context minikube
When you are connected to a specific context, you can issue further commands that work on that
cluster. For example, Figure 16 shows the cluster-info command.
Figure 16. Getting information about a context using the cluster-info command.
19
Kubernetes objects on Microsoft Azure
Admin
This view is for cluster and namespace
administrators and includes Namespaces,
Nodes, Persistent Volume, Storage Classes, and
Roles (V1.7.0 only).
Namespaces
A namespace is a virtual cluster within a
physical cluster and offers a great solution for
running multitenant applications. Physical Figure 17. Menu of the Kubernetes dashboard.
cluster resources can be distributed across
multiple namespaces using policies and resource quotas. Each namespace is isolated from other
namespaces.
You can also use a namespace to isolate multiple areas within an application or microservice. Each
application or microservice can be deployed within namespaces, and access to it can be
controlled.
Each Kubernetes cluster has at least two built-in namespaces. These are default and kube-system.
You can also create custom namespaces, but each time any operation or command is issued on
any component within a custom namespace, you must qualify it with a namespace. This extra step
is time-consuming when operating a large number of custom namespaces. This situation can be
remedied by creating users and credentials that are unique to the namespaces.
Nodes
A node is a host within a Kubernetes cluster. Either physical or virtual servers, nodes keep the
Kubernetes cluster up and running. Each node runs multiple components, such as kubelet and
kube proxy.
Nodes play a key role in cluster capacity planning, high-availability, security, problem detection
20
Kubernetes objects on Microsoft Azure
and resolution, and monitoring. Nodes are managed by master, a collection of components, such
as API server, scheduler, and controller manager. Master itself can run on one of the nodes and
should be distributed across nodes for redundancy. During capacity planning, use an odd number
of nodes) so you can form a quorum and have higher redundancy and reliability.
Depending upon the workload you plan to run on the Kubernetes cluster, it’s a good idea to have
multiple node types. These node types can include—but are not restricted to—CPU, memory, or
IOPS-intensive. Node types can be labelled, so when it is time to run a containerized workload,
you can easily identify and select a suitable node type to run a workload on.
Persistent volume
While nodes represent the compute capacity of a Kubernetes cluster, a persistent volume
represents its storage capacity. To understand why persistent volume is important, consider how
container orchestration works. Containers and cluster nodes are transient by nature. When they
become unresponsive or fail, the orchestrator handles it—Kubernetes creates a new container or
a pod, a basic building block that encapsulates a single container or multiple containers.
In such a volatile environment, storage might be seen as a problem. How do you keep data in
storage when the container or pod is transient and nodes can fail? Kubernetes offers options for
both persistent and nonpersistent volumes:
• Persistent volumes are resources that need to be provisioned separately from the Kubernetes
cluster. Kubernetes can use these resources but does not manage them. They need to be
managed by the cluster administrator. In Azure, you can use Azure Disk or Azure File Storage
as a persistent volume with a Kubernetes cluster.
• Nonpersistent volumes include the emptyDir and HostPath options to enable communication
between pods and between nodes respectively.
Storage classes
Storage classes give you the flexibility to choose multiple volume options for a Kubernetes
cluster. You can use multiple storage solutions from various vendors, including Azure, Amazon
Web Service, and Google Cloud Platform.
Containers request storage from a cluster via a volume claim, which is a declarative claim for a
specific type of volume access mode, capacity, and so on. The Kubernetes cluster evaluates this
claim request, and then assigns a volume from its storage class.
Roles
The roles feature enables role-based access (RBAC) to cluster resources. RBAC replaces the
attribute-based access control (ABAC) used prior to Kubernetes 1.6.0.
You can use roles to grant access to resources within the scope of a namespace or a cluster,
including multiple namespaces. Roles address authorization concerns about accessing API Server,
which provides core services to manage Kubernetes services that use RESTful API endpoints. Roles
and users are associated using RoleBinding.
Workloads
The dashboard shows all the applications running in a selected workspace in the workloads view.
To see the status and relationship between objects, choose one of the details views for the type of
21
Kubernetes objects on Microsoft Azure
workload: deployments, replica sets, replication controllers, daemon sets, jobs, pods, and stateful
sets.
Deployments
This view shows workload deployments. In Kubernetes, a deployment is a resource that ensures
the reliable rollout of an application. A deployment creates a replica set that is used to manage a
group of pods of the same type. If a pod crashes, another one is automatically created through
the replica set.
It is possible to create an individual pod without using a deployment controller, but the pod will
lack the mechanism needed to recreate itself without any manual intervention. That makes the
process unsustainable.
Deployments uses a declarative syntax to define the replicas that ensure the many instances of a
pod are always running. They are also a useful feature to trigger a version upgrade.
Replica sets
This dashboard view shows the replica sets used to manage a group of pods of the same type.
Replica sets ensure that a specific number of pods are always running. They support set-based
label selectors, a type of selection that enables a set of pods to be grouped so that the entire set
is available for an operation defined in the ReplicaSet controller.
If a pod crashes, a replica set restores it. This functionality is certainly useful, but there are not
many use cases—pods are often version-updated. In these cases, the expectation is to update the
pod and ensure that a specific number of pods is always kept running.
The recommendation is to use replica sets along with higher-level controllers such as
deployment. Replica sets and deployments can also be used with the HorizontalPodAutoscaler
controller, a related resource that provides an auto-scale functionality for pods.
Replication controllers
Replication controllers are the predecessors to replica sets. Unlike replica sets, they operate on
name equality. The official recommendation going forward is to use the ReplicaSet controller.
Daemon sets
This dashboard view shows daemon sets, which are a bit like the demons in movies that never die
no matter how many times they are killed, and they play a versatile role in a Kubernetes cluster. A
DaemonSet is a pod that runs on every node. If that pod crashes, Kubernetes tries to bring it back
up automatically. Similarly, any new node that gets added to cluster automatically gets a
DaemonSet pod.
This functionality is very useful in scenarios where every node needs to be monitored for a
potential problem. For those scenarios, you can use the node-problem-detector component,
which is deployed as a DaemonSet.
You can also use daemon sets to scale a container using the nodeSelector node in their template.
This node instructs Kubernetes to run a container on a qualifying node. So, when any new node
gets added in the cluster and qualifies for the nodeSelector criteria (typically via labelling), it
automatically runs a container specified in the DaemonSet template.
Another use case is to label a set of nodes with a particular hardware configuration that is
22
Kubernetes objects on Microsoft Azure
dedicated to persistent storage, and run stateful workloads as a daemon set on those labelled
nodes. It's also possible to use the .spec.updateStrategy.type node in the DaemonSet template
for rolling updates.
Stateful sets
This dashboard view shows the workload API object called StatefulSets that were designed to
solve the problems associated with data loss when pods crash. Stateful sets use persistent storage
volume. By comparison, ReplicaSet or ReplicationControllers help ensure that a specific number of
pods are always running, and they simply spin up a new pod instance when a pod crashes. With a
crash, any data that is written to the pod volume disappears as well. A new pod gets its own new
volume—prohibitive behavior in some workloads, such as distributed databases and stateful
applications.
Stateful sets can be configured to mount a persistent storage volume. So even if a pod crashes,
data is preserved on persistent data storage. The StatefulSets object also has a unique and stable
hostname that can be queried through DNS. They ensure that pods are named (such as pod-1,
pod-2, and so on). A new pod instance (such as pod-2) is not created until pod-1 is created and is
healthy.
Jobs
This dashboard view shows the jobs that are running. Jobs are a Kubernetes resources that create
one or more pods and ensure that they run until they succeed. Jobs are ideal for tasks that run
and achieve a goal, and then stop.
You can customize jobs with restart policies, completions, and parallelism. Internally, jobs make
sure the optimum number of pods are created to execute parallel operations.
By design, when a job completes its execution, it and its pods are kept for telemetry purposes. If
any of the logging information is not needed, a job and its related pod should be manually
deleted. Cron jobs are a special type of jobs that run on schedule either as a single instance or
repeatedly on a schedule.
Pods
This dashboard lists the status of all pods, the Kubernetes building block that simplifies
deployment, scaling, and replication. A pod encapsulates one or more containers and can also
include storage, an IP address, and a configuration option for managing each container.
Storage within a pod is shared among containers and is mounted as a volume on all the
containers inside it. All containers in a pod can communicate by using either localhost or inter-
process communication.
Pods can be created as a Kubernetes resource. However, they lack self-healing capabilities. So
they are usually created using controllers (DaemonSets, Deployment, StatefulSets).
23
Kubernetes objects on Microsoft Azure
Ingresses
An ingress is a layer 7 HTTP load balancer. Ingresses are Kubernetes resources that expose one or
more services to the outside world. An ingress provides externally visible URLs to services and
load-balance traffic with SSL termination. This resource is useful in scenarios where rolling out
such services is not possible or is expensive.
Ingresses can also be used to define network routes between namespaces and pods in
conjunction with network policies. They are managed using ingress controllers, which can limit
requests, redirect URLs, and control access. For details, see the Ingress topic in the Kubernetes
documentation.
Services
A service is a layer 4 TCP load balancer. They are a way to expose an application functionality to
users or other services. A service encompasses one or more pods and can be internal or external.
Internal services are accessed only by other services or jobs in a cluster. An external service is
marked by the presence of either NodePort or load balancer. For details, see the Services topic in
the Kubernetes documentation.
• Reclaim policy: Determines what happens with storage when a claim is deleted.
ConfigMap
ConfigMap helps keep image configuration options separate from containers and pods. It lists
configuration options as a key-value pair. This configuration information is exposed as an
environment variable. When creating pods, a pod template can read the values for ConfigMap
and can provision a pod. In similar fashion, it can also be used to provision volume.
Secrets
ConfigMap provides an easy configuration separation, but it stores a key-value pair as plaintext—
not the ideal solution for sensitive information such as passwords and keys. This is where secrets
come in handy.
Secrets are scoped at namespaces, so their access is restricted to users for that namespace.
Secrets can be mounted as a volume in pod so they can be used to consume information. Note
that secrets are stored in plaintext in an etcd cluster (a data store used by Kubernetes to store its
state). Access to etcd is restricted.
24
Kubernetes objects on Microsoft Azure
Resource Notes
25
Kubernetes objects on Microsoft Azure
PART 3
DEPLOY THE TIERS
It can be a challenge to select the right deployment resource for a given workload. To show which
choices to make when deploying a tiered workload on Kubernetes, this section uses the sample
Kubernetes demo application:
The goal here is to show how to use the resources from Part 2 and demonstrate an approach you
can extend to multitier deployments.
In this sample scenario, if a cluster crashes, data loss is unacceptable for the database files (.mdf,
.ndf, and .ldf). The Kubernetes StatefulSet deployment object allows you to use external persistent
26
Kubernetes objects on Microsoft Azure
storage. This scenario uses Azure Disk Storage as the storage provider, but other storage
solutions can be used.
To deploy a SQL Server database on Azure Storage on Kubernetes, complete the following steps.
Create a secret
Running SQL Server as a container involves passing at least two environment variables:
ACCEPT_EULA and SA_PASSWORD. The latter is obviously a secret.
To create a Kubernetes secrets resource, use the Kubernetes manifest. As Figure 18 shows,
sapassword is declared as a secret with its value specified as a base64-encoded value of a real
password.
Ensure that storageAccount is in the same Azure resource group as the Kubernetes cluster.
27
Kubernetes objects on Microsoft Azure
Create a service
When a database runs as a pod, it needs to include fixed endpoints (protocol, port, and IP
address) that can be called. Kubernetes Service objects provide this abstraction. Unlike a pod, a
service is a set of pods identified by a selector. Running as a Service object decouples the service
from the way it is accessed by other pods or applications.
28
Kubernetes objects on Microsoft Azure
Combining these pieces and creating a YAML file to run yields the result shown in Figure 24.
29
Kubernetes objects on Microsoft Azure
Figure 25. Finding Kubernetes Services (1), the sqlservice service (2), and endpoint (3).
Use that IP address and the SA password to connect from SQL Server Management Studio as
shown in Figure 26.
To create a new database, run the custom database creation script (https://github.com/Mahesh-
MSFT/KubernetesDemoApp/blob/master/sqlscript/dbcreationscript.sql).
StatefulSet creates data disks in the Azure Storage account. You can validate that as well. First
check the volume name in the Kubernetes dashboard as Figure 27 shows.
30
Kubernetes objects on Microsoft Azure
Make sure this volume is available in the Azure Storage account as well by opening the Azure
portal. Figure 28 shows that the external storage is available.
31
Kubernetes objects on Microsoft Azure
Figure 29. Using the fdisk -1 command to verify Azure Disk is attached as a device on the node.
As Figure 29 shows, /dev/sdc matches the 10 GiB storage from Azure Disk. This information can
be used to mount on a directory and verify its content as Figure 30 shows.
Figure 30. 1) Create a temporary directory. 2) Mount it from the external volume. 3) List contents. 4) Go to
/mssql/data (the SQL Server data and log files). 5) List contents; verify that the .mdf and .ldf files are created for
the new database.
This verification step proves that SQL Server data and log files are stored on Azure Disk. Even if
the Kubernetes cluster crashes, the data can be safely recovered from those files.
32
Kubernetes objects on Microsoft Azure
This section shows you how to deploy the sample ASP.NET Core front end using the
horizontalpodautoscaler resource. It demonstrates scaling up and down based on calculating a
pod’s CPU utilization percentage. For details about this tool, see Horizontal Pod Autoscaler
Walkthrough in the Kubernetes documentation.
You might wonder how we know the CPU utilization of a pod. Monitoring provides the answer.
It’s important to monitor a Kubernetes cluster and collect metrics from each node. For our demo
app, Heapster is installed with InfluxDB, a time-series database, and Grafana, a monitoring board.
Metrics are collected, and then used to scale operations up and down.
For more information about the levels of customization that are available, see How to Utilize the
"Heapster + InfluxDB + Grafana" Stack in Kubernetes for Monitoring Pods and Deploying
Heapster to Kubernetes.
33
Kubernetes objects on Microsoft Azure
34
Kubernetes objects on Microsoft Azure
Next, specify a public static IP address that is different from the one used for creating the SQL
Server service in the previous section as Figure 32 shows.
Create services
This step involves running the kubectl create -f. command from a location where the three
YAML files exist. First, copy the path as shown in Figure 33.
As Figure 34 shows, all the components in all the YAML files found in the location shown in Figure
33 are created.
Figure 34. Create the components using the kubectl create -f. command.
To verify that all the services have been created and are running, go to the Kubernetes dashboard.
In the Namespace box, select All namespaces (item 1 in Figure 35). The Services view (2) shows
the new services created in the kube-system namespace (3).
35
Kubernetes objects on Microsoft Azure
Figure 35. Verifying that new services have been created in the Kubernetes dashboard.
The external endpoint for the Grafana service should be the Azure IP address (item 3 in Figure 35).
Click the link to navigate to the Grafana dashboard (Figure 36). The Kubernetes dashboard can
now display the resource utilization information for the cluster and for the pods.
Create deployment
A deployment resource uses a custom Docker image of an ASP.NET Core application that
connects with SQL Server. For the data source, an ASP.NET Core application uses
connectionstring, pointing to <External IP Address of SQL Service, 1433> using the five settings
shown in Figure 37.
36
Kubernetes objects on Microsoft Azure
For more information about how a deployment resource uses a custom Docker image, see the
blog Running SQL Server + ASP.Net Core in a container on Linux in Azure Container Service on
Docker Swarm – Part 2.
Create service
A service has a static IP address that can be used to reach an application regardless of any failures
of the underlying pods. Service addresses problems associated with dynamic IP addresses that
are assigned to pods when they crash and are recreated by the deployment. By comparison,
deployment only ensures that a certain number of pods are created and maintained.
In Figure 38 the LoadBalancer type ensures that a load balancer and associated IP address are
generated in Azure when this service is created. It also uses the selector value app:
aspnetcoreapp, which must match the deployment definition. This service will include all the pods
that have the labels app: aspnetcoreapp.
Figure 38. Creating a service resource. The service generates a load balancer (1) and includes pods labeled
37
Kubernetes objects on Microsoft Azure
aspnetcoreapp (2).
Create horizontalPodAutoScaler
Finally, a horizontalpodautoscaler uses the deployment resource and specifies the scale up
rules. As Figure 39 shows, it targets deployment aspnetcoreapp-deployment (item 1 in Figure
39). The rules are specified using minReplicas and maxReplicas and are associated with
targetCPUUtilizationPercentage.
Deployment replicas can be queried using the get hpa command. As Figure 40 shows,
HorizontalPodAutoScaler is now set to increase the number of pods to two if the CPU utilization
exceeds 20 percent. The value of REPLICAS shows that only one pod is currently running.
Figure 40. Deployment replicas query using kubectl get hpa command.
Troubleshoot issues
Sometimes, Kubernetes doesn't display the current CPU utilization. This known issue is
documented, as is a solution.
38
Kubernetes objects on Microsoft Azure
Figure 41. Current number of replicas using the kubectl get deployment command
Next, launch the application by going to the external URL, and in a new command window, launch
another parallel workload, as shown in Figure 42. This command generates a CPU load and
generates a pod. After a while, the CPU utilization of this pod, as well as the ASP.NET Core pod,
start to increase.
To verify the results, run the get hpa command. As Figure 43 shows, the current CPU utilization is
now more than 20 percent.
Figure 43. Showing the scaling results with the kubectl get hpa command.
Figure 44. Verifying the replica with the kubectl get deployment command.
In addition, the service is now running with two pods, as shown in Figure 45.
39
Kubernetes objects on Microsoft Azure
Next, stop the load-generator deployment and ASP.NET Core application. After a while, use the
kubectl get hpa command again to note the CPU utilization (Figure 46).
Figure 46. Checking CPU utilization with the kubectl get hpa command.
The replica count should also return to one (1) as Figure 47 verifies.
Figure 47. Checking replica count with the kubectl get deployment command.
The Kubernetes dashboard also verifies that the service is back to running with one pod (Figure
48). These steps confirm that Service can scale up pods and replica count when the CPU
percentage increases to match the manifest. Service can also scale down based on same metric.
40
Kubernetes objects on Microsoft Azure
Copy the external endpoint, which is a load-balanced URL serving the application. Paste it in a
new browser tab to go to the application home page (Figure 50).
Figure 50. Go to the designated external endpoint to display the application home page.
41
Kubernetes objects on Microsoft Azure
Figure 51. Verifying the database (1) in SQL Server Management Studio. Choose a table (2), select query (3), to
see results (4).
Conclusion
I hope this guidance helps you choose the deployment most appropriate for you based on the
elements that make up your given workload, whether a stateless web front end or API tier and
stateful database back end. Please use this white paper and the GitHub resources as a jumping off
point for other aspects of development using Kubernetes. Consider setting up local development
and remote Azure clusters. Try configuring and monitoring workloads on each of these
environments. Good luck!
42