-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis-setup.sh
55 lines (45 loc) · 1.82 KB
/
redis-setup.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
#!/bin/bash
# Extract the VPC private ip address (digital ocean)
export PRIVATE_IP=$(curl -s http://169.254.169.254/metadata/v1/interfaces/private/0/ipv4/address)
# Get the total available memory in MB
export TOTAL_MEM_MB=$(free -m | grep Mem | awk '{print $2}')
# Use all expect 300MB memory for redis
export REDIS_MEM_MB=$((TOTAL_MEM_MB - 300))
# Update the system
apt -y update
# Set overcommit to 1, Red details at:
# https://engineering.pivotal.io/post/virtual_memory_settings_in_linux_-_the_problem_with_overcommit/
sysctl vm.overcommit_memory=1
echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
# Disable THP (Transparent Huge Pages) for redis efficiency
# Make sure the THP is disabled within restarts
echo "
[Unit]
Description=Disable Transparent Huge Pages (THP)
DefaultDependencies=no
After=sysinit.target local-fs.target
Before=redis.service
[Service]
Type=oneshot
ExecStart=/bin/sh -c 'echo never | tee /sys/kernel/mm/transparent_hugepage/enabled > /dev/null'
[Install]
WantedBy=basic.target
" > /etc/systemd/system/disable-transparent-huge-pages.service
systemctl daemon-reload
systemctl enable disable-transparent-huge-pages
systemctl start disable-transparent-huge-pages
# Install the redis server (5.0)
apt install -y redis-server
# Update configuration to run via systemd
sed -i "s/^supervised .*/supervised systemd/g" /etc/redis/redis.conf
# Change bind address to the private VPC
sed -i "s/^bind .*/bind $PRIVATE_IP/g" /etc/redis/redis.conf
# Allow connecting from remote clients
sed -i "s/^protected-mode yes/protected-mode no/g" /etc/redis/redis.conf
# Setup the eviction policy to lru
sed -i "s/^# maxmemory-policy .*/maxmemory-policy allkeys-lru/g" /etc/redis/redis.conf
sed -i "s/^# maxmemory .*/maxmemory ${REDIS_MEM_MB}mb/g" /etc/redis/redis.conf
# Restart
systemctl daemon-reload
systemctl enable redis
systemctl restart redis