×
Community Blog Simplifying Knative: Application Deployment and Access

Simplifying Knative: Application Deployment and Access

This article describes the application deployment and service access in Kubernetes and then comparing it in Knative.

By Yuanyi

Introduction

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.

Provide Domain Name Service Access in Kubernetes

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.

1

In Kubernetes, three resources must be created for external access:

  • Ingress: Domain Name Access
  • Service: Service Discovery/Load Balancing
  • Deployment/Statefulset: Workloads

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!

Provide Domain Name Service Access in Knative

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.

2

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!

Summary

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.

0 0 0
Share on

Alibaba Container Service

83 posts | 21 followers

You may also like

Comments