一个 Knative 应用主要包括四类对象:Service(不同于Kubernetes的Service)、Route(类似但不是OpenShift的Route)、Configuration、Revision。
无服务器应用的特点就是当没有外部请求的时候服务器是不需要运行Pod,只有在有请求的时候在启动Pod运行应用处理请求。在Knative中,这种按需的运行应用的效果是通过Activator和Autoscaler实现的。我们可以在OpenShift中的knative-serving项目中看到相关的支撑环境。
一个Knative Serving应用包括Service、Route、Configuration、Revision对象。他们的关系是Service=Route+Configuration,因此可以通过分别操作Route和Configuration或操作Service实现对Knative Serving应用的配置。
$ oc new-project kn-blue-green
apiVersion: serving.knative.dev/v1
kind: Configuration
metadata:
name: blue-green-demo
spec:
template:
spec:
containers:
- image: gcr.io/knative-samples/knative-route-demo:blue
env:
- name: T_VERSION
value: "blue"
$ oc apply -f blue-config.yaml
configuration "blue-green-demo" configured
$ oc get all
NAME READY STATUS RESTARTS AGE
pod/blue-green-demo-jvtg7-deployment-8cb94b4d7-qhvg7 2/2 Running 0 44s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/blue-green-demo-jvtg7-deployment 1/1 1 1 44s
NAME DESIRED CURRENT READY AGE
replicaset.apps/blue-green-demo-jvtg7-deployment-8cb94b4d7 1 1 1 44s
NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON
revision.serving.knative.dev/blue-green-demo-jvtg7 blue-green-demo 1 Unknown Deploying
NAME LATESTCREATED LATESTREADY READY REASON
configuration.serving.knative.dev/blue-green-demo blue-green-demo-jvtg7 Unknown
apiVersion: serving.knative.dev/v1
kind: Route
metadata:
name: blue-green-demo # The name of our route; appears in the URL to access the app
spec:
traffic:
- revisionName: blue-green-demo-XXX # replace XXX with your RevisionName
percent: 100 # All traffic goes to this revision
$ oc apply -f blue-route.yaml
route "blue-green-demo" configured
$ curl $(kn route list blue-green-demo | grep blue-green-demo | awk '{print $2}')
<!DOCTYPE html>
<html lang="en">
<head>
<title>Knative Routing Demo</title>
<link rel="stylesheet" type="text/css" href="/css/app.css" />
</head>
<body>
<div class="blue">App v1</div>
</div>
</body>
apiVersion: serving.knative.dev/v1
kind: Configuration
metadata:
name: blue-green-demo # Configuration name is unchanged, since we're updating an existing Configuration
spec:
template:
spec:
containers:
- image: gcr.io/knative-samples/knative-route-demo:green # URL to the new version of the sample app docker image
env:
- name: T_VERSION
value: "green" # Updated value for the T_VERSION environment variable
$ oc apply -f green-config.yaml
configuration.serving.knative.dev/blue-green-demo configured
$ oc get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/blue-green-demo ExternalName <none> cluster-local-gateway.knative-serving-ingress.svc.cluster.local <none> 43m
service/blue-green-demo-d44kj ClusterIP 172.30.202.156 <none> 80/TCP 32m
service/blue-green-demo-d44kj-private ClusterIP 172.30.114.50 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 32m
service/blue-green-demo-jvtg7 ClusterIP 172.30.254.229 <none> 80/TCP 50m
service/blue-green-demo-jvtg7-private ClusterIP 172.30.214.59 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 50m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/blue-green-demo-d44kj-deployment 0/0 0 0 32m
deployment.apps/blue-green-demo-jvtg7-deployment 0/0 0 0 50m
NAME DESIRED CURRENT READY AGE
replicaset.apps/blue-green-demo-d44kj-deployment-b8fb879f6 0 0 0 32m
replicaset.apps/blue-green-demo-jvtg7-deployment-5bf694ccf9 0 0 0 50m
NAME URL READY REASON
route.serving.knative.dev/blue-green-demo http://blue-green-demo.knative-demo.apps.cluster-shanghai-c30f.shanghai-c30f.example.opentlc.com Unknown IngressNotConfigured
NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON
revision.serving.knative.dev/blue-green-demo-d44kj blue-green-demo blue-green-demo-d44kj 2 True
revision.serving.knative.dev/blue-green-demo-jvtg7 blue-green-demo blue-green-demo-jvtg7 1 True
NAME LATESTCREATED LATESTREADY READY REASON
configuration.serving.knative.dev/blue-green-demo blue-green-demo-d44kj blue-green-demo-d44kj True
apiVersion: serving.knative.dev/v1
kind: Route
metadata:
name: blue-green-demo # Route name is unchanged, since we're updating an existing Route
spec:
traffic:
- revisionName: blue-green-demo-XXX
percent: 0
- revisionName: blue-green-demo-YYY
percent: 100
tag: v2 # A named route
$ oc apply -f blue-green-route.yaml
configuration.serving.knative.dev/blue-green-demo configured
$ curl $(kn route list blue-green-demo | grep blue-green-demo | awk '{print $2}')
<!DOCTYPE html>
<html lang="en">
<head>
<title>Knative Routing Demo</title>
<link rel="stylesheet" type="text/css" href="/css/app.css" />
</head>
<body>
<div class="green">App v2</div>
</div>
</body>
apiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: greeter
spec:
template:
metadata:
name: greeter-v1
spec:
containers:
- image: quay.io/rhdevelopers/knative-tutorial-greeter:quarkus
$ oc apply -f greeter-service.yaml
service.serving.knative.dev/greeter created
$ kn route list greeter
NAME URL READY
greeter http://greeter.knative-demo.apps.cluster-shanghai-c30f.shanghai-c30f.example.opentlc.com True
$ curl $(kn route list greeter | grep greeter | awk '{print $2}')
Hi greeter => '9861675f8845' : 1