1.0.26 • Published 10 months ago

@dgpholdings/backend-mono v1.0.26

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

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 entry 127.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 or npm 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 type thisisunsafe

Npm package @dgpholdings/backend-mono

This package returns typescript types for the front-end app to use the api contracts

To publish update:

  1. Commit all changes in the repo
  2. npm run pub

Docker basic usage

  1. docker build -t <docker-userId>/<service-name> . Note the . dot in th end is imp to build an image
  2. docker 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

  1. kubectl apply -f some.yml this will run any yml script for kubernetes
  2. kubectl get services to list running services
  3. kubectl get pods to list running pods
  4. kubectl describe pod <name> describes a pod
  5. kubectl delete svc <name> to delete a service
  6. kubectl exec -it <pod-name> sh to get inside the file system of a pod
  7. kubectl port-forward <pod-name> 8001:8001 to quickly expose a running pod (in k8s cluster) listening on port 8001 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
    1. First an entry in .dev.env file and then
    2. In server-depl.yaml file the same env entries in env: scope
  • 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)

  1. 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 the image: austin4silvers/auth)
  2. Make an update to your code in the service
  3. Build the image of the service
  4. Push image to the DockerHub
  5. 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)

  1. It will watch for changes in /infra/* directory for changes, then it will automatically apply to our k8s cluster
  2. 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

  1. To emit any error as response, use Error classes that extends BaseError. For example: throw new ErrorGeneral("...) is ✅. But throw new Error("..) is ❌.

Troubleshooting:

  • For error: {"errorMessage": ""}, check your request body, its probably an invalid json.
1.0.26

10 months ago

1.0.25

10 months ago

1.0.23

10 months ago

1.0.22

10 months ago

1.0.21

11 months ago

1.0.20

11 months ago

1.0.19

11 months ago

1.0.18

11 months ago

1.0.17

11 months ago

1.0.16

11 months ago

1.0.15

11 months ago

1.0.14

11 months ago

1.0.13

11 months ago

1.0.12

11 months ago

1.0.10

11 months ago

1.0.9

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago