Amazon EKS 云上托管的 K8S 集群 杨历,AWS 解决方案架构师
议程 AWS 容器技术介绍 Amazon EKS( 云上托管的 K8S 集群 ) 介绍 Amazon EKS 对 K8S 网络的创新 K8S Service 与 AWS 的集成 Amazon EKS 与 AWS 安全解决方案整合 Amazon EKS 日志及监控 Demo 演示
AWS 容器生态系统
Amazon ECS Task & Service Internet LOAD BALANCER ECS Service EC2 INSTANCES TASK Container TASK Container ECS AGENT TASK Container TASK Container ECS AGENT Amazon ECS AGENT COMMUNICATION SERVICE API LOAD BALANCER TASK Container TASK Container ECS AGENT CLUSTER MANAGEMENT ENGINE KEY/VALUE STORE
AWS ECS 适用场景
57% Kubernetes 工作负载运行在 AWS 上 CNCF survey
KOPS ( 在 AWS 上构建 K8S 集群的工具 ) controller-manager --cloud-provider=aws allocate-nodecidrs=true clustercidr=10.101.0.0/16 Service type: LoadBalancer Volumes Persistent Volumes 云特性 CNI 插件 部署文档 :https://kubernetes.io/docs/getting-started-guides/aws/
1. I n s t a l l B i n a r i e s & T o o l s : k o p s, A W S C L I t o o l s, k u b e c t l 2. S e t I A M U s e r t o k o p s 3. A l l o w k o p s u s e r F u l l a c c e s s t o E C 2, R o u t e 5 3, S 3, I A M, V P C 4. C o n f i g u r e A W S c l i e n t t o n e w I A M u s e r k o p s 5. C o n f i g u r e D N S ( o r ) D e p l o y a g o s s i p - b a s e d c l u s t e r : W e h o s t e d t h e s u b d o m a i n d n i s h i. k 8 s d e m o l a b s. c o m i n R o u t e 5 3 6. C r e a t e a S 3 b u c k e t t o s a v e c l u s t e r c o n f i g : d n i s h i - k o p s - s t o r e 7. S e t t h e k o p s e n v i r o n m e n t a l v a r i a b l e s 8. C r e a t e c l u s t e r : k o p s c r e a t e c l u s t e r a n d k o p s v a l i d a t e c l u s t e r kops create cluster \ --api-loadbalancer-type=public \ --vpc vpc-7d4ef914 \ --network-cidr 10.2.0.0/16 \ --master-zones cn-northwest-1a,cn-northwest-1b,cn-northwest-1c --master-count 3 \ --zones cn-northwest-1a,cn-northwest-1b,cn-northwest-1c --node-count 3 \ --node-size t2.medium \ --master-size t2.medium \ --topology private \ --networking amazon-vpc-routed-eni \ --cloud-labels "Team=Dev,Owner=Martin Yang" \ --image ami-1b7f6879 \ kopsdemo.k8s.local
Internet gateway VPC NAT gateway VPC NAT gateway VPC NAT gateway Classic Load Balancer(s) Kubernetes Cluster State Public Subnet Public Subnet Public Subnet M ASG EBS M ASG EBS M ASG EBS W W W Private Subnet Private Subnet Private Subnet Availability Zone Availability Zone Availability Zone
议程 AWS 容器技术介绍 Amazon EKS( 云上托管的 K8S 集群 ) EKS 对 K8S 网络的创新 K8S Service 与 AWS 的集成 EKS 与 AWS 安全解决方案整合 EKS 日志及监控 Demo 演示
容器编排的痛点 服务的注册 / 发现 集群扩展 / 收缩 网络插件管理 监控健康检查 与云平台的集成 任务放置策略 权限管理 配置管理
Amazon EKS 和开源 K u b e r n e t e s 一致体验 U p s t r e a m 保持和上游同步 支持企业生产级别的容器应用 按需和 A W S 服务无缝集成 自动升级打补丁
AWS 托管的高可用性方案 3 个可用区,3 个主节点 Kubectl EKS Endpoint Unhealthy Master Master Master New Master AZ 1 AZ 1 AZ 1
主节点与工作节点的网络连接 PrivateLink PrivateLink Elastic Network Interface K8s API PrivateLink Workers Customer VPC Elastic Network Interface EKS VPC Masters
主节点与工作节点的网络连接 PrivateLink 在 EKS 中, 主节点和 API 服务器通过 PrivateLink 向工作节点公开 在工作节点的 VPC 中以 ENI 方式出现, 在工作节点和主节点之间提供高速网络, 而无需穿越公共互联网
Amazon EKS 的工作原理
Amazon EKS 总结 EKS 是 CNCF 基金会认证的原生 Kubernetes 管理新版本升级 主节点由 AWS 托管, 托管的 3 个主节点分布在三个可用区实现高可用性 您的工作节点在您自己的 VPC 中, VPC 可以是新创建或现有的 VPC 工作节点可以运行在私有子网, 工作节点由用户自己管理, 可以使用定制的 AMI(AWS 提供工具 ), GPU 实例等等. 通过 PrivateLink 来实现主节点与工作节点的高速, 稳定网络连接 AWS 提供 CloudFormation 脚本来自动创建 VPC 及工作节点
Amazon EKS 总结 EKS 会自动备份, 监控 etcd Kube API server, controller, scheduler 日志会发送到 CloudWatch 通过 CloudTrail 来监控 EKS API 调用 未来会支持无服务器化 Fargate
怎样迁移到 Amazon EKS 修改 kubectl 的配置文件指向 EKS 重新发布应用程序 不能通过备份 / 恢复 etcd 的方式来迁移
议程 AWS 容器技术介绍 Amazon EKS( 云上托管的 K8S 集群 ) EKS 对 K8S 网络的创新 K8S Service 与 AWS 的集成 Amazon EKS 与 AWS 安全解决方案整合 Amazon EKS 日志及监控 Demo 演示
Docker: 网络模式 - bridge ( 默认 ) eth0 172.17.0.2/16 eth0 172.17.0.3/16 $ docker run p 8080:8080 maddox/fast-http docker0 (bridge) 172.17.42.1/16 eth0 192.168.0.1/24 Running a container with ports mapped sets up a NAT with iptables 192.168.0.1:8080 -> 172.17.0.2:8080
Docker: 网络模式 - host eth0 192.168.0.1:8080 eth0 192.168.0.1:8081 $ docker run p 8080:8080 maddox/fast-http --network host No performance overhead eth0 192.168.0.1/24 Only one port 8080 per host
Kubernetes 网络 每个 pod 都有一个 IP 地址 容器看到的 IP 与其他人看到的 IP 相同 Kubernetes 通过插件的模式来实现网络解决方案 (CNI)
主流的开源网络插件
amazon-vpc-cni-k8s 网络插件 { } 与 VPC 网络集成的插件 Pods 具有物理的 VPC 网络地址 简单, 安全的网络 开源, 在 Github 上 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved. In Kops, use `--networking amazon-vpc-routed-eni`
amazon-vpc-cni-k8s 插件原理
amazon-vpc-cni-k8s 插件原理
议程 AWS 容器技术介绍 Amazon EKS( 云上托管的 K8S 集群 ) Amazon EKS 对 K8S 网络的创新 K8S Service 与 AWS 的集成 Amazon EKS 与 AWS 安全解决方案整合 Amazon EKS 日志及监控 Demo 演示
Kubernetes Services 将一组容器 (pod) 部署到 Kubernetes 时 ClusterIP virtual IP, accessible from all nodes LoadBalancer automatically creates a public ELB (using IAM role) NodePort bind service to the same port on every host
Services: ClusterIP $ kubectl run nginx --image=nginx --replicas 3 --port=80 $ kubectl expose deployment nginx $ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx ClusterIP 100.67.104.10 <none> 80/TCP 17s Now all hosts can connect to 100.67.104.10 (or via DNS as nginx)
Services: ClusterIP Cluster IP doesn't actually exist on any host, or anywhere. Each node runs a 'kube-proxy' container. kube-proxy creates iptables rules on each host to redirect ClusterIP to pod(s) IPs: $ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx ClusterIP 100.67.104.10 <none> 80/TCP 1m $ iptables-save grep nginx -A KUBE-SEP-26VNBZMXBHB7VWNZ -s 100.123.148.131/32 -m comment --comment "default/nginx:" -j KUBE-MARK-MASQ -A KUBE-SEP-26VNBZMXBHB7VWNZ -p tcp -m comment --comment "default/nginx:" -m tcp -j DNAT --to-destination 100.123.148.131:80 -A KUBE-SEP-BHBFJ25VE6FZ3NVP -s 100.122.216.130/32 -m comment --comment "default/nginx:" -j KUBE-MARK-MASQ -A KUBE-SEP-BHBFJ25VE6FZ3NVP -p tcp -m comment --comment "default/nginx:" -m tcp -j DNAT --to-destination 100.122.216.130:80 -A KUBE-SEP-TVBSTANGUXMLRRM2 -s 100.123.46.130/32 -m comment --comment "default/nginx:" -j KUBE-MARK-MASQ -A KUBE-SEP-TVBSTANGUXMLRRM2 -p tcp -m comment --comment "default/nginx:" -m tcp -j DNAT --to-destination 100.123.46.130:80 pod1 pod2 pod3 -A KUBE-SERVICES! -s 100.96.0.0/11 -d 100.67.104.10/32 -p tcp -m comment --comment "default/nginx: cluster IP" -m tcp --dport 80 -j KUBE-MARK-MASQ -A KUBE-SERVICES -d 100.67.104.10/32 -p tcp -m comment --comment "default/nginx: cluster IP" -m tcp --dport 80 -j KUBE-SVC-4N57TFCL4MD7ZTDA -A KUBE-SVC-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx:" -m statistic --mode random --probability 0.33332999982 -j KUBE-SEP-BHBFJ25VE6FZ3NVP -A KUBE-SVC-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx:" -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-26VNBZMXBHB7VWNZ -A KUBE-SVC-4N57TFCL4MD7ZTDA -m comment --comment "default/nginx:" -j KUBE-SEP-TVBSTANGUXMLRRM2
Iptables 的弱点 Iptables 在有大量规则时, 效率低. 更新期间锁定 5,000 services == 40,000 rules == ~11 分 20,000 services == 160,000 rules == ~5 小时 较早的服务规则出现在 iptables 链的头部 = faster 较新的服务规则稍后会出现在 iptables 链后部 = slower 新版本 (1.11) 会用 IPVS 取代 iptables 这将 160,000 规则的更新时间缩短到 2 毫秒
Services: LoadBalancer $ kubectl run nginx --image=nginx --replicas 3 --port=80 $ kubectl expose deployment nginx --type=loadbalancer $ kubectl get services -o=wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) nginx LoadBalancer 100.70.217.164 a5cefe533ac1d11e7a38f0a67818e472-1987464052.eu-west-1.elb.amazonaws.com 80:31108/TCP
通过 annotations 来配置 ELB service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ service.beta.kubernetes.io/ aws-load-balancer-type aws-load-balancer-internal aws-load-balancer-proxy-protocol aws-load-balancer-access-log-emit-interval aws-load-balancer-access-log-enabled aws-load-balancer-access-log-s3-bucket-name aws-load-balancer-access-log-s3-bucket-prefix aws-load-balancer-connection-draining-enabled aws-load-balancer-connection-draining-timeout aws-load-balancer-connection-idle-timeout aws-load-balancer-cross-zone-load-balancing-enabled aws-load-balancer-extra-security-groups aws-load-balancer-ssl-cert aws-load-balancer-ssl-ports aws-load-balancer-ssl-negotiation-policy aws-load-balancer-backend-protocol aws-load-balancer-additional-resource-tags aws-load-balancer-healthcheck-healthy-threshold aws-load-balancer-healthcheck-unhealthy-threshold aws-load-balancer-healthcheck-timeout aws-load-balancer-healthcheck-interval https://github.com/kubernetes/kubernetes/blob/master/pkg/cloudprovider/providers/aws/aws.go Draining Logging SSL Certs Tagging Security groups Health checks Currently aws only supports elb classic and nlb in EKS. aws-alb-ingress-controller plugin that enables AWS ALB for Kubernetes https://github.com/kubernetes-sigs/aws-alb-ingress-controller)
Network Load Balancer (layer 4) apiversion: v1 kind: Service metadata: name: nginx namespace: default labels: app: nginx annotations: service.beta.kubernetes.io/aws-load-balancer-type: "nlb" spec: type: LoadBalancer externaltrafficpolicy: Local ports: - name: http port: 80 protocol: TCP targetport: 80 selector: app: nginx
议程 AWS 容器技术介绍 Amazon EKS( 云上托管的 K8S 集群 ) Amazon EKS 对 K8S 网络的创新 K8S Service 与 AWS 的集成 Amazon EKS 与 AWS 安全解决方案整合 Amazon EKS 日志及监控 Demo 演示
安全 IAM VPC PrivateLink Heptio kubernete RBAC support Calico network policy
amazon-vpc-cni-k8s 插件不支持 NetworkPolicy EKS 使用 Calico 实现 NetworkPolicy 虽然 Calico 本身就是 CNI, 但我们只会使用它的 NetworkPolicy 功能 Kubernetes 网络策略强制执行网络安全规则 Calico 是网络策略 API 的领导者 开源, 活动的开发者 (> 100 个贡献者 ) Tigera 提供商业支持
Calico 网络安全策略
通过 heptio 来整合 Kubernetes RBAC 与 IAM https://github.com/heptiolabs/kubernetes-aws-authenticator 一种开源解决方案, 可以将 AWS IAM 身份验证与 Kubernetes 集成
Kubectl 使用 IAM 进行身份验证 1) Passes AWS Identity 2) Verifies AWS Identity Kubectl 4) K8s action allowed/denied K8s API AWS Auth 3) Authorizes AWS Identity with RBAC
RBAC
Kubernetes 角色 用户 ( 在我们的例子中,IAM 用户 ) 绑定到 Kubernetes 角色 通常只有一个 Admin 角色 具有有限权限的其他角色 ( 使用 RBAC 分配 ) 允许用户访问群集中的特定命名空间或其他组件
议程 AWS 容器技术介绍 Amazon EKS( 云上托管的 K8S 集群 ) Amazon EKS 对 K8S 网络的创新 K8S Service 与 AWS 的集成 Amazon EKS 与 AWS 安全解决方案整合 Amazon EKS 日志及监控 Demo 演示
监控
日志 CloudWatch Logs CloudTrail Kube API server, controller, scheduler logs in CloudWatch EKS API calls logged to CloudTrail /var/log/kube-apiserver.log - API Server, responsible for serving the API /var/log/kube-scheduler.log - Scheduler, responsible for making scheduling decisions /var/log/kube-controller-manager.log - Controller that manages replication controllers
议程 AWS 容器技术介绍 Amazon EKS( 云上托管的 K8S 集群 ) Amazon EKS 对 K8S 网络的创新 Amazon EKS 与 AWS 安全解决方案整合 Amazon EKS 日志及监控 Demo 演示