Amazon web services AWS EKS,如何从浏览器直接点击Pod?

Amazon web services AWS EKS,如何从浏览器直接点击Pod?,amazon-web-services,docker,kubernetes,Amazon Web Services,Docker,Kubernetes,我对库伯内特斯很陌生。上周我学习了节点、吊舱、集群、服务和部署 有了这些,我试图对库伯内特斯的网络是如何工作的有更多的了解。我只想公开一个简单的nginx docker网页,并从我的浏览器中点击它 我们的VPC设置为直接连接,因此我能够访问EC2实例的私有IP地址。我还使用aws上的UI将EKS集群设置为private。出于测试目的,我添加了所有TCP上允许的cidr范围,作为EKS群集UI中的附加安全组 以下是我的基本服务和部署定义: apiVersion: v1 kind: Service

我对库伯内特斯很陌生。上周我学习了节点、吊舱、集群、服务和部署

有了这些,我试图对库伯内特斯的网络是如何工作的有更多的了解。我只想公开一个简单的nginx docker网页,并从我的浏览器中点击它

我们的VPC设置为直接连接,因此我能够访问EC2实例的私有IP地址。我还使用aws上的UI将EKS集群设置为
private
。出于测试目的,我添加了所有TCP上允许的cidr范围,作为EKS群集UI中的附加安全组

以下是我的基本服务和部署定义:

apiVersion: v1
kind: Service
metadata:
  name: testing-nodeport
  namespace: default
  labels:
    infrastructure: fargate
    app: testing-app
spec:
  type: NodePort
  selector:
    app: testing-app
  ports:
    - port: 80
      targetPort: testing-port
      protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: testing-deployment
  namespace: default
  labels:
    infrastructure: fargate
    app: testing-app
spec:
  replicas: 1
  selector:
    matchLabels:
      infrastructure: fargate
      app: testing-app
  template:
    metadata:
      labels:
        infrastructure: fargate
        app: testing-app
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - name: testing-port
          containerPort: 80
当我运行时,我可以看到一切都正常运行:

kubectl获取全部-n默认值

但是,当我尝试点击端口
80
上的NodePort IP地址时,我无法从浏览器加载它

如果我首先在以下url设置
kubectl代理
(代理在端口
8001
上启动),我可以点击pod:


在这一点上我几乎迷路了。我不知道我做错了什么,也不知道为什么我不能在kubectl代理命令之外使用基本的nginx docker。

想象一下kubernetes集群就像您的AWS VPC。它有自己的内部网络和专用IP,并连接所有POD。Kubernetes只公开你明确要求公开的东西

服务端口80在群集中可用。因此,一个pod可以使用
服务名称:服务端口
与该服务进行通信。但如果您需要从外部访问,则需要
ingress controller/LoadBalancer
。您还可以使用
NodePort
进行测试。节点端口将大于30000(在30000-32767范围内)

您应该能够使用
节点IP:nodeport
访问nginx。在这里,我假设安全组正在打开节点端口


使用这个yaml。我将节点端口更新为31000。您可以在
节点端口:31000
上访问nginx。正如我提到的,您不能使用80,因为它是用于集群内的。如果需要使用80,则需要入口控制器

apiVersion: v1
kind: Service
metadata:
  name: testing-nodeport
  namespace: default
  labels:
    infrastructure: fargate
    app: testing-app
spec:
  type: NodePort
  selector:
    app: testing-app
  ports:
    - port: 80
      targetPort: testing-port
      protocol: TCP
      nodePort: 31000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: testing-deployment
  namespace: default
  labels:
    infrastructure: fargate
    app: testing-app
spec:
  replicas: 1
  selector:
    matchLabels:
      infrastructure: fargate
      app: testing-app
  template:
    metadata:
      labels:
        infrastructure: fargate
        app: testing-app
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - name: testing-port
          containerPort: 80

如果使用
代理
选项怎么办?大概是这样的:

kubectl端口转发-n默认服务/测试节点端口3000:80
转发自127.0.0.1:3000->80
转发自[::1]:3000->80

之后,您可以从
localhost:3000
访问K8S服务。更多信息

好的,经过16个多小时的调试,我终于明白了到底发生了什么。在fargate上,您不能像使用托管节点组那样设置每个节点的安全组。我正在“其他安全组”设置中设置安全组规则。但是,fargate显然完全忽略这些设置,只使用“群集安全组”设置中的安全组。因此,在EKS UI中,我在“集群安全组”中设置了正确的规则,现在我可以直接在fargate实例上点击我的pod


这件事给我们带来了很大的好处。仅对fargate节点使用“群集安全组”。

感谢您的响应!kubectl代理命令将自动为您执行此操作。是的,我已经试过了,但我希望其他人能够直接使用ip地址和端口。我想这就是我在上面所做的?我在上面定义的NodePort服务类型上有错吗?因为我没有在服务定义中定义节点端口,所以它会自动选择一个。我允许端口0-65535作为EKS UI中的附加安全组连接到我的网络cidr。我是否应该添加其他位置,或者我在yaml文件中的定义不正确?@wesleywh,您创建的服务节点端口是正确的。但您希望nginx在集群外的端口80可用。这是不可能的。Kubernetes将在给定范围内分配一些随机端口(如果您没有明确要求)。这就是你必须使用的访问。不是端口80。运行此命令
kubectl get all
-检查端口是否大于30000。使用它来访问你的应用程序。太棒了!谢谢你的详细回答。我将尝试一下,看看效果如何。好的,我使用了您的yaml并运行了
kubectl apply-f testing.yaml
(将其保存到
testing.yaml
)。输出表示服务已更新,部署保持不变。1.我运行了
kubectl-get-all
,获得了IP和端口(来自NodePort服务),并尝试了一下,但没有成功。2.我运行了一个
kubectl descripe
,从那里获得了带有端口的IP地址,但仍然没有运气。3.最后从服务
kubectl description服务测试节点port
获得了endpont,但这也不起作用。所有内容的最终输出。。。以上所有尝试都会导致连接超时。如果是,我不会尝试端口80,而是尝试yaml文件中定义的端口31000。
apiVersion: v1
kind: Service
metadata:
  name: testing-nodeport
  namespace: default
  labels:
    infrastructure: fargate
    app: testing-app
spec:
  type: NodePort
  selector:
    app: testing-app
  ports:
    - port: 80
      targetPort: testing-port
      protocol: TCP
      nodePort: 31000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: testing-deployment
  namespace: default
  labels:
    infrastructure: fargate
    app: testing-app
spec:
  replicas: 1
  selector:
    matchLabels:
      infrastructure: fargate
      app: testing-app
  template:
    metadata:
      labels:
        infrastructure: fargate
        app: testing-app
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - name: testing-port
          containerPort: 80