Context

As companies are continuously seeking ways to become more Agile and embracing DevOps culture and practices, new designs principles have emerged that are more closely aligned with those aspirations.  One such a design principle that had gained more popularity and adoption lately is Microservices.  By decomposing applications to smaller independent components, companies are able to achieve the following benefits:

  • Ability to scale specific components of the system. If “Feature1” is experiencing an unexpected rise in traffic, that component could be scaled without the need to scale the other components which enhances efficiency of infrastructure use
  • Improves fault isolation/resiliency: System can remain functional despite the failure of certain modules
  • Easy to maintain: teams can work on system components independent of one another which enhances agility and accelerates value delivery
  • Flexibility to use different technology stack for each component if desired
  • Allows for container based deployment which optimizes components’ use of computing resources and streamlines deployment process

I wrote a detailed blog on the topic of Microservices here

In this blog, I will focus more on the last bullet.  We will see how easy it is to get started with Containers and have them a deploy mechanism of our Microservices.  I will also cover the challenge that teams might face when dealing with a large number of Microservices and Containers and how to overcome that challenge

Creating a simple web API with Docker Support using Visual Studio

For this section, I am using Visual Studio 2017 Enterprise Edition to create the Web API.

  • In Visual Studio, go to File => New => Project => Web => ASP.NET Core Web Application (.NET Core)

  • Select Web API and make sure that Enable Docker Support is enabled and then click OK to generate the code for the web API

Note that the code generated by Visual Studio has the files needed by Docker to compile and run the generated application

  • Open docker-compose.yml file and prefix the image name with your docker repository name. For example, if your Docker repository name is xyz then the entry in the docker-compose.yml should be:  image: xyz/sampleapi  Here is how that file should look like:
version: '1'
services:
sampleapi:
image: xyz/sampleapi
build:
context: ./SampleAPI
dockerfile: Dockerfile
  • Check in the generated Web API code into Version Control System. You can use the git repository within Visual Studio Team Services for free here
  • Checkout the code to an Ubuntu machine with Docker engine installed. In Azure you can provision an Ubuntu machine with Docker using “Docker on Ubuntu Server” as shown below

  • Run the code by navigating to the directory where the repository was cloned and then run the following command

docker-compose -f docker-compose.ci.build.yml up && docker-compose up

  • Verify that the container is running by entering this command

docker ps

  • This should return the container that was just created in the previous step. The output should look something similar to below

  • Finally push the docker image created (i.e. xyz/sampleapi) to the Docker image Registry

docker push  xyz/sampleapi:latest

As your application becomes more popular and users ask for more features, new microservices will need to be created.  As the number of microservices increases, so is the complexity of deploying, monitoring, scaling and managing the communication among them.  Luckily there are orchestration tools available that make this task more manageable.

Orchestrating Microservices

Managing a large number of Microservices can be a daunting task.  Not only will you need to to track their health, you will also need to ensure that they are scaled properly, deployed without interrupting users and also recover when there are failures.  The following are orchestrators that have been created to meet this need:

  • Service Fabric
  • Docker Swarm
  • DC/OS
  • Kubernetes

Stacking up these tools against one another is out of scope for this blog.  Instead, I will focus more on how we can deploy the Web API we created earlier into Kubernetes using Azure Container Service.

Creating Kubernetes in Azure Container Service (ACS)

For this section, I am using a Windows Server 2016 machine.  You can use any platform to achieve this but you might need to make a few minor changes to the steps detailed below

  • Install Azure CLI using this link
  • Login to Azure using Azure CLI by entering the following command

az login

  • Follow the steps to login to Azure using Azure CLI. Also if you have access to multiple subscriptions, make sure the right subscription is selected

az account set  –subscription “your subscription ID”

  • Create a resource group

az group create -l eastus -n webAPIK8s

  • Create Kubernetes cluster in ACS under the resource group created

az acs create -n myKubeCluster -d myKube -g webAPIK8s  –generate-ssh-keys –orchestrator-type kubernetes –agent-count 1 –agent-vm-size Standard_D1

  • Download the Kubernetes client by entering this command:

az acs kubernetes install-cli –install-location=C:\kubectl\kubectl.exe

Note: Ensure that kubectl folder exists otherwise the command would throw an error

  • Get the Kubernetes cluster credentials

az acs kubernetes get-credentials –resource-group=webAPIK8s  –name=myKubeCluster

  • Tunnel into the Kubernetes cluster

C:\kubectl\kubectl.exe proxy

  • Open a web browser and navigate to http://localhost:8001/ui

Deploying the Web API

To deploy to the Kubernetes cluster, a deployment descriptor is needed.  Create a file called sampleapi.yml in the machine you have used to connect to the Kubernetes cluster and fill it with the following:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: sampleapi-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: sampleapi
spec:
containers:
- name: sampleapi
image: xyz/sampleapi:latest
ports:
- containerPort: 80
  • Deploy the app by opening a command line, navigating to where the yml file is located and running

C:\kubectl\kubectl.exe apply -f sampleapi.yml

  • To expose the API outside of the cluster, run the following command

C:\kubectl\kubectl.exe expose deployments sampleapi-deployment –port=80 –type=LoadBalancer

  • Periodically check whether the service is exposed. Keep running this command until the service gets an External-IP

C:\kubectl\kubectl.exe get services

  • Once the service is exposed, obtain its external IP (i.e. xxx.xxx.xxx.xxx) and check whether you can access the api by using either Rest client (i.e. Postman) or simply opening a web browser and entering:

