EKS Fargate pods无法从互联网访问。

huangapple go评论87阅读模式
英文:

EKS Fargate pods unreachable from internet

问题

我正在尝试创建EKS Fargate集群并部署一个具有1个端点的示例Spring Boot应用程序,我成功地使用以下CloudFormation脚本创建了堆栈:

  1. ---
  2. AWSTemplateFormatVersion: '2010-09-09'
  3. Description: 'AWS CloudFormation template for EKS Fargate managed Kubernetes cluster with exposed endpoints'
  4. # ... (此处省略了一些内容) ...

我运行以下命令来为Fargate打补丁CoreDNS:

  1. kubectl patch deployment coredns \
  2. -n kube-system \
  3. --type json \
  4. -p='[{"op": "remove", "path": "/spec/template/metadata/annotations/eks.amazonaws.com~1compute-type"}]'

然后,我使用以下Kubernetes清单从公共ECR部署我的示例应用程序映像:

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: example-app
  6. # ... (此处省略了一些内容) ...

然后,当我运行:

  1. kubectl get svc

我看到以下结果:

  1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  2. example-service LoadBalancer 172.20.228.77 aa0116829ac2647a7bf39a97bffb0183-1208408433.eu-central-1.elb.amazonaws.com 80:31915/TCP 16m
  3. kubernetes ClusterIP 172.20.0.1 <none> 443/TCP 29m

但是,当我尝试访问LoadBalancer example-service的EXTERNAL-IP时,我收到空响应,我无法访问我的应用程序仅在Spring Boot应用程序中定义的路径:/api/v1/info

  1. server.port=8080
  2. server.servlet.context-path=/api/v1

我漏掉了什么?

一些信息:

  • 我的Pod成功启动,当我运行kubectl logs pod-name时,我可以看到Spring Boot的日志记录。
  • 我的CoreDNS Pod也正常启动。
  • 我使用busybox来测试我的集群的DNS,似乎一切正常。

你可能需要检查以下几点来解决问题:

  1. 确保LoadBalancer example-service的EXTERNAL-IP已经分配,并且状态正常。
  2. 检查Security Group和Network ACL规则,确保它们允许流量通过LoadBalancer到达Pod。
  3. 确保你的Spring Boot应用程序在端口8080上正在监听,并且路径"/api/v1/info"已正确设置。
  4. 检查CoreDNS是否正确配置,确保DNS解析正常工作。
  5. 如果你有网络策略(Network Policies)或其他Kubernetes网络配置,请确保它们没有阻止流量到达你的应用程序。

希望这些提示对你有所帮助,帮助你解决问题。

英文:

