用tsuru部署Go应用
概述
本文档是在tsuru中部署一个简单的Go应用的实战指南。
在tsuru中创建应用
使用app-create
命令创建应用:
$ tsuru app-create <app-name> <app-platform>
对于Go来说,应用平台是Go
!让我们脑洞大开,开发一个’hello world‘教学应用,让我们叫它helloworld
:
$ tsuru app-create helloworld go
使用platform-list
命令列出所有的可用的平台。使用app-list
查看你所有的应用。
$ tsuru app-list
+-------------+-------------------------+--------------------------------+
| Application | Units State Summary | Address |
+-------------+-------------------------+--------------------------------+
| helloworld | 0 of 0 units in-service | helloworld.192.168.50.4.nip.io |
+-------------+-------------------------+--------------------------------+
应用的代码
用Go实现的一个简单web应用main.go
:
package main
import (
"fmt"
"net/http"
"os"
)
func main() {
http.HandleFunc("/", hello)
fmt.Println("listening...")
err := http.ListenAndServe(":" + os.Getenv("PORT"), nil)
if err != nil {
panic(err)
}
}
func hello(res http.ResponseWriter, req *http.Request) {
fmt.Fprintln(res, "hello, world!")
}
通过Git部署
在创建新的应用时,tsuru会显示应该使用的Git远程分支。用app-info
命令可以获得其信息:
$ tsuru app-info --app helloworld
Application: helloworld
Repository: git@192.168.50.4.nip.io:helloworld.git
Platform: go
Teams: admin
Address: helloworld.192.168.50.4.nip.io
Owner: admin@example.com
Team owner: admin
Deploys: 0
Pool: theonepool
App Plan:
+---------------+--------+------+-----------+--------+---------+
| Name | Memory | Swap | Cpu Share | Router | Default |
+---------------+--------+------+-----------+--------+---------+
| autogenerated | 0 MB | 0 MB | 100 | | false |
+---------------+--------+------+-----------+--------+---------+
Git远程分支被用来通过Git部署应用。当修改被推送到tsuru远程分支时,项目同时也被部署:
$ git push git@192.168.50.4.nip.io:helloworld.git master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 430 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: tar: Removing leading `/' from member names
remote: /
remote:
remote: ---- Building application image ----
remote: ---> Sending image to repository (5.57MB)
remote: ---> Cleaning up
remote:
remote: ---- Starting 1 new unit ----
remote: ---> Started unit b21298a64e...
remote:
remote: ---- Binding and checking 1 new units ----
remote: ---> Bound and checked unit b21298a64e
remote:
remote: ---- Adding routes to 1 new units ----
remote: ---> Added route to unit b21298a64e
remote:
remote: OK
To git@192.168.50.4.nip.io:helloworld.git
* [new branch] master -> master
如果遇到"Permission denied (publickey)."的错误,请确保你是团队一员并把公钥加到tsuru中。用key-add
命令添加公钥:
$ tsuru key-add mykey ~/.ssh/id_rsa.pub
使用git remote add
命令来避免每次push代码时都要输入整个远程仓库的链接:
$ git remote add tsuru git@192.168.50.4.nip.io:helloworld.git
然后运行:
$ git push tsuru master
Everything up-to-date
从此之后就可以省略掉—app
标记:
$ tsuru app-info
Application: helloworld
Repository: git@192.168.50.4.nip.io:helloworld.git
Platform: go
Teams: admin
Address: helloworld.192.168.50.4.nip.io
Owner: admin@example.com
Team owner: admin
Deploys: 1
Pool: theonepool
Units: 1
+------------+---------+
| Unit | State |
+------------+---------+
| b21298a64e | started |
+------------+---------+
App Plan:
+---------------+--------+------+-----------+--------+---------+
| Name | Memory | Swap | Cpu Share | Router | Default |
+---------------+--------+------+-----------+--------+---------+
| autogenerated | 0 MB | 0 MB | 100 | | false |
+---------------+--------+------+-----------+--------+---------+
运行应用
tsuru会自动编译和运行应用,但是也可以定制tsuru编译和运行应用的方式。更多内容,请查看Go平台的README:https://github.com/tsuru/basebuilder/blob/master/go/README.md.
应用部署成功,可以通过app-list
命令获得IP或者主机名,然后用浏览器去访问。比如,在下面的列表中:
$ tsuru app-list
+-------------+-------------------------+--------------------------------+
| Application | Units State Summary | Address |
+-------------+-------------------------+--------------------------------+
| helloworld | 1 of 1 units in-service | helloworld.192.168.50.4.nip.io |
+-------------+-------------------------+--------------------------------+
顺利完成!现在我们有了一个部署在tsuru上的简单go项目,现在我们可以访问app-list
返回的blog app
的URL了。(本例中的URL是"helloworld.192.168.50.4.nip.io").
进一步探索
更多信息,可以查看tsuru文档
,或者阅读tsuru命令完全使用指南。