xcoulon / go-url-shortener

Sample URL shortener written in Golang
Apache License 2.0
14 stars 4 forks source link

Unable to run the application in docker and K8s #1

Open sahil-sharma opened 5 years ago

sahil-sharma commented 5 years ago

Hello, I tried to run the application first in docker then in K8s but failed to bring the app in both the scenarios.

Docker use-case OS: Ubuntu:16.04 Docker version: 17.12.1-ce RAM: 4GB

Postgres container:

$ docker run --name some-postgres \
-h mypostgres \
-p 5432:5432 \
-e POSTGRES_DATABASE=url_shortener_db \
-e POSTGRES_USER=user \
-e POSTGRES_PORT=5432 \
-e POSTGRES_HOST=mypostgres \
-e POSTGRES_PASSWORD=mysecretpassword \
-d postgres

URL-Shortner app

$ docker run -d --name url-app \
-p 8080:8080 \
--link some-postgres:mypostgres \
xcoulon/go-url-shortener:0.2.0

URL-Shortner app is unable to connect to the Postgres DB as the DB is listening on localhost:5432. Despite I set the hostname of the DB container and link the DB container to the app container. Logically, it should connect to the DB over the hostname. But, the DB itslef is listening on the localhost:5432 hence connections from the outside won't be accepted. I checked the code and you've hardcoded it as "localhost". Error: db Please advise how can I run the application in docker containers.

Kubernetes use-case OS: Ubuntu:16.04 K8s cluster: 1.12 (Kubeadm single node)

I have all the YAML files. I created secrets at first. Then I created the configmap.yaml. Then created PV-PVC for PostgreSQL and it created successfully.

all

Then created postgres {deployment and service}. It has been created but the volumes didn't mount.

post-desc

There is no change in the YAML files. Can you please look into this?

xcoulon commented 5 years ago

hello @sahil-sharma. For the Docker use-case, you will probably need to set an env variable to the webapp container to define the hostname for the Postgres DB. The localhost value that you mentioned above is a default value, but the actual value is passed by the POSTGRES_HOST env var as defined in https://github.com/xcoulon/go-url-shortener/blob/master/templates/webapp-deploymentconfig.yml#L33-L34. So in your case, that might be some-postgres.

For your k8s use case, I cannot see any error in your screenshot, am I missing something? The last event for the postgres pod is: Started Container

sahil-sharma commented 5 years ago

Thanks for your quick response. Yes, there is no error. That's why I am unable to wrap my head around that what could be the issue. Usually, this is the expected behaviour: dep But, in my case, I am not getting any such message. I think volume is not mounted hence unable to store the data and run the app. Any inputs. Can you try it from your end?

xcoulon commented 5 years ago

@sahil-sharma can you try with the templates from https://github.com/xcoulon/go-url-shortener/commit/3d47f6e4aac800c1374e253de15c4d293d9644a1 ? The master branch contains templates that are aimed at OpenShift deployment (ie, with some OpenShift extensions that are not available in the upstream K8s distro)

sahil-sharma commented 5 years ago

Sure. Let me try. I tried with docker and passing -e POSTGRES_HOST=some-postgres and got this error: ssl

xcoulon commented 5 years ago

Sure. Let me try. I tried with docker and passing -e POSTGRES_HOST=some-postgres and got this error: ssl

ok, I think you're missing the other env vars defined in the template: see your Connecting to Postgres database... log message, which is missing the dbname and username values. See https://github.com/xcoulon/go-url-shortener/blob/master/templates/database-deployment.yml#L17-L36

sahil-sharma commented 5 years ago

You mean to say I have to provide all the details of Postgres the app container as it is: POSTGRES_PASSWORD POSTGRES_USER POSTGRES_DATABASE POSTGRES_PORT POSTGRES_HOST Is my understanding right on this? Please correct me.

xcoulon commented 5 years ago

You mean to say I have to provide all the details of Postgres the app container as it is: POSTGRES_PASSWORD POSTGRES_USER POSTGRES_DATABASE POSTGRES_PORT POSTGRES_HOST Is my understanding right on this? Please correct me.

yes, in the Docker command to start the webapp, you need to specify those env vars. This is what the deployment config does in https://github.com/xcoulon/go-url-shortener/blob/master/templates/database-deployment.yml#L17-L36

sahil-sharma commented 5 years ago

hello @xcoulon,

I tried to run the application using the specific templates from the commit you mentioned and it worked fine. But, I am unable to find any difference. Though the postgres pod is still showing Created container, Started container. But, the data persists. Thanks for the help!

Now, I am exploring how to configure a static/dynamic volume creation using GlusterFS/Ceph/Rook sort of. Because, the approach you used is fine when you're not scaling your DB pods. The moment we have to scale the DB then we need something similar. Is my understanding correct on this?

Let me try and if I will be able to achieve this then I will send you a PR. You can share any links which could help me to achieve this and I want to set-up monitoring and logging for this. Any inputs on this would be of great help. I can do the testing if you have something else I will try to monitor it.