I am trying to create EKS Fargate cluster and deploy example Spring Boot application with 1 endpoint, I successfully create stack with following CloudFormation script:

  1. ---
  2. AWSTemplateFormatVersion: &#39;2010-09-09&#39;
  3. Description: &#39;AWS CloudFormation template for EKS Fargate managed Kubernetes cluster with exposed endpoints&#39;
  4. Resources:
  5. VPC:
  6. Type: AWS::EC2::VPC
  7. Properties:
  8. CidrBlock: 10.0.0.0/16
  9. EnableDnsSupport: true
  10. EnableDnsHostnames: true
  11. InternetGateway:
  12. Type: AWS::EC2::InternetGateway
  13. VPCGatewayAttachment:
  14. Type: AWS::EC2::VPCGatewayAttachment
  15. Properties:
  16. VpcId: !Ref VPC
  17. InternetGatewayId: !Ref InternetGateway
  18. PublicSubnet:
  19. Type: AWS::EC2::Subnet
  20. Properties:
  21. VpcId: !Ref VPC
  22. CidrBlock: 10.0.2.0/24
  23. MapPublicIpOnLaunch: true
  24. AvailabilityZone: !Select [ 0, !GetAZs &#39;&#39; ]
  25. PrivateSubnetA:
  26. Type: AWS::EC2::Subnet
  27. Properties:
  28. VpcId: !Ref VPC
  29. CidrBlock: 10.0.0.0/24
  30. AvailabilityZone: !Select [ 0, !GetAZs &#39;&#39; ]
  31. PrivateSubnetB:
  32. Type: AWS::EC2::Subnet
  33. Properties:
  34. VpcId: !Ref VPC
  35. CidrBlock: 10.0.1.0/24
  36. AvailabilityZone: !Select [ 1, !GetAZs &#39;&#39; ]
  37. PublicRouteTable:
  38. Type: AWS::EC2::RouteTable
  39. Properties:
  40. VpcId: !Ref VPC
  41. PublicRoute:
  42. Type: AWS::EC2::Route
  43. Properties:
  44. RouteTableId: !Ref PublicRouteTable
  45. DestinationCidrBlock: 0.0.0.0/0
  46. GatewayId: !Ref InternetGateway
  47. SubnetRouteTableAssociationA:
  48. Type: AWS::EC2::SubnetRouteTableAssociation
  49. Properties:
  50. SubnetId: !Ref PublicSubnet
  51. RouteTableId: !Ref PublicRouteTable
  52. EIP:
  53. Type: AWS::EC2::EIP
  54. NatGateway:
  55. Type: AWS::EC2::NatGateway
  56. Properties:
  57. SubnetId: !Ref PublicSubnet
  58. AllocationId: !GetAtt EIP.AllocationId
  59. PrivateRouteTable:
  60. Type: AWS::EC2::RouteTable
  61. Properties:
  62. VpcId: !Ref VPC
  63. PrivateRoute:
  64. Type: AWS::EC2::Route
  65. Properties:
  66. RouteTableId: !Ref PrivateRouteTable
  67. DestinationCidrBlock: 0.0.0.0/0
  68. NatGatewayId: !Ref NatGateway
  69. PrivateSubnetRouteTableAssociationA:
  70. Type: AWS::EC2::SubnetRouteTableAssociation
  71. Properties:
  72. SubnetId: !Ref PrivateSubnetA
  73. RouteTableId: !Ref PrivateRouteTable
  74. PrivateSubnetRouteTableAssociationB:
  75. Type: AWS::EC2::SubnetRouteTableAssociation
  76. Properties:
  77. SubnetId: !Ref PrivateSubnetB
  78. RouteTableId: !Ref PrivateRouteTable
  79. EKSCluster:
  80. Type: AWS::EKS::Cluster
  81. Properties:
  82. Name: EKSFargateCluster
  83. Version: &#39;1.26&#39;
  84. ResourcesVpcConfig:
  85. SubnetIds:
  86. - !Ref PrivateSubnetA
  87. - !Ref PrivateSubnetB
  88. RoleArn: !GetAtt EKSClusterRole.Arn
  89. FargateProfile:
  90. Type: AWS::EKS::FargateProfile
  91. Properties:
  92. ClusterName: !Ref EKSCluster
  93. FargateProfileName: FargateProfile
  94. PodExecutionRoleArn: !GetAtt FargatePodExecutionRole.Arn
  95. Selectors:
  96. - Namespace: default
  97. Subnets:
  98. - !Ref PrivateSubnetA
  99. - !Ref PrivateSubnetB
  100. FargateProfileCoredns:
  101. Type: AWS::EKS::FargateProfile
  102. Properties:
  103. ClusterName: !Ref EKSCluster
  104. FargateProfileName: CorednsProfile
  105. PodExecutionRoleArn: !GetAtt FargatePodExecutionRole.Arn
  106. Selectors:
  107. - Namespace: kube-system
  108. Labels:
  109. - Key: k8s-app
  110. Value: kube-dns
  111. Subnets:
  112. - !Ref PrivateSubnetA
  113. - !Ref PrivateSubnetB
  114. FargatePodExecutionRole:
  115. Type: AWS::IAM::Role
  116. Properties:
  117. AssumeRolePolicyDocument:
  118. Version: &#39;2012-10-17&#39;
  119. Statement:
  120. - Effect: Allow
  121. Principal:
  122. Service:
  123. - eks-fargate-pods.amazonaws.com
  124. Action:
  125. - sts:AssumeRole
  126. ManagedPolicyArns:
  127. - arn:aws:iam::aws:policy/AmazonEKSFargatePodExecutionRolePolicy
  128. EKSClusterRole:
  129. Type: AWS::IAM::Role
  130. Properties:
  131. AssumeRolePolicyDocument:
  132. Version: &#39;2012-10-17&#39;
  133. Statement:
  134. - Effect: Allow
  135. Principal:
  136. Service:
  137. - eks.amazonaws.com
  138. Action:
  139. - sts:AssumeRole
  140. ManagedPolicyArns:
  141. - arn:aws:iam::aws:policy/AmazonEKSClusterPolicy
  142. - arn:aws:iam::aws:policy/AmazonEKSVPCResourceController

I run following command to path the CoreDNS for Fargate:

  1. kubectl patch deployment coredns \
  2. -n kube-system \
  3. --type json \
  4. -p=&#39;[{&quot;op&quot;: &quot;remove&quot;, &quot;path&quot;: &quot;/spec/template/metadata/annotations/eks.amazonaws.com~1compute-type&quot;}]&#39;

Then I deploy my example application image from public ECR with following kubernetes manifest:

  1. ---
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: example-app
  6. spec:
  7. replicas: 2
  8. selector:
  9. matchLabels:
  10. app: example-app
  11. template:
  12. metadata:
  13. labels:
  14. app: example-app
  15. spec:
  16. containers:
  17. - name: ventu
  18. image: public.ecr.aws/not_real_url/public_ecr_name:latest
  19. ports:
  20. - containerPort: 8080
  21. ---
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25. name: example-service
  26. spec:
  27. type: LoadBalancer
  28. selector:
  29. app: example-app
  30. ports:
  31. - protocol: TCP
  32. port: 80
  33. targetPort: 8080

Then when I run:

  1. kubectl get svc

I see result:

  1. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  2. example-service LoadBalancer 172.20.228.77 aa0116829ac2647a7bf39a97bffb0183-1208408433.eu-central-1.elb.amazonaws.com 80:31915/TCP 16m
  3. kubernetes ClusterIP 172.20.0.1 &lt;none&gt; 443/TCP 29m

However when I try to reach the EXTERNAL-IP on my LoadBalancer example-service, I get empty response, I can't reach my application on only path defined in my Spring Boot application: /api/v1/info

  1. server.port=8080
  2. server.servlet.context-path=/api/v1

What am I missing?

Couple of information:

  • my pods spin up successfully, I can see Spring Boot logging when I run kubectl logs pod-name
  • my coredns pods spin up correctly as well
  • I use busybox to test my cluster's dns, and everything seems to be working too

答案1

得分: 2

I solved my issue, by following this guide

然后,我将生成的堆栈导出到我的CloudFormation脚本中。

然后,为了部署我的应用程序,我更新了我的Kubernetes清单如下:


apiVersion: v1
kind: Namespace
metadata:
name: example

apiVersion: apps/v1
kind: Deployment
metadata:
namespace: example
name: deployment-example-be-app
spec:
selector:
matchLabels:
app.kubernetes.io/name: example-be-app
replicas: 2
template:
metadata:
labels:
app.kubernetes.io/name: example-be-app
spec:
containers:
- name: example-be-app
image: public.ecr.aws/fake_url/example:latest
imagePullPolicy: Always
ports:
- containerPort: 8080

apiVersion: v1
kind: Service
metadata:
namespace: example
name: service-example-be-app
annotations:
service.beta.kubernetes.io/aws-load-balancer-nlb-target-type: ip
service.beta.kubernetes.io/aws-load-balancer-type: external
service.beta.kubernetes.io/aws-load-balancer-scheme: internet-facing
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
protocol: TCP
selector:
app.kubernetes.io/name: example-be-app

现在我可以通过浏览器访问我的示例应用程序。

英文:

I solved my issue, by following this guide

I then exported resulting stack into my CloudFormation script.

Then to deploy my application I updated my kubernetes manifest to:

  1. ---
  2. apiVersion: v1
  3. kind: Namespace
  4. metadata:
  5. name: example
  6. ---
  7. apiVersion: apps/v1
  8. kind: Deployment
  9. metadata:
  10. namespace: example
  11. name: deployment-example-be-app
  12. spec:
  13. selector:
  14. matchLabels:
  15. app.kubernetes.io/name: example-be-app
  16. replicas: 2
  17. template:
  18. metadata:
  19. labels:
  20. app.kubernetes.io/name: example-be-app
  21. spec:
  22. containers:
  23. - name: example-be-app
  24. image: public.ecr.aws/fake_url/example:latest
  25. imagePullPolicy: Always
  26. ports:
  27. - containerPort: 8080
  28. ---
  29. apiVersion: v1
  30. kind: Service
  31. metadata:
  32. namespace: example
  33. name: service-example-be-app
  34. annotations:
  35. service.beta.kubernetes.io/aws-load-balancer-nlb-target-type: ip
  36. service.beta.kubernetes.io/aws-load-balancer-type: external
  37. service.beta.kubernetes.io/aws-load-balancer-scheme: internet-facing
  38. spec:
  39. type: LoadBalancer
  40. ports:
  41. - port: 80
  42. targetPort: 8080
  43. protocol: TCP
  44. selector:
  45. app.kubernetes.io/name: example-be-app

Now I access my example application form browser.

huangapple
  • 本文由 发表于 2023年5月6日 18:22:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定