• 用tsuru部署Go应用
  • 概述
  • 在tsuru中创建应用
  • 应用的代码
  • 通过Git部署
  • 运行应用
  • 进一步探索

    用tsuru部署Go应用

    概述

    本文档是在tsuru中部署一个简单的Go应用的实战指南。

    在tsuru中创建应用

    使用app-create命令创建应用:

    1. $ tsuru app-create <app-name> <app-platform>

    对于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

    1. package main
    2. import (
    3. "fmt"
    4. "net/http"
    5. "os"
    6. )
    7. func main() {
    8. http.HandleFunc("/", hello)
    9. fmt.Println("listening...")
    10. err := http.ListenAndServe(":" + os.Getenv("PORT"), nil)
    11. if err != nil {
    12. panic(err)
    13. }
    14. }
    15. func hello(res http.ResponseWriter, req *http.Request) {
    16. fmt.Fprintln(res, "hello, world!")
    17. }

    通过Git部署

    在创建新的应用时,tsuru会显示应该使用的Git远程分支。用app-info命令可以获得其信息:

    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. Deploys: 0
    10. Pool: theonepool
    11. App Plan:
    12. +---------------+--------+------+-----------+--------+---------+
    13. | Name | Memory | Swap | Cpu Share | Router | Default |
    14. +---------------+--------+------+-----------+--------+---------+
    15. | autogenerated | 0 MB | 0 MB | 100 | | false |
    16. +---------------+--------+------+-----------+--------+---------+

    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: ---- Starting 1 new unit ----
    15. remote: ---> Started unit b21298a64e...
    16. remote:
    17. remote: ---- Binding and checking 1 new units ----
    18. remote: ---> Bound and checked unit b21298a64e
    19. remote:
    20. remote: ---- Adding routes to 1 new units ----
    21. remote: ---> Added route to unit b21298a64e
    22. remote:
    23. remote: OK
    24. To git@192.168.50.4.nip.io:helloworld.git
    25. * [new branch] master -> master

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

    1. $ tsuru key-add mykey ~/.ssh/id_rsa.pub

    使用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

    从此之后就可以省略掉—app标记:

    1. $ tsuru app-info
    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. Deploys: 1
    10. Pool: theonepool
    11. Units: 1
    12. +------------+---------+
    13. | Unit | State |
    14. +------------+---------+
    15. | b21298a64e | started |
    16. +------------+---------+
    17. App Plan:
    18. +---------------+--------+------+-----------+--------+---------+
    19. | Name | Memory | Swap | Cpu Share | Router | Default |
    20. +---------------+--------+------+-----------+--------+---------+
    21. | autogenerated | 0 MB | 0 MB | 100 | | false |
    22. +---------------+--------+------+-----------+--------+---------+

    运行应用

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

    应用部署成功,可以通过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文档,或者阅读tsuru命令完全使用指南。

    原文: http://doc.oschina.net/tsuru-paas?t=52822