server-keeper

command module
v0.0.0-...-69b6b67 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2022 License: MIT Imports: 3 Imported by: 0

README

server-keeper

serverKeeper do http health check 
by default nginx listen on port 10000 and pass the health check request
to serverKeeper application which is running on port 10001 by default.
serverKeeper application will check all the health of all applications currently running
and will return health if and onlyif all applications healthy

----> nginx(port:10000) ---> redirect to -> serverKeeper(port:10001) --->

---->check app1(port:xxx) healthy
---->check app2(port:xxx) healthy
---->check app3(port:xxx) healthy

-----> return healthy status -> nginx(port:10000) ----->

Http health check

health check work with nginx and other services which run on the server

nginx setting
server {
    listen 10000;   #nginx port
    server_name _;  #enable any server_name 
    location ^~ /api/{
        proxy_pass http://127.0.0.1:10001/api/;  # server_util run on port 10001
    }
}

here we use port 10000 and path '/api/health' to check health, these two values will be used to set health check in aws target group

some services on the server need to check health
//set health_check_list config in config file (./configs/pro.json)

"health_check_list": [
    {
      "name": "dns",  //project name
      "port": 9001, //port
      "path": "/api/health" //api path,start with '/'
    },
    {
      "name": "other", //project name
      "port": 9002, //port
      "path": "/api/health" //api path,start with '/'
    },
  ],

checker will send get request to url http://127.0.0.1:{port}/{path}, if any service fails then output failure

dns-server nginx config
#here is the nginx conf for https://dns.coreservice.io
server {
    listen 443 ssl http2;
    server_name dns.coreservice.io;
    # access_log /home/ec2-user/dns-service/web/log/blog-access.log;
    error_log  /var/www/dns-web/log/blog-error.log;
    #ssl on;
    ssl_certificate /home/ec2-user/coreServiceCert/coreservice.io_chain.crt;
    ssl_certificate_key /home/ec2-user/coreServiceCert/coreservice.io_key.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    # HSTS
    # add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    # add_header X-Frame-Options SAMEORIGIN;
    # add_header X-Content-Type-Options "nosniff";
  
    # web site
    location / {
        root   /var/www/dns-web/dist;
        index  index.html;
        try_files $uri $uri/ /index.html;
    }

    # api
    location ^~ /api/{
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:9001/api/;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 20000M;
    keepalive_timeout 10;
}

certificate file '/home/ec2-user/coreServiceCert/coreservice.io_chain.crt' and '/home/ec2-user/coreServiceCert/coreservice.io_key.key'

How to run severKeeper

example in aws ec2

ec2 with nginx installed, golang also needed if you will build go project on server

install nginx

  • sudo amazon-linux-extras install nginx1 install nginx
  • sudo systemctl enable nginx.service auto start when restart

install golang

nginx config here is an example

#for serverKeeper
server {
    listen 10000;
    server_name _;
    location ^~ /api/{
        proxy_pass http://127.0.0.1:10001/api/;
    }
}

#for some service in same ec2
#here is an example for dns-server
server {
    listen 80;
    server_name dns.coreservice.io;
    return 301 https://$server_name$request_uri;
}
server {
    listen 443 ssl http2;
    server_name dns.coreservice.io;
    # access_log /home/ec2-user/dns-service/web/log/blog-access.log;
    error_log  /var/www/dns-web/log/blog-error.log;
    #ssl on;
    ssl_certificate /home/ec2-user/coreServiceCert/coreservice.io_chain.crt;
    ssl_certificate_key /home/ec2-user/coreServiceCert/coreservice.io_key.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;

    # HSTS
    # add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    # add_header X-Frame-Options SAMEORIGIN;
    # add_header X-Content-Type-Options "nosniff";
  
    # web site
    location / {
        root   /var/www/dns-web/dist;
        index  index.html;
        try_files $uri $uri/ /index.html;
    }

    # api
    location ^~ /api/{
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://127.0.0.1:9001/api/;
    }

    error_page 500 502 503 504 /500.html;
    client_max_body_size 20000M;
    keepalive_timeout 10;
}

clone serverKeeper project from git or upload project to server

build project

check config file (./configs/pro.json)

here is an example for full pro.json

{
  "http_port": 10001,
  "http_api":true,
  "http_static_rel_folder": "/assets/default_/web",

  "api_doc_gen_search_dir":"cmd/default_/api",
  "api_doc_gen_mainfile":"api.go",
  "api_doc_gen_output_dir":"cmd/default_/api_docs",

  "local_log_level": "INFO",
  "health_check_list": [
    {
      "name": "dns",
      "port": 9001,
      "path": "/api/health"
    }
  ]
}

start server-util as service

  • sudo ./server-util service install
  • sudo ./server-util service start

check run status and logs

  • sudo ./server-util service restart
  • sudo ./server-util log

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
cmd
default_/http/api_docs
Package api_docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag
Package api_docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag
log
plugin
src
tools

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL