基于K8S部署fission函数即服务
Fission简介Fission 是一个类似AWS lamba的一个服务,其Github地址:https://github.com/fission/fission相似的同类产品有kubeless,knative,都是属于Faas,其中kubeless可以在katacoda.com优先体验下。相比kubeless在部署和使用上相对简单,而比较受关注的knative,还处于孵化阶段,...
·
Fission简介
Fission 是一个类似AWS lamba的一个服务,其Github地址:
相似的同类产品有kubeless,knative,都是属于Faas,其中kubeless可以在katacoda.com优先体验下。
相比kubeless在部署和使用上相对简单,而比较受关注的knative,还处于孵化阶段,目前暂没办法进行比较。
Faas的优势
用户可以运行任意语言编写的代码
可以运行任意时间
可以在任何地方运行
利用已有服务或者第三方资源
几秒内完成执行
部署fission函数即服务
拉取相关docker镜像
{
docker pull fission/fission-bundle
docker pull fission/fetcher
docker pull fission/alpinecurl
docker pull fission/pre-upgrade-checks
docker pull nats-streaming
docker pull fission/fluentd
docker pull tutum/influxdb
docker pull fission/node-env
}
部署fission
helm install --namespace fission --set serviceType=NodePort,routerServiceType=NodePort http://7j1x5e.com1.z0.glb.clouddn.com/fission-all-0.9.2.0.tgz
安装客户端
curl -Lo fission https://github.com/fission/fission/releases/download/0.9.2/fission-cli-linux && chmod +x fission && sudo mv fission /usr/local/bin/
创建node容器
fission env create --name nodejs --image fission/node-env
加载运行weather.js
wget https://raw.githubusercontent.com/fission/fission/master/examples/nodejs/weather.js
[root@master ~]# fission function create --name weather --env nodejs --code weather.js
function 'weather' created
创建js里面的函数
[root@master ~]# fission route create --method POST --url /weather --function weather
trigger '222c1052-9c19-4e88-a544-a55bdaf18b99' created
测试函数
[root@master ~]# curl -qs -H "Content-Type: application/json" -X POST -d '{"location":"Sieteiglesias, Spain"}' http://127.0.0.1:31314/weather|jq
{
"text": "It is 17 celsius degrees in Sieteiglesias, Spain and Partly Cloudy"
}
测试国内的天气
[root@master ~]# curl -qs -H "Content-Type: application/json" -X POST -d '{"location":"xiamen, China"}' http://127.0.0.1:31314/weather|jq
{
"text": "It is 28 celsius degrees in xiamen, China and Mostly Cloudy"
}
来看下 weather.js
[root@master ~]# cat weather.js
'use strict';
const rp = require('request-promise-native');
module.exports = async function (context) {
const stringBody = JSON.stringify(context.request.body);
const body = JSON.parse(stringBody);
const location = body.location;
if (!location) {
return {
status: 400,
body: {
text: 'You must provide a location.'
}
};
}
try {
const response = await rp(`https://query.yahooapis.com/v1/public/yql?q=select item.condition from weather.forecast where woeid in (select woeid from geo.places(1) where text="${location}") and u="c"&format=json`);
const condition = JSON.parse(response).query.results.channel.item.condition;
const text = condition.text;
const temperature = condition.temp;
return {
status: 200,
body: {
text: `It is ${temperature} celsius degrees in ${location} and ${text}`
},
headers: {
'Content-Type': 'application/json'
}
};
} catch (e) {
console.error(e);
return {
status: 500,
body: e
};
}
}
转载于:https://blog.51cto.com/13922668/2159680
更多推荐
已为社区贡献466条内容
所有评论(0)