0

I'm working on a school project where I have bare metal VPS and I'm trying to deploy web application (nginx ingress) with Postgres database (which has to be reachable from the outside I.e. :5432). I went through tens of link in Google and on StackOverflow and nothing actually worked out - I'm still getting Connection refused. Is the server running on host "<>" and accepting TCP/IP connections on port 5432?

I've successfully deployed MetalLB with few simple steps:

  1. Install metallb
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.5/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.5/manifests/metallb.yaml
kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"

kubectl get configmap kube-proxy -n kube-system -o yaml | \
sed -e "s/strictARP: false/strictARP: true/" | \
kubectl apply -f - -n kube-system
  1. Created config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: production
      protocol: layer2
      addresses:
      - <VPS-external-IP>-<VPS-external-IP>
  1. Created LoadBalancer Service
apiVersion: v1
kind: Service
metadata:
  name: nginx-balancer
  namespace: nginx
spec:
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  - name: https
    port: 443
    protocol: TCP
    targetPort: 443
  - name: db
    port: 5432
    protocol: TCP
    targetPort: 5432
  selector:
    app: nginx
  type: LoadBalancer

  1. And created Nginx deployment (nginx-deployment.yaml)
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: nginx
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        - containerPort: 443

Now after runnign kubectl apply -f config.yaml and kubectl apply -f nginx-deployment.yaml I'm able to resolve "Welcome to nginx!" curl <VPS-external-IP>:80 with output of:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
kubectl get pods 
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7f8d9cf649-cssrb   1/1     Running   0          117m
nginx-deployment-7f8d9cf649-f7q9w   1/1     Running   0          117m
nginx-deployment-7f8d9cf649-fmntb   1/1     Running   0          117m

kubectl get svc -n nginx
NAME             TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)                                     AGE
nginx            ClusterIP      10.108.97.186   <none>         80/TCP,443/TCP                              26m
nginx-balancer   LoadBalancer   10.103.72.75    46.36.38.200   80:32529/TCP,443:30432/TCP,5432:31081/TCP   42m

kubectl get pods -n metallb-system
NAME                          READY   STATUS    RESTARTS   AGE
controller-57c458c998-r78wn   1/1     Running   0          13h
speaker-clr6j                 1/1     Running   0          13h

kubectl get configmap -n metallb-system
NAME               DATA   AGE
config             1      13h
kube-root-ca.crt   1      13h

But the real issue comes now. I need to access Postgres Database using :5432 but I'm honestly just lost in Kubernetes...

I've created all the neccessary stuff for Postgres in order to make it run such as:

  1. PersistentVolume and PersistentVolumeClaim (I don't think it's neccessary to share here, since it's just basic PersistentVolume and PersistentVolumeClaim yaml)
  2. Secrets (I don't think it's neccessary to share here, since it's just basic secrets yaml)
  3. ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-configmap
  namespace: postgres
data:
  POSTGRES_DB: db_production
  1. Postgres Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-deployment
  namespace: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres  
    spec:
      containers:
      - name: postgres
        image: postgres:latest
        ports:
          - containerPort: 5432
        envFrom:
          - secretRef:
              name: postgres-secrets
          - configMapRef:
              name: postgres-configmap  
        volumeMounts:
        - name: postgres-pv-claim
          mountPath: /var/lib/pgsql/data
      volumes:
      - name: postgres-pv-claim
        persistentVolumeClaim:
          claimName: postgres-pv-claim
  1. And of course - Postgres Service
apiVersion: v1
kind: Service
metadata:
  name: db
  namespace: postgres
  labels:
    run: postgres
spec:
  selector:
    name: postgres
  ports:
  - port: 5432
    targetPort: 5432
    protocol: TCP

The postgres by itself seems to work.

kubectl get pods -n postgres
NAME                                   READY   STATUS    RESTARTS   AGE
postgres-deployment-74fff7c576-6kb5q   1/1     Running   0          96m

kubectl get svc -n postgres
NAME   TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
db     ClusterIP   10.102.5.192   <none>        5432/TCP   93m

kubectl get deployments -n postgres
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
postgres-deployment   1/1     1            1           94m

kubectl get pv -n postgres
NAME          CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                        STORAGECLASS   REASON   AGE
postgres-pv   1Gi        RWO            Retain           Bound    postgres/postgres-pv-claim   manual                  55m

kubectl get pvc -n postgres
NAME                STATUS   VOLUME        CAPACITY   ACCESS MODES   STORAGECLASS   AGE
postgres-pv-claim   Bound    postgres-pv   1Gi        RWO            manual         55m

(I've deleted and it recreated it many times (hence why so low ages... :-( ))

So even though I've got postgres/db service running on 5432 and MetalLB LoadBalancer with Nginx working, it just simply doesn't expose 5432 to the world. I'll be very happy for all suggestions because I'm starting to loose the hype I had in the beginning about how nice it'd be to set up Kubernetes... :-)

Thank you.

Update 31.1.2022

I've installed Kubernetes using Kubeadm pretty much step by step using several tutorials online and official docs. Output of kubectl version

Client Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.3", GitCommit:"816c97ab8cff8a1c72eccca1026f7820e93e0d25", GitTreeState:"clean", BuildDate:"2022-01-25T21:25:17Z", GoVersion:"go1.17.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.3", GitCommit:"816c97ab8cff8a1c72eccca1026f7820e93e0d25", GitTreeState:"clean", BuildDate:"2022-01-25T21:19:12Z", GoVersion:"go1.17.6", Compiler:"gc", Platform:"linux/amd64"}

and kubeadm version


kubeadm version: &version.Info{Major:"1", Minor:"23", GitVersion:"v1.23.3", GitCommit:"816c97ab8cff8a1c72eccca1026f7820e93e0d25", GitTreeState:"clean", BuildDate:"2022-01-25T21:24:08Z", GoVersion:"go1.17.6", Compiler:"gc", Platform:"linux/amd64"}

Michal Půlpán
  • 123
  • 1
  • 7
  • Hi, can you share more details about the kubernetes cluster? How was the kubernetes installed and what version? Are you using minikube? – Piotr Malec Jan 31 '22 at 17:11

0 Answers0