基于k8s的私有微服务方案落地

微服务

  • 微服务架构,将应用程序构建为独立的组件,并将每个应用程序进程作为一项服务运行
  • 这些服务使用轻量级 API 通过明确定义的接口进行通信
  • 由于它们是独立运行的,因此可以针对各项服务进行更新、部署和扩展,以满足对应用程序特定功能的需求

背景

  • 传统的物理部署,运维起来相对繁琐,且环境难以维护
  • 单容器部署,难以保证并发性能
  • 容器集群化+自动扩容,可以解决上面两个问题,但是最少会有一个容器运行,对资源会有一定浪费
  • 容器集群化+自动扩容+微服务,解决了资源成本问题,在我们需要时按需启动容器

方案fission落地serverless

  • 项目地址

快速

  • 100msec冷启动
  • Fission 维护着一个“暖”容器池,每个容器都包含一个小的动态加载器。当一个函数第一次被调用时,即“冷启动”,一个正在运行的容器被选择并加载该函数。这个池是使 Fission 快速的原因:冷启动延迟通常约为 100 毫秒
  • 函数即服务

基于Kubernetes

  • 基本所有的运行都以k8s运行

多语言

  • Fission 目前支持 NodeJS、Python、Ruby、Go、PHP、Bash 和任何 Linux 可执行文件

安装serverless集群服务

请确保你已经部署了k8s集群,集群的部署请参考

安装

  • 拉取部署文件
# 下载1.13.1版本的yaml
[ ~ ] wget https://github.com/fission/fission/releases/download/1.13.1/fission-all-1.13.1.yaml 
  • 修改pvc
apiVersion: v1
kind: PersistentVolume
metadata:
  name: fission-storage-pvc
  labels:
    type: local
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  # 存储根据实际情况修改
  hostPath:
    path: "/home/g5/lijiacai/fission/data"

---
# Source: fission-all/templates/pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: fission-storage-pvc
  labels:
    app: fission-storage
    chart: "fission-all-1.13.1"
    release: "fission-1-13-1"
spec:
  storageClassName: manual
  volumeName: fission-storage-pvc
  accessModes:
    - "ReadWriteOnce"
  resources:
    requests:
      storage: "8Gi"
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: fission-alertmanager
  labels:
    type: local
spec:
  capacity:
    storage: 3Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  # 存储根据实际情况修改
  hostPath:
    path: "/home/g5/lijiacai/fission/alertmanager"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    component: "alertmanager"
    app: prometheus
    release: fission-1-13-1
    chart: prometheus-13.2.1
    heritage: Helm
  name: fission-1-13-1-prometheus-alertmanager
  namespace: fission
spec:
  storageClassName: manual
  volumeName: fission-alertmanager
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: "2Gi"
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: fission-prometheus-server
  labels:
    type: local
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  storageClassName: manual
  # 存储根据实际情况修改
  hostPath:
    path: "/home/g5/lijiacai/fission/prometheusserver"
---
# Source: fission-all/charts/prometheus/templates/server/pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  labels:
    component: "server"
    app: prometheus
    release: fission-1-13-1
    chart: prometheus-13.2.1
    heritage: Helm
  name: fission-1-13-1-prometheus-server
  namespace: fission
spec:
  storageClassName: manual
  volumeName: fission-prometheus-server
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: "8Gi"

  • 部署
[ ~ ]# kubectl apply -f .

fission客户端安装

[ ~ ]# curl -Lo fission https://github.com/fission/fission/releases/download/1.13.1/fission-1.13.1-darwin-amd64
[ ~ ]# chmod +x fission && sudo mv fission /usr/local/bin/
[ ~ ]# fisson version
client:
  fission/core:
    BuildDate: "2021-06-21T10:38:48Z"
    GitCommit: d06ad04341377bec6b8c905a440956b7d659d8fb
    Version: 1.13.1
server:
  fission/core:
    BuildDate: "2021-06-21T09:13:11Z"
    GitCommit: 401cbd597b63f5646e3e4be7df38870fdd617485
    Version: 1.13.1

部署效果

基于k8s的私有微服务方案落地_第1张图片

初体验微服务(文档)

ENVIRONMENT IMAGE
NodeJS (Alpine) fission/node-env
NodeJS (Debian) fission/node-env-debian
Python 3 fission/python-env
Python 2.7 fission/python-env-27
Go fission/go-env
Ruby fission/ruby-env
Binary (for executables or scripts) fission/binary-env
.NET fission/dotnet-env
.NET 2.0 fission/dotnet20-env
Perl fission/perl-env
PHP 7 fission/php-env

