@dgpholdings/backend-mono
v1.0.26
Published
We will need the following
Downloads
83
Readme
Development setup
We will need the following
- Docker installed in local system for containerization. Use
docker version
to check installation - Kubernetes Installed through Docker GUI to use the docker container for deployment. User
kubectl version
to check installation. - ingress-nginx installed which acts as a bridge between Kubernetes cluster and outside world + load balancing + routing configuration.
- We need to update our system
hosts
file with an entry127.0.0.1 ensloka.dev
. For MacOs/etc/hosts
and for Windows:C:\Windows\System32\Drivers\etc\hosts
Start Server
- In terminal from the project root-
skaffold dev
ornpm run dev
- Now you can access
https://ensloka.dev/api-authentication/ping
If youre blocked due to certificate, you try by clicking anywhere on the browser tab and typethisisunsafe
Npm package @dgpholdings/backend-mono
This package returns typescript types for the front-end app to use the api contracts
To publish update:
- Commit all changes in the repo
npm run pub
Docker basic usage
docker build -t <docker-userId>/<service-name> .
Note the [. dot in th end is imp] to build an imagedocker push <docker-userId>/<service-name>
next push the image to DockerHub, for the k8s deployment to pull down this image for creating a pod.
Some handy Kubernetes commands
kubectl apply -f some.yml
this will run any yml script for kuberneteskubectl get services
to list running serviceskubectl get pods
to list running podskubectl describe pod <name>
describes a podkubectl delete svc <name>
to delete a servicekubectl exec -it <pod-name> sh
to get inside the file system of a podkubectl port-forward <pod-name> 8001:8001
to quickly expose a running pod (in k8s cluster) listening on port8001
to the outside world
Setting ENVIRONMENT_VARIABLE in Kubernetes
- Example:
kubectl create secret generic <object-name-ref> --from-literal=NODE_ENV=development --from-literal=COUNTRY=de
- There are other ways and options to set environment variables too
- In our app we are setting env variable through
- First an entry in
.dev.env
file and then - In
server-depl.yaml
file the same env entries inenv:
scope
- First an entry in
- Then we can do
kubectl get secrets
to list all the secret objects in the cluster - Now in the
*-depl.yaml
file of the pod where we want to access the environment variable and fo the following
...
spec:
containers:
- name: auth
image: austin4silvers/auth
env: ## <== here we are making new entry
- name: NODE_ENV
valueFrom:
secretKeyRef:
name: <the-object-name-ref>
key: NODE_ENV
Steps to update an image used by a deployment (for PRODUCTION)
- Create a
depl-someservice.yml
and make sure we are using :latest tag by default in the pod spec section of the yml file (when specifying theimage: austin4silvers/auth
) - Make an update to your code in the service
- Build the image of the service
- Push image to the DockerHub
- run
kubectl rollout restart deployment <deployment_name>
Steps to update an image used by a deployment (for DEVELOPMENT) using "Skaffold"
Using Skaffold because. (~/skaffold.yml
houses the configuration)
- It will watch for changes in
/infra/*
directory for changes, then it will automatically apply to our k8s cluster - It will watch for any changes in any our services code, and then sync the changes with their respective containers running inside the cluster
skaffold dev // from the root of the project to start skaffold
kubectl get pods // to check if all the deployed pods are "Running" (in a different terminal)
Development Notes
- To emit any error as response, use Error classes that extends
BaseError
. For example:throw new ErrorGeneral("...)
is ✅. Butthrow new Error("..)
is ❌.
Troubleshooting:
- For error:
{"errorMessage": ""}
, check your request body, its probably an invalid json.