Mr. Neo Chen (陈景峯), netkiller, BG7NYT
节选自《Netkiller Spring Cloud 手札》
多维度架构 - 知乎www.zhihu.com/club/1241768772601950208编辑
1. 环境安装
1.1. 操作系统初始化
操作系统按完成后,使用下面脚本做一次初始化
curl -s https://raw.githubusercontent.com/oscm/shell/master/os/personalise.sh | bash
1.2. 安装 Nginx
curl -s https://raw.githubusercontent.com/oscm/shell/master/web/nginx/stable/nginx.sh | bash
1.3. 安装数据库
1.3.1. MySQL
5.7
curl -s https://raw.githubusercontent.com/oscm/shell/master/database/mysql/5.7/mysql.server.sh | bash
安装完成会看到下面输出
2019-06-04T08:54:24.419092Z 1 [Note] A temporary password is generated for root@localhost: 8X!pE(o+urv;
8.0
curl -s https://raw.githubusercontent.com/oscm/shell/master/database/mysql/8.0/server.sh | bash
curl -s https://raw.githubusercontent.com/oscm/shell/master/database/mysql/8.0/client.sh | bash
1.3.2. PostgreSQL
PostgreSQL 9.4
curl -s https://raw.githubusercontent.com/oscm/shell/master/database/postgresql/postgresql94-centos7.sh | bash
1.3.3. Redis
curl -s https://raw.githubusercontent.com/oscm/shell/master/database/redis/source/redis-5.0.5.sh | bash
1.4. RabbitMQ
curl -s https://raw.githubusercontent.com/oscm/shell/master/queue/rabbitmq/rabbitmq-server-3.7.15.sh | bash
1.5. OpenJDK
curl -s https://raw.githubusercontent.com/oscm/shell/master/lang/java/openjdk/java-1.8.0-openjdk.sh | bash
1.5.1. Maven
curl -s https://raw.githubusercontent.com/oscm/shell/master/lang/java/maven/apache-maven-3.6.1.sh | bash
1.5.2. Gradle
curl -s https://raw.githubusercontent.com/oscm/shell/master/lang/java/gradle/gradle-5.4.1.sh | bash
1.6. Node
前端开发会用到Node.js
curl -s https://raw.githubusercontent.com/oscm/shell/master/lang/node.js/binrary/node-v12.4.0.sh | bash
1.7. Gitlab
curl -s https://raw.githubusercontent.com/oscm/shell/master/project/gitlab/gitlab.centos7.sh | bash
Gitlab 配置请参考 《Netkiller Project 手札》
1.7.1. Gitlab runner
curl -s https://raw.githubusercontent.com/oscm/shell/master/project/gitlab/gitlab-runner.sh | bash
java 编译环境
去 Oracle 网站下载 java 8 jdk-8u212-linux-x64.rpm
yum localinstall jdk-8u212-linux-x64.rpm
curl -s https://raw.githubusercontent.com/oscm/shell/master/project/git.sh | bash
curl -s https://raw.githubusercontent.com/oscm/shell/master/lang/java/maven/apache-maven-3.6.1.sh | bash
2. 操作系统配置与优化
操作系统要求 CentOS 7 Minimal ISO
2.1. 域名
2.1.1. 配置 DNS
修改 /etc/resolv.conf 配置 DNS 确保域名解析正确
echo -ne "
search example.com
nameserver 208.67.222.222
nameserver 202.67.220.220
nameserver 8.8.8.8
nameserver 4.4.4.4
" > /etc/resolv.conf
2.1.2. /etc/hosts
DNS 解析有延迟,很多不可控因素,将域名写入 /etc/hosts 更为保险
192.168.1.1 www.netkiller.cn
192.168.1.2 api.netkiller.cn
192.168.1.3 db.netkiller.cn
使用域名链接数据的例子
spring.datasource.url=jdbc:mysql://db.netkiller.cn:3306/neo
DNS 不可靠的原因分析,因为DNS使用UDP传输,网络拥堵,电磁干扰等等都能导致丢包。
2.2. 历史记录操作留痕
定义 .history 文件格式,记录每一步操作,便于查看什么时间执行了什么命令
通过~/.bash_history文件记录系统管理员的操作记录,定制.bash_history格式
HISTSIZE=1000
HISTFILESIZE=2000
HISTTIMEFORMAT="%Y-%m-%d-%H:%M:%S "
export HISTTIMEFORMAT
看看实际效果
$ history | head
1 2012-02-27-09:10:45 do-release-upgrade
2 2012-02-27-09:10:45 vim /etc/network/interfaces
3 2012-02-27-09:10:45 vi /etc/network/interfaces
4 2012-02-27-09:10:45 ping www.163.com
2.3. 临时文件安全
临时文件不应该有执行权限
/tmp
/dev/sda3 /tmp ext4 nosuid,noexec,nodev,rw 0
同时使用符号连接将/var/tmp 指向 /tmp
/dev/shm
none /dev/shm tmpfs defaults,nosuid,noexec,rw 0 0
2.4. 执行权限
以数据库为例,从安全角度考虑我们需要如下更改
chown mysql:mysql /usr/bin/mysql*
chmod 700 /usr/bin/mysql*
mysql用户是DBA专用用户, 其他用户将不能执行mysql等命令。
2.5. Linux 系统资源调配
2.5.1. /etc/security/limits.conf
很多资料上是这么写的
* soft nofile 65535
* hard nofile 65535
这样做是偷懒,会带来很多问题,如果你的服务器被攻击,由于你的设置,系统将耗光你的资源,直到没有任何响应为止,你可能键盘输入都成问题,你不得不重启服务器,但你会发现重启只能维持短暂几分钟,又会陷入无响应状态。
nobody soft nofile 4096
nobody hard nofile 8192
为什么会设置为nobody用户呢?因为root用户启动系统后web 服务器会使用nobody用户创建子进程,socket连接实际上是nobody用户在处理。root 仅仅是守护父进程。
mysql soft nofile 2048
mysql hard nofile 2048
针对 mysql 做限制
提示
关于 nofile 即打开文件数,这个跟socket有非常紧密的关系,在linux系统中任何设备都被看做是一个文件(字符设备),你连接一个鼠标,键盘,摄像头,硬盘等等都被看作打开一个设备文件,所以默认1024是远远不够的。
cat >> /etc/security/limits.conf <<EOF
root soft nofile 65536
root hard nofile 65536
www soft nofile 65536
www hard nofile 65536
mysql soft nofile 65536
mysql hard nofile 65536
EOF
2.5.2. /etc/sysctl.conf
内核参数调整
cat >> /etc/sysctl.conf <<EOF
net.ipv4.ip_local_port_range = 1024 65500
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 60
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 4096
EOF
2.6. 关闭 SELINUX
curl -s https://raw.githubusercontent.com/oscm/shell/master/os/selinux.sh | bash
2.7. 关闭写磁盘I/O功能
对于某些文件没必要记录文件的访问时间,由其是在高并发的IO密集操作的环境下,通过两个参数可以实现noatime,nodiratime减少不必要的系统IO资源。
编辑/etc/fstab 添加 noatime,nodiratime 参数
/dev/sdb1 /www ext4 noatime,nodiratime 0 0
我一般分区规划是,/系统根分区,swap交换分区,/www数据分区,同时 禁止写入atime时间,因为/www频繁请求会影响IO
临时mount
mount -o remount,noatime,nodiratime /dev/sda3 /mnt/your
LABEL 方式
LABEL=/www /www ext3 defaults,noatime,nodiratime 1 1
标签:oscm,Spring,githubusercontent,cloud,sh,com,https,优化,bash
From: https://blog.csdn.net/u010604770/article/details/140476258