-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmongodb-repl-setup2.sh
376 lines (271 loc) · 11.8 KB
/
mongodb-repl-setup2.sh
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/bin/bash
# Pre-requisites
# Install linux vm with attached data disks
# Script based on 4x Data disks
Install_step1()
{
# Enable swap file on the linux machine through azure agent file waagent.conf and disable selinux using the ex search replace editor
ex -s +%s/ResourceDisk.EnableSwap=n/ResourceDisk.EnableSwap=y/g +%p +x /etc/waagent.conf
ex -s +%s/ResourceDisk.SwapSizeMB=0/ResourceDisk.SwapSizeMB=5120/g +%p +x /etc/waagent.conf
ex -s +%s/SELINUX=enforcing/SELINUX=disabled/g +%p +x /etc/selinux/config
echo 0 > /selinux/enforce
}
Install_step2()
{
#create a var listing available disks to partition
#hdd=$(find /dev/sd* ! -path "/dev/sda*" ! -path "/dev/sdb*")
for i in $hdd;do
echo "n
p
1
t
fd
w
"|fdisk $i;done
# Create a directory which you want to mount to the new disk. mkdir /data_disk
mkdir /data_disk
# Change Permissions
chmod 755 /data_disk
}
#using the find command we search for available disks to raid. using ! to exclude wilcard path sda and sdb.
#We also then check the hdd list for *1* which would indicate existing partition and log. if not we call a function to raid available disks.
Install_step2a()
{
hdd=$(find /dev/sd* ! -path "/dev/sda*" ! -path "/dev/sdb*")
#if [[ $hdd == *1* ]] && echo "It's there" || echo "Couldn't find"
[[ $hdd == *1* ]] && echo "Cannot use. It contains existing partition!" 1>&2 >./mdadm.log || Install_step2
}
Install_step3()
{
#count available devicees to raid
devicecount=$(find /dev/sd*1 ! -path "/dev/sda*" ! -path "/dev/sdb*" | wc -l)
#list available devices to raid
raiddevices=$(find /dev/sd*1 ! -path "/dev/sda*" ! -path "/dev/sdb*")
# install mdam
yum -y install mdadm
# Create the raid device using 4 data disks- DataRaid
mdadm --create /dev/md127 --level 0 --raid-devices $devicecount $raiddevices
# Create the file system on the new RAID device
mkfs.xfs /dev/md127
}
Install_step4()
{
UUID=`lsblk -no UUID /dev/md127`
until [ -n "$UUID" ]; do
sleep 30s
UUID=`lsblk -no UUID /dev/md127`;
done
sed -i:bak "/UUID/a\UUID=$UUID /data_disk xfs defaults,noatime 0 2" /etc/fstab
mount -a
}
Install_step5()
{
# Install Mongo
# Create MongoDB repo
# Pre version 3.2
# echo '
# [mongodb-org-3.0]
# name=MongoDb Repository
# baseurl=http://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.0/x86_64/
# gpgcheck=0
# enabled=1' > /etc/yum.repos.d/mongodb-org-3.0.repo
# 3.2 and later
echo '[mongodb-org-3.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.2.asc' > /etc/yum.repos.d/mongodb-org-3.2.repo
# 3.4 and later
echo '[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc' > /etc/yum.repos.d/mongodb-org-3.4.repo
# Install MongoDB
yum install -y mongodb-org
# Exclude MongoDB from /etc/yum.conf to avoid unintended upgrades of MongoDB
# three options, either place after last line or after specific text pattern (this you must know and hope it does not change)
# or insert after first blank line (must know your file current structure)
# ref http://www.linuxquestions.org/questions/programming-9/insert-line-on-match-only-once-with-sed-657764/
# append after last line
# sed -i:bak '$ a\exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools' /etc/yum.conf
# append after PATTERN /distroverpkg=centos-release/ found
# sed -i:bak '/distroverpkg=centos-release/a\exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools' /etc/yum.conf
# inserting above first open line
sed -i:bak '1,/^$/ {/^$/i\
exclude=mongodb-org,mongodb-org-server,mongodb-org-shell,mongodb-org-mongos,mongodb-org-tools
}' /etc/yum.conf
# Change the mongod.conf file to use yaml startup script with custom config
# delete all file content
ex -s +%d +%p +x /etc/mongod.conf
# insert yaml config
echo 'systemLog:
destination: file
path: "/data_disk/mongo/logs/mongod.log"
logAppend: true
logRotate: rename
storage:
dbPath: "/data_disk/mongo/data"
engine: wiredTiger
directoryPerDB: true
journal:
enabled: true
processManagement:
fork: true
pidFilePath: "/var/run/mongodb/mongod.pid"
net:
port: 27017
replication:
replSetName: "rs0"' > /etc/mongod.conf
# Create Mongo directories
mkdir /data_disk/mongo
mkdir /data_disk/mongo/logs
mkdir /data_disk/mongo/data
# change ownership and group
chown -R mongod:mongod /data_disk
chown -R mongod:mongod /data_disk/mongo
chown -R mongod:mongod /data_disk/mongo/logs
chown -R mongod:mongod /data_disk/mongo/data
# setup log rotation by using linux cron job to run bash script
# create a scripts folder
mkdir /etc/scripts/
# first create your bash script.
# https://disqus.com/home/discussion/mongodb/logging_mongodb_10gen_confluence/
echo '#!/bin/bash
# This script below will rename the mongod log file with the date appended and open a new log file;
# we will then search for renamed log files older than 5 days and zip --removed this step, created new cron job
# maintenance- we will then find renamed files older than 31 days and delete --removed this step, created new cron job
#/bin/kill -SIGUSR1 `cat /data_disk/mongo/data/mongod.lock 2> /dev/null`
mongo --eval "db.getMongo().getDB(\"admin\").runCommand(\"logRotate\")";
#find /data_disk/mongo/logs/mongod.log.* -mtime +5 -execdir zip '{}'.zip '{}' -m -x *.zip \;
#find /data_disk/mongo/logs/mongod.log.* -mtime +31 -exec rm {} \;' > /etc/scripts/mongologrotation.sh
cat /etc/scripts/mongologrotation.sh
# now create the cron job to run the log rotation bash script /etc/scripts/mongologrotation.sh
# append after last line
echo '5 5 * * * root /etc/scripts/mongologrotation.sh' >> /etc/crontab
#echo '5 0 * * * /etc/scripts/mongologrotation.sh'>/etc/scripts/mongologrotation.txt
chmod 755 /etc/scripts/mongologrotation.sh
#chmod 755 mongologrotation.txt
#crontab /etc/scripts/mongologrotation.txt
#crontab -l
echo '#!/bin/bash
# This script below will rename the mongod log file with the date appended and open a new log file --running in own cron jojb now
# we will search for renamed log files older than 5 days and zip
# maintenance- we will then find renamed files older than 31 days and delete
#/bin/kill -SIGUSR1 `cat /data_disk/mongo/data/mongod.lock 2> /dev/null`
#mongo --eval "db.getMongo().getDB(\"admin\").runCommand(\"logRotate\")";
find /data_disk/mongo/logs/mongod.log.* -mtime +5 -execdir zip '{}'.zip '{}' -m -x *.zip \;
find /data_disk/mongo/logs/mongod.log.* -mtime +31 -exec rm {} \;' > /etc/scripts/mongologcleanup.sh
cat /etc/scripts/mongologcleanup.sh
# now create the cron job to run the log cleanup bash script /etc/scripts/mongologcleanup.sh
# append after last line
echo '6 5 * * * root /etc/scripts/mongologcleanup.sh' >> /etc/crontab
#echo '5 0 * * * /etc/scripts/mongologcleanup.sh'>/etc/scripts/mongologcleanup.txt
chmod 755 /etc/scripts/mongologcleanup.sh
#chmod 755 /etc/scripts/mongologcleanup.txt
#crontab /etc/scripts/mongologcleanup.txt
#crontab -l
#chmod 755 mongologrotation.sh
# Note: This will install the cron-file.txt to your crontab, which will also remove your old cron entries.
# So, please be careful while uploading cron entries from a cron-file.txt.
# ref http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/
}
Install_step6()
{
# Disable THP is per mongo best practice
# Create a new custom tuned profile by copying the default tuned profile (virtual-guest in this case)
# Use the copied conf file and edit according to your need and have it echo back into your new custom profile config file
mkdir /usr/lib/tuned/no-thp
cp -r /usr/lib/tuned/virtual-guest/* /usr/lib/tuned/no-thp
# Change the /usr/lib/tuned/no-thp/tuned.conf file to include custom parameters [VM] [Disk] to echo back below
# delete all file content
ex -s +%d +%p +x /usr/lib/tuned/no-thp/tuned.conf
# insert custom config to echo back into /usr/lib/tuned/no-thp/tuned.conf
echo "#
# tuned configuration
#
[main]
include=throughput-performance
[sysctl]
# If a workload mostly uses anonymous memory and it hits this limit, the entire
# working set is buffered for I/O, and any more write buffering would require
# swapping, so it's time to throttle writes until I/O can catch up. Workloads
# that mostly use file mappings may be able to use even higher values.
#
# The generator of dirty data starts writeback at this percentage (system defaul
# is 20%)
vm.dirty_ratio = 30
[vm]
transparent_hugepages=never
[disk]
readahead=>256
# Filesystem I/O is usually much more efficient than swapping, so try to keep
# swapping low. It's usually safe to go even lower than this on systems with
# server-grade storage.
vm.swappiness = 30" > /usr/lib/tuned/no-thp/tuned.conf
# enable custom tuned profile
tuned-adm profile no-thp
# check active profiles
tuned-adm active
# Disable THP-defrag using systemd
# Make a directory for your systemd scripts
mkdir /etc/systemd/system/scripts
# create your script file /etc/systemd/system/scripts/defrag.sh and echo your settings
echo '#!/bin/sh
#Script to disabble THP and defrag of it
if test -f /sys/kernel/mm/transparent_hugepage/khugepaged/defrag; then
echo 0 > /sys/kernel/mm/transparent_hugepage/khugepaged/defrag
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi' > /etc/systemd/system/scripts/defrag.sh
cat /etc/systemd/system/scripts/defrag.sh
# Change permission for your defrag.sh file
chmod 755 /etc/systemd/system/scripts/defrag.sh
# create your systemd service to disable THP-defrag on bootup
# /etc/systemd/system/defrag.service and echo your settings
echo '[Unit]
Description=Disable-THP-Defrag
[Service]
ExecStart=/etc/systemd/system/scripts/defrag.sh
[Install]
WantedBy=multi-user.target' > /etc/systemd/system/defrag.service
cat /etc/systemd/system/defrag.service
# Change permission on /etc/systemd/system/defrag.service
chmod 755 /etc/systemd/system/defrag.service
# Enable the new systemd service /etc/systemd/system/defrag.service
systemctl enable defrag.service
# view service status (might require reboot first
systemctl status defrag.service
# Edit Ulimit as per mongo best practice, create new profile containing your new limits in /etc/security/limits.d/99-mongodb-nproc.conf
echo '#Ulimit for open files
* soft nofile 64000
* hard nofile 64000
#Ulimit for processes/threads
* soft nproc 64000
* hard nproc 64000' > /etc/security/limits.d/99-mongodb-nproc.conf
cat /etc/security/limits.d/99-mongodb-nproc.conf
# Set keep alive to 120 as per mongodb production notes
sed -i:bak '$ a\net.ipv4.tcp_keepalive_time = 120' /etc/yum.conf /etc/sysctl.conf
}
Install_step7()
{
yum -y install https://cloud.mongodb.com/download/agent/automation/mongodb-mms-automation-agent-manager-3.1.0.1831-1.x86_64.rhel7.rpm
sed -i:bak 's/mmsGroupId=/mmsGroupId=57e3e80c3b34b92da9e86270/' /etc/mongodb-mms/automation-agent.config
sed -i:bak 's/mmsApiKey=/mmsApiKey=8089dd927f145674473ed02fbb954ca7/' /etc/mongodb-mms/automation-agent.config
systemctl enable mongodb-mms-automation-agent.service
systemctl start mongodb-mms-automation-agent.service
}
sudo sed -i:bak 's/Defaults requiretty/#Defaults requiretty/' /etc/sudoers
Install_step1
Install_step2a #depends on Install_step2
Install_step3
Install_step4
Install_step5
Install_step6
#Install_step7