เริ่มต้นกับ Microservice #2

จากบทความที่แล้วที่ได้สร้างระบบลงทะเบียน ขั้นตอนต่อไปคือทำระบบจัดการ API และระบบ Monitoring ต่างๆ ด้วย Kong, Prometheus, Grafana
Kong API Gateway

จากรูปก็พอจะเดาๆได้แล้วว่ามันมีหน้าที่ทำอะไร Kong มีหน้าที่ในการจัดการกับการเข้าถึง Internal API ที่เปิดให้ภายนอกเรียกใช้งานได้ ไม่เพียงแค่นั้นตัวมันเองมีความสามารถต่างๆในการจัดการได้มากมาย เช่น Authentication, Rate Limiting
Prometheus

เป็นตัว Monitoring ระบบของเรา และเป็น Time Series Database แต่ในบทความนี้จะใช้เป็นตัว PostgreSQL
สร้าง Directory “kong_dock” โดยภายในประกอบด้วย
.
|__ docker-compose.yml
|__ prometheus.yml
docker-compose.yml
version: "2"
services:
kong:
image: kong:2.0.2-alpine
container_name: kong
depends_on:
- kong_db
- kong_migration
expose:
- "8001"
- "8444"
- "8000"
- "8443"
restart: always
environment:
KONG_DATABASE: postgres
KONG_PG_HOST: kong_db
KONG_PG_PORT: 5432
KONG_PG_DATABASE: kong
KONG_PROXY_ACCESS_LOG: /dev/stdout
KONG_ADMIN_ACCESS_LOG: /dev/stdout
KONG_PROXY_ERROR_LOG: /dev/stderr
KONG_ADMIN_ERROR_LOG: /dev/stderr
KONG_PROXY_LISTEN: 0.0.0.0:8000, 0.0.0.0:8443 ssl
KONG_ADMIN_LISTEN: 0.0.0.0:8001, 0.0.0.0:8444 ssl
KONG_PLUGINS: basic-auth, key-auth, rate-limiting, prometheus, proxy-cache, oauth2
VIRTUAL_HOST: service.lab10.cpsudevops.com
VIRTUAL_PORT: 8000
LETSENCRYPT_HOST: service.lab10.cpsudevops.com
networks:
- webproxy
- defaultkong_db:
image: postgres:9.6
container_name: kong_db
volumes:
- kong_datastore:/var/lib/postgresql/data
restart: always
environment:
POSTGRES_DB: kong
POSTGRES_USER: kong
POSTGRES_HOST_AUTH_METHOD: trustkong_migration:
image: kong:latest
container_name: kong_migration
command: "kong migrations bootstrap"
restart: on-failure
environment:
KONG_PG_HOST: kong_db
depends_on:
- kong_dbkonga:
container_name: konga
image: pantsel/konga
restart: always
environment:
DB_ADAPTER: postgres
DB_HOST: konga_db
DB_USER: konga
DB_DATABASE: konga
NODE_ENV: development
VIRTUAL_HOST: konga.lab10.cpsudevops.com
VIRTUAL_PORT: 1337
LETSENCRYPT_HOST: konga.lab10.cpsudevops.com
expose:
- "1337"
networks:
- webproxy
- defaultkonga_db:
image: postgres:9.6
container_name: konga_db
volumes:
- konga_database:/var/lib/postgresql/data
restart: always
environment:
POSTGRES_DB: konga
POSTGRES_USER: konga
POSTGRES_HOST_AUTH_METHOD: trustprometheus:
image: prom/prometheus:latest
container_name: prometheus
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- prometheus_data:/promtheus
command:
- "--config.file=/etc/prometheus/prometheus.yml"
expose:
- "9090"
restart: alwaysnode_exporter:
image: prom/node-exporter:latest
container_name: node_exporter
expose:
- "9100"
restart: alwaysvolumes:
kong_datastore:
konga_database:
prometheus_data:networks:
webproxy:
external:
name: webproxy
default:
external:
name: kong_network
prometheus.yml
global:
external_labels:
monitor: devops_monitor
scrape_interval: 5s
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- "localhost:9090"- job_name: node_exporter
static_configs:
- targets:
- "node_exporter:9100"- job_name: kong
static_configs:
- targets:
- "kong:8001"
โดยหลังจากที่ทำการ start service เสร็จแล้วก็สามารถ Config Kong ได้ผ่าน
จะพบกับหน้าแรกเพื่อให้สมัคร Admin Account

โดยหลังจากสมัครและเข้าสู่ระบบเรียบร้อยแล้วจะเป็นดังรูปด้านล่างนี้

โดยใส่ข้อมูลดังนี้
Default:
Name: Devops,
Kong Admin URL: http://kong:8001
เมื่อสำเร็จก็จะเด้งไปยังอีกหน้าหนึ่งดังรูป

