This repository is a fork of the open source krakend API gateway. It is used in Unacademy for the following use cases
- Routing requests transparently to multiple upstream microservices and act as a single entrypoint for our services. The routing config can be viewed in krakend-config repo.
- Validating the auth token passed in the request to offload authentication from upstream services.
Install gvm (go version manager) to setup go virtual env, if you already have a virtual env, you can use that as well
- install gvm -
bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
- install go1.16 version
gvm install go1.17
- use the installed go version
gvm use go1.17
- Install dependencies -
go get .
- build binary -
go build \ -ldflags="-X github.com/devopsfaith/krakend/core.KrakendVersion=1.4.2 \ -X github.com/devopsfaith/krakend/core.KrakendHeaderName=X-Gateway \ -X github.com/devopsfaith/krakend/transport/http/server.CompleteResponseHeaderName=X-Gateway-Completed" \ -o krakend ./cmd/krakend-ce
- install runtime plugins
- clone
krakend-plugins
repository and cd into the directory - build plugins in plugin mode using -
make all
- copy plugins into the plugins directory
mkdir plugins && cp <krakend-plugins-dir>/bin/*.so plugins/
- clone
- copy config as per your requirement, to get gamma config, you can copy it from the krakend gamma pipeline, or if you want to test, you can use
examples/simple_config.json
to tet. - copy the env variables from gamma vault and export that variables in your terminal. Comment out variables
FC_ENABLE FC_OUT FC_PARTIALS FC_SETTINGS FC_TEMPLATES
since you are directly copying the completely built config. - run
./krakend run -c <path-to-config-file>
- if you want to setup debugger in vscode, I have attached launch.json file, you can directly run the debugger directly using that, you would have to just update the env key from env from vault.
You can use the following Dockerfile to setup docker container for krakend. Prequisites
- Docker engine
- AWS prod account configs
- ECR Access for your IAM role
FROM 838337956332.dkr.ecr.ap-southeast-1.amazonaws.com/dockerhub:golang_1.17.6
ARG github_token
RUN git config --global url."https://${github_token}@github.com/".insteadOf "https://github.com/"
ENV github_token ""
WORKDIR /krakend
COPY . .
RUN go get .; go build \
-ldflags="-X github.com/devopsfaith/krakend/core.KrakendVersion=1.4.2 \
-X github.com/devopsfaith/krakend/core.KrakendHeaderName=X-Gateway \
-X github.com/devopsfaith/krakend/transport/http/server.CompleteResponseHeaderName=X-Gateway-Completed" \
-o krakend ./cmd/krakend-ce
# pull krakend-plugins repository
ARG KRAKEND_PLUGINS_BRANCH
RUN git clone https://github.com/Unacademy/krakend-plugins.git -b $KRAKEND_PLUGINS_BRANCH
RUN cd krakend-plugins; make all
RUN mkdir plugins; cp krakend-plugins/bin/*.so plugins/
# copy krakend config files
EXPOSE 8080
ENTRYPOINT [".", ".env.sh"]
CMD ./krakend run -c <config-file-path>
To deploy to production, merge your changes in the master branch, and then trigger the following jenkins pipeline
Krakend is attached as a target group in the main Unacademy API load balancer, so to view the metrics for krakend, you need to view the metrics for the krakend tg i.e k8s-krakend-tg
Alarms
- 5xx High, threshold 350 in 5mins
Metrics
You should monitor the following metrics in the ApplicationELB, Per AppELB, per TG Metrics
namespace, link
- RequestCount
- HTTPCode_Target_4XX_Count
- HTTPCode_Target_2XX_Count
- TargetResponseTime
- HTTPCode_Target_5XX_Count
To debug the service in production, you can view the logs for your service on Opensearch in the index pattern p-krakend-20*