paas云平台架构_PaaS:通过Node.js中的RESTful API管理您的云基础架构

paas云平台架构

Once upon a time when someone needs to publish their own website on the internet, they had to go buy expensive server hoping that performance will be enough for growing user base or buy even more expensive server that they were not going to use at its full capacity. But this is only a 20% of the work, then they had to contact an ISP and buy a public IP for the server and pay the monthly internet bill and power bill and then make sure the server is running 24×7.

从前,当有人需要在互联网上发布自己的网站时,他们不得不去购买昂贵的服务器,以期性能足以满足不断增长的用户群,或者购买甚至更昂贵的服务器,而这些服务器将无法完全使用。 但这只是工作的20%,然后他们必须联系ISP并为服务器购买公共IP,并支付每月的互联网账单和电费,然后确保服务器运行24×7。

But today we have a hosting service to make sure every single thing is handled for us for a reasonable price. Whether you are a blogger, entrepreneur, reseller or a developer you just have to sit back and relax. Whether you are growing 1 million users per day or having a rough patch, you only need to pay for what you have used.

但是今天,我们提供了托管服务,以确保以合理的价格为我们处理每件事。 无论您是博客作者,企业家,转销商还是开发人员,您都必须高枕无忧。 无论您是每天增长100万用户还是有粗略的收入,您都只需要为使用过的内容付费。

For example, imagine that you have built a rocket to go to mars and you are building a mobile app to sell tickets. You published your app today and when you wake up tomorrow there are millions of people who want to buy a ticket, but your app is broken since your VPS cannot handle the load or you are a VPS reseller who has a website and manually ordering servers from other platforms.

例如,假设您已经建造了火箭去火星,并且正在构建一个移动应用程序来出售门票。 您今天发布了应用,明天醒来的时候有数百万人想购买票,但是您的应用已损坏,因为您的VPS无法处理负载,或者您是拥有网站并从中手动订购服务器的VPS经销商其他平台。

But what if, you can automate this without ever being wanted to log in to your dashboard again. In this article, we are going to see how to develop it using NodeJS. For this article, I will use the Veesp’s API to do that.

但是,如果可以自动执行此操作,而无需再次登录到仪表板。 在本文中,我们将看到如何使用NodeJS开发它。 对于本文,我将使用Veesp的API来完成。

Further details about this API can be found in the official documentation at following link https://secure.veesp.com/userapi

有关此API的更多详细信息,请参见以下链接的官方文档: https://secure.veesp.com/userapi

paas云平台架构_PaaS:通过Node.js中的RESTful API管理您的云基础架构

So let’s get started!

因此,让我们开始吧!

安装npm请求模块 (Install the npm Request Module)

In order to perform any kind of HTTP requests, you need an HTTP client library. You are free to use your own NodeJS HTTP client. For this article, we are going to use npm request module.

为了执行任何类型的HTTP请求,您需要一个HTTP客户端库。 您可以*使用自己的NodeJS HTTP客户端。 对于本文,我们将使用npm request模块。

So first let’s get started by installing the request module using npm,

所以首先让我们开始使用npm安装请求模块,

1
npm i --save request
1
npm i -- save request

Then create a file called app.js and import the request module.

然后创建一个名为app.js的文件并导入请求模块。

1
const request = require('request');
1
const request = require ( 'request' ) ;

订购VPS (Ordering a VPS)

paas云平台架构_PaaS:通过Node.js中的RESTful API管理您的云基础架构

Veesp provide a variety of service categories including VPS hosting, domain names, DNS hosting and few other services. Also, VPS hosting is divided into a few types, Linux SSD VPS, Linux HDD VPS, Windows VPS and dedicated servers.

Veesp提供各种服务类别,包括VPS托管,域名,DNS托管和其他一些服务。 此外,VPS托管分为几种类型,Linux SSD VPS,Linux HDD VPS,Windows VPS和专用服务器。

Veesp API uses HTTP basic user authentication for authenticating users. You should send your username and password with the request headers in order to use endpoints.

Veesp API使用HTTP基本用户身份验证来验证用户。 您应该将用户名和密码与请求标头一起发送,以便使用端点。

