部署Go服务的策略是什么?

huangapple go评论90阅读模式
英文:

Deployment strategies for Go services?

问题

我正在使用Go编写一些新的Web服务。

无论目标平台如何,我可以使用哪些部署策略?例如,我在Mac上进行开发,但是暂存/生产服务器将在Linux上运行。

是否有一些现有的部署工具可以支持Go?如果没有,有哪些方法可以简化这个过程?

我使用LiteIDE进行开发。有没有办法将LiteIDE与部署过程连接起来?

英文:

I'm writing some new web services in Go.

What are some deployment strategies I can use, regardless of the target platform? For example, I'm developing on a Mac, but the staging/production servers will be running Linux.

Are there some existing deployment tools I can use that support Go? If not, what are some things I can do to streamline the process?

I use LiteIDE for development. Is there any way to hook LiteIDE into the deployment process?

答案1

得分: 9

不幸的是,由于Go语言还很年轻,目前还没有太多相关的工具,或者至少很难找到。我对开发这样的工具也很感兴趣。

我发现有些人自己做了一些工具,或者他们改编了其他工具,比如Capistrano,来帮助他们完成这个任务。

很可能你需要自己来做。而且你不必局限于使用shell脚本 - 你可以用Go来做!实际上,许多Go工具都是用Go语言编写的。你应该避免在目标系统上进行编译,因为在生产系统上拥有构建工具通常是一个不好的做法。Go语言使得交叉编译二进制文件非常容易。例如,这是如何为ARM和Linux编译的示例:

GOARCH=arm GOOS=linux go build myapp

你可以尝试加入#go-nuts freenode IRC频道或者加入Go邮件列表,向其他Gophers询问他们在做什么。

英文:

Unfortunately since Go is such a young language not much exists yet, or at least they've been hard to find. I would also be interested in the development of such tools for Go.

What I have found is that some people have been doing it themselves, or they've adapted other tools, such as Capistrano, to do it for them.

Most likely it's something you'll have to do yourself. And you don't have to limit yourself to shell scripts - do it in Go! In fact many of the Go tools are written in Go. You should avoid compiling on the target system as it's usually a bad practice to have build tools on your production system. Go makes it really easy to cross compile binaries. For example, this is how you compile for ARM & Linux:

GOARCH=arm GOOS=linux go build myapp

One thing you could do is hop on the #go-nuts freenode IRC channel or join the Go mailing list and ask other Gophers what they're doing.

答案2

得分: 1

Capistrano听起来是一个很好的部署工具。你也可以像Luke建议的那样进行交叉编译。两者都可以正常工作。

更一般地说...我也在OS X(开发)和Linux(部署)之间犹豫不决,事实上,我最终选择在VirtualBox和Vagrant上开发虚拟机。我使用TextMate 2进行文本编辑,但在Mac上安装许多开发工具真的很麻烦,而且我更喜欢在后台运行Debian或类似的系统。好处是-这个虚拟环境可以模拟部署环境,这样我在部署代码时就可以避免出现意外,无论使用哪种语言。

英文:

Capistrano sounds like a good idea for deployment alone. You can also do cross-compilation as Luke suggested. Both will work just fine.

More generally though... I'm also kind of torn between OS X (development) and Linux (deployment) and in fact I ended just developing in a virtual machine via VirtualBox and Vagrant. I'm using TextMate 2 for text editing but installing many of development tools on a Mac is just a major PITA and I'm just more comfortable with having Debian or the like running somewhere in the background. The bonus is - this virtual environment can mirror deployment environment so I can avoid surprises when I deploy my code, whatever the language.

答案3

得分: 0

我自己还没有尝试过,但是看起来你可以使用golang交叉编译(可以使用goxc或者Dave Cheney的golang-crosscompile)来编译golang,尽管有一些注意事项。

但是如果你需要与生产环境匹配,大部分情况下最安全的方法是按照Marcin的建议进行操作。

你可以在http://virtualboxes.org/images/上找到一些预先构建的VirtualBox镜像,不过自己创建一个也很简单。

英文:

I haven't tried it myself, but it appears you can cross compile golang (either with goxc or Dave Cheney's golang-crosscompile), albeit with some caveats.

But if you need to match the environment with production, which probably you should most of the time, it's safest to go as Marcin suggested.

You can find some prebuilt VirtualBox images on http://virtualboxes.org/images/ although creating one yourself is pretty easy.

答案4

得分: 0

> 有哪些方法可以简化这个过程?

随着Go 1.5(2015年第三季度)的推出,交叉编译的想法变得更加吸引人,正如Dave Cheney在“Go 1.5中交叉编译变得更好了”中详细介绍的那样:

之前:

> 要成功进行交叉编译,您需要:

> - 目标平台的编译器,如果它们与您的主机平台不同,例如您在darwin/amd64(6g)上,想要为linux/arm(5g)进行编译。

  • 目标平台的标准库,其中包括在构建Go发行版时生成的一些文件。

之后(Go 1.5+):

> 随着将Go编译器转换为Go语言的计划在1.5版本中实现,第一个问题现在得到了解决。

package main

import "fmt"
import "runtime"

func main() {
        fmt.Printf("Hello %s/%s\n", runtime.GOOS, runtime.GOARCH)
}

> 为darwin/386构建

% env GOOS=darwin GOARCH=386 go build hello.go
# 传输到darwin主机
$ ./hello
Hello darwin/386

> 或者为linux/arm构建

% env GOOS=linux GOARCH=arm GOARM=7 go build hello.go
# 传输到linux主机
$ ./hello
Hello linux/arm

> 我在Mac上进行开发,但是暂存/生产服务器将运行Linux。

考虑到Go的编译器是用Go语言编写的,从Mac上生成Linux可执行文件的过程应该变得简单明了。

英文:

> what are some things I can do to streamline the process?

The cross-compilation idea should be even more appealing with Go 1.5 (Q3 2015), as Dave Cheney details in "Cross compilation just got a whole lot better in Go 1.5":

Before:

> For successful cross compilation you would need

> - compilers for the target platform, if they differed from your host platform, ie you’re on darwin/amd64 (6g) and you want to compile for linux/arm (5g).

  • a standard library for the target platform, which included some files generated at the point your Go distribution was built.

After (Go 1.5+):

> With the plan to translate the Go compiler into Go coming to fruition in the 1.5 release the first issue is now resolved.

package main

import "fmt"
import "runtime"

func main() {
        fmt.Printf("Hello %s/%s\n", runtime.GOOS, runtime.GOARCH)
}

> build for darwin/386

% env GOOS=darwin GOARCH=386 go build hello.go
# scp to darwin host
$ ./hello
Hello darwin/386

> Or build for linux/arm

% env GOOS=linux GOARCH=arm GOARM=7 go build hello.go
# scp to linux host
$ ./hello
Hello linux/arm

> I'm developing on a Mac, but the staging/production servers will be running Linux.

Considering the compiler for Go is in Go, the process to produce a Linux executable from your Mac should become straightforward.

huangapple
  • 本文由 发表于 2013年8月4日 09:40:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/18038988.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定