By Kamesh Sampath

Translation: Crazy geek

Original text: serverless – architecture. IO/blog/your – f…

Reproduced without permission

In the first part of this article, we will discuss setting up a development environment suitable for Knative version 0.6.0. The second part describes the deployment of the first Serverless microservice. The basic requirement for creating serverless applications using Knative is a solid knowledge of Kubernetes. If you have no experience, you should follow the official Basic Kubernetes tutorial [1].

There are some tools and programs that must be installed before you can delve into them:

  • Minikube [2]
  • kubectl [3]
  • kubens [4]

For Windows users, WSL [5] has proven to be very useful, so I recommend having it installed as well.

Set the Minikube

Minikube is a single-node Kubernetes cluster that is ideal for daily development using Kubernetes. After setting up, you must perform the following steps to make the Minikube ready for deployment via Knative Serving. Here’s what the code looks like.

Minikube profile knative minikube start -p knative --memory=8192 --cpus=6 \ --kubernetes-version=v1.12.0 \ --disk-size=50g \ --extra-config=apiserver.enable-admission-plugins="LimitRanger,NamespaceExists,NamespaceLifecycle,ResourceQuota,ServiceAccount,DefaultStorageClass,MutatingAdmissionWebhoo k" 
Copy the code

First, you must create a Minikube configuration file, which is what the first line does. The second command is then used to set up a Minikube instance that contains 8 GB OF RAM, 6 cpus, and 50 GB of hard disk space. The boot command also contains some other configurations of the Kubernetes cluster that are necessary to start and run Knative. It is also important that the version of Kubernetes used be no earlier than 1.12.0 or Knative will not work. This is perfectly normal if Minikube is not started immediately; Since it may take a few minutes to complete the setup for the first time, please be patient.

Establish an Istio gateway

Knative requires an entry gateway to route requests to the Knative service. In addition to Istio[6], Gloo [7] is supported as an entry gateway. For our example, Istio will be used. The following steps show how to perform a lightweight installation of Istio with only the Ingress Gateway:

Curl https://raw.githubusercontent.com/knative/serving/release-0.6/third_party/istio-1.1.3/istio-lean.yaml - L \ | sed's/LoadBalancer/NodePort/'\ | kubectl apply - filename -Copy the code

Like Minikube, Istio Pod takes a few minutes to deploy. Using the command kubectl — namespace istio-system get Pods — watch, you can see the status; Ctrl + C ends. The successful deployment can be easily determined by using the command kubectl -namespace istio-system get Pods. If all goes well, the output should look something like the listing below.

NAME                                     READY   STATUS    RESTARTS   AGE
cluster-local-gateway-7989595989-9ng8l   1/1     Running   0          2m14s
istio-ingressgateway-6877d77579-fw97q    2/2     Running   0          2m14s
istio-pilot-5499866859-vtkb8             1/1     Running   0          2m14s
Copy the code

Install the Knative service

You can run serverless workloads on Kubernetes by installing Knative Serving [8]. It also provides automatic scaling and revision tracking. You can install it by running the following command:

kubectl apply --selector knative.dev/crd-install=true\ - filename https://github.com/knative/serving/releases/download/v0.6.0/serving.yaml kubectl apply - filename https://github.com/knative/serving/releases/download/v0.6.0/serving.yaml - the selector networking.knative.dev/certificate-provider! =cert-managerCopy the code

Again, deploying a Knative Pod can take a few minutes; You can check the status using kubectl — namespace knative — serving Get Pods — watch. Stop the check with Ctrl + C as before. Check whether all programs are running using kubectl — namespace knative-serving get Pods. If this is the case, the following output should be displayed.

NAME                               READY   STATUS    RESTARTS   AGE
activator-54f7c49d5f-trr82         1/1     Running   0          27m
autoscaler-5bcd65c848-2cpv8        1/1     Running   0          27m
controller-c795f6fb-r7bmz          1/1     Running   0          27m
networking-istio-888848b88-bkxqr   1/1     Running   0          27m
webhook-796c5dd94f-phkxw           1/1     Running   0          27m
Copy the code

Deploy the demo

The program to be created for demonstration is a simple greeting machine that prints “Hi”. You can use existing Linux container images, which can be found on the Quay website [9].

The first step is to create a traditional Kubernetes deployment, which can then be modified to use the Serverless functionality. This will help you figure out where the actual differences are and how to use Knativeless Server for existing deployments.

Create the Kubernetes resource file

The following steps show how to create a Kubernetes resource file. You must first create a new file called app.yaml into which the following code must be copied.

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: greeter
spec:
  selector:
    matchLabels:
      app: greeter
  template:
    metadata:
      labels:
        app: greeter
    spec:
      containers:
      - name: greeter
        image: quay.io/rhdevelopers/knative-tutorial-greeter:quarkus
        resources:
          limits:
            memory: "32Mi"
            cpu: "100m"
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
        readinessProbe:
          httpGet:
            path: /healthz
            port: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: greeter-svc
spec:
  selector:
    app: greeter
  type: NodePort
  ports:
  - port: 8080
    targetPort: 8080
Copy the code

Create deployments and services