http://xxx.xxx.xxx.xxx/api/values

Running Web API using Docker and Kubernetes的更多相关文章

  1. Gitlab CI 自动部署 asp.net core web api 到Docker容器

    为什么要写这个? 在一个系统长大的过程中会经历不断重构升级来满足商业的需求,而一个严谨的商业系统需要高效.稳定.可扩展,有时候还不得不考虑成本的问题.我希望能找到比较完整的开源解决方案来解决持续集成. ...

  2. 如何将.NET 4.0写的Windows service和Web API部署到docker上面

    Web API. 看这篇文章: https://docs.microsoft.com/en-us/aspnet/mvc/overview/deployment/docker-aspnetmvc Win ...

  3. Docker容器环境下ASP.NET Core Web API应用程序的调试

    本文主要介绍通过Visual Studio 2015 Tools for Docker – Preview插件,在Docker容器环境下,对ASP.NET Core Web API应用程序进行调试.在 ...

  4. 在docker中运行ASP.NET Core Web API应用程序

    本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过程中,也会对docker的使用进行一些简单的描述.对于.NET Cor ...

  5. Docker容器环境下ASP.NET Core Web API

    Docker容器环境下ASP.NET Core Web API应用程序的调试 本文主要介绍通过Visual Studio 2015 Tools for Docker – Preview插件,在Dock ...

  6. docker中运行ASP.NET Core Web API

    在docker中运行ASP.NET Core Web API应用程序 本文是一篇指导快速演练的文章,将介绍在docker中运行一个ASP.NET Core Web API应用程序的基本步骤,在介绍的过 ...

  7. 在Docker容器中运行.Net Core web Api项目

    安装Docker环境 参考本人这篇<CentOS 7 下Docker的安装>文章进行安装以及环境配置,这里不做赘述. 通过.NetCore开发WebApi项目 1. 创建.Net Core ...

  8. 品尝阿里云容器服务:初步尝试ASP.NET Core Web API站点的Docker自动化部署

    部署场景是这样的,我们基于 ASP.NET Core 2.0 Preview 1 开发了一个用于管理缓存的 Web API ,想通过阿里云容器服务基于 Docker 部署为内网服务. 在这篇博文中分享 ...

  9. .net core 微服务架构-docker的部署-包括网关服务(Ocelot)+认证服务(IdentityServer4)+应用服务(asp.net core web api)

    本文主要介绍通过Docker来部署通过.Net Core开发的微服务架构,部署的微服务主要包括统一网关(使用Ocelot开发).统一认证(IdentityServer4).应用服务(asp.net c ...

随机推荐

  1. 后端list集合中的数据传递到前台HTML中显示(表格形式)

    关键字:web项目中前后台数据传递问题 在学习web项目的过程中,我们肯定会遇到前后台数据交换问题.这个问题我也思考了很久,今天借此总结一下.由于博主水平有限,如有不当之处,还请大家多多指正,,废话不 ...

  2. 原生js学习 选择dom

    连bootstrap5 都抛弃jquery了,重新使用原生js来写,所以最近在学习原生js. 一.选择dom元素: id let sure=document.getElementById('sure' ...

  3. [MicroPython]TPYBoard v102炫彩跑马灯WS2812B

    一.实验目的 了解ws2812b的工作原理 学习ws2812b的驱动方法 二.实验器材 TPYBoard v102 1块 ws2812b RGB-Ring-8 1个 micro USB数据线 1条 杜 ...

  4. mysql的模糊匹配

    实例: SQL模糊查询,使用like比较关键字,加上SQL里的通配符,请参考以下: 1.LIKE'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden). 2.LIKE'%inger' ...

  5. Python 学习 第十三篇:数据的读写-文件、DataFrame、json

    Python的文件是一个重要的对象,使用open()函数来打开文件,创建文件对象,进行文件的读写操作.当数据用于交换信息时,通常需要把数据保存为有格式的文本数据,可以保存为有特定的行分隔符和列分隔符的 ...

  6. 未安装Oracle客户端的服务器上,使用ASP.NET远程连接Oracle

    公司服务器一直都是使用 .NET+SQLSERVER的开发方式,有个项目需要进行读取远程Oracle的需求.由于oracle 基本不会,也是一边做一遍摸索. 首先是使用工具测试是否已经联通,因为之前用 ...

  7. 浅谈一类无关序列有前缀和性质的统计问题的离线解法 BZOJ3626

    每次询问[l,r]区间,但所有信息是按另一种序列给出的,因此无法使用区间数据结构做这种题.将每个询问改为[1,x],考虑离线,则从1~n依次修改并查询即可. BZOJ3626 给定一颗树,每次询问给定 ...

  8. Stochastic Optimization of PCA with Capped MSG

    目录 Problem Matrix Stochastic Gradient 算法(MSG) 步骤二(单次迭代) 单步SVD \(project()\)算法 \(rounding()\) 从这里回溯到此 ...

  9. 007-迅雷定时重启AutoHotkey脚本-20190411

    ;; 定时重启迅雷.ahk,;;~ 2019年04月11日;#SingleInstance,forceSetWorkingDir,%A_ScriptDir%DetectHiddenWindows,On ...

  10. PS滤镜给城市夜空照片添加满天星

    原图 一.新建空白图层. 二.填充黑色(编辑→填充). 三.转换为智能对象. 四.添加杂色(滤镜→杂色→添加杂色). 五.使用高斯模糊(滤镜→模糊→高斯模糊). 六.如果你想再次修改模糊效果,可双击该 ...