Access services running on the same host as the k3s Kubernetes cluster - Stack Overflow

admin2025-04-15  0

I have a virtual machine with the following setup:

  1. a proxy which redirects requests from localhost:5432 to a postgres server (I can't change this setup)
  2. k3s installed as single-node cluster

How can I access the localhost:5432 service from a pod in k3s?

I have a virtual machine with the following setup:

  1. a proxy which redirects requests from localhost:5432 to a postgres server (I can't change this setup)
  2. k3s installed as single-node cluster

How can I access the localhost:5432 service from a pod in k3s?

Share Improve this question asked Feb 4 at 14:00 harihari 3044 silver badges9 bronze badges 3
  • A Kubernetes cluster typically has multiple nodes; which node(s) is (are) the service running on? (Can you move the database server into an in-cluster StatefulSet?) – David Maze Commented Feb 4 at 15:29
  • Clean way to connect to services running on the same host as the Kubernetes cluster seems to cover similar space, does the ExternalName Service setup there work for you? If the proxy is hard-wired to localhost then it must run in the same network namespace (Docker container, Kubernetes Pod) as the database, and that might mean you can't run the proxy in a container in this setup. – David Maze Commented Feb 4 at 15:32
  • @DavidMaze Response to your first comment: It is a single-node setup and deploying the PostgreSQL server inside the cluster is not an option :(. Response to your second comment: it is similar but not the same. In that case, the service was accessible via the host IP. In my case, it is only listening on localhost. – hari Commented Feb 4 at 16:07
Add a comment  | 

1 Answer 1

Reset to default 0

I solved by setting

hostNetwork: true
dnsPolicy: ClusterFirstWithHostNet

in the Pod/Deployment specification. While this approach works, it's not ideal due to potential security risks. However, it's currently the only solution I've found.

转载请注明原文地址:http://www.anycun.com/QandA/1744714663a86608.html