azure container instances - techtrainingpoint · hosting azure containers: why use azure container...

23
Krunal Trivedi AZURE CONTAINER INSTANCES -Krunal Trivedi ABSTRACT In this article, I am going to explain what are Azure Container Instances, how you can use them for hosting, when you can use them and what are its features. Along with that, I am going to provide a quick demo of how to create Azure Container Instances.

Upload: others

Post on 22-Jul-2020

5 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Krunal Trivedi

AZURE

CONTAINER

INSTANCES -Krunal Trivedi

ABSTRACT In this article, I am going to explain what are

Azure Container Instances, how you can use

them for hosting, when you can use them and

what are its features. Along with that, I am

going to provide a quick demo of how to create

Azure Container Instances.

Page 2: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Krunal Trivedi

Hosting Azure Containers: Why use Azure Container Instances?

With the Docker containers you can run them locally on your development

machine, but when you are ready to deploy the containers to the cloud, you are

going to need a server for them to run on and Azure offers several different

options for hosting the containers.

1) You could create a virtual machine in Azure and install Docker on it. Then

when you want to run a container, you can simple run Docker commands to

that VM and ask it to create an instance of your container image. And you

could also run multiple containers on a single VM, but there is a backlash to

it. You have not a virtual machine that you need to maintain and keep

paying for even if it’s running any containers or not.

2) Second option is to use a container orchestrator. Here you have a cluster of

virtual machines usually called nodes, each of which is able to run

containers and then you have an orchestrator that decides which of your

nodes in your cluster should actually run the containers. By doing that, you

can tell it which containers form your application and often for the

purposes of resilience and scale you can ask it to run multiple instances of

some of your containers and also spread them out across the cluster. The

most famous orchestrators which all of you must have heard is Kubernetes

and Azure makes this very easy to use with the Azure Kubernetes Service.

AKS gives you a managed instance of Kubernetes that you don’t need to

install on your own. With this service, you have to pay for all of the nodes in

your cluster and you need to pay whether they are actually running any

containers or not.

3) You can also host your containers in Azure App Services if you want to host

your web apps as containers.

4) Azure Batch to host containers is used if you need to work through a queue

of batch jobs.

5) Azure Service Fabric is there to help you build highly distributed and

scalable applications. In fact, Azure Service Fabric is an orchestrator that is

designed by Microsoft itself.

Page 3: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Krunal Trivedi

So, these are five different ways that you can get your containers running in

Azure. Then how do the Azure Container Instances come into the picture? One

thing that all the above five mentioned options have in common is that they

assume that you have already deployed the container hosting infrastructure to

Azure even before you actually run your containers. For instance, I have some

container that I want to get running in Azure. Suppose I don’t a VM running

Docker or a Kubernetes cluster then I will have to create one of those first and

then I will have to deploy my container and that is going to take some time before

I am ready to run my container. Just in case even I already have a Docker VM or a

Kubernetes cluster, there are chances that it has already something running on it,

some that it was originally created for. So, I would not want to re-use my existing

Docker VM. Imagine having a serverless way to run your containers in Azure. You

can just go and tell Azure that here is my container image, run it. Then Azure

would go and spin up a new container instance on a VM that it (Azure itself)

controls. This is exactly what Azure Container Instances allow you to do. You

really don’t have to manage Docker servers or Kubernetes clusters. You just need

to specify the container which you want to run and Azure will provide the

compute that actually runs it, all behind the curtains. And yes, one of the greatest

benefits of using this is that you only pay while your container is running. Azure

Container Instances has per-second billing model. Suppose you only need some

container running for 7 minutes, you create it, let it run for 7 minutes and then

stop it. That is all you have to pay for. You also don’t have to worry about shutting

down the virtual machine that was the running the container when you are

finished with it.

Page 4: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Krunal Trivedi

Azure Container Instances: Features and When to use them

Sometimes we have containerized workloads that require us to permanently run

containers. There could be a website or a database in a container that needs to be

continuously running. However, long-running containers are not cost effective to

host on Azure Container Instance because they would end up costing twice as

much as just hosting in a virtual machine.

Supposing if I want to perform a continuous integration build using Docker

container and a build happens every time I push code to my central repository. If

my build takes 10 minutes and if I commit code twice daily on weekdays, it means

I am going to do 40 builds and take total of 400 minutes. So if I want to allocate a

build container that has 4 GB of RAM and 2 core then 400 minutes with ACI would

only cost me around ₹ 68 approximately. But if I use a virtual machine for the

same, it would cost me around ₹ 3740 approximately. And in fact, the virtual

machine would sit idle for most of the time. So, despite costing more for

continuously running containers, ACI can actually save a lot of money compare to

virtual machines. This is very common when you are developing software to run

small short-lived experiments. If you want to get a test instance of something

running in the cloud, with ACI, you can quickly spin up a container, do your testing

and then delete it when you are done. And here, you will have to only pay for a

few hours of usage.

You can also use Azure Container Instances for batch jobs where you have

