openfaas 单机部署

函数即服务(faas),现在挺火的一个概念,不同于Iaas、Paas、Saas,颗粒度更细致;
看一下github.com一个开源的openfaas;

minikube

按照官方推荐的教程流程,首先使用minikube启动单点的k8s

1.注意minikube启动的时候,会有一下几种情况:

$ minikube start --vm-driver=virtualbox 
Starting local Kubernetes v1.10.0 cluster...  
Starting VM...  
E0808 02:43:48.772569   32480 start.go:174] Error starting host: Error creating host: Error executing step: Running precreate checks.  
: This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory.

 Retrying.
E0808 02:43:48.773323   32480 start.go:180] Error starting host:  Error creating host: Error executing step: Running precreate checks.  
: This computer doesn't have VT-X/AMD-v enabled. Enabling it in the BIOS is mandatory
$ minikube start --vm-driver=none       
Starting local Kubernetes v1.10.0 cluster...  
Starting VM...  
Getting VM IP address...  
Moving files into cluster...  
Downloading kubelet v1.10.0  
Downloading kubeadm v1.10.0  
Finished Downloading kubelet v1.10.0  
Finished Downloading kubeadm v1.10.0  
Setting up certs...  
Connecting to cluster...  
Setting up kubeconfig...  
Starting cluster components...  
E0808 02:46:15.143072   32631 start.go:300] Error starting cluster:  kubeadm init error  
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1 &&  
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1 &&  
sudo /usr/bin/kubeadm init --config /var/lib/kubeadm.yaml --ignore-preflight-errors=DirAvailable--etc-kubernetes-manifests --ignore-preflight-errors=DirAvailable--data-minikube --ignore-preflight-errors=Port-10250 --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-etcd.yaml --ignore-preflight-errors=Swap --ignore-preflight-errors=CRI  &&  
sudo /usr/bin/kubeadm alpha phase addon kube-dns  
 running command: : running command: 
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1 &&  
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1 &&  
sudo /usr/bin/kubeadm init --config /var/lib/kubeadm.yaml --ignore-preflight-errors=DirAvailable--etc-kubernetes-manifests --ignore-preflight-errors=DirAvailable--data-minikube --ignore-preflight-errors=Port-10250 --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-etcd.yaml --ignore-preflight-errors=Swap --ignore-preflight-errors=CRI  &&  
sudo /usr/bin/kubeadm alpha phase addon kube-dns

 output: sudo: unable to resolve host iZ2zehbv3lrr68hy0bu46zZ
3.1: Pulling from google_containers/pause-amd64  
Digest: sha256:759c3f0f6493093a9043cc813092290af69029699ade0e3dbe024e968fcb7cca  
Status: Image is up to date for registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1  
sudo: unable to resolve host iZ2zehbv3lrr68hy0bu46zZ  
sudo: unable to resolve host iZ2zehbv3lrr68hy0bu46zZ  
[init] Using Kubernetes version: v1.10.0
[init] Using Authorization modes: [Node RBAC]
[preflight] Running pre-flight checks.
    [WARNING Hostname]: hostname "minikube" could not be reached
    [WARNING Hostname]: hostname "minikube" lookup minikube on 127.0.0.1:53: no such host
    [WARNING Swap]: running with swap on is not supported. Please disable swap
    [WARNING FileExisting-ebtables]: ebtables not found in system path
    [WARNING FileExisting-socat]: socat not found in system path
    [WARNING FileExisting-crictl]: crictl not found in system path
Suggestion: go get github.com/kubernetes-incubator/cri-tools/cmd/crictl  
Flag --admission-control has been deprecated, Use --enable-admission-plugins or --disable-admission-plugins instead. Will be removed in a future version.  
[certificates] Using the existing ca certificate and key.
[certificates] Using the existing apiserver certificate and key.
[certificates] Generated apiserver-kubelet-client certificate and key.
[certificates] Generated etcd/ca certificate and key.
[certificates] Generated etcd/server certificate and key.
[certificates] etcd/server serving cert is signed for DNS names [localhost] and IPs [127.0.0.1]
[certificates] Generated etcd/peer certificate and key.
[certificates] etcd/peer serving cert is signed for DNS names [minikube] and IPs [172.17.132.77]
[certificates] Generated etcd/healthcheck-client certificate and key.
[certificates] Generated apiserver-etcd-client certificate and key.
[certificates] Generated sa key and public key.
[certificates] Generated front-proxy-ca certificate and key.
[certificates] Generated front-proxy-client certificate and key.
[certificates] Valid certificates and keys now exist in "/var/lib/localkube/certs/"
a kubeconfig file "/etc/kubernetes/admin.conf" exists already but has got the wrong CA cert  
: running command: 
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1 &&  
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1 &&  
sudo /usr/bin/kubeadm init --config /var/lib/kubeadm.yaml --ignore-preflight-errors=DirAvailable--etc-kubernetes-manifests --ignore-preflight-errors=DirAvailable--data-minikube --ignore-preflight-errors=Port-10250 --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-scheduler.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-apiserver.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-kube-controller-manager.yaml --ignore-preflight-errors=FileAvailable--etc-kubernetes-manifests-etcd.yaml --ignore-preflight-errors=Swap --ignore-preflight-errors=CRI  &&  
sudo /usr/bin/kubeadm alpha phase addon kube-dns

