Giter Club home page Giter Club logo

go-api's Introduction

Go Api with Gin

Overview

This repository contains a simple golang service which serves a single endpoint: /:id. For each call to this endpoint, the service increments a counter, and returns the number of times the endpoint has been called for the given ID. State for the application is stored in and retrieved from redis, where the data is stored simply with id as the key, and the count as the value.

In your solution, include a readme containing the necessary steps to set up the environment, as well as to build, package and deploy the application. Also detail and explain your chosen architecture, as well as what tools were used in the deployment process.

Tools used

  • Helm to package the kubernetes resources
  • Kubernetes to host the microservices
    • ingress to handle traffic into the cluster
    • service to route the traffic through the cluster
    • deployment to manage deployment of the pods
    • HPA to handle autoscalling
    • secrets to handle sensitive data (redis password)
  • Minikube for local Kubernetes development
  • Docker to build and publish the images

Deploying the api

Building and pushing the docker image

docker build . -t <name:tag>
docker push <name:tag>

Run on existing cluster

kubectl create namespace api --dry-run=client -o yaml | kubectl apply -f -

helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add metrics-server https://kubernetes-sigs.github.io/metrics-server
helm repo update metrics-server

helm install redis bitnami/redis -n api
helm upgrade --install api ./infra/api -n api

Run on minikube

Tested on minikube v1.27.1, Darwin 12.4, Kubernetes v1.25.2, Docker 20.10.18

minikube start
minikube addons enable ingress
minikube addons enable ingress-dns
minikube ip

kubectl create namespace api --dry-run=client -o yaml | kubectl apply -f -

helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add metrics-server https://kubernetes-sigs.github.io/metrics-server
helm repo update metrics-server

helm install redis bitnami/redis -n api
helm upgrade --install api ./infra/api -n api

Running on docker dev pod

docker run --rm -it --name dev-env \
  -e PORT=9000 \
  -e REDIS_PASSWORD=$REDIS_PASSWORD \
  -e PORT=9000 \
  -e REDIS_HOST=127.0.0.1 \
  -e REDIS_PORT=6379 \
  -e REDIS_DB=0 \
  -e GO111MODULE=on \
  -p 9000:9000 \
  -v $PWD:/app/server \
  --network=host \
  -w /app/server \
  golang:1.14 

go mod download && \
go build 

./devops-techtask 

General notes

Get the redis password:

export REDIS_PASSWORD=$(kubectl get secret --namespace redis my-release-redis -o jsonpath="{.data.redis-password}" | base64 -d)
kubectl port-forward --namespace default svc/my-release-redis-master 6379:6379 &
    REDISCLI_AUTH="$REDIS_PASSWORD" redis-cli -h 127.0.0.1 -p 6379

redis client:

apt update  -y && \
apt install lsb-release  -y && \
curl -fsSL https://packages.redis.io/gpg | gpg --dearmor -o /usr/share/keyrings/redis-archive-keyring.gpg && \
echo "deb [signed-by=/usr/share/keyrings/redis-archive-keyring.gpg] https://packages.redis.io/deb $(lsb_release -cs) main" | tee /etc/apt/sources.list.d/redis.list && \
apt-get update  -y && \
apt-get install redis -y

go-api's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.