我的Go服务器在Cloud Foundry中无法启动。

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

My Go server fails to start in cloud foundry

问题

我有一个简单的GO服务器,我正在尝试将其推送到Bosh Lite(Vagrant + Virtual Box)上的Cloud Foundry。

这是我的Go应用程序的源代码设置:

  • ~/workspace/src/github.com/me/(父目录)

    • Godeps

    • weight
      weight.go <-- 主文件。

    manifest.yml

    Procfile

我的weight.go是一个简单的服务器,监听9000端口。

1)manifest.yml的内容如下:

applications:

  • name: weight
    memory: 128MB
    instances: 1

2)Procfile的内容如下:

worker: bin/weight

3)我使用默认的buildpack。

4)当我使用cf push weight -c "./bin/weight;sleep 1d"命令推送我的应用程序时,我得到以下输出:

a-424e-b509-6df11fb32cc7 ({"state"=>"STOPPED"})
2016-03-13T11:02:45.70-0700 [DEA/0] OUT Got staging request for app with id 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:02:46.84-0700 [API/0] OUT Updated app with guid 16d3795a-8cda-424e-b509-6df11fb32cc7 ({"state"=>"STARTED"})
2016-03-13T11:02:46.89-0700 [STG/0] OUT -----> Downloaded app package (12K)
2016-03-13T11:02:48.76-0700 [STG/0] OUT -----> Downloaded app buildpack cache (78M)
2016-03-13T11:02:48.82-0700 [STG/0] ERR Cloning into '/tmp/buildpacks/go-buildpack'...
2016-03-13T11:03:05.66-0700 [STG/0] OUT Submodule 'compile-extensions' (https://github.com/cloudfoundry/compile-extensions.git) registered for path 'compile-extensions'
2016-03-13T11:03:05.68-0700 [STG/0] ERR Cloning into 'compile-extensions'...
2016-03-13T11:03:07.59-0700 [STG/0] OUT Submodule path 'compile-extensions': checked out '26a578c06a62c763205833561fec1c5c6d34deb6'
2016-03-13T11:03:07.61-0700 [STG/0] OUT -------> Buildpack version 1.7.3
2016-03-13T11:03:09.81-0700 [STG/0] OUT https://pivotal-buildpacks.s3.amazonaws.com/concourse-binaries/godep/godep-v55-linux-x64.tgz
2016-03-13T11:03:09.88-0700 [STG/0] OUT -----> Checking Godeps/Godeps.json file.
2016-03-13T11:03:09.92-0700 [STG/0] OUT -----> Using go1.5.3
2016-03-13T11:03:09.92-0700 [STG/0] OUT -----> Running: godep go install -tags cloudfoundry .
2016-03-13T11:03:11.19-0700 [STG/0] OUT -----> Uploading droplet (2.0M)
2016-03-13T11:03:25.11-0700 [DEA/0] OUT Starting app instance (index 0) with guid 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:03:34.93-0700 [DEA/0] OUT Removing crash for app with id 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:03:34.93-0700 [DEA/0] OUT Stopping app instance (index 0) with guid 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:03:34.93-0700 [DEA/0] OUT Stopped app instance (index 0) with guid 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:03:56.81-0700 [DEA/0] OUT Starting app instance (index 0) with guid 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:03:57.79-0700 [DEA/0] ERR Instance (index 0) failed to start accepting connections
2016-03-13T11:03:57.80-0700 [API/0] OUT App instance exited with guid 16d3795a-8cda-424e-b509-6df11fb32cc7 payload: {"cc_partition"=>"default", "droplet"=>"16d3795a-8cda-424e-b509-6df11fb32cc7", "version"=>"2eeebbf8-e84e-412a-aeba-2adc0cffea6b", "instance"=>"910eba711c6e414bb7c6324565d0a9af", "index"=>0, "reason"=>"CRASHED", "exit_status"=>127, "exit_description"=>"failed to accept connections within health check timeout", "crash_timestamp"=>1457892237}

我尝试设置cf set-env weight PORT 9000,但没有成功。

我在manifest和yaml文件中做了什么奇怪的事情吗?

weight.go在本地运行。

代码:

func main() {
    http.HandleFunc("/weight", weightHandler)
    err := http.ListenAndServe("localhost:"+getPort(), nil)
    if err != nil {
        fmt.Println("got an err")
        log.Fatalln(err)
    } else {
        fmt.Println("Apparently it works?")
    }
    //fmt.Println("Hi")
    //time.Sleep(1*time.Hour)
}

func getPort() string {
    var port string
    if port = os.Getenv("PORT"); len(port) == 0 {
        fmt.Println("Didn't Found it")
        fmt.Println(port)
        port = DEFAULT_PORT
    } else {
        fmt.Println("Gotim")
        fmt.Println(port)
    }
    return port
}

我确实得到了CF分配的随机端口的日志,但仍然卡在这里:

2016-03-13T16:58:40.90-0700 [API/0] OUT App instance exited with guid e5d417bd-c38d-4239-aa61-e9ca67fce79a payload: {"cc_partition"=>"default", "droplet"=>"e5d417bd-c38d-4239-aa61-e9ca67fce79a", "version"=>"8d7e80b3-69d5-4c83-9d37-1159d5deeba8", "instance"=>"14223d325c204406b87a131c065c16cc", "index"=>0, "reason"=>"CRASHED", "exit_status"=>-1, "exit_description"=>"failed to accept connections within health check timeout", "crash_timestamp"=>1457913520}

已解决:问题是在监听localhost上,我没有指定IP,只需指定IP即可解决

英文:

I have a simple GO server that I am trying to push to cloud foundry on Bosh lite ( Vagrant + Virtual box.)

Here is the source code setup from my go application:

  • ~/workspace/src/github.com/me/ ( parent directory)

    -Godeps

    -weight
    weight.go <-- main file.

manifest.yml

Procfile

My weight.go is a simple server that listens on 9000.

  1. manifest.yml looks like this.

    applications:

    • name: weight
      memory: 128MB
      instances: 1
      2 ) Procfile looks like this.

    worker: bin/weight