.: exit status 1

按照以下处理:

export CHANGE_MINIKUBE_NONE_USER=true  
minikube start --vm-driver=none  --bootstrapper=localkube  --registry-mirror=https://registry.docker-cn.com  

2.由于我是在无界面的linux环境安装的,如果查看面板会有问题

 minikube dashboard 会尝试打开Xmanager 6
 minikube dashboard --url 给出url,并且监听的是0.0.0.0端口,这样就能从本机打开

tiller

openfaas依赖tiller

这个教程在第二步helm init --skip-refresh --upgrade --service-account tiller,安装tiller时候,容易卡死

tiller-deploy-b7f79ccd9-dkr95                0/1       ImagePullBackOff    0          3m  

采用[aliyun镜像] (https://blog.csdn.net/weiguang1017/article/details/78045013)

helm init --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.5.1 --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts)  

openfaas

1.教程中的第五步,老是报如下错误

cd chart && helm upgrade --install openfaas openfaas/ --namespace openfaas --set functionNamespace=openfaas-fn

$ helm upgrade --install openfaas openfaas/ --namespace openfaas --set functionNamespace=openfaas-fn         
Error: forwarding ports: error upgrading connection: error dialing backend: dial tcp: lookup iz2zehbv3lrr68hy0bu46zz on 127.0.0.1:53: no such host  

clipboard.png

最终直接采用官方在k8s环境的教程进行部署

kubectl apply -f ./yaml  


2. Build the function

faas-cli build -f callme.yml  
[0] > Building callme.
Clearing temporary build folder: ./build/callme/  
Preparing ./callme/ ./build/callme/function  
Building: callme:latest with node template. Please wait..  
Sending build context to Docker daemon 10.24 kB  
Step 1/21 : FROM node:8.9.1-alpine  
 ---> 8bcb66e2abe9
Step 2/21 : RUN addgroup -S app && adduser app -S -G app  
 ---> Using cache
 ---> ac78da818045
Step 3/21 : RUN apk --no-cache add curl     && echo "Pulling watchdog binary from Github."     && curl -sSL https://github.com/openfaas/faas/releases/download/0.8.9/fwatchdog > /usr/bin/fwatchdog     && chmod +x /usr/bin/fwatchdog     && apk del curl --no-cache  
 ---> Using cache
 ---> 2fd5abba2814
Step 4/21 : WORKDIR /root/  
 ---> Using cache
 ---> 4a40ebb6ce9b
Step 5/21 : ENV NPM_CONFIG_LOGLEVEL warn  
 ---> Using cache
 ---> ec1195e79532
Step 6/21 : RUN mkdir -p /home/app  
 ---> Using cache
 ---> 5571b7131af6
Step 7/21 : WORKDIR /home/app  
 ---> Using cache
 ---> f2684510a3ad
Step 8/21 : COPY package.json ./  
 ---> Using cache
 ---> 91c9bb149578
Step 9/21 : RUN npm i --production  
 ---> Using cache
 ---> a880b1ed1895
Step 10/21 : COPY index.js ./  
 ---> Using cache
 ---> 40d3bd24e4c8
Step 11/21 : WORKDIR /home/app/function  
 ---> Using cache
 ---> 5be452fb1eef
Step 12/21 : COPY function/*.json ./  
 ---> Using cache
 ---> 1bfdd21cded6
Step 13/21 : RUN npm i --production || :  
 ---> Using cache
 ---> 86b8fd4ca535
Step 14/21 : COPY --chown=app:app function/ .  
Unknown flag: chown  
2018/08/09 14:30:13 ERROR - Could not execute command: [docker build -t callme:latest .]  

这是因为docker版本问题 copy --chown is supported in > 17.09.x 找到template dockerfile,修改如下

# COPY --chown=app:app function/ .
COPY function/ .  
RUN chown -R app:app .  

3. faas-cli push

$ faas-cli push -f callme.yml 

报错如下:

Unable to push one or more of your functions to Docker Hub:  
- callme

You must provide a username or registry prefix to the Function's image such as user1/function1  

找到文档 If you are using a single-node Docker cluster on your laptop then you can skip this step.

4. 本地镜像部署失败
本地镜像是不行的,把本地的镜像push到阿里云上,然后把callme.yml的地址改成完整地址 registry.cn-hangzhou.aliyuncs.com/test-openfaas/callme:0.0.1
记住版本要写上,不能用lasest

callme.png

备注

如果启动失败,重新

minikube stop; minikube delete  
docker stop (docker ps -aq)  
rm -r ~/.kube ~/.minikube