โดยขั้นต่อต่อไปเราจะทำการสร้าง Service ใหม่ขึ้นมาหนึ่งตัวดังนี้ โดยกดที่
Service -> Add new service และให้ใส่รายละเอียดดังนี้
Name: registerGateway
Host: Private IP
Port: 7001
Path: /register
Pivate IP สามารถดูได้จากการใช้คำสั่ง ifconfig(unix)หรือ ipconfig(windows)
ต่อไปจะเป็นการสร้าง Route โดยกดที่ RegisterGateway ที่เพิ่งสร้างขึ้นมา และกดไปยังแถบ Routes -> Add Route

โดยใส่ข้อมูลดังนี้
Name: registerRoute
Path: /register
Method: POST
Protocols: http
เมื่อเสร็จแล้วสามารถลองยิง Request ได้ ผ่าน Postman หรืออื่นๆ


เพื่อไม่ให้ Client สามารถส่ง Request ด้วยการ Bypass Kong ไปยัง Internal Service โดยตรง จะต้องมีการปิด Port 7001 บน Firewall เมื่อมีการรัน Service บน Production Server
Authentication
Authentication คือการยืนยันตัวตน ซึ่ง Kong ก็มี Plugins ให้ใช้งาน โดยในกรณีนี้จะทดลองใช้งาน Basic Auth และ API Key โดยมีขั้นตอนดังนี้
สร้าง Consumer โดยไปที่ CONSUMERS -> CREATE CONSUMER
และใส่รายละเอียดลงไปและไปยัง
Credentials -> Basic Auth -> [username | password]
และกลับไปยัง Route -> registerRoute -> Plugins -> Basic Auth -> Add Plugins
และทดลองยิง Request ผ่าน Postman โดยมีการเพิ่ม Auth เข้าไปด้วย

ต่อไปจะเป็นการทดลองแบบ API Key โดยให้ Disable Basic Auth ด้วย
Credentials -> API KEYS -> Create API Key -> Submit
โดยหากปล่อยค่าว่างไว้ตัว Kong จะทำการ Generate ค่า key ให้เอง
และเหมือนเดิมคือ
กลับไปยัง Route -> registerRoute -> Plugins -> Key Auth -> Add Plugins
โดยกรอก key names ด้วย
และทดลองยิง Request อีกเช่นเดิม เพิ่มเติมคือเพิ่มในส่วนของ Auth with API Key

Key = key names
Value = API Keys
Rate Limiting
ด้วยความที่เป็น Gateway API และความสามารถของ Kong ทำให้เราสามารถกำหนด Rate Limit ในการเรียกใช้ API ได้ โดยมีขั้นตอนดังนี้
Route -> registerRoute -> Plugins -> Traffic Control -> Rate Limiting
โดยในบทความนี้ทดลองตั้งค่าไว้ที่ 5000 Request ต่อ ชั่วโมง ซึ่งหากอ่าน Header จาก Response ที่ตอบกลับมาก็จะเห็นดังนี้

Prometheus + Grafana

และสุดท้ายของบทความนี้จะทำระบบ Dashboard ต่างๆด้วย Prometheus ซึ่งมีหน้าที่คอยดึงข้อมูลให้แก่ Grafana
ก่อนอื่นจะต้องทำให้ Prometheus สามารถดึงข้อมูลผ่าน Kong ได้โดยผ่านการตั้งค่าผ่าน Kong ได้โดยดังนี้
CONSUMERS -> devops -> Plugins -> New Plugins -> Analytics & Monitoring -> Prometheus -> ADD PLUGIN
สร้าง Directory ชื่อ grafana_dock และเพิ่มไฟล์ดังนี้
docker-compose.yml
version: "2"services:
grafana:
image: grafana/grafana
restart: always
expose:
- "3000"
external_links:
- prometheus
environment:
GF_INSTALL_PLUGINS: grafana-clock-panel, grafana-simple-json-datasource
VIRTUAL_HOST: grafana.lab10.cpsudevops.com
VIRTUAL_PORT: 3000
LETSENCRYPT_HOST: grafana.lab10.cpsudevops.com
networks:
- webproxy
- defaultnetworks:
webproxy:
external:
name: webproxy
default:
external:
name: kong_network
หลังจากนั้นเข้าสู่ระบบ Grafana ด้วย
โดย Default username/password คือ admin ทั้งคู่ หลังจากเข้าสู่ระบบ ระบบจะให้ตั้ง password ใหม่

ต่อไปให้เลือก Add data source -> Prometheus
URL: http://prometheus:9090/
ต่อไปจะเพิ่ม Dashboard โดยไปที่ + -> Import Dashboard โดยที่ ID คือ 1860

และ Dashboard สำหรับ Kong Official ID คือ 7424

และสามารถหา Dashboard แบบสำเร็จรูปแบบอื่นๆได้จาก
Reference