3 ) I use the default buildpack.

4 ) When I push my app with cf push weight -c "./bin/weight;sleep 1d" I get:

a-424e-b509-6df11fb32cc7 ({&quot;state&quot;=&gt;&quot;STOPPED&quot;})
2016-03-13T11:02:45.70-0700 [DEA/0]      OUT Got staging request for app with id 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:02:46.84-0700 [API/0]      OUT Updated app with guid 16d3795a-8cda-424e-b509-6df11fb32cc7 ({&quot;state&quot;=&gt;&quot;STARTED&quot;})
2016-03-13T11:02:46.89-0700 [STG/0]      OUT -----&gt; Downloaded app package (12K)
2016-03-13T11:02:48.76-0700 [STG/0]      OUT -----&gt; Downloaded app buildpack cache (78M)
2016-03-13T11:02:48.82-0700 [STG/0]      ERR Cloning into &#39;/tmp/buildpacks/go-buildpack&#39;...
2016-03-13T11:03:05.66-0700 [STG/0]      OUT Submodule &#39;compile-extensions&#39; (https://github.com/cloudfoundry/compile-extensions.git) registered for path &#39;compile-extensions&#39;
2016-03-13T11:03:05.68-0700 [STG/0]      ERR Cloning into &#39;compile-extensions&#39;...
2016-03-13T11:03:07.59-0700 [STG/0]      OUT Submodule path &#39;compile-extensions&#39;: checked out &#39;26a578c06a62c763205833561fec1c5c6d34deb6&#39;
2016-03-13T11:03:07.61-0700 [STG/0]      OUT -------&gt; Buildpack version 1.7.3
2016-03-13T11:03:09.81-0700 [STG/0]      OUT https://pivotal-buildpacks.s3.amazonaws.com/concourse-binaries/godep/godep-v55-linux-x64.tgz
2016-03-13T11:03:09.88-0700 [STG/0]      OUT -----&gt; Checking Godeps/Godeps.json file.
2016-03-13T11:03:09.92-0700 [STG/0]      OUT -----&gt; Using go1.5.3
2016-03-13T11:03:09.92-0700 [STG/0]      OUT -----&gt; Running: godep go install -tags cloudfoundry .
2016-03-13T11:03:11.19-0700 [STG/0]      OUT -----&gt; Uploading droplet (2.0M)
2016-03-13T11:03:25.11-0700 [DEA/0]      OUT Starting app instance (index 0) with guid 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:03:34.93-0700 [DEA/0]      OUT Removing crash for app with id 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:03:34.93-0700 [DEA/0]      OUT Stopping app instance (index 0) with guid 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:03:34.93-0700 [DEA/0]      OUT Stopped app instance (index 0) with guid 16d3795a-8cda-424e-b509-6df11fb32cc7
2016-03-13T11:03:56.81-0700 [DEA/0]      OUT Starting app instance (index 0) with guid 16d3795a-8cda-424e-b509-6df11fb32cc7
**2016-03-13T11:03:57.79-0700 [DEA/0]      ERR Instance (index 0) failed to start accepting connections**
2016-03-13T11:03:57.80-0700 [API/0]      OUT App instance exited with guid 16d3795a-8cda-424e-b509-6df11fb32cc7 payload: {&quot;cc_partition&quot;=&gt;&quot;default&quot;, &quot;droplet&quot;=&gt;&quot;16d3795a-8cda-424e-b509-6df11fb32cc7&quot;, &quot;version&quot;=&gt;&quot;2eeebbf8-e84e-412a-aeba-2adc0cffea6b&quot;, &quot;instance&quot;=&gt;&quot;910eba711c6e414bb7c6324565d0a9af&quot;, &quot;index&quot;=&gt;0, &quot;reason&quot;=&gt;&quot;CRASHED&quot;, &quot;exit_status&quot;=&gt;127, &quot;exit_description&quot;=&gt;&quot;failed to accept connections within health check timeout&quot;, &quot;crash_timestamp&quot;=&gt;1457892237}

