Cannot connect to AWS RDS PostgreSQL with Golang using Helm Charts

12/13/2018

I am unable to connect to AWS RDS PostgeSQL when running Helm Chart for a Go App (GORM). All credentials are stored in a kubernetes secret, and the secret is being used in the helm chart.

Few Points:

  • Able to connect locally just fine.
  • The PostgreSQL database is already created in RDS, and made sure that the kubernetes secret as matches with the same creds.
  • Docker image is pushed and pulled from Gitlab without any errors.
  • Command "helm ls" displays the deployment status as "DEPLOYED"
  • When taking "kubectl get pod", I get STATUS as "CrashLoopBackoff"
  • When taking "kubectl describe pod", I get back MESSAGE "Back-off restarting failed container"
  • I then take "kubectl logs pod_name" to track the error, and get back the following:
    1. failed to connect to the database
    2. dial tcp 127.0.0.1:5432: connect: connection refused (Not sure why it is still specifying "127.0.0.1" when i have the secret mounted)
  • Unable to exec into the pod because it is not running.

I have tried:

  • Secure a connection in the same cluster from a different pod using psql to ensure that the creds in the secret are in sync with what was set up in RDS PostgreSQL
  • Changing the api from DB_HOST, to host=%s
  • Tried connecting using fmt.Sprintf, as well as os.Getenv

"Versions"

GO version:

go1.11.1 darwin/amd64

DOCKER version:

Client: Version: 18.06.1-ce API version: 1.38

API.GO (file)

package controllers

import (
    "fmt"
    "log"
    "net/http"
    "os"
    "time"

    "github.com/gorilla/mux"
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
    _ "gitlab.torq.trans.apps.ge.com/503081542/torq-auth-api/models"
)
var err error
type API struct {
    Database *gorm.DB
    Router   *mux.Router
}

func (api *API) Initialize(opts string) {

    // Initialize DB
    dbinfo := os.Getenv("DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, 
DB_PORT sslmode=disable")
    // dbinfo := os.Getenv("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable")
    api.Database, err = gorm.Open("postgres", dbinfo)

    if err != nil {
        log.Print("failed to connect to the database")
        log.Fatal(err)
    }

    // Bind to a port and pass our router in
    // log.Fatal(http.ListenAndServe(":8000", handlers.CORS() . 
  (api.Router)))

    fmt.Println("Connection established")
    log.Printf("Postgres started at %s PORT", config.DB_PORT)
    // MODELS
    type application struct {
        ID        string    `json:"id" gorm:"primary_key"`
        CreatedAt time.Time `json:"-"`
        UpdatedAt time.Time `json:"-"`
        Name      string    `json:"name"`
        Ci        string    `json:"ci"`
    }
    type Cloud struct {
        ID   string `json:"id" gorm:"primary_key"`
        Name string `json:"name"`
    }
    fmt.Println("Tables are created")
    // Enable this line for DB logging
    api.Database.LogMode(true)}

// Initialize Router
    api.Router = mux.NewRouter()
    api.Router.HandleFunc("/api/v1/applications", 
api.HandleApplications)
    api.Router.HandleFunc("/api/v1/application/{id}", 
api.HandleApplication)
    api.Router.HandleFunc("/api/v1/clusters", api.handleClusters)
}

I am not exactly sure where the issue could be here, this is a learning experience for myself. Any ideas would be appreciated.

Thanks in advance!

-- Khaled Aman
go
kubernetes-helm
postgresql
rds

1 Answer

12/13/2018

I think your database initialization code is not doing what you want. Try something like this

var driver = "postgres"
var name = os.Getenv("DB_NAME")
var host = os.Getenv("DB_HOST")
var user = os.Getenv("DB_USER")
var pass = os.Getenv("DB_PASSWORD")
var conn = fmt.Sprintf("host=%v user=%v password=%v dbname=%v sslmode=disable", host, user, pass, name)
api.Database, err := gorm.Open(driver, conn)
-- Mad Wombat
Source: StackOverflow