Converting a Kubernetes Deployment to a Knative Service

This topic shows how to convert a Kubernetes Deployment to a Knative Service.

Benefits

Converting to a Knative Service has the following benefits:

  • Reduces the footprint of the service instance because the instance scales to 0 when it becomes idle.
  • Improves performance due to built-in autoscaling for the Knative Service.

Determine if your workload is a good fit for Knative

In general, if your Kubernetes workload is a good fit for Knative, you can remove a lot of your manifest to create a Knative Service.

There are three aspects you need to consider:

  • All work done is triggered by HTTP.
  • The container is stateless. All state is stored elsewhere or can be re-created.
  • Your workload uses only Secret and ConfigMap volumes.

Example conversion

The following example shows a Kubernetes Nginx Deployment and Service, and shows how it converts to a Knative Service.

Kubernetes Nginx Deployment and Service

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: my-nginx
  5. spec:
  6. selector:
  7. matchLabels:
  8. run: my-nginx
  9. replicas: 2
  10. template:
  11. metadata:
  12. labels:
  13. run: my-nginx
  14. spec:
  15. containers:
  16. - name: my-nginx
  17. image: nginx
  18. ports:
  19. - containerPort: 80
  20. ---
  21. apiVersion: v1
  22. kind: Service
  23. metadata:
  24. name: my-nginx
  25. labels:
  26. run: my-nginx
  27. spec:
  28. ports:
  29. - port: 80
  30. protocol: TCP
  31. selector:
  32. run: my-nginx

Knative Service

  1. apiVersion: serving.knative.dev/v1
  2. kind: Service
  3. metadata:
  4. name: my-nginx
  5. spec:
  6. template:
  7. spec:
  8. containers:
  9. - image: nginx
  10. ports:
  11. - containerPort: 80