We will leave out this module import, and username password authentication after this example. But you should send these authentication details in each request as we mentioned before,

在此示例之后,我们将忽略此模块的导入以及用户名密码验证。 但是您应该像我们之前提到的那样在每个请求中发送这些身份验证详细信息,

So here is the code to get a list of service categories from the API.

因此,这是从API获取服务类别列表的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const request = require('request');
const username = ' [email protected]';
const password = 'yourpassword';
const url = 'https://secure.veesp.com/api/category';
const auth = new Buffer.from(username + ':' + password).toString('base64');
const req = {
    url: url,
    method: 'GET',
    headers: {
        'Authorization': 'Basic ' + auth,
        'Content-Type': 'application/json'
    }
};
request(req, (err, res) => {
    const body = JSON.parse(res.body);
    console.log(body);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const request = require ( 'request' ) ;
const username = ' [email protected] ' ;
const password = 'yourpassword' ;
const url = 'https://secure.veesp.com/api/category' ;
const auth = new Buffer . from ( username + ':' + password ) . toString ( 'base64' ) ;
const req = {
    url : url ,
    method : 'GET' ,
    headers : {
        'Authorization' : 'Basic ' + auth ,
        'Content-Type' : 'application/json'
    }
} ;
request ( req , ( err , res ) = > {
    const body = JSON . parse ( res . body ) ;
    console . log ( body ) ;
} ) ;

When you want to execute the code you can open your terminal and run node app.js. Your console output should look something like this,

当您要执行代码时,可以打开终端并运行node app.js 您的控制台输出应如下所示:

1
2
3
4
5
6
7
8
9
10
11
{ categories:
   [ { id: '18', name: 'Linux SSD VPS', description: '', slug: 'vps' },
     { id: '19',
       name: 'Linux HDD VPS',
       description: '',There is also requests and reonses
       slug: 'hdd-vps' },
     { id: '17',
       name: 'Windows VPS',
       description: '',
       slug: 'windows-vps' },
     …
1
2
3
4
5
6
7
8
9
10
11
{ categories :
   [ { id : '18' , name : 'Linux SSD VPS' , description : '' , slug : 'vps' } ,
     { id : '19' ,
       name : 'Linux HDD VPS' ,
       description : '' , There is also requests and reonses
       slug : 'hdd-vps' } ,
     { id : '17' ,
       name : 'Windows VPS' ,
       description : '' ,
       slug : 'windows-vps' } ,
     …

So we have a list of categories and relevant ID for each category. For this example, we are going to order a new Linux SSD VPS where the ID is 18 as we can see in the response.

因此,我们有一个类别列表和每个类别的相关ID。 对于此示例,我们将订购一个ID为18的新Linux SSD VPS,如我们在响应中所见。

Then we can use this category ID to get a list of products under this category as below.

然后,我们可以使用该类别ID来获取该类别下的产品列表,如下所示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const url = 'https://secure.veesp.com/api/category/18/product';
const req = {
    url: url,
    method: 'GET',
    headers: {
        'Authorization': 'Basic ' + auth,
        'Content-Type': 'application/json'
    }
};
request(req, (err, res) => {
    const body = JSON.parse(res.body);
    console.log(body);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const url = 'https://secure.veesp.com/api/category/18/product' ;
const req = {
     url : url ,
     method : 'GET' ,
     headers : {
         'Authorization' : 'Basic ' + auth ,
         'Content-Type' : 'application/json'
     }
} ;
request ( req , ( err , res ) = > {
     const body = JSON . parse ( res . body ) ;
     console . log ( body ) ;
} ) ;

After executing this code, your console output should look something like this,

执行此代码后,您的控制台输出应如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{ products:
   [ { id: '212',
       type: '11',
       name: 'SSD Sandbox',
       stock: false,
       paytype: 'Regular',
       description:
        'CPU:1 vCore<br>RAM:512MB<br>SSD:10GB<br>Traffic:100GB<br>Bandwidth:200Mbps<br><ul></ul>',
       qty: -603,
       tags: [],
       periods: [Array] },
     { id: '236',
       type: '11',
       name: 'SSD 1',
       stock: false,
       paytype: 'Regular',
       description:
        'CPU:1 vCore<br>RAM:1GB<br>SSD:25GB<br>Traffic:Unlimited<br>Bandwidth:200Mbps<br><ul></ul>',       qty: -436,
       tags: [],
       periods: [Array] },
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{ products :
   [ { id : '212' ,
       type : '11' ,
       name : 'SSD Sandbox' ,
       stock : false ,
       paytype : 'Regular' ,
       description :
         'CPU:1 vCore<br>RAM:512MB<br>SSD:10GB<br>Traffic:100GB<br>Bandwidth:200Mbps<br><ul></ul>' ,
       qty : - 603 ,
       tags : [ ] ,
       periods : [ Array ] } ,
     { id : '236' ,
       type : '11' ,
       name : 'SSD 1' ,
       stock : false ,
       paytype : 'Regular' ,
       description :
         'CPU:1 vCore<br>RAM:1GB<br>SSD:25GB<br>Traffic:Unlimited<br>Bandwidth:200Mbps<br><ul></ul>' ,        qty : - 436 ,
       tags : [ ] ,
       periods : [ Array ] } ,

Now we can order the product. Pass your hostname as a parameter ‘domain’ in the request body. You can select the ID of the product you want to order, for this example we are going to order 512 Mb Ram, 10 Gb SSD with 100 Gb traffic and 200 Mbps network interface. ID relevant to that product is 212 as you can see in the response.

现在我们可以订购产品。 在请求正文中将您的主机名作为参数“域”传递。 您可以选择要订购的产品的ID,在本示例中,我们将订购512 Mb Ram,具有100 Gb流量和200 Mbps网络接口的10 Gb SSD。 如您在响应中看到的,与该产品相关的ID为212。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const url = 'https://secure.veesp.com/api/order/212';
const options = {
    method: 'POST',
    url: url,
    headers: {
        'Authorization': 'Basic ' + auth,
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        domain: 'myhostname'
    })
};
request(options, function (err, res, body) {
    console.log(body);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const url = 'https://secure.veesp.com/api/order/212' ;
const options = {
    method : 'POST' ,
    url : url ,
    headers : {
        'Authorization' : 'Basic ' + auth ,
        'Content-Type' : 'application/json'
    } ,
    body : JSON . stringify ( {
        domain : 'myhostname'
    } )
} ;
request ( options , function ( err , res , body ) {
    console . log ( body ) ;
} ) ;

If you do not get any error from the server, you VPS should now be up and running.

如果未从服务器收到任何错误,则VPS现在应该已启动并正在运行。

列出当前服务 (Listing the Current Services)

You may want to get a list of current VPS to analyze your current usage, and also to control stop or start a VPS when needed.

您可能需要获取当前VPS的列表,以分析当前使用情况,并在需要时控制停止或启动VPS。

Let’s take a list of services by calling the /service API as below

让我们通过调用/ service API来获取服务列表,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const url = 'https://secure.veesp.com/api/service';
const req = {
   url: url,
   method: 'GET',
   headers: {
       'Authorization': 'Basic ' + auth,
       'Content-Type': 'application/json'
   }
};
request(req, (err, res) => {
   const body = JSON.parse(res.body);
   console.log(body);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const url = 'https://secure.veesp.com/api/service' ;
const req = {
   url : url ,
   method : 'GET' ,
   headers : {
       'Authorization' : 'Basic ' + auth ,
       'Content-Type' : 'application/json'
   }
} ;
request ( req , ( err , res ) = > {
   const body = JSON . parse ( res . body ) ;
   console . log ( body ) ;
} ) ;

Execute this code and you should get a response like this,

执行此代码,您应该得到这样的响应,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{ services:
   [ { id: '29618',
       domain: 'xd002',
       total: '4.000',
       status: 'Active',
       billingcycle: 'Monthly',
       next_due: '2019-07-22',
       category: 'Linux SSD VPS',
       category_url: 'vps',
       name: 'SSD Sandbox' },
     { id: '29611',
       domain: 'xdhost',
       total: '4.000',
       status: 'Active',
       billingcycle: 'Monthly',
       next_due: '2019-07-21',
       category: 'Linux SSD VPS',
       category_url: 'vps',
       name: 'SSD Sandbox' } ] }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{ services :
   [ { id : '29618' ,
       domain : 'xd002' ,
       total : '4.000' ,
       status : 'Active' ,
       billingcycle : 'Monthly' ,
       next_due : '2019-07-22' ,
       category : 'Linux SSD VPS' ,
       category_url : 'vps' ,
       name : 'SSD Sandbox' } ,
     { id : '29611' ,
       domain : 'xdhost' ,
       total : '4.000' ,
       status : 'Active' ,
       billingcycle : 'Monthly' ,
       next_due : '2019-07-21' ,
       category : 'Linux SSD VPS' ,
       category_url : 'vps' ,
       name : 'SSD Sandbox' } ] }

Because we have created a Linux SSD VPS above we will get that and a list of all other services we are using. If you only need VPS servers or a certain category only, you can filter by the category_url or category as you wish.

因为我们已经在上面创建了Linux SSD VPS,所以我们将获得它以及我们正在使用的所有其他服务的列表。 如果仅需要VPS服务器或仅某个类别,则可以根据需要按category_url或类别进行过滤。

For example, if you only want to select only Linux SSD VPS category

例如,如果您只想选择Linux SSD VPS类别

1
2
3
4
5
6
7
8
9
10
11
12
request(req, (err, res) => {
   const body = JSON.parse(res.body);
   const services = body.services;
   const linuxSSDVpsList = services.filter((item) => {
       if (item.category === 'Linux SSD VPS') {
           return true;
       }
   });
   console.log(linuxSSDVpsList);
});
1
2
3
4
5
6
7
8
9
10
11
12
request ( req , ( err , res ) = > {
   const body = JSON . parse ( res . body ) ;
   const services = body . services ;
   const linuxSSDVpsList = services . filter ( ( item ) = > {
       if ( item . category === 'Linux SSD VPS' ) {
           return true ;
       }
   } ) ;
   console . log ( linuxSSDVpsList ) ;
} ) ;

停止并启动VPS (Stop and Start a VPS)

Now we have a list of services and their IDs. Finally, now we can get a list of VMs related to the service as below. You should pass your service ID with the following URL. Please, note that the service ID of the above request 29618 is used to get the VM list.

现在,我们有了服务及其ID的列表。 最后,现在我们可以获取与该服务相关的VM列表,如下所示。 您应该使用以下URL传递服务ID。 请注意,以上请求29618的服务ID用于获取VM列表。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const url = 'https://secure.veesp.com/api/service/29618/vms';
const req = {
   url: url,
   method: 'GET',
   headers: {
       'Authorization': 'Basic ' + auth,
       'Content-Type': 'application/json'
   }
};
request(req, (err, res) => {
   const body = JSON.parse(res.body);
   console.log(body);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const url = 'https://secure.veesp.com/api/service/29618/vms' ;
const req = {
   url : url ,
   method : 'GET' ,
   headers : {
       'Authorization' : 'Basic ' + auth ,
       'Content-Type' : 'application/json'
   }
} ;
request ( req , ( err , res ) = > {
   const body = JSON . parse ( res . body ) ;
   console . log ( body ) ;
} ) ;

You should get a response like this from the server with VM IDs and info including the password,

您应该从服务器收到类似这样的响应,其中包含VM ID和信息(包括密码,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
{
  "vms": {
    "17228": {
      "password": "afakepassword",
      "template": "linux-centos-6-i386-min-gen2-v1",
      "template_label": "CentOS 6 32 bit\t",
      "disk": 10,
      "memory": 512,
      "burstmem": -512,
      "bandwidth": 100,
      "pae": 0,
      "pxe": 0,
      "id": "17228",
      "state": "online",
      "ipv6subnets": [
        "2a00:1345:37:13a::/64"
      ],
      "usage": {
        "disk": {
          "total": 10,
          "used": 0,
          "free": 10,
          "percent": "0"
        },
        "memory": {
          "total": 0,
          "used": 0,
          "free": 0,
          "percent": "0"
        },
        "bandwidth": {
          "total": 100,
          "used": 0,
          "free": 100,
          "percent": "0"
        }
      },
      "label": "xd002",
      "ip": [
        "34.34.56.56",
        " 2a00:1345:37:13a::a246"
      ],
      "cpus": "1"
    }
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
{
  "vms" : {
    "17228" : {
      "password" : "afakepassword" ,
      "template" : "linux-centos-6-i386-min-gen2-v1" ,
      "template_label" : "CentOS 6 32 bit\t" ,
      "disk" : 10 ,
      "memory" : 512 ,
      "burstmem" : - 512 ,
      "bandwidth" : 100 ,
      "pae" : 0 ,
      "pxe" : 0 ,
      "id" : "17228" ,
      "state" : "online" ,
      "ipv6subnets" : [
        "2a00:1345:37:13a::/64"
      ] ,
      "usage" : {
        "disk" : {
          "total" : 10 ,
          "used" : 0 ,
          "free" : 10 ,
          "percent" : "0"
        } ,
        "memory" : {
          "total" : 0 ,
          "used" : 0 ,
          "free" : 0 ,
          "percent" : "0"
        } ,
        "bandwidth" : {
          "total" : 100 ,
          "used" : 0 ,
          "free" : 100 ,
          "percent" : "0"
        }
      } ,
      "label" : "xd002" ,
      "ip" : [
        "34.34.56.56" ,
        " 2a00:1345:37:13a::a246"
      ] ,
      "cpus" : "1"
    }
  }
}

As you can see you have the assigned public IP and the password to authenticate the VPS in the response. Now you can automate deploying your application and scaling logic at this stage or give back these credentials to your customers if you are a reseller.

如您所见,您已为响应中的VPS分配了已分配的公共IP和密码。 现在,您可以在此阶段自动部署应用程序和扩展逻辑,或者如果您是经销商,则可以将这些凭据退还给客户。

Now you can perform stop and start operation on your VPS as below,

现在,您可以按以下步骤在VPS上执行停止和启动操作,

To stop the server,

要停止服务器,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const url = 'https://secure.veesp.com/api/service/29618/vms/17228/stop';
const options = {
   method: 'POST',
   url: url,
   headers: {
       'Authorization': 'Basic ' + auth,
       'Content-Type': 'application/json'
   }
};
request(options, function (err, res, body) {
   console.log(body);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const url = 'https://secure.veesp.com/api/service/29618/vms/17228/stop' ;
const options = {
   method : 'POST' ,
   url : url ,
   headers : {
       'Authorization' : 'Basic ' + auth ,
       'Content-Type' : 'application/json'
   }
} ;
request ( options , function ( err , res , body ) {
   console . log ( body ) ;
} ) ;

To start the server,

要启动服务器,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const url = 'https://secure.veesp.com/api/service/29618/vms/17228/start';
const options = {
   method: 'POST',
   url: url,
   headers: {
       'Authorization': 'Basic ' + auth,
       'Content-Type': 'application/json'
   }
};
request(options, function (err, res, body) {
   console.log(body);
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
const url = 'https://secure.veesp.com/api/service/29618/vms/17228/start' ;
const options = {
   method : 'POST' ,
   url : url ,
   headers : {
       'Authorization' : 'Basic ' + auth ,
       'Content-Type' : 'application/json'
   }
} ;
request ( options , function ( err , res , body ) {
   console . log ( body ) ;
} ) ;

Other than that you can rebuild your VPS, restart your VPS, change the boot order of your VPS using this API.

除此之外,您还可以使用此API重建VPS,重新启动VPS,更改VPS的启动顺序。

So this is the basics of how you can use automation and application scaling using HTTP API. Hope you will find this useful to automate and scale up your business as your customer base grows, without even needing to login to the dashboard ever again.

因此,这是如何使用HTTP API使用自动化和应用程序扩展的基础。 希望您会发现这对于随着客户群的增长实现自动化和扩展业务很有用,甚至无需再次登录仪表板。

Happy scaling!

缩放愉快!

Thanks for spending a few minutes to read this article!

感谢您花几分钟阅读本文!

翻译自: https://www.thecrazyprogrammer.com/2019/06/paas-managing-your-cloud-infrastructure-via-restful-api-in-node-js.html

paas云平台架构