如何将 node.js 应用程序 Dockerize、使用 Kubernetes 编排容器、设置 CI/CD 并在云上部署

在本文中,我将向您介绍如何对 node.js 应用程序进行 docker 化、使用 Kubernetes 编排容器、使用持续集成自动交付、使用 CircleCI 进行持续交付 (CI/CD) 以及部署到 Google Cloud 平台

这也是我刚刚结束的 She Code Africa Cloud School Cohort 4 的最终项目。

要求

CircleCI上的Docker Desktop
Account本地机器上下载的Kubectl Cloud account,本文我使用了Google cloud 谷歌云终端GitHub上的Google SDK Account 文本编辑器,我使用了VS-code。您可以使用任何您熟悉的文本编辑器。



 

第 1 步:构建应用程序

我们要做的第一件事是构建应用程序。这个应用程序将是一个简单的索引文件和一个 node.js 文件。
复制并粘贴这些代码并保存在 index.html 文件中

<span style="color:var(--syntax-text-color)"><span style="color:var(--syntax-text-color)"><code><html>
 <head>
  <title>My first CICD Project</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css" rel="stylesheet">
 </head>
 <body>

  <div class="col-md-10 col-md-offset-1" style="margin-top:20px">
   <div class="panel panel-primary">
     <div class="panel-heading">
       <h3 class="panel-title">MY first CICD Project</h3>
     </div>
      <div class="panel-body">
       <div class="alert alert-success">
          <p>This is a basic app used to demonstrate CI/CD on Google Cloud using K8s, CircleCI and Docker </p>
       </div>
         <div class="col-md-9">
           <p>Author:</p>
           <div class="col-md-3">
             <a href="https://twitter.com/delan_hype">Dooshima</a>
           </div>
         </div>
      </div>
  </div>
  </div>

<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script>
 </body>
</html>
</code></span></span>

node.js 文件将包含以下代码,将代码复制并粘贴到 server.js 中

<span style="color:var(--syntax-text-color)"><span style="color:var(--syntax-text-color)"><code>const express = require('express');
const path = require('path');
const morgan = require('morgan');
const bodyParser = require('body-parser');

/* eslint-disable no-console */

const port = process.env.PORT || 3000;
const app = express();

app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: 'true' }));
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));

app.use(express.static(path.join(__dirname, './')));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, './index.html'));
});

app.listen(port, (e
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值