翻译完成

This commit is contained in:
mandeler 2017-11-22 23:00:14 +08:00
parent 992d76fd1f
commit 6ba8782a3c

View File

@ -1,42 +1,64 @@
Getting started with OpenFaaS on minikube
#借助 minikube 上手 OpenFaaS
============================================================
This is a guide on how to setup OpenFaaS _Serverless Functions Made Simple_ on Kubernetes 1.8 with [minikube][4]. minikube is a [Kubernetes][5] distribution which lets you run a Kubernetes cluster on your laptopits available for Mac and Linux, but is most commonly used with MacOS.
本文将介绍如何借助 [minikube][4] 在 Kubernetes 1.8 上搭建 OpenFaaS让 Serverless Function 变得更简单。minikube 是一个 [Kubernetes][5] 发行版,借助它,你可以在笔记本电脑上运行 Kubernetes 群集minikube 支持 Mac 和 Linux 操作系统,但是在 MacOS 上使用得更多一些。
> This post is based upon our [Official Kubernetes Deployment guide][6]
> 本文基于我们最新的部署手册 [Official Kubernetes Deployment guide][6]
** 此处有Canvas,请手动处理 **
![](https://cdn-images-1.medium.com/max/1600/1*C9845SlyaaT1_xrAGOBURg.png)
### Getting set up with Minikube
### 安装部署 Minikube
1. Install the [xhyve driver][1] or [VirtualBox][2] which will be used to create a Linux VM for minikube. I find the VirtualBox option to be the most reliable.
1. 安装 [xhyve driver][1] 或 [VirtualBox][2] ,然后在上面安装 Liux 虚拟机以部署 minikube 。根据我的经验VirtualBox更稳定一些
2. Setup minikube using the [official docs][3]
2. [参照官方文档][3] 安装 minikube
3. Install `faas-cli` using `brew` or `curl -sL cli.openfaas.com | sudo sh`
3. 安装 `faas-cli` 
4. Install the `helm` CLI via brew: `brew install kubernetes-helm`
使用 brew 安装
5. Start minikube: `minikube start`
```brew install faas-cli```
> Docker Captains tip: look out for when Docker Inc starts to ship Kubernetes built-into Docker for Mac and Windows. This will let you use Kubernetes without having to install additional software.
使用官方安装脚本
### Deploy OpenFaaS to minikube
```curl -sL cli.openfaas.com | sudo sh```
1. Create a service account for Helms server component (tiller): `kubectl -n kube-system create sa tiller \
 && kubectl create clusterrolebinding tiller \
 --clusterrole cluster-admin \
 --serviceaccount=kube-system:tiller`
4. 用 brew 安装 `helm`
2. Install `tiller` which is Helms server-side component: `helm init --skip-refresh --upgrade --service-account tiller`
 `brew install kubernetes-helm`
3. Clone faas-netes (Kubernetes driver for OpenFaaS): `git clone https://github.com/openfaas/faas-netes && cd faas-netes`
5. 运行 minikube :
4. Minikube is not configured for RBAC, so well pass an additional flag to turn it off: `helm upgrade --install --debug --reset-values --set async=false --set rbac=false openfaas openfaas/`
`minikube start`
Youll now see the OpenFaaS pods being installed on your minikube cluster. Type in `kubectl get pods` to see them:
> Docker Captain 的小贴士: Mac 和 Windows 版本的 Docker 已经集成了对 Kubernetes 的支持。现在我们使用 Kubernetes 的时候,已经不需要再安装额外的软件了。
### 在 minikube 上面部署 OpenFaaS
1. 给 Helms 服务器组件新建账号 tiller:
`kubectl -n kube-system create sa tiller && kubectl create clusterrolebinding tiller \`
`--verbose_rbose_usterrole cluster-admin \`
`--verbose_rviceaccount=kube-system:tiller`
2. 安装 Helm 的服务端组件 tiller:
  `helm init --skip-refresh --upgrade --service-account tiller`
3. Git Clone faas-netes (Kubernetes 上面的 OpenFaaS 驱动程序): 
`git clone https://github.com/openfaas/faas-netes && cd faas-netes`
4. Minikube 没有配置 RBAC, 这里我们需要把 RBAC 关闭:
  `helm upgrade --install --debug --reset-values --set async=false --set rbac=false openfaas openfaas/`
译者注RBACRole-Based access control基于角色的访问权限控制在计算机权限管理中较为常用详情请参考以下链接
https://en.wikipedia.org/wiki/Role-based_access_control
现在,你可以看到 OpenFaaS pods 已经在你的 minikube 群集上运行起来了。输入 `kubectl get pods` 以查看 OpenFaaS pods:
```
NAME READY STATUS RESTARTS AGE
@ -60,43 +82,43 @@ prometheus-64f9844488-t2mvn 1/1 Running 0 1m
30,000ft:
The API Gateway contains a [minimal UI for testing functions][7] and exposes a [RESTful API][8] for function management. The faas-netesd daemon is a Kubernetes controller which connects to the Kubernetes API server to manage Services, Deployments and Secrets.
API gateway 进程包含了一个 [用于测试的最小化 UI][7] ,同时开放了用于功能管理的 [RESTful API][8] 。
faas-netesd 守护进程是一种 Kubernetes 控制器,用来连接 Kubernetes API 服务实现对 Kubernetes 函数、部署和密码的管理功能。
Prometheus 和 AlertManager 进程协同工作,实现 OpenFaaS 函数的弹性缩放,以满足业务需求。通过 Prometheus metrics 我们可以查看系统的整体运行状态还可以用来开发功能强悍的仪表盘Dashboard
Prometheus and AlertManager work in tandem to enable auto-scaling of functions to match demand. Prometheus metrics give you operational oversight and allow you to build powerful dashboards.
Prometheus dashboard example:
Prometheus 仪表盘示例:
![](https://cdn-images-1.medium.com/max/1600/1*b0RnaFIss5fOJXkpIJJgMw.jpeg)
### Build/ship/run
### 部署/迁移/运行
OpenFaaS uses the Docker image format for the creation and versioning of functions which means that unlike many other FaaS projects you can use this in production to do:
和很多其他的 FaaS 项目不同OpenFaaS 的创建和函数版本控制使用的是 Docker 镜像格式,这意味着咱可以在生产环境中使用 OpenFaaS 实现以下目标:
* vulnerability scanning
* 漏洞扫描(vulnerability scanning;译者注:此处我觉得应该理解为更快地实现漏洞补丁)
* CI/CD
* CI/CD Continuous integration and continuous deployment 持续集成/持续开发
* rolling upgrades
* 滚动更新
You can also deploy OpenFaaS to your existing production cluster and make use of spare capacity. The core services require around 1030MB of RAM each.
你也可以在现有的生产环境群集中利用空闲资源部署 OpenFaaS。每个核心服务组件内存占用大概在 10-30MB 。
> A key advantage of OpenFaaS is that it works with the container orchestration platforms API, which means we integrate natively with both Kubernetes and Docker Swarm. Also, since our functions are properly versioned within a Docker registry, we can scale our functions on demand without any additional latency associated with frameworks that build functions on demand.
> OpenFaaS 一个关键的优势在于,它可以使用容器编排平台的 API ,这样可以和 Kubernetes 以及 Docker Swarm 进行本地集成。同时由于使用Docker registry 进行函数的版本控制,咱可以按需扩展函数。同时不会对按需开发函数的框架造成额外的延时。
### Scaffold a new function
### 新建 function
`faas-cli new --lang python hello`
This will create `hello.yml` along with a `handler` folder containing your handler.py file and requirements.txt for any pip modules you may need. You can edit these at any time without worrying about maintaining a Dockerfilewe do that and use best practices:
以上命令创建文件 `hello.yml` 以及文件夹 `handler`,文件夹有两个文件 handler.py 、requirements.txt ,包含了你可能需要用到的任何 pip 模块。你可以随时编辑这些文件和文件夹,不需要担心如何维护 Dockerfile--我们为你维护,并且使用以下最佳实践:
* multi-stage builds
* 分级创建版本(multi-stage builds
* non-root users
* 非 root 用户(non-root users
* Official Docker Alpine Linux builds for the base (this is swappable)
* 以 Docker Alpine Linux 版本为基础进行镜像构建 (可变更)
### Build your function
### build function
Your function will be built on your local machine and then pushed to a Docker registry. Lets use the Docker Hubjust edit the `hello.yml` file and enter your user account name:
先在本地创建函数,然后 push 到 Docker registry 。 使用 Docker Hub ,打开文件 `hello.yml` 然后输入你的账号名:
```
provider:
@ -130,19 +152,19 @@ functions:
image: alexellis2/hello
```
Now invoke a build. You will need Docker on your local system.
现在,调用一个 build 版本。你的系统上需要安装 Docker 。
`faas-cli build -f hello.yml`
Push the versioned Docker image which contains your function up to the Docker Hub. If youve not logged into the Docker hub then type in `docker login` before carrying on.
把封装好函数的 Docker 镜像版本 push 到 Docker Hub。如果还没有登录 Docker hub ,继续前需要先输入命令 `docker login` 。
`faas-cli push -f hello.yml`
Once you have multiple functions you can use the `--parallel=N` flag to build or push with multiple cores at once. The CLI also supports options such as `--no-cache` and `--squash`.
当系统中有多个函数的时候,可以使用 `--parallel=N` 来调用多核并行处理 build 或 push 任务。命令也支持这些选项: `--no-cache`  `--squash`
### Deploy and test your function
### 部署及测试 function
Now you can deploy your function, see it listed and invoke it. Each time you invoke the function we collect metrics which are made available through Prometheus.
现在,可以部署、列出、调用函数了。每次调用函数时,可以通过 Prometheus 收集 metric 值。
```
$ export gw=http://$(minikube ip):31112
@ -165,13 +187,13 @@ Deployed.
URL: http://192.168.99.100:31112/function/hello
```
You are given a standard route for invoking the function in the deployment message, but can also use the CLI to save on typing:
上面给到的是部署时调用函数的标准方法,你也可以使用下面的命令:
```
$ echo test | faas-cli invoke hello --gateway $gw
```
Now list the functions deployed and you will see the invocation count has gone up.
现在可以通过以下命令列出部署好的函数,你将看到调用计数器数值增加。
```
$ faas-cli list --gateway $gw
@ -182,32 +204,35 @@ Function Invocations Replicas
hello 1 1
```
_Note: this command also accepts a _ `_--verbose_` _ flag for more information._
_提示:这条命令也可以 also accepts a `_--verbose_` flag for more information._
Since we are running OpenFaaS on a remote cluster (a Linux VM) we set up a `--gateway` override environmental variable. This could also be a remote host on a cloud platform. The alternative is to update the gateway value in your .yml file.
由于我们是在远端群集( Linux 虚拟机)上面运行 OpenFaaS命令里面加上一条 `--gateway` 用来来覆盖环境变量. 这个选项同样适用于云平台上的远程主机。除了加上这条选项以外,还可以通过编辑 .yml 文件里面的 gateway 值来达到同样的效果。
### Moving beyond minikube
### 迁移到 minikube 以外的环境
Once you are familiar with OpenFaaS on minikube you can deploy to any Kubernetes cluster running on Linux hosts. Heres an example of OpenFaaS running on Kubernetes on Googles GKE platform by Stefan Prodan of WeaveWorks demonstrating the built-in auto-scaling capabilities:
一旦你在熟悉了在 minikube 上运行 OpenFaaS ,就可以在任意 Linux 主机上搭建 Kubernetes 群集来部署 OpenFaaS 了。下图是由来自 WeaveWorks 的 Stefan Prodan 做的 OpenFaaS Demo ,这个 Demo 部署在 Google GKE 平台上的 Kubernetes 上面。图片上展示的是 OpenFaaS 内置的自动扩容的功能:
>译者注下面图片来自twitter可能访问不了
![](https://twitter.com/stefanprodan/status/931490255684939777/photo/1)
### Keep learning
### 继续学习
We have dozens of guides and blog available to get you up and running on the “FaaS Train”head over to GitHub and bookmark our guides:
我们的 Github上面有很多手册和博文可以带你轻松“上车”把我们的页面保存成书签吧
[openfaas/faas
faas - OpenFaaS - Serverless Functions Made Simple for Docker & Kubernetesgithub.com][9][][10]
Ive included a short 15min video from Dockercon 2017 in Copenhagen where I gave an overview of Serverless and OpenFaaS at the Moby Summit.
2017 哥本哈根 Dockercon Moby 峰会上,我做了关于 Serverless 和 OpenFaaS 的概述演讲,这里我把视频放上来,视频不长,大概 15 分钟左右(译者注视频在youtube,需要科学访问)。
[Youtube视频](https://youtu.be/UaReIKa2of8)
Make sure to follow [OpenFaaS on Twitter][11] for all the latest Cool Hacks, demos and news.
最后,别忘了关注 [OpenFaaS on Twitter][11] 这里有最潮的新闻、最酷的技术和 Demo 展示。
--------------------------------------------------------------------------------
via: https://medium.com/@alexellisuk/getting-started-with-openfaas-on-minikube-634502c7acdf
作者:[Alex Ellis ][a]
译者:[译者ID](https://github.com/译者ID)
译者:[mandeler](https://github.com/mandeler)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出