我们为什么要关心容器的发展(译文)
By robot-v1.0
本文链接 https://www.kyfws.com/games/why-should-we-care-about-containers-for-developmen-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2844 个词 阅读量 0我们为什么要关心容器的发展(译文)
原文地址:https://www.codeproject.com/Articles/1246996/Why-Should-We-Care-About-Containers-for-Developmen
原文作者:Shayne P Boyer
译文由本站 robot-v1.0 翻译
前言
Why should we care about containers for development
我们为什么要关心容器的开发
在您的开发生涯中,可能已经有不止一次的时间,您花了几个小时对问题进行故障排除,只是发现它是依赖项或版本问题,对吗?(There has probably been more than one time in your development career where you’ve spent a few hours troubleshooting an issue only to find out it was a dependency or versions issue, right?) 从一个环境到另一个环境,过时的组件和设置开发机器都是我们所有人无法避免的挫折.(Environments varying from one to next, outdated components and setting up development machines are frustrations we can all do without.) 我们已经使用虚拟机解决了其中的一些问题,但是管理整个计算机并针对每种环境充分利用它们的成本很高.这是容器解决许多挑战的地方.(Some of these issues we’ve solved with VMs, but managing the entire machine and underutilizing them for each environment is costly. This is where containers have come to solve many challenges.)
为什么选择集装箱(Why Containers)
毫无疑问,您在过去一年或更长时间里听到过有关集装箱的热议.如果不是容器,则与之关联的某些技术,框架或工具; Docker,Kubernetes,微服务只是其中的一些.从开发人员的角度来看有什么好处?(It’s no doubt that you have heard the buzz about containers over the last year or longer. If not containers, then some technology, framework, or tooling associated with it; Docker, Kubernetes, microservices are just a few of these. What are the benefits from a developer perspective?)
在我的机器上工作(Works on My Machine)
您的代码或应用程序部署到的每个环境的方案可能不同.多少服务器,CPU,RAM等的配置可能会因开发,质量检查和生产而异,因此不能保证该应用程序将执行相同的操作.使用容器提供了在启动时定义这些参数的能力,从而使每个映像都具有相同的要求,无论其部署在何处.(The scenario of each environment your code or application is deployed to could be different. How many servers, the configuration of CPU, RAM, etc. may vary from Development, QA, and Production and there is no guarantee the app will perform the same. Using containers provides the capability to define these parameters upon startup such that each image has the same requirements regardless of where it is deployed.)
docker run --cpus=2 --memory=1.5g myapp
微服务(Microservices)
包含许多服务的应用程序可能无法使用相同的语言或框架进行开发.例如,如果您正在.NET Core中开发服务,以调用用Node.js或Go编写的下游服务,则负责该服务的开发团队可以构建容器映像并将其在您的私有注册表中使用.(Applications that are comprised of many services may not be developed in the same language or framework. If you are developing a service in .NET Core for instance that calls a downstream service written in Node.js or Go, the development team responsible for that service can build the container image and make it available in your private registry.) 用一个(Using a)**码头工人组成(docker-compose)**文件,然后可以引入该容器映像而无需在计算机上安装Node.js或Go并针对开发版本测试调用.(file, you can then bring in that container image without needing Node.js or Go installed on your machine and test your calls against the development version.)
services:
nodeservice:
image: nodeservice:dev
environment:
- NODE_ENV=Development
namesweb:
depends_on:
- nodeservice
image: namesweb
build:
context: .
dockerfile: namesweb/Dockerfile
environment:
- NODE_SERVICE_ENDPOINT=NodeService-Dev
ports:
- "57270:80"
测试数据(Test Data)
很少有不涉及数据的应用程序,因此在本地维护生产系统的实例是一项任务.安装工具,平台,服务器等,以及使所有这些项目保持最新状态,已经超出了您作为开发人员可能要担心的范围.(There are a rare amount of applications that do not involve data and maintaining an instance of your production system locally can be a task. Installing the tools, platform, server, etc., as well as keeping all of these items up to date is above and beyond what you’d probably want to worry about as a developer.) 在此docker-compose文件中,启动了Linux上的SQL Server实例,并使用脚本文件创建了测试数据库,最后,使用(Here in this docker-compose file, an instance of SQL Server on Linux is started and with a script file, the test database is created, and finally, test data is loaded using the) 批量复制工具(Bulk Copy tool) .(.)
version: '3.0'
services:
mssql:
image: microsoft/mssql-server-linux:latest
container_name: mssql
ports:
- 1433:1433
volumes:
- /var/opt/mssql
# we copy our scripts onto the container
- ./sql:/usr/src/app
# bash will be executed from that path, our scripts folder
working_dir: /usr/src/app
# run the entrypoint.sh that will import the data AND sqlserver
command: sh -c ' chmod +x ./start.sh; ./start.sh & /opt/mssql/bin/sqlservr;'
environment:
ACCEPT_EULA: 'Y'
SA_PASSWORD: P@$$w0rd
start.sh(start.sh)
#!/bin/bash
wait_time=20s
echo creating resources in $wait_time
sleep $wait_time
echo starting...
echo 'creating Names DB'
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P $SA_PASSWORD -i ./init.sql
echo 'creating Names Table'
/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P $SA_PASSWORD -i ./data/NameTable.sql
echo 'importing data...'
# Bulk load data from a csv file
/opt/mssql-tools/bin/bcp Names in data/Names.csv -S 0.0.0.0 -U sa -P $SA_PASSWORD -d Names -c -t ','
echo 'add uniqueid column'
/opt/mssql-tools/bin/sqlcmd -S localhost -d Names -U SA -P $SA_PASSWORD -I
-Q "ALTER TABLE Names ADD ID UniqueIdentifier DEFAULT newid() NOT NULL;"
echo 'checking data'
/opt/mssql-tools/bin/sqlcmd -S localhost -d Names -U SA -P $SA_PASSWORD -I
-Q "SELECT TOP 5 ID,FullName FROM Names;"
init.sql和NameTable.sql(init.sql & NameTable.sql)
DROP DATABASE Names
CREATE DATABASE Names;
GO
USE Names;
GO
CREATE TABLE Names(
LastName nvarchar(max),
FirstName nvarchar(max),
FullName nvarchar(max)
);
GO
使用此测试数据库映像,将加载生产数据的一部分,并可供应用程序使用.的(Using this test database image, a subset of the production data is loaded and available for the application to use. The) container_name: mssql
设置为(is set as the)服务器名称(server name)用于连接字符串.该应用程序所需要做的就是立即更改此服务器,只需更改设置即可.(for the connection string. All that is needed for the application is to hit this server now is a change to the setting.)
如果对所有容器都使用docker-compose,则将自动处理DNS名称解析.要单独使用数据库容器,请使用以下命令获取IP地址(If you are using docker-compose for all of the containers, the DNS name resolution is handled automatically. To use the database container individually, get the IP Address by using the command) docker inspect -f "{{ .NetworkSettings.IPAddress }}" <containerId>
"ConnectionStrings": {
"NamesConnection": "Server=mssql;Initial Catalog=Names;User=sa;
Password=P@55w0rd;MultipleActiveResultSets=true"
}
部署方式(Deployment)
从来没有一种容易部署应用程序的场景,对吗?回滚失败的过程更困难,有时,这本身几乎是另一种部署.(Never is there a scenario of deploying applications easy, right? Rolling back on failures ever a harder story and at times, this is almost in itself another deployment.)
无论您是使用完整的CI/CD系统来构建容器,还是通过命令行工具(Whether you are using a full CI/CD system to build your containers, command line tooling through) docker build
或Visual Studio Docker工具;从注册表部署到主机时,所有依赖项都与映像隔离,它仍然是同一容器.(or Visual Studio Docker Tools; all of the dependencies are isolated to the image when deployed to the host machine from a registry, it is still that same container.)
如果部署存在问题,事情肯定会出错,将部署回滚到某个版本就像将映像的版本号从1.1更改为1.0一样简单,并且您的应用会重置.(Should there be problems with a deployment, things do go wrong, rolling a deployment back a version can be as simple as changing the version number of an image from 1.1 to 1.0 and your app is reset.)
概要(Summary)
容器为我们的应用程序及其依赖项提供了打包和部署机制.容器注册表是一个强大的概念,可帮助我们部署和分发应用程序.(Containers provide a packaging and deployment mechanism for our application and its dependencies. The container registry is a powerful concept that helps with the deployment and distribution of our application.) 容器在本地工作时还可以改善我们开发经验的"内在循环",尤其是当我们倾向于通过单片应用程序构建微服务时.它们在包括云在内的本地和远程环境中提供了更高的奇偶校验,并帮助我们的基础架构变得更加不变.(Containers also improve the “inner loop” of our development experience when working locally, particularly as we trend towards building microservices over monolithic applications. They provide greater parity across local and remote environments including the cloud and help our infrastructure to become more immutable.) 围绕容器的充满活力的工具生态系统还帮助我们使用云原生平台和开发方法.无论我们使用无服务器容器,支持容器的PaaS平台还是Kubernetes之类的业务流程协调器,我们都专注于我们的应用程序,而不是考虑和管理将其部署到的单个或多个主机.(The vibrant ecosystem of tooling around containers also help us consume cloud-native platforms and development methodologies. Whether we are using serverless containers, a PaaS platform that supports containers, or an orchestrator like Kubernetes, we focus on our application instead of thinking about and managing the individual host or hosts we deploy it to.)
资源资源(Resources)
- Azure容器开发中心(Azure Container Dev Center)
- Visual Studio Docker工具(Visual Studio Docker Tools)
- Azure容器实例(Azure Container Instances)
- Azure Kubernetes服务(AKS)(Azure Kubernetes Service (AKS))
- Linux上的SQL Server(SQL Server on Linux)
代码项目(CodeProject)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
cloud .NET-Core Linux SQL-Server DevOps docker containers 新闻 翻译