Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/danielepalaia/kubernetes-postgres
playing with kubernetes, minikube, docker, rest-api
https://github.com/danielepalaia/kubernetes-postgres
docker go golang kubernetes minikube postgresql rest-api
Last synced: 5 days ago
JSON representation
playing with kubernetes, minikube, docker, rest-api
- Host: GitHub
- URL: https://github.com/danielepalaia/kubernetes-postgres
- Owner: DanielePalaia
- Created: 2019-07-27T11:12:07.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-09-22T13:08:09.000Z (about 5 years ago)
- Last Synced: 2024-06-19T13:45:22.956Z (5 months ago)
- Topics: docker, go, golang, kubernetes, minikube, postgresql, rest-api
- Language: Go
- Homepage:
- Size: 14.8 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Introduction
Scope of this software is just to play around with Kubernetes (Minikube).
This software is exposing a set of rest-api to manage a collection of ToDo operations and is accessing postgres to store and read Todos.
We 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.
In the past I also used this app using mysql:
https://github.com/DanielePalaia/web-service-kubernetes## Datastore and rest api
The todos operations are saved in a postgres datastore defined in datastore.sql
Don't put much attention to the table schema as this is just a test for Docker and Kubernetes
Just 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").```
CREATE TABLE ToDo (
ID serial,
Topic varchar(255),
Completed int,
Due varchar(255) DEFAULT '',
PRIMARY KEY (ID)
);
```The software exposes these rest api (GET, POST, PUT, DELETE) which can be tested with curl.
Use GET to get all todo items inside the collection
Use PUT to create a new item for the Todo Collection
Use POST for update and DELETE for deletion```
curl http://localhost:8080/todos
curl -H "Content-Type: application/json" -d '{"Topic":"New TodoElem", "Completed":0}' -X PUT http://localhost:8080/todos
curl http://localhost:8080/todos/1
curl -H "Content-Type: application/json" -d '{"Id":0,"name":"New TodoElem Updated"}' -X POST http://localhost:8080/todos
curl -X DELETE http://localhost/todos/1
curl -X DELETE http://localhost/todos
```A Swagger documentation that allows you to test this interface is also provided:
![Screenshot](./pics/pic2.png)
![Screenshot](./pics/pic3.png)## Running the app locally
In conf file please specify the right database connection information,
then you can simply run the binary provided in ./kubernetes-postgres (for mac) and run the binary**./kubernetes-postgres**
Then go to the swagger interface
http://localhost:8080/docs/index.html
![Screenshot](./pics/pic4.png)
Initially you will receive an empty list of todos. Fill the todo with the other rest api with curl or the swagger doc.## Running on kubernetes/minikube
### 1. Install minikube
On mac is enough to:
**brew cask install virtualbox**
**brew cask install kubectl**
**brew cask install docker**### 2. Start minikube and dashboard
Start minikube:
**minikube start**
Run dashboard
**minikube dashboard**![Screenshot](./pics/minikube.png)
### 3. Install Postgresql on minikube
Follow this guideline to create a volume a pod and a service:
https://severalnines.com/blog/using-kubernetes-deploy-postgresqlyaml file have been provided in kub-yaml folder. You can just:
**kubectl create -f postgres-configmap.yaml**
**kubectl create -f postgres-storage.yaml**
**kubectl create -f postgres-deployment.yaml**
to create a configmap, a storage a pod and a deployment for postgresql.![Screenshot](./pics/minikube2.png)
### 4. Configuring postgresql on minikube and creating database and table
You need to create database and table in the postgres instance in your minikube env
you can use this command to access the pod
kubectl exec -it bash**
**Also change passwd for postgres user to changeme**
then swith to user postgres and do psql
Create same database and table as for datastore.sql### 5. Deploy this service kubernetes-postgres on minikube and link to the postgres instance
A version of the microservice has already been deployed on dockerhub:
https://cloud.docker.com/u/danielepalaia/repository/docker/danielepalaia/kubernetes-postgresAlso a Dockerfile is provided. You may want to create an image you need to:
**docker build -t kubernetes-postgres .**
```
Danieles-MBP:kubernetes-postgres dpalaia$ docker build -t kubernetes-postgres .
Sending build context to Docker daemon 40.18MB
Step 1/10 : FROM golang
---> be63d15101cb
Step 2/10 : ADD . /go/src/kubernetes-postgres
---> 0effa7f0cd0f
Step 3/10 : RUN go get github.com/gorilla/mux
---> Running in a869896a9863
Removing intermediate container a869896a9863
---> 119d271a84c0
Step 4/10 : RUN go get github.com/lib/pq
---> Running in 3d80920c4029
Removing intermediate container 3d80920c4029
---> ce05cab4fafa
Step 5/10 : RUN go get github.com/swaggo/http-swagger
---> Running in 21f1a722790f
Removing intermediate container 21f1a722790f
---> dcb30e1aee12
Step 6/10 : RUN go get github.com/alecthomas/template
---> Running in c3867ea3a849
Removing intermediate container c3867ea3a849
---> cc5731748581
Step 7/10 : ADD conf /go
---> 7ffe14d67cfd
Step 8/10 : RUN go build /go/src/kubernetes-postgres
---> Running in c5d6d3be35cf
Removing intermediate container c5d6d3be35cf
---> 3bc4c6d70443
Step 9/10 : ENTRYPOINT /go/src/kubernetes-postgres/kubernetes-postgres
---> Running in 68f435ac926b
Removing intermediate container 68f435ac926b
---> 207c2512b411
Step 10/10 : EXPOSE 8080
---> Running in faafd404a00b
Removing intermediate container faafd404a00b
---> 254945f7ff05
Successfully built 254945f7ff05
Successfully tagged kubernetes-postgres:latest
```then
```
docker run --publish 6060:8080 --name test kubernetes-postgres
docker tag kubernetes-postgres danielepalaia/kubernetes-postgres
docker push
```if you wanto to push back to DockerHub.
You 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:
``
USERNAME:postgres
PASSWD:changeme
HOST:postgres
DATABASE:tododatastore
PORT:5432
``### 5. Deploy the pod on minikube
Best thing in this case would be to create a service as load balancer linked to multiple pods where the service is deployed.
It 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.
As the image is already on Dockerhub we can use the minikube web interface to create a new deployment and pod.![Screenshot](./pics/deploy.png)
![Screenshot](./pics/deploy2.png)### 6. Use port-forward to access the service from localhost
Best thing to do would be to create a LoadBalancer service to control the pods where this service will run.
As 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.
you can do in this way:
**kubectl port-forward pod-name 8080:8080**### 7. Access web service with curl or swagger doc api
You can now access the api as it is locally.
![Screenshot](./pics/screen.png)