将 Kubernetes Deployment 转换为 Knative Service¶
本主题展示了如何将 Kubernetes Deployment 转换为 Knative Service。
优势¶
转换为 Knative Service 具有以下优势:
- 减少服务实例的占用空间,因为当实例空闲时,它会缩放至 0。
- 由于 Knative Service 内置的自动扩缩功能,性能得到提升。
确定您的工作负载是否适合 Knative¶
通常,如果您的 Kubernetes 工作负载适合 Knative,您可以删除大部分清单来创建 Knative Service。
您需要考虑三个方面:
- 所有完成的工作都由 HTTP 触发。
- 容器是无状态的。所有状态都存储在其他地方或可以重新创建。
- 您的工作负载仅使用 Secret 和 ConfigMap 卷。
转换示例¶
以下示例展示了 Kubernetes Nginx Deployment 和 Service,并展示了如何将其转换为 Knative Service。
Kubernetes Nginx Deployment 和 Service¶
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: my-nginx
labels:
run: my-nginx
spec:
ports:
- port: 80
protocol: TCP
selector:
run: my-nginx
Knative Service¶
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: my-nginx
spec:
template:
spec:
containers:
- image: nginx
ports:
- containerPort: 80