基于 Service Mesh 的 海量容器管理平台实践
关于我 刘超 网易云解决方案总架构师 10 余年云计算领域研发及架构经验, 先后在 EMC,CCTV 证券资讯频道,HP, 华为, 网易从事云计算和大数据架构工作 毕业于上海交通大学 曾出版 Lucene 应用开发揭秘 多次作为邀请讲师参加 Dockone 容器技术大会,Segmentfault 开发者大会,InfoQ 全球架构师峰会 ( 明星讲师 ),CSDN SDCC 大会,51CTO WOTA 大会等 知名技术博主, 博客可搜索 popsuper1982, 多篇文章推荐至全球最大 IT 社区 CSDN 首页及 程序员 杂志 在工作中积累了大量运营商系统, 互联网金融系统, 电商系统等容器化和微服务化经验
目录 从 Docker 到 K8s 到 Service Mesh Service Mesh 介绍 深入解析 Service Mesh 网易云实践
企业上云三大架构 快速迭代 高并发 开发 应用架构 CAPEX 大数据分析 运维 IT 架构 数据架构 运营 OPEX 数字化运营
容器技术的三种视角 微服务的交付形式 Kubernetes 应用架构 IT 架构 数据架构 轻量级的 IT 运维模式 Swarm 资源利用率高的任务执行模式 Mesos
Kubernetes + Docker 是 Dev 和 Ops 融合的一个桥梁 开发, 构建, 测试 Dev 服务发现配置中心熔断降级 Kubernetes + Docker Ops Dockerfile 镜像环境交付 提供资源, 部署, 运维
Kubernetes 更加适合微服务和 DevOps 的设计 微服务设计设计要点一 :API 网关设计要点二 : 无状态化, 区分有状态的和无状态的应用 设计要点三 : 数据库的横向扩展 设计要点四 : 缓存设计要点五 : 服务拆分和服务发现设计要点六 : 服务编排与弹性伸缩设计要点七 : 统一配置中心设计要点八 : 统一的日志中心设计要点九 : 熔断, 限流, 降级设计要点十 : 全方位的监控 Kubernetes 功能 Ingress 无状态对应 Deployment, 有状态对应 StatefulSet headless service 指向 PaaS 服务, 或者 StatefulSet 部署 headless service 指向 PaaS 服务, 或者 StatefulSet 部署 Service Deployment 的 Replicas ConfigMap DaemonSet 部署日志 Agent Service Mesh Cadvisor,DaemonSet 部署监控 Agent
目录 从 Docker 到 K8s 到 Service Mesh Service Mesh 介绍 深入解析 Service Mesh 网易云实践
Service Mesh 的范例 Istio
一切从 envoy 开始 轻量级的 proxy 静态配置, 热加载, 热重启 动态配置, 拉取模式 Listener LDS Routes RDS Clusters CDS Endpoints EDS
Envoy 之静态配置 admin: access_log_path: /tmp/admin_access.log address: socket_address: { address: 127.0.0.1, port_value: 9901 } static_resources: listeners: - name: listener_0 address: socket_address: { address: 127.0.0.1, port_value: 10000 } filter_chains: - filters: - name: envoy.http_connection_manager config: stat_prefix: ingress_http codec_type: AUTO route_config: name: local_route virtual_hosts: - name: local_service domains: ["*"] routes: - match: { prefix: "/" } route: { cluster: some_service } http_filters: - name: envoy.router clusters: - name: some_service connect_timeout: 0.25s type: STATIC lb_policy: ROUND_ROBIN hosts: [{ socket_address: { address: 127.0.0.2, port_value: 1234 }}] Listener Routes Clusters Endpoints
Envoy 之动态配置 admin: access_log_path: /tmp/admin_access.log address: socket_address: { address: 127.0.0.1, port_value: 9901 } dynamic_resources: lds_config: api_config_source: api_type: GRPC cluster_names: [xds_cluster] cds_config: api_config_source: api_type: GRPC cluster_names: [xds_cluster] static_resources: clusters: - name: xds_cluster connect_timeout: 0.25s type: STATIC lb_policy: ROUND_ROBIN http2_protocol_options: {} hosts: [{ socket_address: { address: 127.0.0.3, port_value: 5678 }}]
加入控制面 Pilot
Pilot 路由策略
Pilot 的负载均衡
目录 从 Docker 到 K8s 到 Service Mesh Service Mesh 介绍 深入解析 Service Mesh 网易云实践
这个简单例子背后发生的事情
Productpage 是一个 python 程序 @app.route('/api/v1/products') def productsroute(): return json.dumps(getproducts()), 200, {'Content-Type': 'application/json'} @app.route('/api/v1/products/<product_id>') def productroute(product_id): headers = getforwardheaders(request) status, details = getproductdetails(product_id, headers) return json.dumps(details), status, {'Content-Type': 'application/json'} @app.route('/api/v1/products/<product_id>/reviews') def reviewsroute(product_id): headers = getforwardheaders(request) status, reviews = getproductreviews(product_id, headers) return json.dumps(reviews), status, {'Content-Type': 'application/json'} @app.route('/api/v1/products/<product_id>/ratings') def ratingsroute(product_id): headers = getforwardheaders(request) status, ratings = getproductratings(product_id, headers) return json.dumps(ratings), status, {'Content-Type': 'application/json'} details = { "name" : "http://details:9080", "endpoint" : "details", "children" : [] } ratings = { "name" : "http://ratings:9080", "endpoint" : "ratings", "children" : [] } reviews = { "name" : "http://reviews:9080", "endpoint" : "reviews", "children" : [ratings] }
使用 Kubernetes 编排 productpage apiversion: v1 kind: Service metadata: name: productpage labels: app: productpage spec: ports: - port: 9080 name: http selector: app: productpage --- apiversion: extensions/v1beta1 kind: Deployment metadata: name: productpage-v1 spec: replicas: 1 template: metadata: labels: app: productpage version: v1 spec: containers: - name: productpage image: istio/examples-bookinfo-productpagev1:1.5.0 imagepullpolicy: IfNotPresent ports: - containerport: 9080 ---
使用 Kubernetes 编排 reviews apiversion: v1 kind: Service metadata: name: reviews labels: app: reviews spec: ports: - port: 9080 name: http selector: app: reviews --- apiversion: extensions/v1beta1 kind: Deployment metadata: name: reviews-v1 spec: replicas: 1 template: metadata: labels: app: reviews version: v1 spec: containers: - name: reviews image: istio/examples-bookinfo-reviews-v1:1.5.0 imagepullpolicy: IfNotPresent ports: - containerport: 9080 --- apiversion: extensions/v1beta1 kind: Deployment metadata: name: reviews-v2 spec: replicas: 1 template: metadata: labels: app: reviews version: v2 spec: containers: - name: reviews image: istio/examples-bookinfo-reviews-v2:1.5.0 imagepullpolicy: IfNotPresent ports: - containerport: 9080 --- apiversion: extensions/v1beta1 kind: Deployment metadata: name: reviews-v3 spec: replicas: 1 template: metadata: labels: app: reviews version: v3 spec: containers: - name: reviews image: istio/examples-bookinfo-reviews-v3:1.5.0 imagepullpolicy: IfNotPresent ports: - containerport: 9080 ---
嵌入 proxy_init 作为 InitContainer Init Containers: istio-init: Image: docker.io/istio/proxy_init:0.7.1 Port: <none> Host Port: <none> Args: -p 15001 -u 1337 Environment: <none> Mounts: <none> enable-core-dump: Image: alpine Port: <none> Host Port: <none> Command: /bin/sh Args: -c sysctl -w kernel.core_pattern=/etc/istio/proxy/core.%e.%p.%t && ulimit -c unlimited Environment: <none> Mounts: <none>
proxy_init 设置 iptables 规则 /usr/local/bin/prepare_proxy.sh -p PORT -u UID 15001 iptables -t nat -A ISTIO_OUTPUT -m owner -- uid-owner ${ENVOY_UID} -j RETURN iptables -t nat -A ISTIO_REDIRECT -p tcp -j REDIRECT --to-port ${ENVOY_PORT} 9080 iptables -t nat -A PREROUTING -j ISTIO_REDIRECT iptables -t nat -A OUTPUT -p tcp -j ISTIO_OUTPUT iptables -t nat -A ISTIO_OUTPUT -o lo! -d 127.0.0.1/32 -j ISTIO_REDIRECT
嵌入 proxy 容器作为 sidecar istio-proxy: Image: docker.io/istio/proxy_debug:0.7.1 Port: <none> Host Port: <none> Args: proxy sidecar --configpath /etc/istio/proxy --binarypath /usr/local/bin/envoy --servicecluster productpage --drainduration 45s --parentshutdownduration 1m0s --discoveryaddress istio-pilot.istio-system:8080 --discoveryrefreshdelay 1s
SideCar 内启动的进程 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND istio-p+ 1 0.0 0.2 32656 19108? Ssl 08:55 0:00 /usr/local/bin/pilot-agent proxy sidecar --configpath /etc/istio/proxy --binarypath /usr/local/bin/envoy --servicecluster productpage --drainduration 45s -- parentshutdownduration 1m0s --discoveryaddress istio-pilot.istio-system:8080 --discoveryrefreshdelay 1s -- zipkinaddress zipkin.istio-system:9411 --connecttimeout 10s --statsdudpaddress istio-mixer.istio-system:9125 -- proxyadminport 15000 --controlplaneauthpolicy NONE istio-p+ 11 3.9 0.5 127352 41068? Sl 08:55 13:19 /usr/local/bin/envoy -c /etc/istio/proxy/envoy-rev0.json --restart-epoch 0 --drain-time-s 45 --parent-shutdown-time-s 60 --service-cluster productpage --service-node sidecar~192.168.1.114~productpage-v1-8666ffbd7c-mss5p.default~default.svc.cluster.local --max-obj-name-len 189 -l info --v2-config-only
Envoy 进程的配置 "admin": { "access_log_path": "/dev/stdout", "address": { "socket_address": { } }, } "address": "127.0.0.1", "port_value": 15000 "dynamic_resources": { "lds_config": { "api_config_source": { "api_type": "REST_LEGACY", "refresh_delay": {"seconds": 1, "nanos": 0}, "cluster_names": [ "rds" ] } }, "cds_config": { "api_config_source": { "api_type": "REST_LEGACY", "refresh_delay": {"seconds": 1, "nanos": 0}, "cluster_names": [ "rds" ] } } "static_resources": { "clusters": [ { "name": "rds", "type": "STRICT_DNS", "connect_timeout": {"seconds": 10, "nanos": 0}, "lb_policy": "ROUND_ROBIN", "hosts": [ { "socket_address": {"address": "istiopilot.istio-system", "port_value": 8080} } ] 管理端口动态资源静态资源 },
Pilot Agent 的作用 Wrapper of envoy 当 envoy 的静态配置改变的时候, 对 envoy 进行热重启后生效 ( 例如 TLS)
Pilot 的工作机制 apiversion: config.istio.io/v1alpha2 kind: RouteRule metadata: name: reviews-default namespace: default spec: destination: name: reviews precedence: 1 route: - labels: version: v1 weight: 50 - labels: version: v3 weight: 50
在 pilot 上配置 route apiversion: config.istio.io/v1alpha2 kind: RouteRule metadata: name: reviews-default spec: destination: name: reviews precedence: 1 route: - labels: version: v1
查看 envoy 的管理端口 curl http://127.0.0.1:15000/clusters { "name": "reviews.default.svc.cluster.local http", "domains": [ "reviews:9080", "reviews", "reviews.default:9080", "reviews.default", "reviews.default.svc:9080", "reviews.default.svc", "reviews.default.svc.cluster:9080", "reviews.default.svc.cluster", "reviews.default.svc.cluster.local:9080", "reviews.default.svc.cluster.local", "10.104.14.93:9080", "10.104.14.93" ], "routes": [ { "match": { "prefix": "/" }, "route": { "cluster": "out.reviews.default.svc.cluster.local http version=v1", "timeout": "0s" }, "decorator": { "operation": "reviews-default" } } ] } istio-proxy@productpage-v1-8666ffbd7c-mss5p:/$ curl http://127.0.0.1:15000/clusters grep reviews % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed out.reviews.default.svc.cluster.local http version=v1::default_priority::max_connections::1024 100 52770 0 52770 0 0 16.1M 0 --:--:-- --:--:-- --:--:-- 25.1M out.reviews.default.svc.cluster.local http version=v1::default_priority::max_pending_requests::1024 out.reviews.default.svc.cluster.local http version=v1::default_priority::max_requests::1024 out.reviews.default.svc.cluster.local http version=v1::default_priority::max_retries::3 out.reviews.default.svc.cluster.local http version=v1::high_priority::max_connections::1024 out.reviews.default.svc.cluster.local http version=v1::high_priority::max_pending_requests::1024 out.reviews.default.svc.cluster.local http version=v1::high_priority::max_requests::1024 out.reviews.default.svc.cluster.local http version=v1::high_priority::max_retries::3 out.reviews.default.svc.cluster.local http version=v1::added_via_api::true out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::cx_active::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::cx_connect_fail::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::cx_total::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::rq_active::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::rq_error::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::rq_success::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::rq_timeout::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::rq_total::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::health_flags::healthy out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::weight::1 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::region:: out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::zone:: out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::sub_zone:: out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::canary::false out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::success_rate::-1
修改 pilot 的 route root@ip-172-31-15-9:~# cat./istio-0.7.1/samples/bookinfo/kube/route-rule-reviews-50-v3.yaml apiversion: config.istio.io/v1alpha2 kind: RouteRule metadata: name: reviews-default spec: destination: name: reviews precedence: 1 route: - labels: version: v1 weight: 50 - labels: version: v3 weight: 50
Envoy 中的规则被修改 { "name": "reviews.default.svc.cluster.local http", "domains": [ "reviews:9080", "reviews", "reviews.default:9080", "reviews.default", "reviews.default.svc:9080", "reviews.default.svc", "reviews.default.svc.cluster:9080", "reviews.default.svc.cluster", "reviews.default.svc.cluster.local:9080", "reviews.default.svc.cluster.local", "10.104.14.93:9080", "10.104.14.93" ], "routes": [ { "match": { "prefix": "/" }, "route": { "weighted_clusters": { "clusters": [ { "name": "out.reviews.default.svc.cluster.local http version=v1", "weight": 50 }, { "name": "out.reviews.default.svc.cluster.local http version=v3", "weight": 50 } ] }, "timeout": "0s" }, "decorator": { "operation": "reviews-default" istio-proxy@productpage-v1-8666ffbd7c-mss5p:/$ curl http://127.0.0.1:15000/clusters grep reviews 0out.reviews.default.svc.cluster.local http version=v3::default_priority::max_connections::1024 out.reviews.default.svc.cluster.local http version=v3::default_priority::max_pending_requests::1024 out.reviews.default.svc.cluster.local http version=v3::default_priority::max_requests::1024 out.reviews.default.svc.cluster.local http version=v3::default_priority::max_retries::3 out.reviews.default.svc.cluster.local http version=v3::high_priority::max_connections::1024 out.reviews.default.svc.cluster.local http version=v3::high_priority::max_pending_requests::1024 out.reviews.default.svc.cluster.local http version=v3::high_priority::max_requests::1024 out.reviews.default.svc.cluster.local http version=v3::high_priority::max_retries::3 out.reviews.default.svc.cluster.local http version=v3::added_via_api::true out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::cx_active::0 out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::cx_connect_fail::0 out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::cx_total::0 out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::rq_active::0 out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::rq_error::0 out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::rq_success::0 out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::rq_timeout::0 out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::rq_total::0 out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::health_flags::healthy out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::weight::1 out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::region:: out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::zone:: out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::sub_zone:: out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::canary::false out.reviews.default.svc.cluster.local http version=v3::192.168.2.24:9080::success_rate::-1 out.reviews.default.svc.cluster.local http version=v1::default_priority::max_connections::1024 out.reviews.default.svc.cluster.local http version=v1::default_priority::max_pending_requests::1024 out.reviews.default.svc.cluster.local http version=v1::default_priority::max_requests::1024 out.reviews.default.svc.cluster.local http version=v1::default_priority::max_retries::3 out.reviews.default.svc.cluster.local http version=v1::high_priority::max_connections::1024 out.reviews.default.svc.cluster.local http version=v1::high_priority::max_pending_requests::1024 out.reviews.default.svc.cluster.local http version=v1::high_priority::max_requests::1024 out.reviews.default.svc.cluster.local http version=v1::high_priority::max_retries::3 out.reviews.default.svc.cluster.local http version=v1::added_via_api::true out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::cx_active::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::cx_connect_fail::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::cx_total::0 out.reviews.default.svc.cluster.local http version=v1::192.168.2.23:9080::rq_active::0
目录 从 Docker 到 K8s 到 Service Mesh Service Mesh 介绍 深入解析 Service Mesh 网易云实践
基于 SpringCloud 的微服务框架
微服务十二原则
Haproxy 作为 service mesh 的 Agent 订单 平台 haproxy haproxy 会员 /agents/haproxy/haproxy.ctmpl haproxy /consul-template 注册中心 /haproxy -f /agents/haproxy/haproxy.conf consul consul consul 配置中心
Haproxy 作为 service mesh 的 Agent
新一代微服务框架设计要点 Agent 热加载 兼容 SpringCloud 框架 Restful API 控制面租户隔离 适配 VPC 网络 可视化 横向扩展 高可用 同 IaaS 或者 Kubernetes 解耦 熔断降级
新一代微服务治理平台 服务 B jar NQS envoy Data Stream Kafka 服务 A jar envoy Storm 集群 监控平台 Redis 服务 C jar Hadoop 注册中心 Service Mesh 控制面 envoy Hbase DDB 降级限流路由日志收集日志分析调用次数调用时间 服务治理日志分析统计监控 UAS Agent 用户空间 VPC 管理控制服务 云主机云网络云硬盘 OpenStack 对象存储 UAS Server 管控节点 ( 主 ) 管控节点 ( 备 ) 监控节点 ( 主 ) 监控节点 ( 备 )
与 VPC 集成实现租户隔离
与 VPC 集成实现租户隔离
基于 Service Mesh 的容器管理平台
容器管理平台本身也是微服务 所有的多租户容器请求入口流量 高可用, 横向扩展 对接多个业务 :OpenStack, Kubernetes, 所有 PaaS, 持 续集成, 镜像仓库, 计费, 用 户, 认证, 负载均衡, 路由 熔断, 限流, 降级 可靠消息 监控, 统计
容器管理平台也是用 Kubernetes 部署 API 网 关 微服务 框架 Kubernetes
共创云上精彩世界