You can create deployments and services by applying the YAML files you created earlier. This can be done with the kubectl apply — filename app.yaml command. Again, at this point, the command kubectl get Pods — Watch can be used to get information about the status of the application, and CTRL + C can terminate the whole process. If all goes well, we should now have a deployment called Greeter and a service called Greeter-SVc.

$ kubectl get deployments
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
greeter   1         1         1            1           16s

$ kubectl get svc
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
greeter-svc   NodePort    10.110.164.179           8080:31633/TCP   50s
Copy the code

To activate the service, you can also open the service URL in your browser using a Minikube shortcut (for example, Minikube Service Greeter-svc). If you prefer curl to open the same URL, you must use the command curl $(minikube service greeter-svc — URL). You should now see a text similar to the following: Hi greeter =>’ 9861675F8845 ‘:1

Migrate traditional Kubernetes deployments to Serverless via Knative

Start by simply copying the app.yaml file, naming it serverless-app-yaml and updating it to what is shown in the code below.

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: greeter
spec:
  template:
    metadata:
      labels:
        app: greeter
    spec:
      containers:
      - image: quay.io/rhdevelopers/knative-tutorial-greeter:quarkus
        resources:
          limits:
            memory: "32Mi"
            cpu: "100m"
        ports:
        - containerPort: 8080
        livenessProbe:
          httpGet:
            path: /healthz
        readinessProbe:
          httpGet:
            path: /healthz
Copy the code

If we compare the traditional Kubernetes application * (app.yaml) with the Serverless application (Serverless-app.yaml) *, we find three things: First, no other services are needed because Knative automatically creates and routes services. Second, since the definition of the service is done manually, a selector is no longer required, so the following lines are omitted:

 selector:
        matchLabels:
          app: greeter
Copy the code

Finally, in the TEMPLATE | SPEC | CONTAINERS name was omitted, because the name automatically generated by the Knative. There is also no need to define ports for probe activity and readiness.

Deploy the Serverless application

The deployment follows the same pattern as before, using the command kubectl apply — filename serverless-app.yaml. After the Serverless application is successfully deployed, the following objects should be created: Deployment should now be added (Listing 1). Some new services should also be available (Listing 2), including the ExternalName service, which points to istio-ingressgateway.istio-system.svc.cluster.local. You should also have a Knative service that provides a URL to which you can send requests (Listing 3).

Listing 1: $ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE greeter 1 1 1 1 30m greeter-bn8cm-deployment 1 1  1 1 59sCopy the code
Listing 2: $ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE greeter ExternalName Local 114s Greeter-BN8cm ClusterIP 10.110.208.72 80/TCP 2M21s istio-ingressgateway.istio-system.svc.cluster.local 114s Greeter-BN8cm ClusterIP 10.110.208.72 80/TCP 2M21s Greeter-bn8cm-metrics ClusterIP 10.100.237.125 9090/TCP 2m21s Greeter-BN8cm-priv ClusterIP 10.107.104.53 80/TCP 2m21sCopy the code
Listing 3 $kubectl get services. Serving. Knative. Dev NAME URL LATESTCREATED LATESTREADY READY "REASON greeter http://greeter.default.example.com greeter-bn8cm greeter-bn8cm True Attention In a Minikube deployment we will have neither LoadBalancer nor DNS to resolve anything to *.example.com or a service URL like http://greeter.default.example.com. To call a service, the host header must be used with http/curl.Copy the code

In order to be able to invoke the service, the request must be made through an entry or gateway (Istio in our case). To find out the address of the Istio gateway that we must use in the HTTP /curl call, use the following command:

IP_ADDRESS="$(minikube ip):$(kubectl get svc istio-ingressgateway --namespace istio-system --output 'jsonpath={.spec.ports[?(@.port==80)].nodePort}')"
Copy the code

This command receives the NodePort of the istio-ingressgateway service in the namespace istio-system. If we have a NodePort for istio-ingressGateway, we can call the greeter service with $IP_ADDRESS by passing a host header with an HTTP/curl call.

curl -H "Host:greeter.default.example.com" $IP_ADDRESS
Copy the code

You will now get the same result as a traditional Kubernetes deployment (Hi Greeter =>’ 9861675F8845 ‘:1). If the deployment is allowed to be in idle mode for about 90 seconds, the deployment terminates. On the next call, then reactivate the planned deployment and answer the request.

Congratulations, you have successfully deployed and invoked your first Serverless application!

See articles and links


Welcome to pay attention to the front end of the public number: front end pioneer, free to receive a full series of webpack front end tutorial.


  1. Kubernetes. IO/docs/tutori… ↩ ︎

  2. Kubernetes. IO/docs/tasks /… ↩ ︎

  3. Kubernetes. IO/docs/tasks /… ↩ ︎

  4. Github.com/ahmetb/kube… ↩ ︎

  5. Docs.microsoft.com/en-us/windo… ↩ ︎

  6. Istio. IO ↩ ︎

  7. Gloo. Solo. IO ↩ ︎

  8. Knative. Dev/docs/servin… ↩ ︎

  9. Quay. IO/rhdeveloper… ↩ ︎