通过一个IP(最好是Master Node)访问Kubernetes pods/services
Accessing Kubernetes pods/services through one IP (Preferably Master Node)
我有一个带有一个主节点和两个工作节点的本地 Kubernetes 安装。有没有办法通过主节点的 ip 访问所有将安装在 Kubernetes 上的 services/pods?
我的意思是说我在每个工作人员的端口 30001 上都有一个测试服务 运行,我想像 http://master-node:30001 一样访问此服务。感谢您的帮助。
您可以使用"the proxy verb"访问节点,pods,或通过master访问服务。只能代理 HTTP 和 HTTPS。看到这些 docs and these docs.
有一些方法可以做到:
- 定义一个 NodePort Kubernetes 服务
- 使用kubefwd或端口转发命令
- 使用代理命令(仅支持 HTTP 和 HTTPS)
在这个回答中,我解释了如何定义 NodePort 服务。
NodePort服务解释如下(Service - Kubernetes)
NodePort: Exposes the Service on each Node's IP at a static port (the NodePort). A ClusterIP Service, to which the NodePort Service routes, is automatically created. You'll be able to contact the NodePort Service, from outside the cluster, by requesting :.
这是 PostgreSQL 的 NodePort 服务示例:
apiVersion: v1
kind: Service
metadata:
name: postgres
namespace: postgres
labels:
app: postgres
spec:
ports:
- port: 5432
type: NodePort
selector:
app: postgres
port
字段代表服务端口和默认目标端口。还有一个 nodePort
字段,允许您选择从集群外部访问服务的端口(通过节点的 IP 和 nodePort)
要查看节点的端口(如果您没有从清单中指定它),您可以运行命令:
kubectl get services -n postgres
输出应类似于:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
postgres NodePort 10.96.156.75 <none> 5432:30864/TCP 6d9h app=postgres
在这种情况下,nodePort 是 30864
,这是从集群外部访问服务的端口。
要找出节点的IP,要使用的命令是:
kubectl get nodes -o wide
输出应类似于:
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
homedev-control-plane Ready master 30d v1.19.1 172.18.0.2 <none> Ubuntu Groovy Gorilla (development branch) 5.9.1-arch1-1 containerd://1.4.0
如果你只需要IP:
kubectl get nodes -o wide --no-headers | awk '{print }'
本例中节点的IP为172.18.0.2
。因此,要从您的主机连接到本地 Kubernetes 集群中的 Postgres,命令如下所示:
psql -U postgres -h 172.18.0.2 -p 30864-d postgres
我有一个带有一个主节点和两个工作节点的本地 Kubernetes 安装。有没有办法通过主节点的 ip 访问所有将安装在 Kubernetes 上的 services/pods?
我的意思是说我在每个工作人员的端口 30001 上都有一个测试服务 运行,我想像 http://master-node:30001 一样访问此服务。感谢您的帮助。
您可以使用"the proxy verb"访问节点,pods,或通过master访问服务。只能代理 HTTP 和 HTTPS。看到这些 docs and these docs.
有一些方法可以做到:
- 定义一个 NodePort Kubernetes 服务
- 使用kubefwd或端口转发命令
- 使用代理命令(仅支持 HTTP 和 HTTPS)
在这个回答中,我解释了如何定义 NodePort 服务。
NodePort服务解释如下(Service - Kubernetes)
NodePort: Exposes the Service on each Node's IP at a static port (the NodePort). A ClusterIP Service, to which the NodePort Service routes, is automatically created. You'll be able to contact the NodePort Service, from outside the cluster, by requesting :.
这是 PostgreSQL 的 NodePort 服务示例:
apiVersion: v1
kind: Service
metadata:
name: postgres
namespace: postgres
labels:
app: postgres
spec:
ports:
- port: 5432
type: NodePort
selector:
app: postgres
port
字段代表服务端口和默认目标端口。还有一个 nodePort
字段,允许您选择从集群外部访问服务的端口(通过节点的 IP 和 nodePort)
要查看节点的端口(如果您没有从清单中指定它),您可以运行命令:
kubectl get services -n postgres
输出应类似于:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
postgres NodePort 10.96.156.75 <none> 5432:30864/TCP 6d9h app=postgres
在这种情况下,nodePort 是 30864
,这是从集群外部访问服务的端口。
要找出节点的IP,要使用的命令是:
kubectl get nodes -o wide
输出应类似于:
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
homedev-control-plane Ready master 30d v1.19.1 172.18.0.2 <none> Ubuntu Groovy Gorilla (development branch) 5.9.1-arch1-1 containerd://1.4.0
如果你只需要IP:
kubectl get nodes -o wide --no-headers | awk '{print }'
本例中节点的IP为172.18.0.2
。因此,要从您的主机连接到本地 Kubernetes 集群中的 Postgres,命令如下所示:
psql -U postgres -h 172.18.0.2 -p 30864-d postgres