Go REST api example project with minikube deployment steps
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
# Install mods
go get -u github.com/gorilla/mux
go get -u github.com/go-resty/resty
go get -u github.com/stretchr/testify/assert
go get -u github.com/stretchr/testify/suite
go build
./go_restapi
GET api/books
GET api/books/{id}
GET api/books/titles/
DELETE api/books/{id}
DELETE api/books
POST api/books
# Request sample
# {
# "isbn":"4545454",
# "title":"Book Three",
# "author":{"firstname":"Harry", "lastname":"White"}
# }
PUT api/books/{id}
# Request sample
# {
# "isbn":"4545454",
# "title":"Updated Title",
# "author":{"firstname":"Harry", "lastname":"White"}
# }
GET healthz
GET readyz
Set Paths:
export GOPATH=/Users/$USER/go
export PATH=$PATH:$GOPATH/bin;
From:
$GOPATH/src/github.com/$USER/restapi
docker build -t my-go-app .
Enter container using Almquist shell after starting:
docker run -it --rm my-go-app /bin/ash
Run container in detached mode
docker run -p 8080:8001 -d my-go-app
Run Unit tests only
go test -v -run Unit
Run tests with coverage report
go test -coverprofile=c.out
See report in browser (generates html file)
go tool cover -html=c.out -o coverage.html
minikube delete
minikube start
eval $(minikube docker-env)
https://registry-1.docker.io/v2 connection error while pulling imageminikube 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
Start registrydocker run -d -p 5000:5000 --restart=always --name registry registry:2
Build imagedocker build -t my-go-app .
Tag imagedocker tag my-go-app:latest localhost:5000/my-go-app
Push to local registrydocker push localhost:5000/my-go-app
Remove image local - does not remove from registrydocker image remove localhost:5000/my-go-app
minikube kubectl create deployment testdev -- --image=localhost:5000/my-go-app
Build imagedocker build -t my-go-app .
Tag imagedocker tag my-go-app:latest localhost:5000/my-go-app
Point your terminal to use the docker daemon inside minikubeeval $(minikube docker-env)
Check to see all imagesdocker 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 minikubeminikube 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 cacheminikube 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 daemondocker tag c22dbba37091 localhost:5000/my-go-app
Create deployment in minikubeminikube kubectl create deployment testdev -- --image=localhost:5000/my-go-app
Get deployment yamlminikube kubectl get deploy testdev -- -o yaml --export >> testdev.yaml
In testdev.yamlimagePullPolicy: Always
change toimagePullPolicy: Never
Apply yamlminikube kubectl apply -- -f testdev.yaml
Set deployment to expose deployment of type node portminikube kubectl expose deployment testdev -- --type=NodePort --port=8001
Get port that has been exposed externallyminikube kubectl get svc testdev
Get minikube ipminikube ip
Test interaction with clustercurl 192.168.50:32145/api/books