iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 23
1
Software Development

服務開發雜談系列 第 23

NATS & NATS Streaming 叢集安裝 By Docker Compose

  • 分享至 

  • xImage
  •  

今晚來透過Docker-compose,來建立2個節點的NATS Cluster + 3個節點的NATS Streaming Cluster.
方便後面使用.

version: "3.6"

services:
    nats-1:
        image: nats:2.1.8-alpine3.11
        networks:
            nat-cluster_net:
              ipv4_address: 172.16.230.100
        command: 
            - "--debug"
            - "--cluster"
            - "nats://0.0.0.0:6222"
            - "--http_port"
            - "8222"
            - "--port" 
            - "4222"
        ports: 
            - "14222:4222"
            - "18222:8222"
        expose: 
            - 4222
            - 8222
            - 6222
    nats-2:
        image: nats:2.1.8-alpine3.11
        networks:
            nat-cluster_net:
                ipv4_address: 172.16.230.101
        command: 
            - "--debug"
            - "--cluster" 
            - "nats://0.0.0.0:6222"
            - "--http_port"
            - "8222"
            - "--port" 
            - "4222"
            - "--routes" 
            - "nats://nats-1:6222"
        ports: 
            - "14223:4222"
            - "18223:8222"
        expose: 
            - 4222
            - 8222
            - 6222
        depends_on:
            - nats-1
    nats-streaming-1:
        image: nats-streaming:0.18.0-alpine
        networks:
            nat-cluster_net:
                ipv4_address: 172.16.230.105
        command: 
            - "--clustered"
            - "--nats_server"
            - "nats://nats-1:6222"
            - "--cluster_bootstrap"
            - "--cluster_node_id"
            - "node1"
            - "--cluster_log_path"
            - "/data/log"
            - "--cluster_raft_logging"
            - "--debug"
            - "--dir"
            - "/data/msg"
            - "--store"
            - "file"
            - "--cluster"
            - "nats://0.0.0.0:6222"
            - "--stan_debug"
            - "--http_port"
            - "8222"
            - "--hb_interval"
            - 2s
            - "--hb_fail_count"
            - "1"
            - "--hb_timeout"
            - 5s
        ports: 
            - "18224:8222"
        volumes:
            - "./nats-streaming-1:/data"
        depends_on:
            - nats-1
    nats-streaming-2:
        image: nats-streaming:0.18.0-alpine
        networks:
            nat-cluster_net:
                ipv4_address: 172.16.230.106
        command: 
            - "--clustered"
            - "--nats_server"
            - "nats://nats-1:6222"
            - "--cluster_node_id"
            - "node2"
            - "--cluster_log_path"
            - "/data/log"
            - "--cluster_raft_logging"
            - "--debug"
            - "--dir"
            - "/data/msg"
            - "--store"
            - "file"
            - "--stan_debug"
            - "--cluster"
            - "nats://0.0.0.0:6222"
            - "--routes"
            - "nats://nats-streaming-1:6222"
            - "--http_port"
            - "8222"
            - "--hb_interval"
            - 2s
            - "--hb_fail_count"
            - "1"
            - "--hb_timeout"
            - 5s
        ports: 
            - "18225:8222"
        volumes:
            - "./nats-streaming-2:/data"
        depends_on:
            - nats-1
            - nats-streaming-1
    nats-streaming-3:
        image: nats-streaming:0.18.0-alpine
        networks:
            nat-cluster_net:
                ipv4_address: 172.16.230.107
        command: 
            - "--clustered"
            - "--nats_server"
            - "nats://nats-1:6222"
            - "--cluster_node_id"
            - "node3"
            - "--cluster_log_path"
            - "/data/log"
            - "--cluster_raft_logging"
            - "--debug"
            - "--dir"
            - "/data/msg"
            - "--store"
            - "file"
            - "--stan_debug"
            - "--cluster"
            - "nats://0.0.0.0:6222"
            - "--routes"
            - "nats://nats-streaming-1:6222"
            - "--http_port"
            - "8222"
            - "--hb_interval"
            - 2s
            - "--hb_fail_count"
            - "1"
            - "--hb_timeout"
            - 5s
        ports: 
            - "18226:8222"
        volumes:
            - "./nats-streaming-3:/data"
        depends_on:
            - nats-1
            - nats-streaming-1
networks:
    nat-cluster_net:
        driver: bridge
        ipam:
            driver: default
            config:
            -
                subnet: 172.16.230.0/24

參數說明

主要有3個Port422282226222
4222主要是給Client使用的.
8222是給Monitor的http API用的
6222就是給內部叢集用的.

--cluster_id用來指定cluster id, 預設是test-cluster
--routes用來加入自己到指定的cluster主節點.
--cluster 用來指定自己listen哪個Port, 來處理cluster的請求連入
--store則是有MEMORY/FILE/SQL, 可以來存放raft log, 預設是MEMORY
--dir因為這裡是選擇FILE, 所以透過dir指定log存放位子
--sql_driver如果選擇是SQL, 則這裡有mysql/postgres的驅動選項能選擇
MySQL Schema:

CREATE TABLE IF NOT EXISTS ServerInfo (uniquerow INT DEFAULT 1, id VARCHAR(1024), proto BLOB, version INTEGER, PRIMARY KEY (uniquerow));
CREATE TABLE IF NOT EXISTS Clients (id VARCHAR(1024), hbinbox TEXT, PRIMARY KEY (id(256)));
CREATE TABLE IF NOT EXISTS Channels (id INTEGER, name VARCHAR(1024) NOT NULL, maxseq BIGINT UNSIGNED DEFAULT 0, maxmsgs INTEGER DEFAULT 0, maxbytes BIGINT DEFAULT 0, maxage BIGINT DEFAULT 0, deleted BOOL DEFAULT FALSE, PRIMARY KEY (id), INDEX Idx_ChannelsName (name(256)));
CREATE TABLE IF NOT EXISTS Messages (id INTEGER, seq BIGINT UNSIGNED, timestamp BIGINT, size INTEGER, data BLOB, CONSTRAINT PK_MsgKey PRIMARY KEY(id, seq), INDEX Idx_MsgsTimestamp (timestamp));
CREATE TABLE IF NOT EXISTS Subscriptions (id INTEGER, subid BIGINT UNSIGNED, lastsent BIGINT UNSIGNED DEFAULT 0, proto BLOB, deleted BOOL DEFAULT FALSE, CONSTRAINT PK_SubKey PRIMARY KEY(id, subid));
CREATE TABLE IF NOT EXISTS SubsPending (subid BIGINT UNSIGNED, `row` BIGINT UNSIGNED, seq BIGINT UNSIGNED DEFAULT 0, lastsent BIGINT UNSIGNED DEFAULT 0, pending BLOB, acks BLOB, CONSTRAINT PK_MsgPendingKey PRIMARY KEY(subid, `row`), INDEX Idx_SubsPendingSeq(seq));
CREATE TABLE IF NOT EXISTS StoreLock (id VARCHAR(30), tick BIGINT UNSIGNED DEFAULT 0);

# Updates for 0.10.0
ALTER TABLE Clients ADD proto BLOB;

Postgres Schema:

CREATE TABLE IF NOT EXISTS ServerInfo (uniquerow INTEGER DEFAULT 1, id VARCHAR(1024), proto BYTEA, version INTEGER, PRIMARY KEY (uniquerow));
CREATE TABLE IF NOT EXISTS Clients (id VARCHAR(1024), hbinbox TEXT, PRIMARY KEY (id));
CREATE TABLE IF NOT EXISTS Channels (id INTEGER, name VARCHAR(1024) NOT NULL, maxseq BIGINT DEFAULT 0, maxmsgs INTEGER DEFAULT 0, maxbytes BIGINT DEFAULT 0, maxage BIGINT DEFAULT 0, deleted BOOL DEFAULT FALSE, PRIMARY KEY (id));
CREATE INDEX Idx_ChannelsName ON Channels (name(256));
CREATE TABLE IF NOT EXISTS Messages (id INTEGER, seq BIGINT, timestamp BIGINT, size INTEGER, data BYTEA, CONSTRAINT PK_MsgKey PRIMARY KEY(id, seq));
CREATE INDEX Idx_MsgsTimestamp ON Messages (timestamp);
CREATE TABLE IF NOT EXISTS Subscriptions (id INTEGER, subid BIGINT, lastsent BIGINT DEFAULT 0, proto BYTEA, deleted BOOL DEFAULT FALSE, CONSTRAINT PK_SubKey PRIMARY KEY(id, subid));
CREATE TABLE IF NOT EXISTS SubsPending (subid BIGINT, row BIGINT, seq BIGINT DEFAULT 0, lastsent BIGINT DEFAULT 0, pending BYTEA, acks BYTEA, CONSTRAINT PK_MsgPendingKey PRIMARY KEY(subid, row));
CREATE INDEX Idx_SubsPendingSeq ON SubsPending (seq);
CREATE TABLE IF NOT EXISTS StoreLock (id VARCHAR(30), tick BIGINT DEFAULT 0);

-- Updates for 0.10.0
ALTER TABLE Clients ADD proto BYTEA;

--hb_*一些heart beat選項設定

官方NATS選項
官方NATS Streaming選項
Docker NATS選項
Docker NATS Streaming選項

建立成功後, 打開瀏覽器輸入localhost:18222-localhost:18226都要能看到Monitor頁面才是.

有環境後, 之後才好操作!


上一篇
NATS & NATS Streaming介紹
下一篇
NATS Client使用&支持場景
系列文
服務開發雜談33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言