I am running Docker Desktop on Windows 10
- I have Docket Kubernetes enabled
- In my Kubernetes cluster I have a single pod with a single container running Postgres, built with kubectl apply (call this
inside
) - Outside of Kubernetes, I have another container running postgres built with docker desktop (call this
outside
).
I want to be able to connect to the outside
postgres server by name from the inside
to the outside
My kubernetes yaml configuration that builds inside
:
apiVersion: v1
kind: PersistentVolume # Create a PV
metadata:
name: postgresql-data # Sets PV's name
labels:
type: local # Sets PV's type to local
spec:
storageClassName: manual
capacity:
storage: 10Gi # Sets PV Volume
accessModes:
- ReadWriteOnce
hostPath: #/run/desktop/mnt/host/c points to c:\ on the windows host
path: "/run/desktop/mnt/host/c/mk/data/volume" # Sets the volume's path
---
apiVersion: v1
kind: PersistentVolumeClaim # Create PVC
metadata:
name: postgresql-data-claim # Sets name of PV
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce # Sets read and write access
resources:
requests:
storage: 10Gi # Sets volume size
---
apiVersion: v1
kind: ConfigMap
metadata:
name: postgres-secret
labels:
app: postgres
data:
POSTGRES_DB: appdb
POSTGRES_USER: user
POSTGRES_PASSWORD: password
---
apiVersion: apps/v1
kind: Deployment # Create a deployment
metadata:
name: postgres # Set the name of the deployment
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:12.10 # Docker image
imagePullPolicy: "IfNotPresent"
ports:
- containerPort: 5432 # Exposing the container port 5432 for PostgreSQL client connections.
envFrom:
- configMapRef:
name: postgres-secret # Using the ConfigMap postgres-secret
volumeMounts:
- mountPath: /var/lib/postgresql/data
name: postgresdata
volumes:
- name: postgresdata
persistentVolumeClaim:
claimName: postgresql-data-claim
---
apiVersion: v1
kind: Service # Create service
metadata:
name: postgres # Sets the service name
labels:
app: postgres # Defines app to create service for
spec:
type: NodePort # Sets the service type
ports:
- protocol: TCP # Sets the port to run the postgres application
port: 5432
targetPort: 5432
nodePort: 31225
selector:
app: postgres
My docker-compose that builds outside
version: '3.6'
services:
client_portal:
image: postgres:13.3
ports:
- 5435:5432
environment:
POSTGRES_PASSWORD: appdb
POSTGRES_USER: user
POSTGRES_DB: password
From the windows host, I am able to access inside
at localhost:5432
. Outside
is accessed at localhost:31225
From inside
I can access outside by it's ip address. I got the ip address by running hostname -i on
outside`'s console (172.18.0.3) .
This works (on inside
):
psql postgresql://user:[email protected]:5432/appdb
I want to address it by name (not ip) so that when I share this with someone else or the ip address of outside changes, my config does not change.
CodePudding user response:
You can check the node object in your cluster to see what is the hostname
for this node. It should probably be docker-desktop
. You can check this as:
kubectl get node docker-desktop -oyaml
This should provide an output similar to the following:
...
status:
addresses:
- address: 192.168.65.4
type: InternalIP
- address: docker-desktop
type: Hostname
...
I believe in your case the InternalIP
address is 172.18.0.3
. It is the one you can use to address the node
from inside the cluster.
Assuming that when you provide this setup to someone else, and they will also use docker desktop, the hostname should be docker-desktop
.
You can simply use the following command from inside
, safely:
psql postgresql://user:password@docker-desktop:5432/appdb
Just make sure docker-desktop
is the hostname
for the node, as checked earlier.