How would one deploy a Go microservice on Google Cloud in practice?

xuanbao · · 502 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Trying to get this to work. I have written a microservice in Golang, it&#39;s a basic http server with one single REST endpoint, and it gets data from a public API that it processes and then exposes. It works well on localhost :o)</p> <p>However, I&#39;d like for my react-native app to be able to hit this endpoint from anywhere(obviously), but how does one practically deploy this to Google Cloud?</p> <p>I will create an instance with Ubuntu. On this instance, I&#39;ll run a Docker container which has the Go app. But how do you point a public IP towards it? </p> <p>Do you need nginx/apache in front of it, or is there a more simple approach?</p> <p>I&#39;m fine with hitting the static IP for now rather than a domain. </p> <p>I&#39;m sure someone here has experience with it. I&#39;ll appreciate any advice/best practice :) </p> <hr/>**评论:**<br/><br/>anonfunction: <pre><p>Use -p 80:8081 to expose the container service on the instance’s http port. Then you will need to allow traffic to port 80 using google cloud network settings. Open the instance then click edit and then under firewall check allow http traffic. You should then be able to access your api on the instance’s external IP address. </p></pre>IAmCodeMachine: <pre><p>Spinning up an Ubuntu instance, running Docker on it hosting your single REST API endpoint Go app in Google Cloud Platform is a little bit overkill on a few levels.</p> <p>Save yourself pain and suffering and just use <a href="https://cloud.google.com/appengine/docs/go/" rel="nofollow">Go on App Engine</a> where you pretty much only need to worry about your Go code and little else.</p></pre>cbll: <pre><p>This is as much to learn how to use Google Cloud as the functionality.</p> <p>Doesn&#39;t the App Engine require you to download an SDK and all sorts of stuff? </p></pre>IAmCodeMachine: <pre><p>Well yes, this is one bit of functionality to learn if you so desire and it scales really well for cheap :) If you&#39;re doing docker you might as well let the platform handle it for you with Kubernetes.</p> <p>Yes, the App Engine requires you to download and install an SDK, but I wouldn&#39;t say &#39;all sorts of stuff&#39;. The <a href="https://cloud.google.com/appengine/docs/flexible/go/quickstart" rel="nofollow">Quickstart for Go</a> will take you all of 5 minutes to run through including setup and the <a href="https://github.com/GoogleCloudPlatform/golang-samples/tree/master/appengine_flexible/helloworld" rel="nofollow">Helloworld Go App</a> as part of that is almost too easy.</p></pre>cbll: <pre><p>Holy shit you weren&#39;t kidding. Took me like 30min to spin up a Go app on App Engine. And that includes me being a retard and not understanding imports. </p></pre>Cidan: <pre><p>I&#39;m glad you like it! Another thing to note, you can use App Engine Flex and there&#39;s no need for a special SDK.</p> <p>Have fun!</p></pre>Perhyte: <pre><p>Beware though: then you don&#39;t get the free tier. App Engine&#39;s &#34;standard environment&#34; is usually entirely free if you don&#39;t get a lot of traffic.</p></pre>shovelpost: <pre><p>Judging by your current requirements I also think that App Engine is a good fit for this project. </p> <p>The problem with App Engine is that as soon as you need slightly more complex stuff (say data persistence) you need to start using their internal datastore service. So you end up with a lot of code that only runs on App Engine.</p></pre>cbll: <pre><p>Cheers. </p> <p>I&#39;m a bit stubborn and I have to make this first thing when. Obviously App Engine appears smarter(I realize that now), and I will use that later on. After I make this shit work. And catch some sleep.. </p> <p>Thanks :P </p></pre>redyed: <pre><p>This seems to be a very docker specific problem/question you are trying to solve. If its a POC, I would suggest you spin up a micro instance, just drop the binary and follow <a href="/u/hell_0n_wheel" rel="nofollow">hell_0n_wheel</a>&#39;s advice to keep it running always (even after panics). To make it a little more secure so that you dont have to expose the static IP, use cloudflare and https the cloudflare endpoint (under an existing domain if you have ready access of its DNS setting). Using a docker instance for a POC might seem like an overkill.</p></pre>hell_0n_wheel: <pre><blockquote> <p>Do you need nginx/apache in front of it, or is there a more simple approach?</p> </blockquote> <p>Either you&#39;re not asking the right questions here because you haven&#39;t done your homework... or you&#39;re hiding some aspect of your design that would help us arrive at the right answer for you.</p> <p>I fear you&#39;ll have larger problems than figuring out how to deploy your service: namely, securing your cloud instance. In researching what it takes to securely deploy a service, I guarantee you, you&#39;ll find the answers to the rest of your questions.</p> <p>The simplest deployment is to push the executable to your node and register it with systemd or whatever. Even better, use <code>runit</code> to ensure it never stays down should it panic. Pop a hole in your firewall for the service and you&#39;re done... until someone discovers the hole. </p></pre>cbll: <pre><p>I&#39;m well aware of the security implications, this is simply to prototype a react-native app for a few minutes at a time. I don&#39;t care if anyone else gets the data in that period, it isn&#39;t secret(it&#39;s publicly available elsewhere anyhow). Running it over http to a static IP is OK for this purpose :=) </p> <p>If you need me to clarify anything, let me know. It&#39;s basically exposing some JSON at <code>:8081/stocks</code>, I&#39;d like to access that from my react-native device somehow.. Blurry thoughts, sorry I know. </p></pre>hell_0n_wheel: <pre><p>So what&#39;s your question, then? You need help copying an executable? I don&#39;t understand...</p></pre>cbll: <pre><p>My question is how you in practice &#34;expose&#34; a Go endpoint in a docker container, inside a google cloud instance to the static IP attached to this instance. I know how to expose the port from the docker container to the host instance(so it will be localhost:8081/stocks on there, too) - but how would you tie that to the IP that can then be accessed from anywhere?</p> <p>I&#39;ve been coding for like 16 hours in a row and my brain is about to explode. Sorry for extremely shitty explaination :D</p></pre>hell_0n_wheel: <pre><p>This has nothing to do with go or google cloud, you&#39;re just asking a Docker question. Use the &#34;-p&#34; option for that. <a href="https://docs.docker.com/engine/userguide/networking/default_network/binding/" rel="nofollow">https://docs.docker.com/engine/userguide/networking/default_network/binding/</a></p></pre>

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

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