Skip to content

Commit a68e76d

Browse files
committed
gobgpの導入
1 parent 1bac9ac commit a68e76d

23 files changed

+1038
-380
lines changed

cmd/db-controller/cli.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package main
1717
import (
1818
"flag"
1919
"fmt"
20+
"strings"
2021
)
2122

2223
var (
@@ -34,6 +35,16 @@ var (
3435
globalInterfaceNameFlag string
3536
// chainNameForDBAclFlag is a cli-flag that specifies the nftables chain name for DB access control list.
3637
chainNameForDBAclFlag string
38+
// bgpAsNumberFlag is a cli-flag that specifies the as number of ours
39+
bgpAsNumberFlag int
40+
// bgpServingPortFlag is a cli-flag that specifies the port of bgp
41+
bgpServingPortFlag int
42+
// bgpKeepaliveIntervalSecFlag is a cli-flag that specifies the interval seconds of bgp keepalive
43+
bgpKeepaliveIntervalSecFlag int
44+
// bgpPeerAddressesFlag is a cli-flag that specifies comma sparated peers of bgp.
45+
bgpPeerAddressesFlag string
46+
// gobgpGrpcPortFlag is a cli-flag that specifies port of gobgp gRPC
47+
gobgpGrpcPortFlag int
3748

3849
// mainPollingSpanSecondFlag is a cli-flag that specifies the span seconds of the loop in main.go.
3950
mainPollingSpanSecondFlag int
@@ -60,12 +71,17 @@ func parseAllFlags(args []string) error {
6071
fs.StringVar(&globalInterfaceNameFlag, "global-interface-name", "eth0", "the interface name of global")
6172
fs.StringVar(&chainNameForDBAclFlag, "chain-name-for-db-acl", "mariadb", "the chain name for DB access control")
6273
fs.StringVar(&dbReplicaUserNameFlag, "db-replica-user-name", "repl", "the username for replication")
74+
fs.StringVar(&bgpPeerAddressesFlag, "bgp-peer-addresses", "", "the peers of bgp")
6375

6476
fs.IntVar(&mainPollingSpanSecondFlag, "main-polling-span-second", 4, "the span seconds of the loop in main.go")
6577
fs.IntVar(&httpAPIServerPortFlag, "http-api-server-port", 54545, "the port the http api server listens")
6678
fs.IntVar(&prometheusExporterPortFlag, "prometheus-exporter-port", 50505, "the port the prometheus exporter listens")
6779
fs.IntVar(&dbReplicaSourcePortFlag, "db-replica-source-port", 13306, "the port of primary as replication source")
6880
fs.IntVar(&dbServingPortFlag, "db-serving-port", 3306, "the port of database service")
81+
fs.IntVar(&bgpAsNumberFlag, "bgp-as-number", 65001, "the as number of ours")
82+
fs.IntVar(&bgpServingPortFlag, "bgp-serving-port", 179, "the port of bgp")
83+
fs.IntVar(&bgpKeepaliveIntervalSecFlag, "bgp-keepalive-interval-sec", 3, "the interval seconds of bgp keepalive")
84+
fs.IntVar(&gobgpGrpcPortFlag, "gobgp-grpc-port", 50051, "the listen port of gobgp gRPC")
6985

7086
fs.BoolVar(&enablePrometheusExporterFlag, "prometheus-exporter", true, "enables the prometheus exporter")
7187
fs.BoolVar(&enableHTTPAPIFlag, "http-api", true, "enables the http api server")
@@ -83,6 +99,11 @@ func validateAllFlags() error {
8399
return fmt.Errorf("--prometheus-exporter-port must be the range of uint16(tcp port)")
84100
}
85101

102+
peers := strings.Split(bgpPeerAddressesFlag, ",")
103+
if len(peers) != 2 {
104+
return fmt.Errorf("insufficient bgp peer addresses: %s", bgpPeerAddressesFlag)
105+
}
106+
86107
return nil
87108
}
88109

cmd/db-controller/main.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/labstack/gommon/log"
3434
"github.com/prometheus/client_golang/prometheus/promhttp"
3535
apiv0 "github.com/sakura-internet/distributed-mariadb-controller/cmd/db-controller/api/v0"
36+
"github.com/sakura-internet/distributed-mariadb-controller/pkg/bgpserver"
3637
"github.com/sakura-internet/distributed-mariadb-controller/pkg/controller"
3738
"github.com/sakura-internet/distributed-mariadb-controller/pkg/nftables"
3839
"github.com/vishvananda/netlink"
@@ -66,13 +67,45 @@ func main() {
6667
panic(err)
6768
}
6869

69-
// prepare controller instance
70+
// get my global ip address
7071
myHostAddress, err := getNetIFAddress(globalInterfaceNameFlag)
7172
if err != nil {
7273
panic(err)
7374
}
7475
logger.Debug("host address", "address", myHostAddress)
7576

77+
// create context
78+
ctx, cancel := context.WithCancel(context.Background())
79+
defer cancel()
80+
81+
// start bgpserver
82+
logger.Debug("starting bgpserver", "hostaddress", myHostAddress)
83+
bgpServerConnect := bgpserver.NewDefaultConnector(
84+
logger,
85+
bgpserver.WithAsn(uint32(bgpAsNumberFlag)),
86+
bgpserver.WithRouterId(myHostAddress),
87+
bgpserver.WithListenPort(int32(bgpServingPortFlag)),
88+
bgpserver.WithGrpcPort(gobgpGrpcPortFlag),
89+
)
90+
if err := bgpServerConnect.Start(ctx); err != nil {
91+
panic(err)
92+
}
93+
94+
// add peers to bgpserver
95+
peers := strings.Split(bgpPeerAddressesFlag, ",")
96+
if len(peers) != 2 {
97+
panic("insufficient bgp peer addresses")
98+
}
99+
for _, v := range peers {
100+
logger.Debug("adding peer", "neighbor", v)
101+
bgpServerConnect.AddPeer(
102+
v,
103+
uint32(bgpAsNumberFlag),
104+
uint32(bgpServingPortFlag),
105+
uint64(bgpKeepaliveIntervalSecFlag),
106+
)
107+
}
108+
76109
dbReplicaPassword, err := readDBReplicaPassword(dbReplicaPasswordFilePathFlag)
77110
if err != nil {
78111
panic(err)
@@ -87,14 +120,11 @@ func main() {
87120
controller.WithDBReplicaPassword(dbReplicaPassword),
88121
controller.WithDBReplicaSourcePort(uint16(dbReplicaSourcePortFlag)),
89122
controller.WithDBAclChainName(chainNameForDBAclFlag),
123+
controller.WithBgpServerConnector(bgpServerConnect),
90124
)
91125

92126
// start goroutines
93-
ctx, cancel := context.WithCancel(context.Background())
94-
defer cancel()
95-
96127
wg := new(sync.WaitGroup)
97-
98128
wg.Add(1)
99129
go func(ctx context.Context, wg *sync.WaitGroup, c *controller.Controller) {
100130
defer wg.Done()

docs/operation-guide.md

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ Sakura-DBCは、内部的に以下の4つの状態を持ち、状況に応じて
3737
## BGP経路の属性
3838

3939
Sakura-DBCは、BGP経路のCommunity属性として表現することで、他のノードに自身の状態を広告します。
40-
41-
vtyshコマンドを経由してFRRouting bgpdの設定を変更し、経路広告を行います。
40+
データベースサーバでは、デーモンとして起動するdb-controllerにgobgpが組み込まれており、BGPピアを確立します。
4241

4342
| 状態 | BGP Community |
4443
| --------- | ------------- |
@@ -53,16 +52,16 @@ vtyshコマンドを経由してFRRouting bgpdの設定を変更し、経路広
5352
Sakura-DBCを起動するには以下のようにコマンドを入力します。
5453

5554
```
56-
[root@test-db1 ~]# systemctl start sakura-controller
57-
[root@test-db1 ~]# systemctl status sakura-controller
58-
sakura-controller.service - Database Controller
59-
Loaded: loaded (/etc/systemd/system/sakura-controller.service; enabled; vendor preset: disabled)
55+
# systemctl start db-controller
56+
# systemctl status db-controller
57+
db-controller.service - Database Controller
58+
Loaded: loaded (/etc/systemd/system/db-controller.service; enabled; vendor preset: disabled)
6059
Active: active (running) since Thu 2023-07-13 16:56:21 JST; 4s ago
61-
Main PID: 1391344 (sakura-controll)
60+
Main PID: 1391344 (db-controller)
6261
Tasks: 9 (limit: 24876)
6362
Memory: 5.5M
64-
CGroup: /system.slice/sakura-controller.service
65-
└─1391344 /root/distributed-mariadb-controller/bin/sakura-controller --log-level info --db-repilica-password-filepath /root/.db-replica-password
63+
CGroup: /system.slice/db-controller.service
64+
└─1391344 /root/distributed-mariadb-controller/bin/db-controller --log-level info --db-repilica-password-filepath /root/.db-replica-password
6665
<snip>
6766
```
6867

@@ -71,12 +70,12 @@ Sakura-DBCを起動するには以下のようにコマンドを入力します
7170
Sakura-DBCを停止するには以下のようにコマンドを入力します。
7271

7372
```
74-
[root@test-db1 ~]# systemctl stop sakura-controller
75-
[root@test-db1 ~]# systemctl status sakura-controller
73+
# systemctl stop db-controller
74+
# systemctl status db-controller
7675
● sakura-controller.service - Database Controller
77-
Loaded: loaded (/etc/systemd/system/sakura-controller.service; enabled; vendor preset: disabled)
76+
Loaded: loaded (/etc/systemd/system/db-controller.service; enabled; vendor preset: disabled)
7877
Active: inactive (dead) since Thu 2023-07-13 16:55:35 JST; 7s ago
79-
Process: 694 ExecStart=/root/distributed-mariadb-controller/bin/sakura-controller --log-level info --db-repilica-password-filepath /root/.db-replica-password (code=exited, status=0/SUCCESS)
78+
Process: 694 ExecStart=/root/distributed-mariadb-controller/bin/db-controller --log-level info --db-repilica-password-filepath /root/.db-replica-password (code=exited, status=0/SUCCESS)
8079
Main PID: 694 (code=exited, status=0/SUCCESS)
8180
<snip>
8281
```
@@ -86,15 +85,15 @@ Sakura-DBCを停止するには以下のようにコマンドを入力します
8685
Sakura-DBCは、状態遷移や、それに伴い実行したコマンドなどをログ出力します。ログを確認するにはjournalctlコマンドを利用します。
8786

8887
```
89-
journalctl -u sakura-controller -e
88+
# journalctl -u db-controller -e
9089
```
9190

9291
## 現在の内部状態の確認方法
9392

9493
Sakura-DBCの現在の状態を確認するには、curlコマンドなどで以下のエンドポイントをHTTPリクエストします。
9594

9695
```
97-
[root@test-db1 ~]# curl http://127.0.0.1:54545/status
96+
# curl http://127.0.0.1:54545/status
9897
{"state":"replica"}
9998
```
10099

@@ -104,7 +103,7 @@ Sakura-DBCがGSLBに対し、どのようにレスポンスを行っているか
104103

105104
```
106105
! primaryの場合(200 OKが返る)
107-
[root@test-db2 ~]# curl -v http://127.0.0.1:54545/healthcheck
106+
# curl -v http://127.0.0.1:54545/healthcheck
108107
* Connected to 127.0.0.1 (127.0.0.1) port 54545 (#0)
109108
> GET /healthcheck HTTP/1.1
110109
> Host: 127.0.0.1:54545
@@ -114,7 +113,7 @@ Sakura-DBCがGSLBに対し、どのようにレスポンスを行っているか
114113
< Content-Length: 0
115114
116115
! primary以外の場合(503 Service Unavailableが返る)
117-
[root@test-db1 ~]# curl -v http://127.0.0.1:54545/healthcheck
116+
# curl -v http://127.0.0.1:54545/healthcheck
118117
* Connected to 127.0.0.1 (127.0.0.1) port 54545 (#0)
119118
> GET /healthcheck HTTP/1.1
120119
> Host: 127.0.0.1:54545
@@ -126,10 +125,12 @@ Sakura-DBCがGSLBに対し、どのようにレスポンスを行っているか
126125

127126
## BGP経路の確認方法
128127

128+
### アンカーサーバ
129+
129130
BGPピアの状態を確認するには、以下のようにvtyshコマンドを用います。
130131

131132
```
132-
[root@test-db1 ~]# vtysh -c 'show ip bgp summary'
133+
# vtysh -c 'show ip bgp summary'
133134
134135
IPv4 Unicast Summary (VRF default):
135136
BGP router identifier xx.xx.xx.xx, local AS number 65001 vrf-id 0
@@ -147,7 +148,7 @@ Total number of neighbors 2
147148
BGP経路情報を確認するには、以下のようにvtyshコマンドを用います。
148149

149150
```
150-
[root@test-db1 ~]# vtysh -c 'show ip bgp'
151+
# vtysh -c 'show ip bgp'
151152
BGP table version is 4, local router ID is xx.xx.xx.xx, vrf id 0
152153
Default local pref 100, local AS 65001
153154
Status codes: s suppressed, d damped, h history, * valid, > best, = multipath,
@@ -167,7 +168,7 @@ RPKI validation codes: V valid, I invalid, N Not found
167168
168169
Displayed 3 routes and 5 total paths
169170
170-
[root@test-db1 ~]# vtysh -c 'show ip bgp community-list primary'
171+
# vtysh -c 'show ip bgp community-list primary'
171172
<snip>
172173
173174
Network Next Hop Metric LocPrf Weight Path
@@ -177,23 +178,47 @@ Displayed 3 routes and 5 total paths
177178
Displayed 1 routes and 5 total paths
178179
```
179180

181+
### DBサーバ
182+
183+
BGPピアの状態を確認するには、以下のようにgobgpコマンドを用います。
184+
db-controllerがgobgpのgRPCポート(50051)を待ち受けており、そこから情報が取得されます。
185+
186+
```
187+
# gobgp neighbor
188+
Peer AS Up/Down State |#Received Accepted
189+
xx.xx.xx.xx 65001 00:30:02 Establ | 1 1
190+
xx.xx.xx.xx 65001 00:31:05 Establ | 3 2
191+
```
192+
193+
BGP経路情報を確認するには、以下のようにgobgpコマンドを用います。
194+
195+
```
196+
# gobgp global rib
197+
Network Next Hop AS_PATH Age Attrs
198+
*> xx.xx.xx.xx/32 xx.xx.xx.xx 00:34:41 [{Origin: i} {Communities: 65001:3}]
199+
*> xx.xx.xx.xx/32 xx.xx.xx.xx 00:34:18 [{Origin: i} {LocalPref: 100} {Communities: 65001:4} {Originator: xx.xx.xx.xx} {ClusterList: [xx.xx.xx.xx]}]
200+
* xx.xx.xx.xx/32 xx.xx.xx.xx 00:34:35 [{Origin: i} {LocalPref: 100} {Communities: 65001:4} {Originator: xx.xx.xx.xx} {ClusterList: [xx.xx.xx.xx]}]
201+
*> xx.xx.xx.xx/32 xx.xx.xx.xx 00:00:00 [{Origin: i} {Med: 0} {LocalPref: 100} {Communities: 65001:10} {Originator: xx.xx.xx.xx} {ClusterList: [xx.xx.xx.xx]}]
202+
* xx.xx.xx.xx/32 xx.xx.xx.xx 00:35:20 [{Origin: i} {Med: 0} {LocalPref: 100} {Communities: 65001:10}]
203+
```
204+
180205
## ログレベルの変更方法
181206

182207
[クイックスタートガイド](quick-start-guide.md)の手順では、通常の運用において推奨されるinfoログレベルにて設定するようになっています。
183208
もし、ログレベルを変更するには、以下のようにします。
184209

185210
```
186-
vi /etc/systemd/system/sakura-controller.service
211+
# vi /etc/systemd/system/db-controller.service
187212
188-
! infoになっている部分を変更します
189-
ExecStart = /root/distributed-mariadb-controller/bin/sakura-controller --log-level info --db-repilica-password-filepath /root/.db-replica-password
213+
! ログレベルを変更します(debugに変更する場合)
214+
ExecStart = /root/distributed-mariadb-controller/bin/db-controller --log-level debug ...
190215
```
191216

192-
systemdに反映し、Sakura-DBCを再起動します
217+
systemdに反映し、db-controllerを再起動します
193218

194219
```
195-
systemctl daemon-reload
196-
systemctl restart sakura-controller
220+
# systemctl daemon-reload
221+
# systemctl restart db-controller
197222
```
198223

199224
指定可能なログレベルと、各レベルにおいて出力されるログの基準は以下の通りです。

docs/prometheus-setup.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,5 @@ systemctl start alertmanager
123123

124124
以下の場合に通知を行うような設定となっていますので、通知が届くかどうか確認してください。
125125

126-
- sakura-controllerデーモンがダウンしている場合(サーバがダウンしている状態も含む)
126+
- db-controllerデーモンがダウンしている場合(サーバがダウンしている状態も含む)
127127
- primary, replica以外の状態の場合

0 commit comments

Comments
 (0)