By Yuanyi
Knative is an open-source Serverless framework based on Kubernetes, which aims to make services easier to be deployed and used. It also provides other rich functions, such as automatic scaling, traffic routing, Canary release, and event-driven. This article describes the application deployment and service access in Kubernetes and then comparing it in Knative. By comparing Kubernetes with Knative, we can see how Knative eases service deployment and access.
It’s known that a domain name access portal is generally required to provide online external services. In addition, service discovery, load balancing, and workloads are also necessary. The requirements for Kubernetes are introduced as follows.
In Kubernetes, three resources must be created for external access:
Take the simplest deployment of hello-world as an example, Deployment of workloads need to be first created:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world-deployment
labels:
app: hello-world
spec:
selector:
matchLabels:
app: hello-world
replicas: 2
template:
metadata:
labels:
app: hello-world
spec:
containers:
- name: hello-world
image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56
ports:
- name: http
containerPort: 8080
env:
- name: TARGET
value: "Go Sample v1"
Save the preceding code as hello_deployment.yaml, and then run kubectl apply:
$ kubectl apply -f hello_deployment.yaml
It can be seen that two Pods are in running state:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-world-deployment-86923fxfs-4wfw 1/1 Running 0 30s
hello-world-deployment-86923fxfs-xsr 1/1 Running 0 30s
Next, create service access Service:
apiVersion: v1
kind: Service
metadata:
name: hello-world
spec:
selector:
app: hello-world
type: ClusterIP
ports:
- port: 80
targetPort: 8080
name: http
Run kubectl to create Service as follows:
$ kubectl apply -f hello_k8s_svc.yaml
Finally, Ingress is required to provide services through a gateway. Generally, nginx ingress is used. The deployment of nginx ingress controller is not described in detail here. For more information, see the deployment method in the community: https://github.com/kubernetes/ingress-nginx
After nginx ingress controller is deployed, Ingress is created. The specific yaml content is as follows:
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: hello-world
spec:
rules:
- host: helloworld-go.knative.top
http:
paths:
- path: /
backend:
serviceName: hello-world
servicePort: 80
Run kubectl, and then set domain name resolution (nginx ingress controller slb) for helloworld-go.knative.top. Directly access the domain name:
$ curl helloworld-go.knative.top
Hello Go Sample v1!
To provide domain name access in Knative, it’s only required to create Knative Service. In other words, Knative Service = Ingress + Kubernetes Service + Deployment. In addition, Knative provides a variety of gateway plug-ins, such as Istio and Kourier.
A simple Knative Service yaml:
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: helloworld-go
namespace: default
spec:
template:
spec:
containers:
- image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56
env:
- name: TARGET
value: "Go Sample v1"
They seem to be similar to that of Deployment. Actually, spec in this case is pod spec. For those familiar with Kubernetes workloads, it’s very easy to use Knative Service.
Run kubectl to create the resource:
$ kubectl apply -f ksvc.yaml
It can be seen that Pod has been created:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
helloworld-go-cdqm7-deployment-75b4dc8c9-xxzcw 2/2 Running 0 11s
How to access the domain name service then? It's very simple. Just check Knative Sevice:
$ kubectl get ksvc
NAME URL LATESTCREATED LATESTREADY READY REASON
helloworld-go http://helloworld-go.default.knative.top helloworld-go-5c52g helloworld-go-5c52g True
Set domain name resolution to the corresponding gateway slb and directly access the domain name via curl. If Istio is used, the corresponding foreign IP of istio-ingressgateway can be directly obtained:
$ curl helloworld-go.default.knative.top
Hello Go Sample v1!
The above comparison shows that Knative can more easily deploy and provide services. Knative encapsulates the original Kubernetes resources and abstracts the application model Knative Service. By doing so, users’ resource operation interface is converged and the threshold for service deployment and access is reduced, while enabling functions. Knative Service also provides multi-version management and traffic-based gray release capabilities, which will be further introduced in later articles.
Understanding Kubernetes from the Perspective of Application Development
167 posts | 30 followers
FollowAlibaba Container Service - July 19, 2021
Alibaba Cloud Native Community - April 9, 2024
Alibaba Container Service - September 14, 2022
Alibaba Cloud Native - November 3, 2022
Alibaba Cloud Native Community - November 15, 2023
Alibaba Container Service - July 22, 2021
167 posts | 30 followers
FollowVisualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios
Learn MoreServerless Application Engine (SAE) is the world's first application-oriented serverless PaaS, providing a cost-effective and highly efficient one-stop application hosting solution.
Learn MoreProvides a control plane to allow users to manage Kubernetes clusters that run based on different infrastructure resources
Learn MoreRespond to sudden traffic spikes and minimize response time with Server Load Balancer
Learn MoreMore Posts by Alibaba Container Service