Kubernetes : Golang application container does not start

7/27/2020

I have simple golang application which talks to postgres database. The posrgres container is working as expected However my golang application does not start. In my config.go the environment variables is specified as

type Config struct {
	Port        uint   `env:"PORT" envDefault:"8000"`
	PostgresURL string `env:"POSTGRES_URL" envDefault:"postgres://user:pass@127.0.0.1/simple-service"`
}

My golang application service and deployment yaml looks like:

apiVersion: v1
kind: Service
metadata:
  name: simple-service-webapp-service
  labels:
    app: simple-service-webapp
spec:
  ports:
  - port: 8080
    name: http
  selector:
    app: simple-service-webapp
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: simple-service-webapp-v1
  labels:
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: simple-service-webapp
      version: v1
  template:
    metadata:
      labels:
        app: simple-service-webapp
        version: v1
    spec:
      containers:
      - name: simple-service-webapp
        image: docker.io/225517/simple-service-webapp:v1
        resources:
          requests:
            cpu: 100m
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
        env:
          - name: POSTGRES_HOST
            value: postgresdb
          - name: POSTGRES_PORT
            value: "5432"
          - name: POSTGRES_DB
            value: simple-service 
          - name: POSTGRES_USER
            value: user
          - name: POSTGRES_PASSWORD
            value: pass
        readinessProbe:
            httpGet:
                path: /live
                port: 8080
---

Below is the service and deployment yaml file for postgres service

apiVersion: v1
kind: Service
metadata:
  name: postgresdb
  labels:
    app: postgresdb
spec:
  ports:
  - port: 5432
    name: tcp
  selector:
    app: postgresdb
  type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgresdb-v1
  labels:
    app: postgresdb
    version: v1
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgresdb
      version: v1
  template:
    metadata:
      labels:
        app: postgresdb
        version: v1
    spec:
      containers:
      - name: postgresdb
        image: postgres
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 5432
        env:
          - name: POSTGRES_DB
            value: simple-service 
          - name: POSTGRES_USER
            value: user
          - name: POSTGRES_PASSWORD
            value: pass
        readinessProbe:
          exec:
            command: ["psql", "-W", "pass", "-U", "user", "-d", "simple-service", "-c", "SELECT 1"]
          initialDelaySeconds: 15
          timeoutSeconds: 2
        livenessProbe:
          exec:
            command: ["psql", "-W", "pass", "-U", "user", "-d", "simple-service", "-c", "SELECT 1"]
          initialDelaySeconds: 45
          timeoutSeconds: 2             
---

Do I need to change anything in config.go? Could you please help thanks!

-- Sweta Sharma
docker
go
kubernetes

1 Answer

8/3/2020

This is a community wiki answer based on the info from the comments, feel free to expand on it. I'm placing this for better visibility for the community.

Question was solved by setting appropriate postgres DB port (5432) and rewriting config so it look for the correct variable set on the deployment.

-- acid_fuji
Source: StackOverflow