Git Product home page Git Product logo

backend-k8s-template's Introduction

Project Description

this is a template for building a microservice project in kubernetes. This will allow to create multiple microservices communicating with each other, persistent data using mongodb, a functional react app using redux to communicate with the microservices, and a detailed plan to add microservices and deploy to digital ocean.
Link to LucidChart architecture overview

This project includes

  • Template for multiple microservices and how they communicate with each other:
  • Error handling, middlewares and Models for nodejs in common code:
    • How to handle errors on the microservices
    • Middlewares for different functionalities and their implementation in the microservices
    • Models and their implementation of hashing and class methods
  • React setup with Redux:
    • Functional react communicating with microservices
    • Using redux to send requests
  • Deployment instruction to digital ocean
    • How to set up digital ocean
    • How to deploy the k8s to the managed kubernetes of digital ocean
    • How to set up persistent data in digital ocean
  • What can be still be added
    • MongoDB sharding/distributed across multiple nodes
    • MongoDB replication

##How to start the project

  • install Docker desktop and kubernetes
  • Install ingress nginx by running this command (Same command as Mac):
  • overwrite the local host to point to a URL, it should be the same as the host in the ingress controller file:
    • Modify hosts file of widows and write the desired url next to the 127.0.0.1 ip
    1. the host file 127.0.0.1 backend.com

      path: C:\Windows\System32\drivers\etc\hosts

    2. the ingress controller file

    - host: backend.com
  • start the desired services/deployments inside the k8s folder and run the ingress-controller file kubectl apply -f <name_of_files>
  • you can access the url specified in the host with the specified routes

Microservices

  • how to add a microservice

  • Create a new server (using the template if it's a nodejs server) with the desired routes (the routes and port should be unique)
  • Dockerize the server and push it to docker hub
  • Create a kubernetes deployment and service using the files inside the k8s folder as a template
  • Add the database and setup persistent data if needed. Again, use the files inside k8s folder as a template
  • Add the microservice to the ingress-controller file, as a new element of the array path, while specifying the correct port, service name, and the path reserved to that service example:
    paths:
      - path: /auth/?(.*)
        backend:
        serviceName: auth-srv
        servicePort: 3000
  • Add the commands to stop/deploy the yaml files and docker images in deploy.sh and stop.sh
  • run ./scripts/stop_deploy.sh

Errors, Middlewares and Models

  • Errors

all errors will be handled by error-handler.js all we need to do is import it in index.js file:

app.use(error_handler);
  • Midllewares

  • error-handler.js is a middleware to handle all of the errors inside the nodejs servers

  • require-auth.js is a middleware to get the token from the headers and verify it. Need to be passed for all routes that need auth

  • validate-request.js is a middleware that will take the req and throw an error if the params are not correctly setup

  • Models

  • The models are in the common/models folder. There's the User and Password model to compare different passwords

React and Redux

add redux

  • go to the types.js file and add the desired enums
  • go to the reducer file and add your new reducer file with the logic to handle the enums and state
  • add action file and add you logic
  • go to the component and bring it

Load user

useEffect(() => {
        store.dispatch(load_user());
    }, [])

call this in App.js to load user as soon as they log in the page. We set the deffault header and store the token in local storage

Digital Ocean

run the cluster

  • start kubernetes cluster
  • install doctl
  • create api key in digital ocean
  • doct auth init (from powershell)
  • series of commands to connect digital ocean context locally
    • doctl kubernetes cluster kubeconfig save <cluster_name (i.e backend-template)>
      • now we are connected to cloud kubernetes
  • install ingress nginx: kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.45.0/deploy/static/provider/do/deploy.yaml
  • to watch ingress status: kubectl get pods -n ingress-nginx -l app.kubernetes.io/name=ingress-nginx --watch
  • to see contexts: kubectl config view
    • to switch context: kubectl config use-context

create the dbs

https://www.mongodb.com/digital-ocean

  • create droplet
  • ssh into droplet: ssh [email protected]
  • install and run
  • create an admin user and connect to it
    • use admin
    •   db.createUser({ user: "root", pwd: "root", roles: [ { role: "userAdminAnyDatabase", db: "admin" }, "readWriteAnyDatabase" ]})
    - enable auth
     - sudo vi /etc/mongod.conf
     - uncomment security and add
       ```shell
       ...
       security:
         authorization: "enabled"
       ...
       ```
     - sudo service mongod restart (restard mongodb_
     - To connect
       - mongo -u root -p --authenticationDatabase admin
    
  • configure firewall
    • sudo ufw enable
    • sudo ufw allow 27017 or sudo ufw allow from your_trusted_server_ip/32 to any port 27017 (your_trusted_server_ip=)
    • sudo ufw status -configure mongo to listen to remote server
      • sudo vi /etc/mongod.conf
      • add the droplet server ip
        # network interfaces
        net:
        port: 27017
        bindIp: 127.0.0.1,mongodb_server_ip
      • sudo service mongod restart

backend-k8s-template's People

Contributors

wassimkha avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.