项目作者: kieronlangdon

项目描述 :
Go REST api example project with minikube deployment steps
高级语言: Go
项目地址: git://github.com/kieronlangdon/go_api_example.git
创建时间: 2020-08-06T13:57:19Z
项目社区:https://github.com/kieronlangdon/go_api_example

开源协议:GNU General Public License v3.0

下载


go_api_example

Files for course from https://github.com/bradtraversy/go_restapi
Modified since then!

Simple GO Lang REST API

Simple RESTful API to create, read, update and delete books. No database implementation yet

Quick Start

  1. # Install mods
  2. go get -u github.com/gorilla/mux
  3. go get -u github.com/go-resty/resty
  4. go get -u github.com/stretchr/testify/assert
  5. go get -u github.com/stretchr/testify/suite
  1. go build
  2. ./go_restapi

Endpoints

Get All Books

  1. GET api/books

Get Single Book

  1. GET api/books/{id}

Get All Books just titles

  1. GET api/books/titles/

Delete Book

  1. DELETE api/books/{id}

Delete all Books

  1. DELETE api/books

Create Book

  1. POST api/books
  2. # Request sample
  3. # {
  4. # "isbn":"4545454",
  5. # "title":"Book Three",
  6. # "author":{"firstname":"Harry", "lastname":"White"}
  7. # }

Update Book

  1. PUT api/books/{id}
  2. # Request sample
  3. # {
  4. # "isbn":"4545454",
  5. # "title":"Updated Title",
  6. # "author":{"firstname":"Harry", "lastname":"White"}
  7. # }

Health probe

  1. GET healthz

Readiness probe

  1. GET readyz

Build steps

  1. Set Paths:
  2. export GOPATH=/Users/$USER/go
  3. export PATH=$PATH:$GOPATH/bin;
  4. From:
  5. $GOPATH/src/github.com/$USER/restapi
  6. docker build -t my-go-app .
  7. Enter container using Almquist shell after starting:
  8. docker run -it --rm my-go-app /bin/ash
  9. Run container in detached mode
  10. docker run -p 8080:8001 -d my-go-app
  11. Run Unit tests only
  12. go test -v -run Unit
  13. Run tests with coverage report
  14. go test -coverprofile=c.out
  15. See report in browser (generates html file)
  16. go tool cover -html=c.out -o coverage.html

Minikube setup

  1. minikube delete
  2. minikube start
  3. eval $(minikube docker-env)

Minikube steps for error:

https://registry-1.docker.io/v2 connection error while pulling image
minikube ssh
sudo vi /etc/systemd/network/10-eth1.network add DNS=8.8.8.8 under [Network]
sudo vi /etc/systemd/network/20-dhcp.network add DNS=8.8.8.8 under [Network]
sudo systemctl restart systemd-networkd

Local registry steps:

Start registry
docker run -d -p 5000:5000 --restart=always --name registry registry:2
Build image
docker build -t my-go-app .
Tag image
docker tag my-go-app:latest localhost:5000/my-go-app
Push to local registry
docker push localhost:5000/my-go-app
Remove image local - does not remove from registry
docker image remove localhost:5000/my-go-app

Create deployment in minikube

minikube kubectl create deployment testdev -- --image=localhost:5000/my-go-app

Alt steps for running minikube and to avoid going down a rabbit hole of nonsense

Build image
docker build -t my-go-app .
Tag image
docker tag my-go-app:latest localhost:5000/my-go-app
Point your terminal to use the docker daemon inside minikube
eval $(minikube docker-env)
Check to see all images
docker images
Notice that the image we just created and tagged is not here? We’re using minikube’s docker daemon now
Push to cache in minikube
minikube cache add localhost:5000/my-go-app
This will fail as the image doesn’t exist in this daemon
So in another terminal OR after undoing eval $(minikube docker-env)
Re-run minikube cache add localhost:5000/my-go-app
Check image is in minikube cache
minikube cache list
We will see an image with no repo & tag, check the imageId, it’s the same as the local image in non minikube docker
Tag image in minikube docker daemon
docker tag c22dbba37091 localhost:5000/my-go-app
Create deployment in minikube
minikube kubectl create deployment testdev -- --image=localhost:5000/my-go-app

Alter deployment yaml to pull from minikube local

Get deployment yaml
minikube kubectl get deploy testdev -- -o yaml --export >> testdev.yaml
In testdev.yaml
imagePullPolicy: Always
change to
imagePullPolicy: Never
Apply yaml
minikube kubectl apply -- -f testdev.yaml

Expose container port using service

Set deployment to expose deployment of type node port
minikube kubectl expose deployment testdev -- --type=NodePort --port=8001
Get port that has been exposed externally
minikube kubectl get svc testdev
Get minikube ip
minikube ip
Test interaction with cluster
curl 192.168.50:32145/api/books