container image that has to process the jobs in your batch like media transcoding.

Apart from all this, there are chances that your workload is sometimes highly

variable. Probably the incoming traffic to a website might be extremely high or

you might have to upload large amounts of data overnight. One way it is possible

to scale your cluster up by adding extra virtual machines. But the other way, and

the better one is to use ACI to elastically handle bursts of load without you

needing to provision any type of extra hardware. You can say that Azure

Container Instances might not be the right fit for every type of containerized

Page 5: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Krunal Trivedi

workload but they can be the best option there is when you need to run short-

lived containers to handle occasional workloads or high bursts of additional work.

Continuously Running Occasionally Running Websites Continuous Integration

Databases Quick Experiments Not cost effective compared to virtual machines

Load Testing and Batch Jobs

Handle Spikes in Load

Features of Azure Container Instances:

1) Azure Container Instances are easy to create using scripting languages such

as Azure CLI. You can add and manage them using PowerShell, C# SDK or

ARM templates.

2) You can configure networking features like assigning a public IP address,

add a prefix for its domain name and you can also choose what ports to

expose.

3) Azure Container Instances can run either Windows or Linux containers.

4) Linux containers are faster to start up because their image sizes are much

smaller compared to Windows containers. Windows containers currently

have a number of limitations and do not support all the exact same

features that Linux containers do but hopefully that will change soon.

5) Azure Container Instances can have restart policy allowing you to state that

what should happened when the container stops.

6) Azure Container Instances also allow you to mount volumes, where Azure

file share is the most common use case. Other than that, you can also

mount secret volumes or even git repositories as a volume.

7) At the time of creating your container instance, you can optionally specify

the command line just like you could it Docker where you can override the

default initial command for a container.

8) You can specify environment variables for your containers.

Page 6: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Krunal Trivedi

9) Last but not the least, you can also access the logs that the Azure Container

Instances emit.

Page 7: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Creating Azure Container Instances : Creating Container Group – Step By Step Demo

Krunal Trivedi - MVP Azure

Page 8: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

• There are multiple ways to create an Azure Container Instance• You can create them directly in the Azure Portal

• You can use the PowerShell cmdlets

• You can use Azure Resource Manager Templates

• You can use Azure Command Line Interface

Krunal Trivedi - MVP Azure

Page 9: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

• In this demo, I will be using the Azure CLI because it is cross platform and can be used across any platform.

• Also in the Azure CLI, the command syntax is very simple.

• We are going to deploy a website running the open source Ghost blogging platform.

Krunal Trivedi - MVP Azure

Page 10: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Login to your Azure account and switch to your preferred subscription.

Krunal Trivedi - MVP Azure

Page 11: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Save the resource group name and location name in their respective variables.

Krunal Trivedi - MVP Azure

Page 12: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Create a resource group by running the following command.

Krunal Trivedi - MVP Azure

Page 13: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

Next, store the container group’s name in a variable.

Krunal Trivedi - MVP Azure

Page 14: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

This command creates the container group. We have specified the resource group name, the container group name, the official Ghost image of the Ghost blogging platform which we are going to pull down from Docker hub. The default port is 2368 by Ghostand we have specified that the IP address should be public and with that we have given it a DNS label name.

Krunal Trivedi - MVP Azure

Page 15: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

It would take some time for the container to be up and running so initially, it would show Pending in the provisioning state.

Krunal Trivedi - MVP Azure

Page 16: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

To check the status, you can run the following command.

Krunal Trivedi - MVP Azure

Page 17: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

You would be able to see after some time that it shows the provisioning state as succeeded. It would give a public IP address and a fully qualified domain name as well.

Krunal Trivedi - MVP Azure

Page 18: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

If you access the domain name at port 2368, you would be able to see something like this.

Krunal Trivedi - MVP Azure

Page 19: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

• So you can say that this is a very fast way to get up and running with a bit of open source software.

• Supposing that we wanted it to run permanently, we would have found a cheaper way of hosting it, maybe by hosting the container on Azure App Service.

• This is just an easy way to see how things are actually done.

Krunal Trivedi - MVP Azure

Page 20: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

• Next, let us see how we can access the logs of our container.

Krunal Trivedi - MVP Azure

Page 21: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

You can simple run this command to see the container logs.

Krunal Trivedi - MVP Azure

Page 22: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

• It is important to note that here in our container group, we just have a single container. Although we can have multiple containers in the same container group and can access the same through Azure CLI.

• We are done with this part of the demo so we need to delete the resource group now.

• You can also just delete the container group and that would stop the container and ensure that you are not paying anymore but it is a good practice to always delete the resource group just in case you might have created some additional resources as a part of your experiment.

Krunal Trivedi - MVP Azure

Page 23: AZURE CONTAINER INSTANCES - Techtrainingpoint · Hosting Azure Containers: Why use Azure Container Instances? With the Docker containers you can run them locally on your development

You can delete the resource group by running this command with the –y flag that would tell that yes, you really want to delete it.

Krunal Trivedi - MVP Azure