动态编译 静态编译_静态编译的阿里巴巴云容器服务

动态编译 静态编译

We received a lot of great entries in our recent competition to find the best tip for making the most out of Alibaba Cloud services. It was a fun but challenging task for our judges to pick the winners amongst so many helpful and interesting entries. But alas after fiery deliberations and heated debates they’ve decided that the third prize of the competition goes to David Banham. His winning entry is a succinct tutorial on statically compiling a Go program, and using Docker to containerize and distribute it.

在最近的竞赛中,我们收到了很多精彩的作品,以找到充分利用阿里云服务的最佳技巧。 对于我们的评委来说,从众多有用和有趣的参赛作品中选出获奖者是一项有趣而艰巨的任务。 但是,经过激烈的讨论和激烈的辩论,他们决定将比赛的三等奖授予David Banham。 他的获奖作品是关于静态编译Go程序并使用Docker对其进行容器化和分发的简洁教程。

Alibaba Cloud’s Container Service is a great example of how Docker and Kubernetes are revolutionising the cloud landscape. The curmudgeons will rail that it’s all still just some software running on someone else’s computer, but the transformative difference is that k8s and Docker provide what is effectively a platform-agnostic management API. If you build your DevOps pipelines against k8s you have the lowest possible switching friction between AWS, Google Cloud, Azure and Alibaba. The closer we can get to the dream of write once, run anywhere, the better!

阿里云容器服务是Docker和Kubernetes如何彻底改变云环境的一个很好的例子。 抱怨者会抱怨说,所有这些仍然只是在他人计算机上运行的某些软件,但与之不同的是,k8和Docker提供了与平台无关的有效管理API。 如果您针对k8构建DevOps管道,则在AWS,Google Cloud,Azure和阿里巴巴之间的切换摩擦将达到最低。 我们离梦想一次写梦,在任何地方运行都越近越好!

Another tool I love for enhancing software portability is the Go language. Cross compilation in Go is as easy as falling off a log. I develop software on my Linux laptop and in the blink of an eye I can have binaries built for Windows, OSX, WASM, etc! Here’s the Makefile snippet I use to do it:

我喜欢增强软件可移植性的另一个工具是Go语言。 Go中的交叉编译就像摆脱日志一样容易。 我在Linux笔记本电脑上开发软件,眨眼间就可以为Windows,OSX,WASM等构建二进制文件! 这是我用来做的Makefile片段:

name = demo

PLATFORMS := linux/amd64 windows/amd64 linux/arm darwin/amd64

temp = $(subst /, ,$@)
os = $(word 1, $(temp))
arch = $(word 2, $(temp))

release:

make -l inner_release

inner_release: $(PLATFORMS)

$(PLATFORMS):
@-mkdir -p ../web/api/clients/$(os)-$(arch)
@-rm ../web/api/clients/$(os)-$(arch)/*
GOOS=$(os) GOARCH=$(arch) go build -o '../web/api/clients/$(os)-$(arch)/$(name) .
@chmod +x ../web/api/clients/$(os)-$(arch)/$(name)
@if [ $(os) = windows ]; then mv ../web/api/clients/$(os)-$(arch)/$(name) ../web/api/clients/$(os)-$(arch)/$(name).exe; fi
zip --junk-paths ../web/api/clients/$(os)-$(arch)/$(name)$(os)-$(arch).zip ../web/api/clients/$(os)-$(arch)/*
@if [ $(os) = windows ]; then cp ../web/api/clients/$(os)-$(arch)/$(name).exe ../web/api/clients/$(os)-$(arch)/$(name); fi

Neat! That will get you a tidy little binary that will run on your target operating systems. Even that is overkill if you’re targeting a Docker host like Cloud Container Service. All you need to do there is just GOOS=linux GOARCH=amd64 go build and you’re done! Then, you just need to choose your favorite Linux distribution and build that into the Dockerfile.

整齐! 这样您将获得一个整洁的小二进制文件,它将在您的目标操作系统上运行。 如果您以Cloud Container Service之类的Docker主机为目标,那甚至是过高的。 您只需要执行GOOS=linux GOARCH=amd64 go build ! 然后,您只需要选择自己喜欢的Linux发行版并将其构建到Dockerfile中即可。

What if we wanted to simplify our lives even further, though? What if we could do away with the Linux distribution entirely?

但是,如果我们想进一步简化生活怎么办? 如果我们可以完全取消Linux发行版怎么办?

Go supports the compilation of statically linked binaries. That means we can write code that doesn’t rely on any external DLLs at all. Observe this magic Dockerfile:

Go支持静态链接二进制文件的编译。 这意味着我们可以编写完全不依赖任何外部DLL的代码。 观察以下神奇的Dockerfile:

FROM scratch

ADD ./bin/web /app

ENTRYPOINT ["/app"]

What is scratch? It’s nothing. That tells Docker to bundle in nothing except the Linux kernel and the bare bones required to make Docker work. Smaller than Ubuntu? There’s Alpine. Smaller than Alpine? There’s scratch.

什么是刮擦? 没什么。 这告诉Docker除了Linux内核和使Docker工作所需的裸露骨骼外,什么都没有捆绑。 比Ubuntu小? 有高山。 比高山小吗? 有刮痕

What’s cooler than being Alpine? Ice cold! Uh, scratch. Alright alright alright alright.

有什么比阿尔卑斯山凉爽的? 冰冷! scratch 好吧好吧好吧好吧。

The benefit of this is that we remove a whole host of moving parts from our deployment. There’s no chance that a distro maintainers decision to bundle or not-bundle some service in their distro will take down your production environment. There are no security holes introduced by distro code. There are no updates to manage. There’s just… nothing.

这样做的好处是,我们从部署中删除了大量的活动部件。 发行版维护人员决定在发行版中捆绑或不捆绑某些服务的机会不会降低您的生产环境。 发行版代码没有引入安全漏洞。 没有要管理的更新。 只是……什么都没有。

As a bonus, you get a teeny tiny Docker image that’s easier to ship around. Easier to ship around to any cloud you like!

作为奖励,您将获得一个很小的Docker小型映像,该映像更易于交付。 轻松运送到您喜欢的任何云!

Give it a shot. Write some Go, statically compile it and put it in a scratch Docker container. Deploy it across Alibaba, AWS and Google Cloud just for funsies! See which service suits you the best.

试一试。 编写一些Go并进行静态编译,然后将其放入一个临时Docker容器中。 仅出于娱乐目的,将其部署在阿里巴巴,AWS和Google Cloud上! 查看最适合您的服务。

翻译自: https://www.sitepoint.com/statically-compiled-go-on-alibaba-cloud-container-service/

动态编译 静态编译

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值