How to deploy golang?

agolangf · 2017-03-26 11:00:05 · 644 次点击    
这是一个分享于 2017-03-26 11:00:05 的资源,其中的信息可能已经有所发展或是发生改变。

Hi,

I am new in golang. I develop web api using golang on mac os. My production environment is ubuntu 14.04 on Digitalocean. How can I deploy golang? what is the bast practice? How can I use docker?


评论:

silenceofnight:

You can pretty much just copy the binary to the server and you are done. You can use Docker if you want, but there isn't any need to.

tech_tuna:

Just started doing some work (DevOps, CI/CD, etc) for a client building a web app in Go. This was pretty much my initial conversation with him.

Then I asked about needing Nginx and he said "No, no need for that really, you just can just run the Go app, open the ports and you're done."

My response "Wow. I like that."

robvdl:

The only thing you can run into is needing special permissions to open port 80 because it's a lower port there is some protection around opening lower ports.

Running the app as root will do it but is not desirable, I read that Nginx/apache do some magic to temporarily become root, open the port, then become the www-data user to achieve this. I am not sure if Caddy does this too, I've been meaning to check that out some time.

ozbilenersin:

Thank you for your response. Can I build for Ubuntu 14.04 on Mac Os.

atamiri:

Yes, you can:

GOOS=linux GOARCH=amd64 go build

(Other valid architectures are, for example, arm, arm64, mips etc., it depends on what server you have.)

8lall0:

$ GOOS=linux go build

kjk:

Recently I moved deploy of my Go software to CoreOS on DigitalOcean. I describe the important pieces here http://blog.kowalczyk.info/article/5/Blueprint-for-deploying-web-apps-on-CoreOS.html

Before that I was using fabric (http://blog.kowalczyk.info/article/ucle/Using-Fabric-for-deploying-server-software.html) and after that ansible. They are all good solutions with plenty of documentation.

earthboundkid:

Great post.

pierrrre:

My workflow at work: Git tag => Travis CI build => build binary => upload to AWS S3 => pull binary from all servers

cdoxsey:

I would switch to ubuntu 16 and systemd. You just need a file:

[Unit]
Description=www
[Service]
ExecStart=/opt/www/www
WorkingDirectory=/opt/www
Restart=always
[Install]
WantedBy=multi-user.target

And then you just need to build the binary for linux and upload it:

[remote] $ systemctl stop www
[local ] $ rsync -az --delete /tmp/build-www/ {USER}@{MACHINE}:/opt/www
[remote] $ systemctl start www

These days I use a cloud init script (DO should have something like it): https://github.com/calebdoxsey/cloud-machine/blob/master/install.bash

It also creates a proxy with caddy and updates DNS entries in cloudflare to give me automatic failover if an instance dies.

I just download the code and build it on the box.

Or you can spend the next year attempting to learn modern service orchestration with kubernetes, docker, etc... :)

robvdl:

I pretty much do the same thing except build it into a .deb that self installs the systemd script, creates a special user for the app, etc. Call me old school but I still prefer deploying with .deb packages.

scopeh:

You'll need to compile the binary to the correct os/arch. This link should help you with that. https://dave.cheney.net/2015/08/22/cross-compilation-with-go-1-5

Once you've done that, just upload the binary and run it.

mcandre:

export GOARCH=amd64 export GOOS=linux go build

That will compile your Go application for modern Linux environments. You can inspect the resulting binary with the file tool, to confirm that the binary has the right attributes for the target environment.

Then copy the binary to the desired server and run.

durthu_smith:

just use heroku?

ewanvalentine:

I use Jenkins to ssh into my digital ocean server, got a git pull, build my go binary, then build it into a docker container, stop the old one and start the new one. That's pretty much it!

Here's an example: https://gist.github.com/EwanValentine/8441ab15030b100f60bf3423bf9f7c8d


入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

644 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传