I have tried setting

cf set-env weight PORT 9000

No success,
Am I doing something funky with the manifest and yaml ?

The weight.go runs locally.

Code :

func main (){
	http.HandleFunc(&quot;/weight&quot;, weightHandler)
	err:=http.ListenAndServe(&quot;localhost:&quot;+getPort(), nil)
	if err != nil {
		fmt.Println(&quot;got an err &quot;)
		log.Fatalln(err)
	}else{
		fmt.Println(&quot;Apparently it works ?&quot;)
	}
	//fmt.Println(&quot;Hi&quot;)
	//time.Sleep(1*time.Hour)
}

func getPort() string {
	var port string
	if port = os.Getenv(&quot;PORT&quot;); len(port) == 0 {
		fmt.Println(&quot;Didn&#39;t Found it&quot;)
		fmt.Println(port)
		port = DEFAULT_PORT
	}else{
		fmt.Println(&quot;Gotim&quot;)
		fmt.Println(port)
	}
	return port
}

I do get the Log for the random PORT CF assigns, still stuck at

2016-03-13T16:58:40.90-0700 [API/0]      OUT App instance exited with guid e5d417bd-c38d-4239-aa61-e9ca67fce79a payload: {&quot;cc_partition&quot;=&gt;&quot;default&quot;, &quot;droplet&quot;=&gt;&quot;e5d417bd-c38d-4239-aa61-e9ca67fce79a&quot;, &quot;version&quot;=&gt;&quot;8d7e80b3-69d5-4c83-9d37-1159d5deeba8&quot;, &quot;instance&quot;=&gt;&quot;14223d325c204406b87a131c065c16cc&quot;, &quot;index&quot;=&gt;0, &quot;reason&quot;=&gt;&quot;CRASHED&quot;, &quot;exit_status&quot;=&gt;-1, &quot;exit_description&quot;=&gt;&quot;failed to accept connections within health check timeout&quot;, &quot;crash_timestamp&quot;=&gt;1457913520}

***FIXED IT : The problem was listening on localhost, I didn't specify the IP and I was good to go ***

答案1

得分: 2

从Cloud Foundry文档中关于故障排除的部分:

> **确保你的应用代码使用PORT环境变量。**你的应用可能失败是因为它在错误的端口上监听。不要硬编码应用监听的端口,而是使用PORT环境变量。[来源]

从Cloud Foundry文档中关于环境变量的部分:

> 应用程序应该监听的端口。Cloud Foundry运行时为每个应用程序实例动态分配一个端口,因此获取或使用应用程序端口的代码应该通过PORT环境变量引用它。[来源]

因此,你的应用程序不应该硬编码监听9000端口,而应该监听由PORT环境变量指定的端口,如果该环境变量未设置,则默认使用9000端口。

英文:

From Cloud Foundry docs on troubleshooting:

> Make sure your application code uses the PORT environment variable. Your application may be failing because it is listening on the wrong port. Instead of hard coding the port on which your application listens, use the PORT environment variable. [Source]

From Cloud Foundry docs on environment variables:

> The port on which the application should listen for requests. The Cloud Foundry runtime allocates a port dynamically for each instance of the application, so code that obtains or uses the application port should refer to it via the PORT environment variable. [Source]

So rather than your app being hardcoded to listen on port 9000, it should listen on the port specified by the PORT environment variable, and can then default to 9000 if that environment variable is not set.

huangapple
  • 本文由 发表于 2016年3月14日 02:13:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/35973890.html
匿名

发表评论

匿名网友

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

确定