前言

我很久之前做过简单的redis 运维监控,当然实现思路就是 操作缓存的时候在内存里累加值,用到的技术是spring boot +html 模板。很简单。肯定不能上生产。简单的统计一些缓存的数据作为系统的监控(毕竟缓存不可靠性要求不高,一般不去care)。正好这次公司内部推荐使用cc(cache cloud),它是专门负责缓存的运维、监控操作。让我们一起去看看吧。

一 急速入门

1.1 拉取源码

  1. git clone git@github.com:sohutv/cachecloud.git

用idea 打开应该是这样的
image.png

1.2 初始化数据库

初始化代码里的sql
../cachecloud-web/sql/cc2.0.sql
image.png
即如下:
image.png

1.3 配置maven 启动-官网推荐

我这里配置了两个mvn命令

  1. install

image.png

  1. run

image.png

  1. 更改数据库密码

image.png

1.4 springboot 启动-个人推荐

image.png

1.5 代码测试

用户名密码 admin/admin
image.png
image.png
image.png

1.6 代码调试

举例断点登录接口
image.png
点击注销,可以进入断点
image.png

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

  1. # create public key & secret key
  2. createSshkey() {
  3. mkdir -p /opt/ssh
  4. chown -R $1:$2 /opt/ssh
  5. ssh-keygen -t rsa -f /opt/ssh/id_rsa -P '' -C $1
  6. local privateKeyFile="/opt/ssh/id_rsa"
  7. chmod 600 ${privateKeyFile}
  8. mkdir -p /home/$1/.ssh
  9. local publicKeyFile="/home/$1/.ssh/authorized_keys"
  10. cat /opt/ssh/id_rsa.pub >>${publicKeyFile}
  11. chown -R $1:$2 /home/$1/.ssh
  12. chmod 755 ${publicKeyFile}
  13. echo -e "\033[41;36m OK: create public key & secret key done. \033[0m"
  14. }
  15. # let's go
  16. username="cachecloud-open"
  17. password="cachecloud-open"
  18. if [[ $# > 0 && -n "$1" ]]; then
  19. username="$1"
  20. echo -e "\033[41;36m please set password for user: ${username} \033[0m"
  21. stty -echo
  22. read password
  23. stty echo
  24. fi
  25. echo -e "\033[41;36m use username: ${username}. \033[0m"
  26. # create public key & secret key
  27. createSshkey "${username}" "${password}"

校验成功
image.png

2.1.2 init 脚本

cachecloud项目中的cachecloud-init.sh(目录:cachecloud-open-web\src\main\resources\script\cachecloud-init.sh)脚本是用来初始化服务器的cachecloud环境,主要工作如下:

  1. 创建cachecloud项目用户:因为cachecloud项目的部分功能(redis启动、服务器监控)是通过ssh完成的,所以这里的用户和密码要和项目中的相对应,具体详见第三节。
  2. 创建cachecloud项目的工作目录、数据目录、配置目录、日志目录、redis安装目录、临时目录等等。(/opt/cachecloud/data、/opt/cachecloud/conf、/opt/cachecloud/logs、/opt/cachecloud/redis、/tmp/cachecloud)
  3. 安装最新的release版本的Redis

PS:注意需要安装这个工具(sh使用了该命令):yum install wget

  1. #!/bin/bash
  2. ############################################################################
  3. # @desc:
  4. # 1. initial config
  5. # 2. add system config
  6. # 3. create user;
  7. # 4. create default directories and authorize;
  8. # 5. install sshpass tool
  9. # 6. install redis
  10. # @usage: sh cachecloud-init.sh [username]
  11. ###########################################################################
  12. set -o nounset
  13. set -o errexit
  14. # Redis6.0及以上版本需要依赖操作系统gcc 4.9.0以上版本编译
  15. redis_array=("redis-3.0.7" "redis-3.2.12" "redis-4.0.14" "redis-5.0.9")
  16. # initial config
  17. initConfig() {
  18. sysctl vm.overcommit_memory=1
  19. echo 511 >/proc/sys/net/core/somaxconn
  20. echo never >/sys/kernel/mm/transparent_hugepage/enabled
  21. echo never >/sys/kernel/mm/transparent_hugepage/defrag
  22. echo 0 >/proc/sys/vm/swappiness &
  23. nohup swapoff -a >swap.out 2>&1
  24. echo -e "\033[41;36m OK: initial config done. \033[0m"
  25. }
  26. # add system config
  27. initSysConfig() {
  28. echo "vm.overcommit_memory = 1" >>/etc/sysctl.conf
  29. echo 'vm.swappiness=0' >>/etc/sysctl.conf
  30. echo "echo 511 > /proc/sys/net/core/somaxconn" >>/etc/rc.d/rc.local
  31. echo "echo never > /sys/kernel/mm/transparent_hugepage/enabled" >>/etc/rc.d/rc.local
  32. echo "echo never > /sys/kernel/mm/transparent_hugepage/defrag" >>/etc/rc.d/rc.local
  33. echo -e "\033[41;36m OK: add system config done. \033[0m"
  34. }
  35. # check if the user exists
  36. checkExist() {
  37. local num=$(cat /etc/passwd | grep -w $1 | wc -l)
  38. #cat /etc/passwd | grep -q "$1"
  39. if [[ $num == 1 ]]; then
  40. echo -e "\033[41;36m user $1 exists, overwrite user and init all data*: [y/n]? \033[0m"
  41. read replace
  42. if [[ ${replace} == "y" ]]; then
  43. echo -e "\033[41;36m delete existed user: $1. \033[0m"
  44. userdel -r "$1"
  45. createUser "$1" "$2"
  46. init "$1" "$2"
  47. return 0
  48. fi
  49. else
  50. createUser "$1" "$2"
  51. init "$1" "$2"
  52. fi
  53. return 0
  54. }
  55. # create the user
  56. createUser() {
  57. # create a user
  58. groupadd -g 400 $1
  59. useradd -u 400 -g 400 -m -d /home/$1 -s /bin/bash $1
  60. # give the user a password
  61. echo $2 | passwd --stdin $1
  62. # add the user to sudoers
  63. # echo "$1 ALL=(ALL) ALL" >> /etc/sudoers
  64. # Maximum number of days between password change
  65. chage -M 9999 $1
  66. echo -e "\033[41;36m OK: create user: $1 done. \033[0m"
  67. }
  68. # create defautl dirs and authorize
  69. init() {
  70. # create working dirs and a tmp dir
  71. mkdir -p /opt/cachecloud/conf
  72. mkdir -p /opt/cachecloud/data
  73. mkdir -p /opt/cachecloud/logs
  74. mkdir -p /tmp/cachecloud
  75. mkdir -p /data/redis
  76. # change owner
  77. chown -R $1:$2 /opt/cachecloud
  78. chown -R $1:$2 /tmp/cachecloud
  79. chown -R $1:$2 /home/$1
  80. chown -R $1 /var/run
  81. chown -R $1:$2 /data/redis
  82. echo -e "\033[41;36m OK: init done. \033[0m"
  83. }
  84. # install sshpass tool
  85. installSshpass() {
  86. yum install -y sshpass
  87. echo -e "\033[41;36m OK: install sshpass tool done. \033[0m"
  88. }
  89. # output
  90. output() {
  91. echo "somaxconn="
  92. cat /proc/sys/net/core/somaxconn
  93. echo "overcommit_memory="
  94. cat /proc/sys/vm/overcommit_memory
  95. echo "transparent_hugepage/enabled="
  96. cat /sys/kernel/mm/transparent_hugepage/enabled
  97. echo "transparent_hugepage/defrag="
  98. cat /sys/kernel/mm/transparent_hugepage/defrag
  99. echo "swappiness="
  100. cat /proc/sys/vm/swappiness
  101. echo "user="
  102. cat /etc/passwd | grep cachecloud
  103. echo "auth_key="
  104. cat /home/$1/.ssh/authorized_keys
  105. echo "sshpass="
  106. sshpass -V | head -1
  107. echo -e "\033[41;36m OK: config output done. \033[0m"
  108. }
  109. # install redis
  110. installRedis() {
  111. yum install -y gcc
  112. for redisVersion in ${redis_array[*]}; do
  113. local redisDir="/opt/cachecloud/${redisVersion}"
  114. local redisTarGz="${redisVersion}.tar.gz"
  115. mkdir -p ${redisDir} && cd ${redisDir}
  116. wget http://download.redis.io/releases/${redisTarGz} && tar zxvf ${redisTarGz} --strip-component=1
  117. make && make install
  118. if [[ $? == 0 ]]; then
  119. echo -e "\033[41;36m OK: ${redisTarGz} is installed, exit. \033[0m"
  120. chown -R $1:$2 ${redisDir}
  121. if [[ ${redisVersion} == "redis-5.0.9" ]]; then
  122. export PATH=$PATH:${redisDir}/src
  123. echo $PATH
  124. fi
  125. else
  126. echo -e "\033[41;36m ERROR: ${redisTarGz} is NOT installed, exit. \033[0m"
  127. fi
  128. done
  129. return
  130. }
  131. # let's go
  132. username="cachecloud-open"
  133. password="cachecloud-open"
  134. if [[ $# > 0 && -n "$1" ]]; then
  135. username="$1"
  136. echo -e "\033[41;36m please set password for user: ${username} \033[0m"
  137. stty -echo
  138. read password
  139. stty echo
  140. fi
  141. echo -e "\033[41;36m use username: ${username}. \033[0m"
  142. # 1. initial config
  143. initConfig
  144. # 2. add system config
  145. initSysConfig
  146. # 3. check & create user
  147. checkExist "${username}" "${password}"
  148. # 4.install sshpass tool
  149. installSshpass
  150. # 5.output
  151. output "${username}"
  152. # 6.install install
  153. installRedis "${username}" "${password}"

2.2 添加机器

2.2.1 新建机房

image.png

2.2.2 新建机器

image.png
设置参数
image.png

2.2.3 新建用户(选项)

image.png

2.2.4 申请应用

image.png

2.2.5 审批应用

image.png

2.2.6 连接测试

image.png
image.png
image.png

三 用户管理

四 运维管理