coutour自身也是Ingress控制器
,所以v1beta1
和v1
版本的ingress资源规范,在coutour上也能使用,只不过其kubernetes.io/ingress.class
:为contour
。
除了ingress之外,其还支持其独有的资源规范,是其通过CRD引入的,名字为httpproxies.projectcontour.io
1 2 3 4 5 root@k8s-master01:~/yaml/chapter13 extensionservices extensionservice,extensionservices projectcontour.io/v1alpha1 true ExtensionService httpproxies proxy,proxies projectcontour.io/v1 true HTTPProxy tlscertificatedelegations tlscerts projectcontour.io/v1 true TLSCertificateDelegation
我们要想通过contour实现各种高级功能是通过httpproxies来实现的。
httpproxies资源规范 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 apiVersion: projectcontour.io/v1 kind: HTTPProxy metadata: name <string> namespace <string> spec: virtualhost <VirtualHost> fqdn <string> tls <TLS> secretName <string> minimumProtocolVersion <string> passthrough <boolean> clientValidation <DownstreamValidation> caSecret <string> routes <[]Route> conditions <[]Condition> prefix <String> permitInsecure <Boolean> services <[]Service> name <String> port <Integer> protocol <String> validation <UpstreamValidation> caSecret <String> subjectName <string>
httpproxy示例 1.编写资源清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 root@k8s-master01:~/yaml/chapter13 apiVersion: projectcontour.io/v1 kind: HTTPProxy metadata: name: httpproxy-demo namespace: default spec: virtualhost: fqdn: www.mylinuxops.cn tls: secretName: mylinuxops-tls minimumProtocolVersion: "tlsv1.1" routes: - conditions: - prefix: / services: - name: demoapp port: 80 permitInsecure: true
2.应用配置清单
1 2 root@k8s-master01:~/yaml/chapter13 httpproxy.projectcontour.io/httpproxy-demo created
3.测试访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 root@k8s-master01:~/yaml/chapter13 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE contour ClusterIP 10.100.169.73 <none> 8001/TCP 49m envoy LoadBalancer 10.110.150.215 <pending> 80:32182/TCP,443:30273/TCP 49m root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.0 !! ClientIP: 192.168.131.23, ServerName: deployment-demo-fb544c5d8-qbwtm, ServerIP: 192.168.131.20! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.0 !! ClientIP: 192.168.131.23, ServerName: deployment-demo-fb544c5d8-h97bv, ServerIP: 192.168.96.23! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.0 !! ClientIP: 192.168.131.23, ServerName: deployment-demo-fb544c5d8-d2k7v, ServerIP: 192.168.96.24! curl: (35) OpenSSL SSL_connect: Connection reset by peer in connection to 172.16.11.81:30273 root@k8s-master01:~/yaml/chapter13 root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.0 !! ClientIP: 192.168.30.24, ServerName: deployment-demo-fb544c5d8-qbwtm, ServerIP: 192.168.131.20! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.0 !! ClientIP: 192.168.30.24, ServerName: deployment-demo-fb544c5d8-h97bv, ServerIP: 192.168.96.23!
HTTPProxy高级路由资源规范 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 spec: routes <[]Route> conditions <[]Condition> prefix <String> header <HeaderCondition> name <String> present <Boolean> contains <String> notcontains <String> exact <String> notexact <String> services <[]Service> name <String> port <Integer> protocol <String> weight <Int64> mirror <Boolean> requestHeadersPolicy <HeadersPolicy> set <[]HeaderValue> name <String> value <String> remove <[]String> responseHeadersPolicy <HeadersPolicy> loadBalancerPolicy <LoadBalancerPolicy> strategy <String> requestHeadersPolicy <HeadersPolicy> reHeadersPolicy <HeadersPolicy> pathRewritePolicy <PathRewritePolicy> replacePrefix <[]ReplacePrefix> prefix <String> replacement <String>
HTTPProxy高级应用示例 准备工作 1.创建出名称空间
1 2 root@k8s-master01:~/yaml/chapter13 namespace/test created
2.部署出两个版本的deployment
1 2 3 4 root@k8s-master01:~/yaml/chapter13 deployment.apps/demoappv11 created root@k8s-master01:~/yaml/chapter13 deployment.apps/demoappv12 created
3.为两个版本的deployment创建service
1 2 3 4 root@k8s-master01:~/yaml/chapter13 service/demoappv11 created root@k8s-master01:~/yaml/chapter13 service/demoappv12 created
基于标头的路由 1.编写资源清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 root@k8s-master01:~/yaml/chapter13 apiVersion: projectcontour.io/v1 kind: HTTPProxy metadata: name: httpproxy-headers-routing namespace: test spec: virtualhost: fqdn: www.myk8s.com routes: - conditions: - header: name: X-Canary present: true - header: name: User-Agent contains: curl services: - name: demoappv12 port: 80 - services: - name: demoappv11 port: 80
2.应用后测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 root@k8s-master01:~/yaml/chapter13 httpproxy.projectcontour.io/httpproxy-headers-routing created root@k8s-master01:~/yaml/chapter13 root@k8s-master01:~/yaml/chapter13 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE contour ClusterIP 10.100.169.73 <none> 8001/TCP 114m envoy LoadBalancer 10.110.150.215 <pending> 80:32182/TCP,443:30273/TCP 114m root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.2 !! ClientIP: 192.168.30.24, ServerName: demoappv12-64c664955b-skq7g, ServerIP: 192.168.30.27! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.2 !! ClientIP: 192.168.30.24, ServerName: demoappv12-64c664955b-skq7g, ServerIP: 192.168.30.27! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.2 !! ClientIP: 192.168.30.24, ServerName: demoappv12-64c664955b-skq7g, ServerIP: 192.168.30.27!
流量拆分 1.编写资源清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 root@k8s-master01:~/yaml/chapter13 apiVersion: projectcontour.io/v1 kind: HTTPProxy metadata: name: httpproxy-traffic-splitting namespace: test spec: virtualhost: fqdn: www.myk8s.com routes: - conditions: - prefix: / services: - name: demoappv11 port: 80 weight: 90 - name: demoappv12 port: 80 weight: 10
2.应用资源清单并测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 root@k8s-master01:~/yaml/chapter13 httpproxy.projectcontour.io/httpproxy-traffic-splitting configured root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.2 !! ClientIP: 192.168.30.24, ServerName: demoappv12-64c664955b-skq7g, ServerIP: 192.168.30.27! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! iKubernetes demoapp v1.2 !! ClientIP: 192.168.30.24, ServerName: demoappv12-64c664955b-skq7g, ServerIP: 192.168.30.27!
流量镜像 1.编写资源清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 root@k8s-master01:~/yaml/chapter13 apiVersion: projectcontour.io/v1 kind: HTTPProxy metadata: name: httpproxy-traffic-mirror namespace: test spec: virtualhost: fqdn: www.myk8s.com routes: - conditions: - prefix: / services: - name: demoappv11 port: 80 - name: demoappv12 port: 80 mirror: true
2.应用配置并测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 root@k8s-master01:~/yaml/chapter13 httpproxy.projectcontour.io/httpproxy-traffic-mirror created root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! root@k8s-master01:~/yaml/chapter13 iKubernetes demoapp v1.1 !! ClientIP: 192.168.30.24, ServerName: demoappv11-59544d568d-5tpb9, ServerIP: 192.168.30.28! root@k8s-master01:~/yaml/chapter13 demoappv11-59544d568d-5tpb9 demoappv12-64c664955b-skq7g root@k8s-master01:~/yaml/chapter13 * Running on http://0.0.0.0:80/ (Press CTRL+C to quit) 192.168.30.24 - - [23/Aug/2021 09:43:10] "GET / HTTP/1.1" 200 - 192.168.30.24 - - [23/Aug/2021 09:43:16] "GET / HTTP/1.1" 200 - 192.168.30.24 - - [23/Aug/2021 09:43:18] "GET / HTTP/1.1" 200 - 192.168.30.24 - - [23/Aug/2021 09:43:22] "GET / HTTP/1.1" 200 - 192.168.30.24 - - [23/Aug/2021 09:43:37] "GET / HTTP/1.1" 200 - 192.168.30.24 - - [23/Aug/2021 09:43:39] "GET / HTTP/1.1" 200 - 192.168.30.24 - - [23/Aug/2021 09:43:40] "GET / HTTP/1.1" 200 -
负载均衡模式 1.编写资源清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 root@k8s-master01:~/yaml/chapter13 apiVersion: projectcontour.io/v1 kind: HTTPProxy metadata: name: httpproxy-lb-strategy namespace: test spec: virtualhost: fqdn: www.myk8s.com routes: - conditions: - prefix: / services: - name: demoappv11 port: 80 - name: demoappv12 port: 80 loadBalancerPolicy: strategy: Random
HTTPProxy服务弹性 如果后端服务器挂了可以定义超时策略,重试策略,也可以自己设定健康状态检测
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 spec: routes <[]Route> timeoutPolicy <TimeoutPolicy> response <String> idle <String> retryPolicy <RetryPolicy> count <Int64> perTryTimeout <String> healthCheckPolicy <HTTPHealthCheckPolicy> path <String> host <String> intervalSeconds <Int64> timeoutSeconds <Int64> unhealthyThresholdCount <Int64> healthyThresholdCount <Int64>
服务弹性示例 1.编写资源清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 root@k8s-master01:~/yaml/chapter13 apiVersion: projectcontour.io/v1 kind: HTTPProxy metadata: name: httpproxy-retry-timeout namespace: dev spec: virtualhost: fqdn: www.myk8s.com routes: - timeoutPolicy: response: 2s idle: 5s retryPolicy: count: 3 perTryTimeout: 500ms services: - name: demoapp port: 80