hello体验(单文件)

  • 创建一个运行环境
# 新建一个环境,镜像使用官方内置的python镜像
[ ~ ]# fission environment create --name python --image fission/python-env 
# fission/python-env 可以根据需求自定义镜像
  • 编写运行函数hello.py
def main():
    return "Hello, world!"
  • 创建一个函数
# --env python为上一步骤中创建的环境
[ ~ ]# fission function create --name hello --env python --code hello.py 
  • 函数测试
[ ~ ]# fission function test --name hello
Hello, world!
  • 暴露http接口
[ ~ ]# fission route create --url /hello --function hello --method GET
[ ~ ]# kubectl get svc router -n fission
NAME     TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
router   LoadBalancer   10.43.50.10                 80:31188/TCP   43h
[ ~ ]# curl http://localhost:31188/hello
hello, world!

hello world体验(多文件)

  • 创建一个运行环境
# --builder 允许以包的形式创建函数,并支持编译,安装依赖
[ ~ ]# fission env create --name pythonbuilder --image fission/python-env --builder fission/python-builder --mincpu 40 --maxcpu 80 --minmemory 64 --maxmemory 128 --poolsize 4 
# --mincpu 40 --maxcpu 80 --minmemory 64 --maxmemory 128 --poolsize 4  扩容参数
  • 编写运行函数pkg/world.py
def world():
    return "world"
  • 编写运行函数pkg/hello.py
from world import world
def main():
    return "Hello " + world()
  • 打包代码
[ ~ ]# zip -jr demo-src-pkg.zip pkg/
  • 代码包上传至fission库
[ ~ ]# fission package create --sourcearchive demo-src-pkg.zip --env pythonbuilder
Package 'demo-src-pkg-zip-hnwm' created
[ ~ ]# fission package info --name demo-src-pkg-zip-hnwm
Name:        demo-src-pkg-zip-hnwm
Environment: pythonbuilder
Status:      succeeded
Build Logs:
+ '[' -f /packages/demo-src-pkg-zip-hnwm-fxg0n4/requirements.txt ]
+ cp -r /packages/demo-src-pkg-zip-hnwm-fxg0n4 /packages/demo-src-pkg-zip-hnwm-fxg0n4-6j7zf7
  • 创建函数
[ ~ ]# fission fn create --name theyourszero --pkg demo-src-pkg-zip-hnwm --entrypoint "hello.main" --executortype poolmgr
# --executortype poolmgr 有容器缓冲池,访问延迟较低
# --executortype newdeploy 延迟较高,冷启动问题比较明显,但是资源成本控制了
  • 创建http
[ ~ ]# fission route create --url /theyourszero --function theyourszero --method GET
  • 压测
    • 压测前
      在这里插入图片描述
    • 压测
[ ~ ]# hey -c 250 -n 10000 http://127.0.0.1:31188/theyourszero
Summary:
  Total:        7.8584 secs
  Slowest:      4.1861 secs
  Fastest:      0.0678 secs
  Average:      0.1953 secs
  Requests/sec: 1272.5231
  
  Total data:   40000 bytes
  Size/request: 4 bytes

Response time histogram:
  0.068 [1]     |
  0.480 [9749]  |■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
  0.891 [0]     |
  1.303 [0]     |
  1.715 [0]     |
  2.127 [0]     |
  2.539 [0]     |
  2.951 [0]     |
  3.362 [0]     |
  3.774 [0]     |
  4.186 [250]   |■


Latency distribution:
  10% in 0.0882 secs
  25% in 0.0898 secs
  50% in 0.0939 secs
  75% in 0.0984 secs
  90% in 0.1063 secs
  95% in 0.1078 secs
  99% in 4.1340 secs

Details (average, fastest, slowest):
  DNS+dialup:   0.0012 secs, 0.0678 secs, 4.1861 secs
  DNS-lookup:   0.0000 secs, 0.0000 secs, 0.0000 secs
  req write:    0.0000 secs, 0.0000 secs, 0.0076 secs
  resp wait:    0.1939 secs, 0.0677 secs, 4.1264 secs
  resp read:    0.0000 secs, 0.0000 secs, 0.0052 secs

Status code distribution:
  [200] 10000 responses
- 压测后

在这里插入图片描述

fission-function展示

基于k8s的私有微服务方案落地_第2张图片

原理剖析请参考

你可能感兴趣的:(k8s,k8s,serverless,fission)