serving的工作模式
- Knative Serving会为每个Pod注入一个称为Queue Proxy的容器
- 为业务代码提供代理功能
- Ingress GW接收到请求后,将其发往目标Pod实例上由Queue Proxy监听的8012端口
- 而后,Queue Proxy再将请求转发给业务代码容器监听的端口
- 报告实例上的多个有关客户端请求的关键指标为给Autoscaler
- Pod健康状态检测(Kubernetes探针)
- 为超出实例最大并发量的请求提供缓冲队列
- 为业务代码提供代理功能
运行knative应用
yaml
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
ports:
- containerPort: 8080
env:
- name: TARGET
value: "World"
资源部署
kubectl apply -f hello-world.yaml
资源查看 ``` root@k8s-master1:~# kubectl get pod #处于Terminating状态 NAME READY STATUS RESTARTS AGE hello-world-deployment-5c446fc685-9sz5t 2/2 Terminating 0 4m49s
root@k8s-master1:~# kubectl get configuration NAME LATESTCREATED LATESTREADY READY REASON hello hello-world hello-world True
root@k8s-master1:~# kubectl get revision NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON ACTUAL REPLICAS DESIRED REPLICAS hello-world hello 1 True 0 0
root@k8s-master1:~# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE hello-world-deployment 0/0 0 0 6m31s
root@k8s-master1:~# kubectl get kpa NAME DESIREDSCALE ACTUALSCALE READY REASON hello-world 0 0 False NoTraffic
root@k8s-master1:~# kubectl get route NAME URL READY REASON hello http://hello.default.example.com True
root@k8s-master1:~# curl -H “HOST: hello.default.example.com” 10.168.56.110 #请求之后会激活pod Hello World! root@k8s-master1:~# kubectl get pod NAME READY STATUS RESTARTS AGE hello-world-deployment-5c446fc685-sgg45 2/2 Running 0 11s
root@k8s-master1:~/knative-in-practise-main/serving/basic# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello ExternalName
root@k8s-master1:~/knative-in-practise-main/serving/basic# 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”] 10m hello-mesh [“mesh”] [“hello.default”,”hello.default.svc”,”hello.default.svc.cluster.local”] 10m
root@k8s-master1:~# kubectl run client-$RANDOM —image=ikubernetes/admin-box:v1.2 —restart=Never -it —command — /bin/bash #集群内访问 If you don’t see a command prompt, try pressing enter. root@client-10092 /# curl http://hello.default.svc.cluster.local Hello World!
root@k8s-master1:~/knative-in-practise-main/serving/basic# kubectl describe svc hello-world
Name: hello-world
Namespace: default
Labels: app=hello-world
networking.internal.knative.dev/serverlessservice=hello-world
networking.internal.knative.dev/serviceType=Public
serving.knative.dev/configuration=hello
serving.knative.dev/configurationGeneration=1
serving.knative.dev/configurationUID=1af77209-76a2-4182-a645-36198952ae52
serving.knative.dev/revision=hello-world
serving.knative.dev/revisionUID=a6129c41-ef8c-48ff-be07-a30b6107a530
serving.knative.dev/service=hello
serving.knative.dev/serviceUID=2b115578-7155-443c-a57e-52cf04ed10ef
Annotations: autoscaling.knative.dev/class: kpa.autoscaling.knative.dev
serving.knative.dev/creator: admin
Selector:
- 通过kn查看资源
root@k8s-master1:~/knative-in-practise-main/serving/basic# kn service list
NAME URL LATEST AGE CONDITIONS READY REASON
hello http://hello.default.example.com hello-world 20m 3 OK / 3 True
root@k8s-master1:~/knative-in-practise-main/serving/basic# kn service describe hello
Name: hello
Namespace: default
Age: 21m
URL: http://hello.default.example.com
Revisions:
100% @latest (hello-world) [1] (21m)
Image: ikubernetes/helloworld-go (at 5ea96b)
Replicas: 0/0
Conditions:
OK TYPE AGE REASON
++ Ready 17m
++ ConfigurationsReady 18m
++ RoutesReady 17m
root@k8s-master1:~/knative-in-practise-main/serving/basic# kn revision list NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON hello-world hello 100% 1 21m 3 OK / 4 True
root@k8s-master1:~/knative-in-practise-main/serving/basic# kn revision describe hello-world Name: hello-world Namespace: default Age: 22m Image: ikubernetes/helloworld-go (at 5ea96b) Replicas: 0/0 Port: 8080 Env: TARGET=World Service: hello
Conditions:
OK TYPE AGE REASON
++ Ready 19m
++ ContainerHealthy 19m
++ ResourcesAvailable 19m
I Active 4m NoTraffic
root@k8s-master1:~/knative-in-practise-main/serving/basic# kn route list NAME URL READY hello http://hello.default.example.com True root@k8s-master1:~/knative-in-practise-main/serving/basic# kn route describe hello Name: hello Namespace: default Age: 23m URL: http://hello.default.example.com Service: hello
Traffic Targets:
100% @latest (hello-world)
Conditions:
OK TYPE AGE REASON
++ Ready 19m
++ AllTrafficAssigned 19m
++ CertificateProvisioned 19m TLSNotEnabled
++ IngressReady 19m
<a name="LRidk"></a>
## knative Service资源规范
- Knative Service资源的spec配置段主要由ConfigurationSpec和RouteSpec组成
- ConfigurationSpec:定义Configuration资源规范,其核心组成即为Revision Template
- RouteSpec:定义Route资源规范,旨在定义流量目标(Traffic Target)
- 查看资源类型
root@k8s-master1:~/knative-in-practise-main/serving# kubectl api-resources | grep knative metrics autoscaling.internal.knative.dev/v1alpha1 true Metric podautoscalers kpa,pa autoscaling.internal.knative.dev/v1alpha1 true PodAutoscaler images img caching.internal.knative.dev/v1alpha1 true Image certificates kcert networking.internal.knative.dev/v1alpha1 true Certificate clusterdomainclaims cdc networking.internal.knative.dev/v1alpha1 false ClusterDomainClaim ingresses kingress,king networking.internal.knative.dev/v1alpha1 true Ingress serverlessservices sks networking.internal.knative.dev/v1alpha1 true ServerlessService configurations config,cfg serving.knative.dev/v1 true Configuration domainmappings dm serving.knative.dev/v1beta1 true DomainMapping revisions rev serving.knative.dev/v1 true Revision routes rt serving.knative.dev/v1 true Route services kservice,ksvc serving.knative.dev/v1 true Service
- 服务访问
root@k8s-master1:~/knative-in-practise-main/serving# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello ExternalName
<a name="MkdyN"></a>
### DomainMapping
- 域名映射依赖于ClusterDomainClaim,创建方法(二选一)
- ~$ kubectl patch configmap config-network -n knative-serving -p '{"data":{"autocreate-cluster-domain-claims":"true"}}’
- 全局配置参数,允许自动创建CDC资源
- 手动创建相关的CDC资源,名称为要使用的外部映射域名
- 基于ClusterDomainClaim资源规范创建,仅需要指定名称,且namespace要在spec字段中定义
- 查看CDC资源
- kubectl get cdc [name]
- kubectl describe cdc [name]
- DomainMapping的功能
- 将外部域名映射至Knative Service或Knative Route
- 支持跨Kubernetes Namespace对Knative Service进行名称映射
- 创建domain Mapping
root@k8s-master1:~/knative-in-practise-main/serving# kn route list NAME URL READY hello http://hello.default.example.com True root@k8s-master1:~/knative-in-practise-main/serving# kn domain create hello.cropy.cn —ref ksvc:hello Domain mapping ‘hello.cropy.cn’ created in namespace ‘default’.
root@k8s-master1:~/knative-in-practise-main/serving# kn domain describe hello.cropy.cn Name: hello.cropy.cn Namespace: default Age: 27m
Reference:
APIVersion: serving.knative.dev/v1
Kind: Service
Name: hello
Conditions:
OK TYPE AGE REASON
!! Ready 27m DomainAlreadyClaimed
?? CertificateProvisioned 27m
!! DomainClaimed 27m DomainAlreadyClaimed
?? IngressReady 27m IngressNotConfigured
?? ReferenceResolved 27m
root@k8s-master1:~/knative-in-practise-main/serving/domainmapping# cat cdc-hello.magedu.com.yaml apiVersion: networking.internal.knative.dev/v1alpha1 kind: ClusterDomainClaim metadata: name: hello.cropy.cn spec: namespace: default
root@k8s-master1:~/knative-in-practise-main/serving/domainmapping# kubectl apply -f cdc-hello.cropy.cn.yaml root@k8s-master1:~/knative-in-practise-main/serving/domainmapping# kubectl get cdc root@k8s-master1:~/knative-in-practise-main/serving/domainmapping# kn domain list NAME URL READY KSVC hello.cropy.cn http://hello.cropy.cn False hello
- 设置自动创建cdc
root@k8s-master1:~# kubectl edit cm config-network -n knative-serving apiVersion: v1 data: autocreate-cluster-domain-claims: “true”
root@k8s-master1:~# curl -H “Host: hello.cropy.cn” 10.168.56.110 Hello World!
<a name="TVrmJ"></a>
### Revision
- 关于Revision
- 应用程序代码及相关容器配置某个版本的不可变快照
- KService上的spec.template的每次变动,都会自动生成一个新的Revision
- 通常不需要手动创建及维护
- Revision的使用场景
- 将流量切分至不同版本的应用程序间(Canary Deployment、Blue/Green Deployment)
- 版本回滚
- 设置revision实现版本管理
root@k8s-master1:~/knative-in-practise-main/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
ports:
- containerPort: 8080
env:
- name: TARGET
value: "World-002"
root@k8s-master1:~/knative-in-practise-main/serving/basic# kubectl apply -f hello-world-002.yaml root@k8s-master1:~# curl -H “Host: hi.cropy.cn” 10.168.56.110 Hello World-002!
root@k8s-master1:~# kn service list
NAME URL LATEST AGE CONDITIONS READY REASON
hello http://hello.default.example.com hello-world-002 8d 3 OK / 3 True
root@k8s-master1:~# kn route list
NAME URL READY
hello http://hello.default.example.com True
root@k8s-master1:~# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello ExternalName
root@k8s-master1:~# kn revision list #查看revision
NAME SERVICE TRAFFIC TAGS GENERATION AGE CONDITIONS READY REASON
hello-world-002 hello 100% 2 7m33s 3 OK / 4 True
hello-world hello 1 8d 3 OK / 4 True
```