Skip to content
William Kennedy edited this page May 31, 2020 · 27 revisions

Requirements

This project has been configured to run in a Kubernetes (k8s) environment. A decision was made to use KIND for the local k8s environment. Any k8s environment would work after careful setup of your k8s cluster. This section is not going to talk about installing your own k8s environment. Instead, it will focus on using KIND for your k8s environment, setting up your cluster there and deploying and running the project locally.

To install KIND follow these instructions.

To install the K8s kubectl client follow these instructions.

Makefile

A makefile has also been provide to allow for the building, running, and testing the project. It is accessible under the K8s/kind folder.

$ cd $HOME/code/service/k8s/kind

Commands

Run these commands in the order they are presented.

make all

This command builds all the containers for use in the K8s cluster.

$ make all

make up

This command will start the cluster via Docker. It assigns the name ardan-starter-cluster to the cluster and configures the cluster is run a single Node for simplicity. It also opens all ports for easy development access.

$ make up

kind create cluster --name ardan-starter-cluster --config kind-config.yaml

make load

This command loads the sales-api and meterics containers into the k8s environment. The zipkin and postgres containers don't need to be loaded since K8s can find them in the Docker registery.

$ make load

kind load docker-image gcr.io/ardan-starter-kit/sales-api-amd64:1.0 --name ardan-starter-cluster
kind load docker-image gcr.io/ardan-starter-kit/metrics-amd64:1.0 --name ardan-starter-cluster

make services

This command deploys two PODS into the single node. One POD is for the Postgres database and the second contains the sales-api, metrics and zipkin services.

kubectl create -f deploy-postgres.yaml
kubectl create -f deploy-sales-api.yaml

kubectl logs -f <POD_NAME>

If you want to see the logs from the sales-api you need to get information about the pods. You will need to use the kubectl command to see the pod names.

$ kubectl get pods

NAME                         READY   STATUS    RESTARTS   AGE
sales-api-7c7f56664b-lx26n   1/1     Running   0          53s
metrics-855ccbf98f-wx2lh     1/1     Running   0          53s

With the pod name, you can run this command to see the logs.

$ kubectl logs -f sales-api-855ccbf98f-wx2lh

Seed Database

You are now ready to test if the system is operational. You must seed the database just like when working in your development environment. Since the databsae port is not exposed, the admin tool has been added to the sales-api container to run the seed command.

Get information about the pod. You need the pod name.

kubectl get pods

With the pod name, replace that and run this command to get a shell inside the sales-api container.

kubectl exec -it <POD NAME> --container sales-api  -- /bin/sh

Now run the migrate command to structure the database and seed to populate it with data.

./admin --db-disable-tls=1 migrate
./admin --db-disable-tls=1 seed

Authenticated Requests

Before any requests can be sent you must acquire an auth token. Make a request using HTTP Basic auth with the test user email and password to get the token.

curl --user "admin@example.com:gophers" http://0.0.0.0:3000/v1/users/token

I suggest putting the resulting token in an environment variable like $TOKEN.

export TOKEN="COPY TOKEN STRING FROM LAST CALL"

To make authenticated requests put the token in the Authorization header with the Bearer prefix.

curl -H "Authorization: Bearer ${TOKEN}" http://0.0.0.0:3000/v1/users

Run Load

If you want to run a little load on the service you can use hey.

hey -H "Authorization: Bearer ${TOKEN}" -c 100 -n 10000 http://0.0.0.0:3000/v1/users

09: View Traces

If you want to see a few of the traces generated.

http://localhost:9411/zipkin/