Faas 和无服务器架构
Faas 和無服務器架構
從IaaS、PaaS、SaaS到CaaS,再到火熱的微服務架構,人們孜孜不倦的追求著將硬件資源抽象化,從虛擬機到容器, 再到現在的無服務器架構,FaaS是Functions as a Service的簡稱,代表業務方視角,而Serverless更多是 從部署的視角,其實描述的是類似的事情。目前AWS的Lambda是無服務架構的代表,你可以很輕易的嘗試,目前每個月的前100萬次的請求是免費的,快去體驗吧。- 1
- 2
- 3
- 4
- 5
當前流行的faas開源組件
有好多,你去github搜一下,今天需要學習的是目前star數最多的OpenFaas,話說這個組件的作者,各個場合都在不遺余力的宣傳,希望大家去標星它。廢話不多說了,開始部署。先附上這個項目的地址:openfaas。
準備環境
- Build a cluster?
它可以部署在swarm集群或者k8s集群上,所以前提是你要有一個K8s集群,關于k8s集群的搭建,請參照?kubernetes 1.9.6 集群搭建。如果是用把k8s的各個組件,一個個作為服務去部署的化,你可能會踩無數的坑,但是對你理解k8s會有很大幫助。省事的話就用kubeadm。but!這里需要提醒的是,OpenFaas因為在編譯鏡像時使用了,多階段構建的特性,因此docker版本要求必須是,17.05 or higher。 - Deploy OpenFaaS?
在master節點上執行:?
git clone https://github.com/openfaas/faas-netes?
faas-nets是一個將faas組件部署在k8s集群的項目,內含一些yaml文件的定義。?
權限、網關、prometheus等。?
cd faas-netes && \?
kubectl apply -f ./namespaces.yml,./yaml?
這里會創建出2個命名空間:openfaas?和openfaas-fn?
還有其他和faas有關的pod出來。? -
Use OpenFaaS?
使用CLI,這里需要訪問aws的存儲主機,所以國內的話還是被墻,沒辦法正常下載部分github上的release包。?
curl -sL https://cli.openfaas.com | sudo sh?
這里(可能)會失敗,需要你去看下這個腳本,單獨的下載下來(這個文件在你可以在我這里下載),然后閱讀這個很簡單的腳本。- 手工復制到/usr/local/bin目錄下
- 加個執行權限
- 創建個軟鏈接
-
Deploy a function?
mkdir -p ~/functions && \ cd ~/functions
準備一個函數吧,python的:- 1
- 2
faas-cli new --lang python hello-python?
hello-python.yml hello-python文件夾
這會生成- 1
- 2
改下hello-python/handler.py:
def handle(req):print("Hello! You said: " + req)- 1
- 2
以及hello-python.yml:
provider:name: faasgateway: http://clusterIp:31112functions:hello-python:lang: pythonhandler: ./hello-pythonimage: yourName/hello-python(這個地址是dockerhub地址)- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
在這里記得先docker login一下。否則分發時會提示你沒有權限push。?
名詞解釋:- gateway- here we can specify a remote gateway if we need to, what the programming language is and where our handler is located within the filesystem.(即是你的網管地址:xx:31112)
- functions - this block defines the functions in our stack(描述函數信息)
- lang: python - even though Docker is used behind the scenes to package your function. You don’t have to write your own Dockerfile unless you want to.(語言)
- handler - this is the folder / path to your handler.py file and any other source code you need(函數內容)
- image - this is the Docker image name. If you are going to push to the Docker Hub change the prefix from hello-python to include your Docker Hub account (例如lmxia/hello-python)
-
faas-cli build -f ./hello-python.yml?
這里可能會報錯,如果下載失敗,或者超時,你又要科學上網了,或者從寫我七牛云上的一個長久有效的地址:http://oikou4x2o.bkt.clouddn.com/faas-cli?和?http://oikou4x2o.bkt.clouddn.com/fwatchdog?在你的模板目錄下,將你的函數對應的模板里Dockerfile里出現的fwathdog全部替換成剛才的鏈接。?
find . -type f | xargs grep "/fwatchdog"?找到所有的fwatchdog地址部分,然后替換掉。 -
驗證?
以上步驟成功后?
docker images | grep hello-python?
能看到本地的docker images,這個就是所謂你的函數運行的image了,所以無服務器架構還是有服務器的。只不過你認為它是透明的就好了。?
然后分發到docker.hub上?
faas-cli push -f ./hello-python.yml?
這樣你的node節點,就可以有地方下載了,當然如果你有私有鏡像倉庫,就傳到私有鏡像倉庫。我這里使用的是docker.hub。 -
接下來部署
faas-cli deploy -f ./hello-python.yml?
curl 127.0.0.1:31112/function/hello-python -d “hello, world!”?
這個時候你會發現,k8s神奇的給我們在node節點上部署了一個pod,這個pod鏡像正是我們剛剛產生的鏡像。 -
更復雜的用法?
比如你的python包還有其他依賴包,比如第三方包等,這塊先不寫了。?
回頭補上。
總結
以上是生活随笔為你收集整理的Faas 和无服务器架构的全部內容,希望文章能夠幫你解決所遇到的問題。