Kubernetes NodePort Service: 访问特定节点上的 Pod
要将访问 "node1" 节点的 8080 端口转发到该节点上的 Pod,可以使用 Kubernetes 的 Service 资源将这三个 Pod 绑定到一个 Service 上,并将该 Service 的类型设置为 NodePort。\n\n下面是一个示例的 Service 配置文件:\n\nyaml\napiVersion: v1\nkind: Service\nmetadata:\n name: my-service\nspec:\n type: NodePort\n selector:\n app: my-app\n ports:\n - port: 8080\n targetPort: 8080\n nodePort: 30080\n\n\n解释一下这个配置文件的各个部分:\n\n- metadata.name:Service 的名称。\n- spec.type:Service 的类型,这里设置为 NodePort。\n- spec.selector:选择要绑定到该 Service 的 Pod,这里使用了一个标签选择器 app: my-app,你需要根据你的 Pod 的标签来修改这里的选择器。\n- spec.ports:定义 Service 暴露的端口,这里将 8080 端口映射到 Pod 的 8080 端口,并设置了一个 NodePort,这里设置为 30080,你可以根据需要修改。\n\n然后,使用 kubectl 命令创建该 Service:\n\n\nkubectl create -f service.yaml\n\n\n这样,你就可以通过访问 node1 节点的 30080 端口来访问该节点上的 Pod,Kubernetes 会自动将流量转发到对应的 Pod。
原文地址: http://www.cveoy.top/t/topic/ps8j 著作权归作者所有。请勿转载和采集!