用tsuru部署Go应用

概述

在tsuru中创建应用

使用命令创建应用:

对于Go来说,应用平台是Go!让我们脑洞大开,开发一个’hello world‘教学应用,让我们叫它helloworld

  1. $ tsuru app-create helloworld go

使用platform-list命令列出所有的可用的平台。使用app-list查看你所有的应用。

  1. $ tsuru app-list
  2. +-------------+-------------------------+--------------------------------+
  3. | Application | Units State Summary | Address |
  4. +-------------+-------------------------+--------------------------------+
  5. | helloworld | 0 of 0 units in-service | helloworld.192.168.50.4.nip.io |
  6. +-------------+-------------------------+--------------------------------+

应用的代码

用Go实现的一个简单web应用main.go

通过Git部署

  1. $ tsuru app-info --app helloworld
  2. Application: helloworld
  3. Repository: git@192.168.50.4.nip.io:helloworld.git
  4. Platform: go
  5. Teams: admin
  6. Address: helloworld.192.168.50.4.nip.io
  7. Owner: admin@example.com
  8. Team owner: admin
  9. Pool: theonepool
  10. App Plan:
  11. +---------------+--------+------+-----------+--------+---------+
  12. | Name | Memory | Swap | Cpu Share | Router | Default |
  13. +---------------+--------+------+-----------+--------+---------+
  14. | autogenerated | 0 MB | 0 MB | 100 | | false |
  15. +---------------+--------+------+-----------+--------+---------+

Git远程分支被用来通过Git部署应用。当修改被推送到tsuru远程分支时,项目同时也被部署:

  1. $ git push git@192.168.50.4.nip.io:helloworld.git master
  2. Counting objects: 3, done.
  3. Delta compression using up to 4 threads.
  4. Compressing objects: 100% (2/2), done.
  5. Writing objects: 100% (3/3), 430 bytes | 0 bytes/s, done.
  6. Total 3 (delta 0), reused 0 (delta 0)
  7. remote: tar: Removing leading `/' from member names
  8. remote: /
  9. remote:
  10. remote: ---- Building application image ----
  11. remote: ---> Sending image to repository (5.57MB)
  12. remote: ---> Cleaning up
  13. remote:
  14. remote: ---> Started unit b21298a64e...
  15. remote:
  16. remote: ---- Binding and checking 1 new units ----
  17. remote:
  18. remote: ---- Adding routes to 1 new units ----
  19. remote: ---> Added route to unit b21298a64e
  20. remote:
  21. remote: OK
  22. To git@192.168.50.4.nip.io:helloworld.git
  23. * [new branch] master -> master

如果遇到"Permission denied (publickey)."的错误,请确保你是团队一员并把公钥加到tsuru中。用key-add命令添加公钥:

使用git remote add命令来避免每次push代码时都要输入整个远程仓库的链接:

  1. $ git remote add tsuru git@192.168.50.4.nip.io:helloworld.git

然后运行:

  1. $ git push tsuru master
  2. Everything up-to-date

运行应用

tsuru会自动编译和运行应用,但是也可以定制tsuru编译和运行应用的方式。更多内容,请查看Go平台的README:.

应用部署成功,可以通过app-list命令获得IP或者主机名,然后用浏览器去访问。比如,在下面的列表中:

  1. $ tsuru app-list
  2. +-------------+-------------------------+--------------------------------+
  3. | Application | Units State Summary | Address |
  4. +-------------+-------------------------+--------------------------------+
  5. | helloworld | 1 of 1 units in-service | helloworld.192.168.50.4.nip.io |
  6. +-------------+-------------------------+--------------------------------+

顺利完成!现在我们有了一个部署在tsuru上的简单go项目,现在我们可以访问app-list返回的blog app的URL了。(本例中的URL是"helloworld.192.168.50.4.nip.io").

进一步探索

更多信息,可以查看,或者阅读tsuru命令完全使用指南