Knative Serving — Serverless 平台
什么是 Knative
Knative 在 Kubernetes 上构建 Serverless 平台,核心特性:
- 自动伸缩:根据请求量自动扩缩容,支持缩容到 0
- 流量分割:支持蓝绿部署、金丝雀发布
- 事件驱动:Knative Eventing 支持事件源和事件处理
安装
bash
# 安装 Knative Serving
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.13.0/serving-crds.yaml
kubectl apply -f https://github.com/knative/serving/releases/download/knative-v1.13.0/serving-core.yaml
# 安装网络层(Kourier)
kubectl apply -f https://github.com/knative/net-kourier/releases/download/knative-v1.13.0/kourier.yaml
kubectl patch configmap/config-network \
--namespace knative-serving \
--type merge \
--patch '{"data":{"ingress-class":"kourier.ingress.networking.knative.dev"}}'Knative Service
yaml
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: my-function
namespace: production
spec:
template:
metadata:
annotations:
# 自动伸缩配置
autoscaling.knative.dev/class: kpa.autoscaling.knative.dev
autoscaling.knative.dev/metric: concurrency
autoscaling.knative.dev/target: "100" # 每个 Pod 处理 100 并发
autoscaling.knative.dev/minScale: "0" # 缩容到 0
autoscaling.knative.dev/maxScale: "50" # 最多 50 个 Pod
autoscaling.knative.dev/scale-to-zero-pod-retention-period: "1m"
spec:
containerConcurrency: 100
timeoutSeconds: 300
containers:
- image: my-function:v1.0
ports:
- containerPort: 8080
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "1"
memory: "512Mi"
env:
- name: TARGET
value: "World"
# 流量分割
traffic:
- latestRevision: true
percent: 80
- revisionName: my-function-v1
percent: 20
tag: stable常用操作
bash
# 查看 Knative Service
kubectl get ksvc -n production
# 查看 Revision
kubectl get revision -n production
# 查看自动伸缩状态
kubectl get pa -n production # PodAutoscaler
# 发送请求
curl -H "Host: my-function.production.example.com" http://<ingress-ip>/