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
};
}
}
複製程式碼