本文共 14854 字,大约阅读时间需要 49 分钟。
zuul集成cloud
Laravel has in popularity over the last few years. The Laravel community even says that Laravel has made writing PHP more enjoyable instead of a pain. Laravel 6 has some interesting new . Getting a super scaleable working URL for your application takes hours if not days. Setting up something like Kubernetes is a huge task. This is where Google Cloud Run shines: you can get a working HTTPS URL for any of your containerized apps in minutes.
Laravel已经流行,在过去的几年。 Laravel社区甚至说Laravel使编写PHP变得更有趣而不是痛苦。 Laravel 6具有一些有趣的新 。 为您的应用程序获取超级可扩展的工作URL需要花费数小时甚至数天的时间。 设置Kubernetes之类的东西是一项艰巨的任务。 这就是Google Cloud Run的亮点:您可以在几分钟内为任何容器化应用程序获取有效的HTTPS URL。
is serverless and fully managed by Google. You get super scale, billing by the second, HTTPS URLs, and your own domain mapping. If you want to run stateless containers, Cloud Run is hands down the easiest way to do it. In this post, I will detail how to get your Laravel 6 app working on Google cloud run with Continuous Integration (CI).
是无服务器的,由Google完全管理。 您将获得超大规模,按秒计费,HTTPS URL和您自己的域映射。 如果要运行无状态容器,Cloud Run是最简单的方法。 在本文中,我将详细介绍如何通过持续集成(CI)使您的Laravel 6应用程序在Google云上运行。
Have a working Google cloud account (they give you free for 1 yr, no reasons not to have an account)
拥有一个有效的Google云帐户(他们会为您提供的1年免费 ,没有理由不开设帐户)
For two main reasons:
主要有两个原因:
Below are the steps to set up and deploy Laravel 6 on Cloud Run:
以下是在Cloud Run上设置和部署Laravel 6的步骤:
Start by cloning Laravel or using composer or the Laravel CLI as indicated in the official guide. I am using composer to get the latest Laravel as below:
首先按照官方指南中的指示,克隆Laravel或使用composer或Laravel CLI。 我正在使用作曲家来获取最新的Laravel,如下所示:
I ran the following command to get the latest Laravel:
我运行以下命令以获取最新的Laravel:
composer create-project --prefer-dist laravel/laravel laravel6-on-google-cloud-run
Then run cd laravel6-on-google-cloud-run
then php artisan serve
to see if it is working. For me it was fine when I went to http://localhost:8000
on a web browser. I had PHP 7.2 installed locally.
然后运行cd laravel6-on-google-cloud-run
然后cd laravel6-on-google-cloud-run
php artisan serve
以查看其是否正常运行。 对我来说,当我通过网络浏览器访问http://localhost:8000
时,这很好。 我在本地安装了PHP 7.2。
Create a new repository on Github like below:
在Github上创建一个新的存储库,如下所示:
You can use any Git hosting provider, but for this example I will be using to run tests (and Github is the most popular git hosting tool).
您可以使用任何Git托管服务提供商,但是在本示例中,我将使用运行测试(Github是最受欢迎的git托管工具)。
Now after you have the repo created, add it to your local Laravel copy and push the Readme file. To do this run the following commands on your CLI:
现在,在创建存储库之后,将其添加到本地Laravel副本中并推送自述文件。 为此,请在CLI上运行以下命令:
git initcode . # I used VS code to change the readmegit add readme.mdgit commit -m "Initial commit -- App Readme"git remote add origin git@github.com:geshan/laravel6-on-google-cloud-run.gitgit push -u origin master
Now let’s add the whole app as a PR to the Github repo by executing the following commands:
现在,通过执行以下命令,将整个应用程序作为PR添加到Github存储库中:
git checkout -b laravel6-full-appgit add .gitignoregit add .git commit -m "Add the whole Laravel 6 app"git push origin laravel6-full-app
After that go and open a Pull Request (PR), on the repo like one. You might be thinking I am the only one working on this, why do I need a PR? Well, it is always better to do things methodically even if it is just one person working on the project :).
此后去打开一个pull请求(PR),就像回购一个。 您可能会认为我是唯一从事此工作的人,为什么我需要PR? 好吧,即使只有一个人在项目上工作,也最好有条理地做事:)。
After that merge your pull request.
之后,合并您的拉取请求。
Now the fun part: after you merged your PR now Github knows that this is a Laravel project. Click on the Actions
tab on your repo page and you should be able to see something like below:
现在最有趣的部分是:合并PR后,Github知道这是一个Laravel项目。 单击回购页面上的“ Actions
选项卡,您应该能够看到如下所示的内容:
Click the Set up this workflow
under Laravel
then on the next page click the Start commit
button on the top right. After that add a commit message like below and click Commit new file
.
单击Laravel
下的“ Set up this workflow
,然后在下一页上,单击右上角的“ Start commit
按钮。 之后,添加如下所示的提交消息,然后单击Commit new file
。
There you go, you have your CI setup. Laravel default tests will run on each git push now. Wasn’t that easy? Thank Github for this great intelligence. No more creating .myCIname.yml
files :).
到这里,您就可以设置CI。 Laravel默认测试现在将在每个git push上运行。 那不是那么容易吗? 感谢Github如此出色的情报。 不再创建.myCIname.yml
文件:)。
Now let’s add docker and docker-compose to run the app locally without PHP or artisan serve.
现在,让我们添加docker和docker-compose,以在本地运行应用程序,而无需PHP或artisan服务。
We will need the container to run Laravel on Google Cloud Run too. This part is inspired by the post by Nicolas. If you want to learn more about and Laravel please refer to this .
我们也需要该容器在Google Cloud Run上运行Laravel。 这部分的灵感来自发表的文章。 如果您想了解有关和Laravel的更多信息,请参考这篇 。
Run the following commands first to get your master up to date as we added the workflow
file from Github interface:
当我们从Github界面添加workflow
文件时,首先运行以下命令以使您的母版保持最新:
git checkout mastergit fetchgit pull --rebase origin master # as we added the workflow file from github interfacegit checkout -b docker
Add a key to the .env.example
file. Copy it from the .env
file like below:
将密钥添加到.env.example
文件。 从.env
文件复制它,如下所示:
APP_NAME=LaravelAPP_ENV=localAPP_KEY=base64:DJkdj8L5Di3rUkUOwmBFCrr5dsIYU/s7s+W52ClI4AA=APP_DEBUG=trueAPP_URL=http://localhost
As this is just a demo this is ok to do. For a real app always be careful with secrets. For production-ready apps do turn of the debugging and other dev related things.
因为这只是一个演示,所以可以这样做。 对于真正的应用程序,请始终谨慎对待秘密。 对于可用于生产环境的应用程序,请关闭调试和其他与开发人员相关的工作。
Add the following Dockerfile
on the project root:
在项目根目录上添加以下Dockerfile
:
FROM composer:1.9.0 as buildWORKDIR /appCOPY . /appRUN composer global require hirak/prestissimo && composer installFROM php:7.3-apache-stretchRUN docker-php-ext-install pdo pdo_mysqlEXPOSE 8080COPY --from=build /app /var/www/COPY docker/000-default.conf /etc/apache2/sites-available/000-default.confCOPY .env.example /var/www/.envRUN chmod 777 -R /var/www/storage/ && \ echo "Listen 8080" >> /etc/apache2/ports.conf && \ chown -R www-data:www-data /var/www/ && \ a2enmod rewrite
Then add the following file at docker/000-default.conf
然后在docker/000-default.conf
添加以下文件
ServerAdmin webmaster@localhost DocumentRoot /var/www/public/ AllowOverride All Require all granted ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
After that add the following docker-compose.yml
之后添加以下docker-compose.yml
version: '3'services: app: build: context: ./ volumes: - .:/var/www ports: - "8080:8080" environment: - APP_ENV=local
If you try to understand everything here it might be overwhelming, so let me boil down the main parts:
如果您试图了解这里的所有内容,那么可能会令人不知所措,所以让我归纳一下主要部分:
As Google Cloud Run requires the web-server to be listening to port 8080
and we are using 000-default.conf
to configure this
由于Google Cloud Run要求网络服务器监听8080
端口,因此我们使用000-default.conf
进行配置
To make things easy to run with the single command docker-compose up
we are using docker-compose.
为了使单个命令docker-compose up
易于运行,我们使用docker-compose。
Now as you have read this far, run docker-compose up
on your root and then after everything runs go to http://localhost:8080
to see that Laravel 6 is running locally on Docker. Below is my docker-compose up
output towards the end:
现在,您已经阅读了到目前为止,在根目录上运行docker-compose up
,然后在一切运行之后转到http://localhost:8080
以查看Laravel 6在Docker本地运行。 下面是我的docker-compose up
输出:
As Laravel is running fine with Docker, let’s open a PR like one to add Docker to our project. I ran the following commands on the root of the project before opening the Pull Request (PR):
由于Laravel在Docker上运行良好,让我们打开一个像的PR,将Docker添加到我们的项目中。 在打开请求请求(PR)之前,我在项目的根目录上运行了以下命令:
git status
It should give you something like below:
它应该给您类似以下内容:
On branch dockerUntracked files: (use "git add..." to include in what will be committed) Dockerfile docker-compose.yml docker/nothing added to commit but untracked files present (use "git add" to track)
Now run the following commands:
现在运行以下命令:
git add .git commit -m "Add docker and docker compose"git push origin docker
As a bonus it will run the Laravel default test on the push, like you can see below:
作为奖励,它将在推送时运行Laravel默认测试,如下所示:
Only the owner of the repo has access to the Actions
tab so other people don’t necessarily need to know the results of your test builds :).
只有仓库的所有者才可以访问“ Actions
选项卡,因此其他人不必知道测试构建的结果:)。
Now let’s deploy this Laravel setup to Google Cloud Run the easy way. Given that you have merged your PR from the docker
branch, let’s run the following commands:
现在,让我们将这种Laravel设置部署到Google Cloud Run变得简单。 鉴于您已经从docker
分支合并了PR,让我们运行以下命令:
git checkout mastergit fetchgit pull --rebase origin mastergit checkout -b cloud-run-button
Then add the following to your readme.md
file:
然后将以下内容添加到您的readme.md
文件中:
### Run on Google cloud run[![Run on Google Cloud](https://storage.googleapis.com/cloudrun/button.svg)](https://console.cloud.google.com/cloudshell/editor?shellonly=true&cloudshell_image=gcr.io/cloudrun/button&cloudshell_git_repo=https://github.com/geshan/laravel6-on-google-cloud-run.git)
Be careful and replace the last part with your repo’s HTTPs
URL. For example, if your repo is at https://github.com/ghaleroshan/laravel6-on-google-cloud-run
it will be https://github.com/ghaleroshan/laravel6-on-google-cloud-run.git
, then commit and push. Your PR should look something like one.
请注意,将最后一部分替换为您的仓库的HTTPs
URL。 例如,如果您的存储库位于https://github.com/ghaleroshan/laravel6-on-google-cloud-run
则它将为https://github.com/ghaleroshan/laravel6-on-google-cloud-run.git
,然后提交并推送。 您的PR应该看起来像 。
After you merge your Pull Request (PR), then go to your repo page and click on the Run on Google Cloud
button.
合并您的请求请求(PR)之后,然后转到您的回购页面并单击Run on Google Cloud
上Run on Google Cloud
按钮。
After that, if you are logged into your Google account and have Google cloud setup with 1 project, click “Proceed”. You might need to wait a bit, then
之后,如果您已登录Google帐户并设置了包含1个项目的Google云,请单击“继续”。 您可能需要稍等一下,然后
Choose the project – Choose a project to deploy this application
选择项目– Choose a project to deploy this application
Choose the region – Choose a region to deploy this application
, I usually go with us-central-1
选择区域– Choose a region to deploy this application
,我通常选择us-central-1
If everything goes fine on your Google Cloud Shell
, you will see the HTTPS URL that you can hit to see your Laravel app running like below:
如果您在Google Cloud Shell
上一切正常,您将看到可以单击以查看Laravel应用运行的HTTPS URL,如下所示:
What just happened above is:
上面发生的事情是:
After choosing the region, the script built a docker container image from the Dockerfile
in the repo
选择区域后,脚本从Dockerfile
中的Dockerfile
中构建了一个Dockerfile
容器映像
Then it pushed the built image to
然后将生成的图像推送到
After that using the CLI it deployed the built image to Cloud Run, which gave back the URL.
之后,使用 CLI将生成的映像部署到Cloud Run,这会返回URL。
After you git the URL you should see your app working on Google Cloud Run like below:
混入网址后,您应该会看到您的应用在Google Cloud Run上运行,如下所示:
If you want to deploy another version you can merge your PR to master and click the button again to deploy.
如果要部署另一个版本,可以将PR合并到master,然后再次单击按钮进行部署。
The for Google Cloud Run is very generous. You can run any containerized app or web app on Google cloud run. I ran a pet project that got ~ 1 request per minute and I did not have to pay anything.
Google Cloud Run的非常优惠。 您可以在Google Cloud Run上运行任何容器化的应用程序或Web应用程序。 我运行了一个宠物项目,每分钟收到约1个请求,而我无需支付任何费用。
Behind the scenes, it is using and . It can also be run on your Kubernetes cluster but who would choose to manage a K8s cluster if you can just push and get a scaleable serverless fully managed app :).
在后台,它使用和 。 它也可以在您的Kubernetes集群上运行,但是如果您可以推送并获得可扩展的无服务器完全托管应用程序,谁会选择管理K8s集群:)。
To run Laravel 6 on Google Cloud Run quickly follow the steps below:
要在Google Cloud Run上运行Laravel 6,请快速执行以下步骤:
Make sure you are logged into your
确保您已登录
Go to
转到
There you go – running a Laravel app on Google cloud run was pretty easy. You have even got test running on Github with Github actions. Hope it helps. To do a CI/CD approach you can check out this . It shows deployment using Cloud build. As the same container is running for local and production (Google Cloud Run) environment you don’t need to learn a new framework to go Serverless.
随您去吧–在Google云上运行Laravel应用非常简单。 您甚至已经使用Github动作在Github上运行了测试。 希望能帮助到你。 要执行CI / CD方法,您可以查看这篇 。 它显示了使用Cloud build进行的部署。 由于同一容器正在本地和生产环境(Google Cloud Run)上运行,因此您无需学习新框架即可使用无服务器。
Any containerized web app can be run on Google Cloud Run, it is a great service. You can read more at https://geshan.com.np
任何容器化的网络应用都可以在Google Cloud Run上运行,这是一项出色的服务。 您可以在https://geshan.com.np上了解更多信息。
翻译自:
zuul集成cloud
转载地址:http://sihwd.baihongyu.com/