前言
我很久之前做过简单的redis 运维监控,当然实现思路就是 操作缓存的时候在内存里累加值,用到的技术是spring boot +html 模板。很简单。肯定不能上生产。简单的统计一些缓存的数据作为系统的监控(毕竟缓存不可靠性要求不高,一般不去care)。正好这次公司内部推荐使用cc(cache cloud),它是专门负责缓存的运维、监控操作。让我们一起去看看吧。
一 急速入门
1.1 拉取源码
git clone git@github.com:sohutv/cachecloud.git
1.2 初始化数据库
初始化代码里的sql
../cachecloud-web/sql/cc2.0.sql
即如下:
1.3 配置maven 启动-官网推荐
我这里配置了两个mvn命令
- install
- run
- 更改数据库密码
1.4 springboot 启动-个人推荐
1.5 代码测试
1.6 代码调试
1.7 遇到问题
因为我一向是拥抱开源(逼格高),使用的是openjdk8 ,导致我编译源码直接报错因为 openjdk 8 对javafx不支持(openjdk9 之后已经集成了FX,当然openjdk 8 可以将FX打进来,比较麻烦)。然后果断换回了oracle jdk8环境 (浪费了好长时间)
二 机器管理
2.1 初始化机器
vi /etc/ssh/sshd_config
service sshd restart
2.1.1 创建 ssh key
ssh-keygen.sh 执行该脚本生成key
# create public key & secret key
createSshkey() {
mkdir -p /opt/ssh
chown -R $1:$2 /opt/ssh
ssh-keygen -t rsa -f /opt/ssh/id_rsa -P '' -C $1
local privateKeyFile="/opt/ssh/id_rsa"
chmod 600 ${privateKeyFile}
mkdir -p /home/$1/.ssh
local publicKeyFile="/home/$1/.ssh/authorized_keys"
cat /opt/ssh/id_rsa.pub >>${publicKeyFile}
chown -R $1:$2 /home/$1/.ssh
chmod 755 ${publicKeyFile}
echo -e "\033[41;36m OK: create public key & secret key done. \033[0m"
}
# let's go
username="cachecloud-open"
password="cachecloud-open"
if [[ $# > 0 && -n "$1" ]]; then
username="$1"
echo -e "\033[41;36m please set password for user: ${username} \033[0m"
stty -echo
read password
stty echo
fi
echo -e "\033[41;36m use username: ${username}. \033[0m"
# create public key & secret key
createSshkey "${username}" "${password}"
2.1.2 init 脚本
cachecloud项目中的cachecloud-init.sh(目录:cachecloud-open-web\src\main\resources\script\cachecloud-init.sh)脚本是用来初始化服务器的cachecloud环境,主要工作如下:
- 创建cachecloud项目用户:因为cachecloud项目的部分功能(redis启动、服务器监控)是通过ssh完成的,所以这里的用户和密码要和项目中的相对应,具体详见第三节。
- 创建cachecloud项目的工作目录、数据目录、配置目录、日志目录、redis安装目录、临时目录等等。(/opt/cachecloud/data、/opt/cachecloud/conf、/opt/cachecloud/logs、/opt/cachecloud/redis、/tmp/cachecloud)
- 安装最新的release版本的Redis
PS:注意需要安装这个工具(sh使用了该命令):yum install wget
#!/bin/bash
############################################################################
# @desc:
# 1. initial config
# 2. add system config
# 3. create user;
# 4. create default directories and authorize;
# 5. install sshpass tool
# 6. install redis
# @usage: sh cachecloud-init.sh [username]
###########################################################################
set -o nounset
set -o errexit
# Redis6.0及以上版本需要依赖操作系统gcc 4.9.0以上版本编译
redis_array=("redis-3.0.7" "redis-3.2.12" "redis-4.0.14" "redis-5.0.9")
# initial config
initConfig() {
sysctl vm.overcommit_memory=1
echo 511 >/proc/sys/net/core/somaxconn
echo never >/sys/kernel/mm/transparent_hugepage/enabled
echo never >/sys/kernel/mm/transparent_hugepage/defrag
echo 0 >/proc/sys/vm/swappiness &
nohup swapoff -a >swap.out 2>&1
echo -e "\033[41;36m OK: initial config done. \033[0m"
}
# add system config
initSysConfig() {
echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
echo 'vm.swappiness=0' >>/etc/sysctl.conf
echo "echo 511 > /proc/sys/net/core/somaxconn" >>/etc/rc.d/rc.local
echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >>/etc/rc.d/rc.local
echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag" >>/etc/rc.d/rc.local
echo -e "\033[41;36m OK: add system config done. \033[0m"
}
# check if the user exists
checkExist() {
local num=$(cat /etc/passwd | grep -w $1 | wc -l)
#cat /etc/passwd | grep -q "$1"
if [[ $num == 1 ]]; then
echo -e "\033[41;36m user $1 exists, overwrite user and init all data*: [y/n]? \033[0m"
read replace
if [[ ${replace} == "y" ]]; then
echo -e "\033[41;36m delete existed user: $1. \033[0m"
userdel -r "$1"
createUser "$1" "$2"
init "$1" "$2"
return 0
fi
else
createUser "$1" "$2"
init "$1" "$2"
fi
return 0
}
# create the user
createUser() {
# create a user
groupadd -g 400 $1
useradd -u 400 -g 400 -m -d /home/$1 -s /bin/bash $1
# give the user a password
echo $2 | passwd --stdin $1
# add the user to sudoers
# echo "$1 ALL=(ALL) ALL" >> /etc/sudoers
# Maximum number of days between password change
chage -M 9999 $1
echo -e "\033[41;36m OK: create user: $1 done. \033[0m"
}
# create defautl dirs and authorize
init() {
# create working dirs and a tmp dir
mkdir -p /opt/cachecloud/conf
mkdir -p /opt/cachecloud/data
mkdir -p /opt/cachecloud/logs
mkdir -p /tmp/cachecloud
mkdir -p /data/redis
# change owner
chown -R $1:$2 /opt/cachecloud
chown -R $1:$2 /tmp/cachecloud
chown -R $1:$2 /home/$1
chown -R $1 /var/run
chown -R $1:$2 /data/redis
echo -e "\033[41;36m OK: init done. \033[0m"
}
# install sshpass tool
installSshpass() {
yum install -y sshpass
echo -e "\033[41;36m OK: install sshpass tool done. \033[0m"
}
# output
output() {
echo "somaxconn="
cat /proc/sys/net/core/somaxconn
echo "overcommit_memory="
cat /proc/sys/vm/overcommit_memory
echo "transparent_hugepage/enabled="
cat /sys/kernel/mm/transparent_hugepage/enabled
echo "transparent_hugepage/defrag="
cat /sys/kernel/mm/transparent_hugepage/defrag
echo "swappiness="
cat /proc/sys/vm/swappiness
echo "user="
cat /etc/passwd | grep cachecloud
echo "auth_key="
cat /home/$1/.ssh/authorized_keys
echo "sshpass="
sshpass -V | head -1
echo -e "\033[41;36m OK: config output done. \033[0m"
}
# install redis
installRedis() {
yum install -y gcc
for redisVersion in ${redis_array[*]}; do
local redisDir="/opt/cachecloud/${redisVersion}"
local redisTarGz="${redisVersion}.tar.gz"
mkdir -p ${redisDir} && cd ${redisDir}
wget http://download.redis.io/releases/${redisTarGz} && tar zxvf ${redisTarGz} --strip-component=1
make && make install
if [[ $? == 0 ]]; then
echo -e "\033[41;36m OK: ${redisTarGz} is installed, exit. \033[0m"
chown -R $1:$2 ${redisDir}
if [[ ${redisVersion} == "redis-5.0.9" ]]; then
export PATH=$PATH:${redisDir}/src
echo $PATH
fi
else
echo -e "\033[41;36m ERROR: ${redisTarGz} is NOT installed, exit. \033[0m"
fi
done
return
}
# let's go
username="cachecloud-open"
password="cachecloud-open"
if [[ $# > 0 && -n "$1" ]]; then
username="$1"
echo -e "\033[41;36m please set password for user: ${username} \033[0m"
stty -echo
read password
stty echo
fi
echo -e "\033[41;36m use username: ${username}. \033[0m"
# 1. initial config
initConfig
# 2. add system config
initSysConfig
# 3. check & create user
checkExist "${username}" "${password}"
# 4.install sshpass tool
installSshpass
# 5.output
output "${username}"
# 6.install install
installRedis "${username}" "${password}"