-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmulti.tf
348 lines (292 loc) · 10.2 KB
/
multi.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
### 概要
# DBやNFSアプライアンスも利用した複合構成を構築するサンプルテンプレート
#
# このテンプレートはWebサーバを2台構成してLBにて負荷分散する構成になっています。
# また、DBアプライアンスを利用してDBを構築します。
# ローカル側にはVPCルータを経由しL2TP-IPSecで接続する構成です。
# ローカルサーバはファイルサーバとして利用を想定してNFSアプライアンスを構築します。
#
# ホスト名やrootパスワードの設定などは"構成構築"タブ内をご覧ください。
# WebサーバのローカルIPはリモートコンソールより手動での設定をお願いします。
#
# 構成
# Webサーバ×2台、DBアプライアンス×1台、RT+SW×1台、SW×1台、LB×1台
# VPCルータ×1台、Fileサーバ×1台、NFS×1台
#
# -----------------------------------------------
# 変数定義
# -----------------------------------------------
# L2TP/IPSec 事前共有キー
variable "pre_shared_secret" {
default = "PutYourSecret"
}
# L2TP/IPSec ユーザー名/パスワード
variable "vpn_username" {
default = "PutYourName"
}
variable "vpn_password" {
default = "PutYourPassword"
}
# サーバー管理者パスワード
variable "server_password" {
default = "PutYourPassword"
}
# Webサーバ1のホスト名
variable "server01_hostname" {
default = "your-hostname1"
}
# Webサーバ2のホスト名
variable "server02_hostname" {
default = "your-hostname2"
}
# Fileサーバのホスト名
variable "file_hostname" {
default = "file-hostname"
}
### 構成構築
# -----------------------------------------------
# スイッチ+ルーター+VPCルーター
# -----------------------------------------------
resource "sakuracloud_internet" "router" {
name = "wan_switch"
}
resource "sakuracloud_load_balancer" "lb" {
switch_id = sakuracloud_internet.router.switch_id
high_availability = false
plan = "standard"
vrid = 1
ipaddress1 = sakuracloud_internet.router.ipaddresses[0]
ipaddress2 = sakuracloud_internet.router.ipaddresses[1]
nw_mask_len = sakuracloud_internet.router.nw_mask_len
default_route = sakuracloud_internet.router.gateway
name = "load_balancer"
}
resource "sakuracloud_switch" "sw01" {
name = "lacal_switch"
}
resource "sakuracloud_vpc_router" "vpc" {
name = "vpc_router"
}
resource "sakuracloud_vpc_router_interface" "eth1" {
vpc_router_id = sakuracloud_vpc_router.vpc.id
index = 1
switch_id = sakuracloud_switch.sw01.id
ipaddress = ["192.168.100.1"] // VPCルータIPアドレスの設定
nw_mask_len = 24 // ネットワークマスク
}
resource "sakuracloud_vpc_router_l2tp" "l2tp" {
vpc_router_id = sakuracloud_vpc_router.vpc.id
vpc_router_interface_id = sakuracloud_vpc_router_interface.eth1.id
pre_shared_secret = var.pre_shared_secret
range_start = "192.168.100.251" // IPアドレス動的割り当て範囲(開始)
range_stop = "192.168.100.254" // IPアドレス動的割り当て範囲(終了)
}
resource "sakuracloud_vpc_router_user" "user1" {
vpc_router_id = sakuracloud_vpc_router.vpc.id
name = var.vpn_username
password = var.vpn_password
}
resource "sakuracloud_vpc_router_port_forwarding" "forward_http" {
vpc_router_id = sakuracloud_vpc_router.vpc.id
vpc_router_interface_id = sakuracloud_vpc_router_interface.eth1.id
protocol = "tcp"
global_port = 80
private_address = "192.168.100.101"
private_port = 80
}
resource "sakuracloud_vpc_router_port_forwarding" "forward_https" {
vpc_router_id = sakuracloud_vpc_router.vpc.id
vpc_router_interface_id = sakuracloud_vpc_router_interface.eth1.id
protocol = "tcp"
global_port = 443
private_address = "192.168.100.101"
private_port = 443
}
# -----------------------------------------------
# ロードバランサーの設定
# -----------------------------------------------
resource "sakuracloud_load_balancer_vip" "vip1" {
load_balancer_id = sakuracloud_load_balancer.lb.id
vip = sakuracloud_internet.router.ipaddresses[2]
port = 80
delay_loop = 10
sorry_server = sakuracloud_internet.router.ipaddresses[3]
}
resource "sakuracloud_load_balancer_server" "server01" {
load_balancer_vip_id = sakuracloud_load_balancer_vip.vip1.id
ipaddress = sakuracloud_internet.router.ipaddresses[4]
check_protocol = "ping"
}
resource "sakuracloud_load_balancer_server" "server02" {
load_balancer_vip_id = sakuracloud_load_balancer_vip.vip1.id
ipaddress = sakuracloud_internet.router.ipaddresses[5]
check_protocol = "ping"
}
resource "sakuracloud_note" "lb_dsr" {
name = "lb_dsr"
content = <<EOF
PARA1="${sakuracloud_internet.router.ipaddresses[2]}"
PARA2="net.ipv4.conf.all.arp_ignore = 1"
PARA3="net.ipv4.conf.all.arp_announce = 2"
PARA4="DEVICE=lo:0"
PARA5="IPADDR="$PARA1
PARA6="NETMASK=255.255.255.255"
VERSION=$(rpm -q centos-release --qf %%{VERSION}) || exit 1
case "$VERSION" in
6 ) ;;
7 ) firewall-cmd --add-service=http --zone=public --permanent
firewall-cmd --reload;;
* ) ;;
esac
cp --backup /etc/sysctl.conf /tmp/ || exit 1
echo $PARA2 >> /etc/sysctl.conf
echo $PARA3 >> /etc/sysctl.conf
sysctl -p 1>/dev/null
cp --backup /etc/sysconfig/network-scripts/ifcfg-lo:0 /tmp/ 2>/dev/null
touch /etc/sysconfig/network-scripts/ifcfg-lo:0
echo $PARA4 > /etc/sysconfig/network-scripts/ifcfg-lo:0
echo $PARA5 >> /etc/sysconfig/network-scripts/ifcfg-lo:0
echo $PARA6 >> /etc/sysconfig/network-scripts/ifcfg-lo:0
ifup lo:0 || exit 1
exit 0
EOF
}
resource "sakuracloud_note" "install_httpd" {
name = "install_httpd"
content = <<EOF
yum install -y httpd || exit 1
echo 'This is a TestPage!!' >> /var/www/html/index.html || exit1
systemctl enable httpd.service || exit 1
systemctl start httpd.service || exit 1
firewall-cmd --add-service=http --zone=public --permanent || exit 1
exit 0
EOF
}
# ----------------------------------------------------------
# サーバーで利用するパブリックアーカイブ(CentOS7)
# ----------------------------------------------------------
data "sakuracloud_archive" "centos" {
os_type = "centos"
}
# ----------------------------------------------------------
# パケットフィルタの設定
# ----------------------------------------------------------
resource "sakuracloud_packet_filter" "www" {
name = "www"
expressions {
protocol = "tcp"
source_port = "0-65535"
dest_port = "80"
allow = true
}
expressions {
protocol = "tcp"
source_port = "0-65535"
dest_port = "443"
allow = true
}
expressions {
protocol = "icmp"
source_nw = sakuracloud_internet.router.ipaddresses[0]
allow = true
}
expressions {
protocol = "ip"
source_nw = "0.0.0.0/0"
allow = false
description = "Deny all"
}
}
# ----------------------------------------------------------
# サーバー構築
# ----------------------------------------------------------
resource "sakuracloud_disk" "disk01" {
name = "disk01"
plan = "ssd"
size = 40
source_archive_id = data.sakuracloud_archive.centos.id
}
resource "sakuracloud_server" "web-server01" {
name = "web-server01"
disks = [sakuracloud_disk.disk01.id]
nic = sakuracloud_internet.router.switch_id
ipaddress = sakuracloud_internet.router.ipaddresses[4]
gateway = sakuracloud_internet.router.gateway
nw_mask_len = sakuracloud_internet.router.nw_mask_len
packet_filter_ids = [sakuracloud_packet_filter.www.id]
additional_nics = [sakuracloud_switch.sw01.id]
core = 2
memory = 2
hostname = var.server01_hostname
password = var.server_password
note_ids = [sakuracloud_note.lb_dsr.id, sakuracloud_note.install_httpd.id]
}
resource "sakuracloud_disk" "disk02" {
name = "disk02"
plan = "ssd"
size = 40
source_archive_id = data.sakuracloud_archive.centos.id
}
resource "sakuracloud_server" "web-server02" {
name = "web-server02"
disks = [sakuracloud_disk.disk02.id]
nic = sakuracloud_internet.router.switch_id
ipaddress = sakuracloud_internet.router.ipaddresses[5]
gateway = sakuracloud_internet.router.gateway
nw_mask_len = sakuracloud_internet.router.nw_mask_len
packet_filter_ids = [sakuracloud_packet_filter.www.id]
additional_nics = [sakuracloud_switch.sw01.id]
core = 2
memory = 2
hostname = var.server02_hostname
password = var.server_password
note_ids = [sakuracloud_note.lb_dsr.id, sakuracloud_note.install_httpd.id]
}
resource "sakuracloud_disk" "disk03" {
name = "disk03"
plan = "ssd"
size = 40
source_archive_id = data.sakuracloud_archive.centos.id
}
resource "sakuracloud_server" "file-server" {
name = "file-server"
disks = [sakuracloud_disk.disk03.id]
nic = sakuracloud_switch.sw01.id
ipaddress = "192.168.100.101"
nw_mask_len = 24
gateway = "192.168.100.250"
core = 2
memory = 2
hostname = var.file_hostname
password = var.server_password
note_ids = [sakuracloud_note.lb_dsr.id, sakuracloud_note.install_httpd.id]
}
# ----------------------------------------------------------
# データベースアプライアンス
# ----------------------------------------------------------
resource "sakuracloud_database" "webdb" {
database_type = "mariadb"
plan = "30g"
user_name = "dbusername" // データベース接続用アカウント
user_password = var.server_password
port = 3306
backup_time = "00:00"
backup_weekdays = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
switch_id = sakuracloud_switch.sw01.id
ipaddress1 = "192.168.100.11"
nw_mask_len = 24
default_route = "192.168.100.250"
name = "webdb"
}
# ----------------------------------------------------------
# NFSアプライアンス
# ----------------------------------------------------------
resource "sakuracloud_nfs" "fs" {
name = "fs"
switch_id = sakuracloud_switch.sw01.id
plan = "hdd" //プラン[hdd/ssd]
size = "500" //サイズ[100/500/1024(1T)/2048(2T)/4096(4T)]
ipaddress = "192.168.100.102"
nw_mask_len = 24
default_route = "192.168.100.250"
}