前言
我很久之前做过简单的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 keycreateSshkey() {mkdir -p /opt/sshchown -R $1:$2 /opt/sshssh-keygen -t rsa -f /opt/ssh/id_rsa -P '' -C $1local privateKeyFile="/opt/ssh/id_rsa"chmod 600 ${privateKeyFile}mkdir -p /home/$1/.sshlocal publicKeyFile="/home/$1/.ssh/authorized_keys"cat /opt/ssh/id_rsa.pub >>${publicKeyFile}chown -R $1:$2 /home/$1/.sshchmod 755 ${publicKeyFile}echo -e "\033[41;36m OK: create public key & secret key done. \033[0m"}# let's gousername="cachecloud-open"password="cachecloud-open"if [[ $# > 0 && -n "$1" ]]; thenusername="$1"echo -e "\033[41;36m please set password for user: ${username} \033[0m"stty -echoread passwordstty echofiecho -e "\033[41;36m use username: ${username}. \033[0m"# create public key & secret keycreateSshkey "${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 nounsetset -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 configinitConfig() {sysctl vm.overcommit_memory=1echo 511 >/proc/sys/net/core/somaxconnecho never >/sys/kernel/mm/transparent_hugepage/enabledecho never >/sys/kernel/mm/transparent_hugepage/defragecho 0 >/proc/sys/vm/swappiness &nohup swapoff -a >swap.out 2>&1echo -e "\033[41;36m OK: initial config done. \033[0m"}# add system configinitSysConfig() {echo "vm.overcommit_memory = 1" >>/etc/sysctl.confecho 'vm.swappiness=0' >>/etc/sysctl.confecho "echo 511 > /proc/sys/net/core/somaxconn" >>/etc/rc.d/rc.localecho "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >>/etc/rc.d/rc.localecho "echo never > /sys/kernel/mm/transparent_hugepage/defrag" >>/etc/rc.d/rc.localecho -e "\033[41;36m OK: add system config done. \033[0m"}# check if the user existscheckExist() {local num=$(cat /etc/passwd | grep -w $1 | wc -l)#cat /etc/passwd | grep -q "$1"if [[ $num == 1 ]]; thenecho -e "\033[41;36m user $1 exists, overwrite user and init all data*: [y/n]? \033[0m"read replaceif [[ ${replace} == "y" ]]; thenecho -e "\033[41;36m delete existed user: $1. \033[0m"userdel -r "$1"createUser "$1" "$2"init "$1" "$2"return 0fielsecreateUser "$1" "$2"init "$1" "$2"fireturn 0}# create the usercreateUser() {# create a usergroupadd -g 400 $1useradd -u 400 -g 400 -m -d /home/$1 -s /bin/bash $1# give the user a passwordecho $2 | passwd --stdin $1# add the user to sudoers# echo "$1 ALL=(ALL) ALL" >> /etc/sudoers# Maximum number of days between password changechage -M 9999 $1echo -e "\033[41;36m OK: create user: $1 done. \033[0m"}# create defautl dirs and authorizeinit() {# create working dirs and a tmp dirmkdir -p /opt/cachecloud/confmkdir -p /opt/cachecloud/datamkdir -p /opt/cachecloud/logsmkdir -p /tmp/cachecloudmkdir -p /data/redis# change ownerchown -R $1:$2 /opt/cachecloudchown -R $1:$2 /tmp/cachecloudchown -R $1:$2 /home/$1chown -R $1 /var/runchown -R $1:$2 /data/redisecho -e "\033[41;36m OK: init done. \033[0m"}# install sshpass toolinstallSshpass() {yum install -y sshpassecho -e "\033[41;36m OK: install sshpass tool done. \033[0m"}# outputoutput() {echo "somaxconn="cat /proc/sys/net/core/somaxconnecho "overcommit_memory="cat /proc/sys/vm/overcommit_memoryecho "transparent_hugepage/enabled="cat /sys/kernel/mm/transparent_hugepage/enabledecho "transparent_hugepage/defrag="cat /sys/kernel/mm/transparent_hugepage/defragecho "swappiness="cat /proc/sys/vm/swappinessecho "user="cat /etc/passwd | grep cachecloudecho "auth_key="cat /home/$1/.ssh/authorized_keysecho "sshpass="sshpass -V | head -1echo -e "\033[41;36m OK: config output done. \033[0m"}# install redisinstallRedis() {yum install -y gccfor redisVersion in ${redis_array[*]}; dolocal 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=1make && make installif [[ $? == 0 ]]; thenecho -e "\033[41;36m OK: ${redisTarGz} is installed, exit. \033[0m"chown -R $1:$2 ${redisDir}if [[ ${redisVersion} == "redis-5.0.9" ]]; thenexport PATH=$PATH:${redisDir}/srcecho $PATHfielseecho -e "\033[41;36m ERROR: ${redisTarGz} is NOT installed, exit. \033[0m"fidonereturn}# let's gousername="cachecloud-open"password="cachecloud-open"if [[ $# > 0 && -n "$1" ]]; thenusername="$1"echo -e "\033[41;36m please set password for user: ${username} \033[0m"stty -echoread passwordstty echofiecho -e "\033[41;36m use username: ${username}. \033[0m"# 1. initial configinitConfig# 2. add system configinitSysConfig# 3. check & create usercheckExist "${username}" "${password}"# 4.install sshpass toolinstallSshpass# 5.outputoutput "${username}"# 6.install installinstallRedis "${username}" "${password}"
2.2 添加机器
2.2.1 新建机房
2.2.2 新建机器
2.2.3 新建用户(选项)
2.2.4 申请应用







