Knative工作模式
service
route RevisionX
configuration
IngressGateway(Envoy) 收发流量
Revision**
k8s-Service Avtivator(0) SKS Deploument Pod(Envoy Queue container) 拉取指标到 Autoscaler HPA
Route** Ingress(Istio) VirtualService Service-to-service Pod(envoy Queue container) Knative Serving会为每个Pod注入一个称为Queue Proxy的容器 ◼ 为业务代码提供代理功能 ◆Ingress GW接收到请求后,将其发往目标Pod实例上由Queue Proxy监听的8012端口 ◆而后,Queue Proxy再将请求转发给业务代码容器监听的端口 ◼ 报告实例上的多个有关客户端请求的关键指标为给Autoscaler ◼ Pod健康状态检测(Kubernetes探针) ◼ 为超出实例最大并发量的请求提供缓冲队列 为超出实例最大并发量的请求提供缓冲队列 Queue Proxy预留使用了如下几个端口 ◆ 8012:HTTP协议的代理端口 ◆ 8013:HTTP/2端口,用于代理gRPC ◆ 8022:管理端口,如健康状态检测等 ◆ 9090:暴露给Autoscaler进行指标采集的端口 ◆ 9091:暴露给Prometheus进行监控指标采集的端口运行Knative应用
在Serving上,可通过创建Knative Service对象来运行应用;该Service资源会自动创建 ◼ 一个Configuration对象,它会创建一个Revision,并由该Revision自动创建如下两个对象 ◆一个Deployment对象 ◆一个PodAutoscaler对象 ◼ 一个Route对象,它会创建 ◆一个Kubernetes Service对象 ◆一组Istio VirtualService对象 ⚫ {kservice-name}-ingress ⚫ {kservice-name}-mesh Knative Service资源(简称kservice或ksvc)的资源规范主要有两个字段 ◼ template:用于创建或更新Configuration,任何更新,都将创建新的Revision对象 ◼ traffic:用于创建或更新Route对象root@master01:/opt/knative-in-practise# kubectl get svc -nistio-system NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE istio-egressgateway ClusterIP 10.100.141.15 <none> 80/TCP,443/TCP 12d istio-ingressgateway LoadBalancer 10.100.43.136 192.168.80.251 15021:36049/TCP,80:29931/TCP,443:25555/TCP 12d #南北向流量 istiod ClusterIP 10.100.35.2 <none> 15010/TCP,15012/TCP,443/TCP,15014/TCP 12d knative-local-gateway ClusterIP 10.100.5.126 <none> 80/TCP 45h #东西向流量
root@master01:/opt/knative-in-practise/serving/basic# cat hello-world.yaml apiVersion: serving.knative.dev/v1 kind: Service metadata: name: hello spec: template: metadata: # This is the name of our new "Revision," it must follow the convention {service-name}-{revision-name} name: hello-world spec: containers: #- image: gcr.io/knative-samples/helloworld-go - image: ikubernetes/helloworld-go:latest ports: - containerPort: 8080 env: - name: TARGET value: "World"
链接:https://pan.baidu.com/s/1XrIcxCkDwy0hggoH1wqrggView Code
root@master01:~# kubectl get configurations,revisions,deployments,podautoscalers NAME LATESTCREATED LATESTREADY READY REASON configuration.serving.knative.dev/hello hello-world hello-world True NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON ACTUAL REPLICAS DESIRED REPLICAS revision.serving.knative.dev/hello-world hello 1 True 1 1 NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/hello-world-deployment 1/1 1 1 10m NAME DESIREDSCALE ACTUALSCALE READY REASON podautoscaler.autoscaling.internal.knative.dev/hello-world 1 1 True
root@master01:~# kubectl get routes,services,virtualservice NAME URL READY REASON route.serving.knative.dev/hello http://hello.default.example.com True NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/demoappv10 ClusterIP 10.100.20.78 <none> 8080/TCP 13d service/hello ExternalName <none> knative-local-gateway.istio-system.svc.cluster.local 80/TCP 7m34s service/hello-world ClusterIP 10.100.87.121 <none> 80/TCP 8m43s service/hello-world-private ClusterIP 10.100.186.63 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP 8m43s service/kubernetes ClusterIP 10.100.0.1 <none> 443/TCP 13d service/proxy ClusterIP 10.100.225.55 <none> 80/TCP 13d NAME GATEWAYS HOSTS AGE virtualservice.networking.istio.io/hello-ingress ["knative-serving/knative-ingress-gateway","knative-serving/knative-local-gateway"] ["hello.default","hello.default.example.com","hello.default.svc","hello.default.svc.cluster.local"] 7m34s virtualservice.networking.istio.io/hello-mesh ["mesh"] ["hello.default","hello.default.svc","hello.default.svc.cluster.local"]
kubectl edit deployment.apps/hello-world-deployment image: gcr.io/knative-releases/knative.dev/serving/cmd/queue:latest
root@master01:~# curl -H "Host: hello.default.example.com" 192.168.80.251 Hello World!
root@master01:~# kn service list NAME URL LATEST AGE CONDITIONS READY REASON hello http://hello.default.example.com hello-world 80s 3 OK / 3 True
root@master01:~# kn service describe hello Name: hello Namespace: default Age: 2m URL: http://hello.default.example.com Revisions: 100% @latest (hello-world) [1] (2m) Image: ikubernetes/helloworld-go:latest (at 5ea96b) Replicas: 1/1 Conditions: OK TYPE AGE REASON ++ Ready 1m ++ ConfigurationsReady 1m ++ RoutesReady 1m
root@master01:~# kn revision list NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON hello-world hello 100% 1 3m30s 4 OK / 4 True root@master01:~# kn revision describe hello-world Name: hello-world Namespace: default Age: 3m Image: ikubernetes/helloworld-go:latest (at 5ea96b) Replicas: 1/1 Port: 8080 Env: TARGET=World Service: hello Conditions: OK TYPE AGE REASON ++ Ready 3m ++ ContainerHealthy 3m ++ ResourcesAvailable 3m ++ Active 3m
root@master01:~# kn route list NAME URL READY hello http://hello.default.example.com True root@master01:~# kn route describe hello Name: hello Namespace: default Age: 5m URL: http://hello.default.example.com Service: hello Traffic Targets: 100% @latest (hello-world) Conditions: OK TYPE AGE REASON ++ Ready 4m ++ AllTrafficAssigned 4m ++ CertificateProvisioned 4m TLSNotEnabled ++ IngressReady 4m
root@master01:~# kn --help
root@master01:~# kn route list NAME URL READY hello http://hello.default.example.com True root@master01:~# kn domain create --help root@master01:~# kn domain create hello.chuan.com --ref ksvc:hello #创建域名映射 Domain mapping 'hello.chuan.com' created in namespace 'default'.
root@master01:~# kubectl get domainmapping NAME URL READY REASON hello.chuan.com http://hello.magedu.com False DomainAlreadyClaimed root@master01:~# kn domain describe hello.magedu.com Name: hello.chuan.com Namespace: default Age: 4m URL: http://hello.magedu.com Reference: APIVersion: serving.knative.dev/v1 Kind: Service Name: hello Conditions: OK TYPE AGE REASON !! Ready 4m DomainAlreadyClaimed ?? CertificateProvisioned 4m !! DomainClaimed 4m DomainAlreadyClaimed ?? IngressReady 4m IngressNotConfigured ?? ReferenceResolved 4m
root@master01:/opt/knative-in-practise/serving/domainmapping# cat cdc-hello.magedu.com.yaml #域名映射需调整比列 apiVersion: networking.internal.knative.dev/v1alpha1 kind: ClusterDomainClaim metadata: name: hello.magedu.com spec: namespace: default
root@master01:/opt/knative-in-practise/serving/domainmapping# kubectl apply -f cdc-hello.magedu.com.yaml
root@master01:/opt/knative-in-practise/serving/domainmapping# kubectl get cdc NAME AGE hello.magedu.com 16s
root@master01:/opt/knative-in-practise/serving/domainmapping# kubectl get vs NAME GATEWAYS HOSTS AGE hello-ingress ["knative-serving/knative-ingress-gateway","knative-serving/knative-local-gateway"] ["hello.default","hello.default.example.com","hello.default.svc","hello.default.svc.cluster.local"] 43m hello-mesh ["mesh"] ["hello.default","hello.default.svc","hello.default.svc.cluster.local"] 43m hello.magedu.com-ingress ["knative-serving/knative-ingress-gateway"] ["hello.magedu.com"]
域名映射自动
root@master01:~# kubectl get cm -nknative-serving kubectl edit cm config-network -nknative-serving # autocreate-cluster-domain-claims: "false" autocreate-cluster-domain-claims: "true" #缩进两格,自动创建域名映射机制
root@master01:~# kn domain create hi.chuan.com --ref kroute:hello Domain mapping 'hi.chuan.com' created in namespace 'default'.
root@master01:~# while true;do curl -H "Host: hi.chuan.com" 192.168.80.251;sleep 1;done Hello World!
queue
root@master01:/opt/knative-in-practise/serving/basic# kubectl apply -f hello-world-002.yaml service.serving.knative.dev/hello configured root@master01:/opt/knative-in-practise/serving/basic# cat hello-world-002.yaml apiVersion: serving.knative.dev/v1 kind: Service metadata: name: hello spec: template: metadata: # This is the name of our new "Revision," it must follow the convention {service-name}-{revision-name} name: hello-world-002 spec: containerConcurrency: 10 containers: #- image: gcr.io/knative-samples/helloworld-go - image: ikubernetes/helloworld-go:latest ports: - containerPort: 8080 env: - name: TARGET value: "World-002" root@client /# while true;do curl -H "Host: hello.magedu.com" 192.168.80.251;sleep 1;done Hello World-002! Hello World-002!
root@master01:/opt/knative-in-practise/serving/basic# kn service list NAME URL LATEST AGE CONDITIONS READY REASON hello http://hello.default.example.com hello-world-002 63m 3 OK / 3 True root@master01:/opt/knative-in-practise/serving/basic# kn route list NAME URL READY hello http://hello.default.example.com True root@master01:/opt/knative-in-practise/serving/basic# kubectl get svc hello ExternalName <none> knative-local-gateway.istio-system.svc.cluster.local 80/TCP 62m hello-world ClusterIP 10.100.38.120 <none> 80/TCP 63m hello-world-002 ClusterIP 10.100.9.130 <none> 80/TCP 5m15s hello-world-002-private ClusterIP 10.100.23.65 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP,8012/TCP 5m15s
调整比例
root@master01:/opt/knative-in-practise/serving/basic# cat hello-world.yaml apiVersion: serving.knative.dev/v1 kind: Service metadata: name: hello spec: template: metadata: # This is the name of our new "Revision," it must follow the convention {service-name}-{revision-name} name: hello-world spec: containers: #- image: gcr.io/knative-samples/helloworld-go - image: ikubernetes/helloworld-go:latest ports: - containerPort: 8080 env: - name: TARGET value: "World" root@master01:/opt/knative-in-practise/serving/basic# cat he.yaml apiVersion: serving.knative.dev/v1 kind: Service metadata: name: he spec: template: metadata: # This is the name of our new "Revision," it must follow the convention {service-name}-{revision-name} name: he-world spec: containers: #- image: gcr.io/knative-samples/helloworld-go - image: ikubernetes/helloworld-go:latest ports: - containerPort: 8080 env: - name: TARGET value: "chuan"
root@master01:/opt/knative-in-practise/serving/revision-and-route# cat 004-hello-CloudNative.yaml
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: fenge spec: template: spec: containers: - image: ikubernetes/helloworld-go:latest ports: - containerPort: 8080 env: - name: TARGET value: "Cloud-Native" traffic: - latestRevision: true percent: 0 tag: staging - revisionName: he-world #分别50% percent: 50 - revisionName: hello-world percent: 50
root@master01:/opt/knative-in-practise/serving/basic# kn route list NAME URL READY fenge http://fenge.default.example.com True he http://he.default.example.com True hello http://hello.default.example.com True root@master01:/opt/knative-in-practise/serving/basic# kn revision list NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON fenge-00001 fenge staging 1 3m9s 3 OK / 4 True he-world he 100% 1 11m 4 OK / 4 True hello-world hello 100% 1 5m10s 4 OK / 4 True
root@nt888 /# while true;do curl -H "Host: fenge.default.example.com" 192.168.80.251;sleep 1;done Hello chuan! Hello chuan! Hello World! Hello World! Hello chuan! Hello World! Hello chuan!
kubectl get vs staging-fenge.default.svc.cluster.local
root@nt888 /# curl http://staging-fenge.default.svc.cluster.local Hello Cloud-Native! root@nt888 /# curl http://staging-fenge.default.svc.cluster.local
增加标签
root@master01:~# kn service update fenge --tag fenge-00001=oldet Updating Service 'fenge' in namespace 'default': 0.033s The Route is still working to reflect the latest desired specification. 0.075s Ingress has not yet been reconciled. 0.138s Waiting for load balancer to be ready 0.337s Ready to serve. Service 'fenge' with latest revision 'fenge-00001' (unchanged) is available at URL: http://fenge.default.example.com
root@nt888 /# while true;do curl http://oldet-fenge.default.svc.cluster.local;sleep 1;done Hello Cloud-Native! Hello Cloud-Native! Hello Cloud-Native!
root@master01:~# kn revision list NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON fenge-00001 fenge staging,oldet 1 8m4s 4 OK / 4 True he-world he 100% 1 75m 4 OK / 4 True hello-world hello 100% 1 68m 4 OK / 4 True