A demo application written in Go, with Docker, Kubernetes, and Ansible provisioning for Vagrant
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
Chinmay D. Pai 772f4584a8
readme: run the built binary
10 months ago
app build: add dist command to makefile 10 months ago
dist deploy: move docker to a separate folder 10 months ago
docker deploy: move docker to a separate folder 10 months ago
kubernetes deploy: create a kubernetes service for deployment 10 months ago
vagrant build: vagrant up using ansible provisioner 10 months ago
.gitignore build: vagrant up using ansible provisioner 10 months ago
README.md readme: run the built binary 10 months ago
docker-compose.yml deploy: move docker to a separate folder 10 months ago
env-example chore: add missing env-example for docker-compose 10 months ago

README.md

Go demo

Building the application

The application provides with two, static and dynamic, builds. To build the application, run the Makefile inside the app/ directory:

$ cd app/
$ make build # to build the dynamically-linked binary
$ make dist  # to build the statically-linked binary

The static binary does not require any additional dependencies, and hence can be distributed with any package. The dist/ folder contains a statically-linked binary, which is used in one of the docker images

To run the built binary:

$ cp app/demo.bin dist/main
$ DEMO_REDIS_ADDR=":6379" DEMO_APP_ADDR=":8000" ./dist/main

This is assuming that your redis service is running on port :6379 locally, and that you want the application to listen on port :8000. You may change these values as per your convenience.

Setting up the docker images

Single Docker Builds

There's two docker image builds provided inside the docker folder, one built with golang:alpine as the base image, and the other serving the static binary inside a scratch image. The scratch image is consists of just the go application binary and timeout from coreutils.

To build either of the docker images, run:

$ docker build -t <project-name> -f docker/Dockerfile .

Replace Dockerfile with Dockerfile.scratch if you wish to build the significantly smaller scratch image.

The built image can then be run with:

$ docker run -p 8000:8000 --net="host" <project-name>

--net ensures that the docker uses the host network instead of the container network. This also assumes that you are running the redis service on your host machine's port :6379. To circumvent this, provide --build-arg DEMO_REDIS_ADDR="<your-redis-host>" while building the image.

Docker Compose

The docker compose file builds and runs both the required services. To get started, copy the env-example to .env and set the APP_PORT value in it to the port that you want to serve on. To run the compose file:

$ docker-compose -p <project-name> up -d

By default, it fetches the prebuilt thunderbottom/go-demo-app image from dockerhub. To build your own image instead, edit the docker-compose.yml file to:

  app:
    build: docker/
    # image: thunderbottom/go-demo-app:latest

and then you may run:

$ docker-compose -p <project-name> up -d --build

Setting up kubernetes

The kubernetes/ directory consists of a deployment and a service for the demo application. To deploy the application to a <namespace> on your cluster:

$ kubectl apply -f kubernetes/app-deployment.yaml -n <namespace>

This would setup the application as a deployment on the kubernetes cluster. To access this locally, you need to port-forward:

$ kubectl port-forward -n <namespace> <pod-name> 8001:8000

This will make the application available to you on localhost:8001.

Now, to convert the deployment into a service and to make it available through the entire cluster, run:

$ kubectl apply -f kubernetes/app-service.yaml -n <namespace>

Setting up Vagrant

The Vagrant image provisions a VM running ubuntu 18.04 and serves the demo application on port :8000 of the host machine. To run the vagrant file:

$ cd vagrant
$ vagrant up

Vagrant provisions the image with all the required defaults and setup, including docker and docker-compose services, and then serves the application on port :8080 of the guest machine. This can be accessed on the host machine on the :8000 port.