{"id":19275091,"url":"https://github.com/danielepalaia/kubernetes-postgres","last_synced_at":"2026-04-12T09:38:01.350Z","repository":{"id":131056376,"uuid":"199154536","full_name":"DanielePalaia/kubernetes-postgres","owner":"DanielePalaia","description":"playing with kubernetes, minikube, docker, rest-api","archived":false,"fork":false,"pushed_at":"2019-09-22T13:08:09.000Z","size":15563,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-23T21:15:24.508Z","etag":null,"topics":["docker","go","golang","kubernetes","minikube","postgresql","rest-api"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DanielePalaia.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-27T11:12:07.000Z","updated_at":"2019-12-28T15:18:49.000Z","dependencies_parsed_at":null,"dependency_job_id":"112f794d-0939-430e-981e-cceea348d157","html_url":"https://github.com/DanielePalaia/kubernetes-postgres","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/DanielePalaia/kubernetes-postgres","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielePalaia%2Fkubernetes-postgres","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielePalaia%2Fkubernetes-postgres/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielePalaia%2Fkubernetes-postgres/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielePalaia%2Fkubernetes-postgres/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DanielePalaia","download_url":"https://codeload.github.com/DanielePalaia/kubernetes-postgres/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DanielePalaia%2Fkubernetes-postgres/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266753968,"owners_count":23979144,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["docker","go","golang","kubernetes","minikube","postgresql","rest-api"],"created_at":"2024-11-09T20:48:16.743Z","updated_at":"2026-04-12T09:37:56.317Z","avatar_url":"https://github.com/DanielePalaia.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Introduction\n\nScope of this software is just to play around with Kubernetes (Minikube). \u003c/br\u003e\nThis software is exposing a set of rest-api to manage a collection of ToDo operations and is accessing postgres to store and read Todos. \u003c/br\u003e\nWe will see how this app can be deployed locally, using just Docker containers to link one container to another or using kubernetes, minikube in this case. \u003c/br\u003e\nIn the past I also used this app using mysql: \u003c/br\u003e\nhttps://github.com/DanielePalaia/web-service-kubernetes\u003c/br\u003e\n\n## Datastore and rest api\n\nThe todos operations are saved in a postgres datastore defined in datastore.sql \u003c/br\u003e\nDon't put much attention to the table schema as this is just a test for Docker and Kubernetes \u003c/br\u003e\nJust topic is a description of the todotopic, Completed can be a value which show the degree of completness of the todo ex (from one to 10) and Due can be a date or something like (\"in 2 days\"). \u003c/br\u003e\n\n```\nCREATE TABLE ToDo (\n\t    ID serial,\n\t    Topic varchar(255),\n\t    Completed int,\n\t    Due varchar(255) DEFAULT '',\n\t    PRIMARY KEY (ID)\n);\n```\n\nThe software exposes these rest api (GET, POST, PUT, DELETE) which can be tested with curl.\u003c/br\u003e\nUse GET to get all todo items inside the collection \u003c/br\u003e\nUse PUT to create a new item for the Todo Collection \u003c/br\u003e\nUse POST for update and DELETE for deletion \u003c/br\u003e\n\n```\ncurl http://localhost:8080/todos\ncurl -H \"Content-Type: application/json\" -d '{\"Topic\":\"New TodoElem\", \"Completed\":0}' -X PUT http://localhost:8080/todos\ncurl http://localhost:8080/todos/1\ncurl -H \"Content-Type: application/json\" -d '{\"Id\":0,\"name\":\"New TodoElem Updated\"}' -X POST http://localhost:8080/todos\ncurl -X DELETE http://localhost/todos/1\ncurl -X DELETE http://localhost/todos\n```\n\nA Swagger documentation that allows you to test this interface is also provided:\n\n![Screenshot](./pics/pic2.png)\n![Screenshot](./pics/pic3.png)\n\n## Running the app locally\nIn conf file please specify the right database connection information,\nthen you can simply run the binary provided in ./kubernetes-postgres (for mac) and run the binary \u003c/br\u003e\n\n**./kubernetes-postgres**\u003c/br\u003e\u003c/br\u003e\nThen go to the swagger interface\u003c/br\u003e\nhttp://localhost:8080/docs/index.html\u003c/br\u003e\n![Screenshot](./pics/pic4.png) \u003c/br\u003e\nInitially you will receive an empty list of todos. Fill the todo with the other rest api with curl or the swagger doc.\n\u003c/br\u003e\n\n## Running on kubernetes/minikube\n\n### 1. Install minikube\nOn mac is enough to:\u003c/br\u003e\n**brew cask install virtualbox**\u003c/br\u003e\n**brew cask install kubectl**\u003c/br\u003e\n**brew cask install docker** \u003c/br\u003e\n\n### 2. Start minikube and dashboard\nStart minikube: \u003c/br\u003e\n**minikube start** \u003c/br\u003e\nRun dashboard \u003c/br\u003e\n**minikube dashboard**\u003c/br\u003e\n\n![Screenshot](./pics/minikube.png) \u003c/br\u003e\n\n### 3. Install Postgresql on minikube \nFollow this guideline to create a volume a pod and a service: \u003c/br\u003e\nhttps://severalnines.com/blog/using-kubernetes-deploy-postgresql\n\u003c/br\u003e\nyaml file have been provided in kub-yaml folder. You can just: \u003c/br\u003e\n**kubectl create -f postgres-configmap.yaml** \u003c/br\u003e\n**kubectl create -f postgres-storage.yaml** \u003c/br\u003e\n**kubectl create -f postgres-deployment.yaml** \u003c/br\u003e\nto create a configmap, a storage a pod and a deployment for postgresql. \n\n![Screenshot](./pics/minikube2.png) \u003c/br\u003e\n\n### 4. Configuring postgresql on minikube and creating database and table\nYou need to create database and table in the postgres instance in your minikube env \u003c/br\u003e\nyou can use this command to access the pod \u003c/br\u003e\nkubectl exec -it \u003cpodname\u003e bash** \u003c/br\u003e\n**Also change passwd for postgres user to changeme**\nthen swith to user postgres and do psql \u003c/br\u003e\nCreate same database and table as for datastore.sql \u003c/br\u003e\n\n### 5. Deploy this service kubernetes-postgres on minikube and link to the postgres instance\nA version of the microservice has already been deployed on dockerhub: \u003c/br\u003e\u003c/br\u003e \nhttps://cloud.docker.com/u/danielepalaia/repository/docker/danielepalaia/kubernetes-postgres \u003c/br\u003e\u003c/br\u003e\n\nAlso a Dockerfile is provided. You may want to create an image you need to: \u003c/br\u003e\n**docker build -t kubernetes-postgres .** \u003c/br\u003e\n```\nDanieles-MBP:kubernetes-postgres dpalaia$ docker build -t kubernetes-postgres  .\nSending build context to Docker daemon  40.18MB\nStep 1/10 : FROM golang\n ---\u003e be63d15101cb\nStep 2/10 : ADD . /go/src/kubernetes-postgres\n ---\u003e 0effa7f0cd0f\nStep 3/10 : RUN go get github.com/gorilla/mux\n ---\u003e Running in a869896a9863\nRemoving intermediate container a869896a9863\n ---\u003e 119d271a84c0\nStep 4/10 : RUN go get github.com/lib/pq\n ---\u003e Running in 3d80920c4029\nRemoving intermediate container 3d80920c4029\n ---\u003e ce05cab4fafa\nStep 5/10 : RUN go get github.com/swaggo/http-swagger\n ---\u003e Running in 21f1a722790f\nRemoving intermediate container 21f1a722790f\n ---\u003e dcb30e1aee12\nStep 6/10 : RUN go get github.com/alecthomas/template\n ---\u003e Running in c3867ea3a849\nRemoving intermediate container c3867ea3a849\n ---\u003e cc5731748581\nStep 7/10 : ADD conf /go\n ---\u003e 7ffe14d67cfd\nStep 8/10 : RUN go build /go/src/kubernetes-postgres\n ---\u003e Running in c5d6d3be35cf\nRemoving intermediate container c5d6d3be35cf\n ---\u003e 3bc4c6d70443\nStep 9/10 : ENTRYPOINT /go/src/kubernetes-postgres/kubernetes-postgres\n ---\u003e Running in 68f435ac926b\nRemoving intermediate container 68f435ac926b\n ---\u003e 207c2512b411\nStep 10/10 : EXPOSE 8080\n ---\u003e Running in faafd404a00b\nRemoving intermediate container faafd404a00b\n ---\u003e 254945f7ff05\nSuccessfully built 254945f7ff05\nSuccessfully tagged kubernetes-postgres:latest\n```\n\nthen \u003c/br\u003e\n\n```\ndocker run --publish 6060:8080 --name test kubernetes-postgres\ndocker tag kubernetes-postgres danielepalaia/kubernetes-postgres\ndocker push\n```\n\nif you wanto to push back to DockerHub. \u003c/br\u003e\nYou many need to modify your conf file to database connection if different from standard values. In the image pushed on dockerhub these values are set: \u003c/br\u003e\n``\nUSERNAME:postgres\nPASSWD:changeme\nHOST:postgres\nDATABASE:tododatastore\nPORT:5432\n``\n\n### 5. Deploy the pod on minikube\nBest thing in this case would be to create a service as load balancer linked to multiple pods where the service is deployed.\nIt seems that minikube doesn't support services of type loadBalancer so we can just deploy the service in one pod do a port forward and try it out. \u003c/br\u003e\nAs the image is already on Dockerhub we can use the minikube web interface to create a new deployment and pod. \u003c/br\u003e\n\n![Screenshot](./pics/deploy.png) \u003c/br\u003e\n![Screenshot](./pics/deploy2.png) \u003c/br\u003e\n\n### 6. Use port-forward to access the service from localhost\nBest thing to do would be to create a LoadBalancer service to control the pods where this service will run.\nAs minikube doesn't support services of type load balancer or ingress all we can do is to forward the port of a pod and access it directly. \u003c/br\u003e\nyou can do in this way: \u003c/br\u003e\n**kubectl port-forward pod-name 8080:8080**\n\n### 7. Access web service with curl or swagger doc api\nYou can now access the api as it is locally.\n![Screenshot](./pics/screen.png) \u003c/br\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielepalaia%2Fkubernetes-postgres","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielepalaia%2Fkubernetes-postgres","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielepalaia%2Fkubernetes-postgres/lists"}