环境准备
切换到root账户
1 | su - root |
安装wget
1 | yum install wget -y |
备份原始Yum源
1 | mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup |
下载新的Yum源
1 | wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo |
安装EPEL源
1 | yum install epel-release -y |
备份原始EPEL源
1 | mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup |
下载新的EPEL源
1 | wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo |
清除Yum缓存
1 | yum clean all |
构建Yum缓存
1 | yum makecache |
安装常用工具组
1 | yum groupinstall -y "Compatibility Libraries" \ |
安装常用工具包
1 | yum install -y yum-utils \ |
安装X11图形支持(按需)
1 | yum install -y xorg-x11-xauth \ |
禁用yum自动更新(根据实际需要选择)
停止yum自动更新服务
1 | systemctl stop yum-cron |
禁用yum自动更新服务
1 | systemctl disable yum-cron |
查看yum自动更新服务状态
1 | systemctl status yum-cron |
设置系统时区
1 | ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime |
设置语言(按需)
1 | echo "LANG=\"zh_CN.UTF-8\"" > /etc/locale.conf |
NTP服务
安装NTP服务
1 | yum install ntpdate -y |
设置时间同步上游服务器
编辑
1 | vim /etc/ntp/step-tickers |
修改
1 | 0.centos.pool.ntp.org |
启用ntpdate服务
1 | systemctl enable ntpdate |
启动ntpdate服务
1 | systemctl restart ntpdate |
有时启动不成功,多试几次,还不成功就刷新dns缓存
1 | systemctl restart nscd |
查看ntpdate服务状态
1 | systemctl status ntpdate |
防火墙
停止防火墙
1 | systemctl stop firewalld |
禁用防火墙
1 | systemctl disable firewalld |
查看防火墙状态
1 | systemctl status firewalld |
关闭SELinux
1 | setenforce 0 |
内核参数设置
编辑
1 | vim /etc/profile |
追加
1 | modprobe -a br_netfilter \ |
使其生效
1 | source /etc/profile |
查看未加载模块(什么都不显示就对了)
1 | for module in br_netfilter ip6_udp_tunnel ip_set ip_set_hash_ip ip_set_hash_net iptable_filter iptable_nat iptable_mangle iptable_raw nf_conntrack_netlink nf_conntrack nf_conntrack_ipv4 nf_defrag_ipv4 nf_nat nf_nat_ipv4 nf_nat_masquerade_ipv4 udp_tunnel veth vxlan x_tables xt_addrtype xt_conntrack xt_comment xt_mark xt_multiport xt_nat xt_recent xt_set xt_statistic xt_tcpudp; |
编辑
1 | vim /etc/sysctl.conf |
追加或修改内容
1 | # 优化参数 |
使配置立即生效
1 | sysctl -p |
设置文件描述符
编辑
1 | vim /etc/security/limits.conf |
追加或修改内容
1 | * soft nofile 1048576 |
编辑
1 | vim /etc/security/limits.d/20-nproc.conf |
追加或修改内容
1 | * soft nofile 1048576 |
编辑
1 | vim /etc/pam.d/login |
追加或修改内容
1 | # 32位系统 |
关闭虚拟内存
1 | swapoff -a |
配置虚拟内存设置【可选】
物理内存小于4G(虚拟内存统一设置为4G)
1 | dd if=/dev/zero of=/swap bs=1024 count=4096000 |
物理内存小于16G(虚拟内存统一设置为8G)
1 | dd if=/dev/zero of=/swap bs=1024 count=8192000 |
物理内存小于64G(虚拟内存统一设置为16G)
1 | dd if=/dev/zero of=/swap bs=1024 count=16384000 |
物理内存小于256G(虚拟内存统一设置为32G)
1 | dd if=/dev/zero of=/swap bs=1024 count=32768000 |
设置改挂载点
1 | vim /etc/fstab |
编辑或新增
1 | /swap swap swap defaults 0 0 |
关闭transparent_hugepage
临时关闭
1 | echo "never" > /sys/kernel/mm/transparent_hugepage/enabled |
永久关闭
编辑
1 | vim /etc/default/grub |
在GRUB_CMDLINE_LINUX加入选项transparent_hugepage=never
1 | # ext4 |
生成grub文件(bios)
1 | grub2-mkconfig -o /boot/grub2/grub.cfg |
生成文件的grub文件(efi)
1 | grub2-mkconfig -o /boot/efi/EFI/redhat/grub.cfg |
添加执行路径环境变量配置
1 | echo 'export PATH=.:$PATH' > /etc/profile.d/here.sh |
密码策略配置
编辑
1 | vim /etc/login.defs |
修改
1 | PASS_MAX_DAYS 99999 |
执行
1 | chage --maxdays 180 root |
密码复杂度配置
编辑
1 | vim /etc/security/pwquality.conf |
修改
1 | # minlen = 9 |
密码重用限制
编辑
1 | vim /etc/pam.d/password-auth |
修改
1 | password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok |
编辑
1 | vim /etc/pam.d/system-auth |
修改
1 | password sufficient pam_unix.so sha512 shadow nullok try_first_pass use_authtok |
SSH限制策略
编辑
1 | vim /etc/ssh/sshd_config |
修改
1 | #ClientAliveInterval 0 |
创建物理卷
1 | pvcreate /dev/sdb |
创建卷组
1 | vgcreate storage /dev/sdb |
创建逻辑卷
1 | lvcreate -l 100%Free -n data storage |
格式化逻辑卷
1 | mkfs.xfs /dev/storage/data |
创建挂载点
1 | mkdir -p /storage/data |
添加挂载信息
编辑
1 | vim /etc/fstab |
追加
1 | /dev/mapper/storage-data /storage/data xfs defaults 0 0 |
挂载
1 | mount -a |
查看
1 | df -hT |
重启
1 | shutdown -r now |
重启后切换到root账户
1 | su - root |
开始安装Docker
安装Docker
1 | yum remove docker \ |
添加Docker仓库
官方源
1 | yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo |
阿里云源
1 | yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo |
安装docker和docker-compose
1 | yum install -y docker-ce docker-ce-cli containerd.io docker-compose |
配置docker监听地址(2375按需开,因为没有密码,至少不能配置为监听公网地址)
1 | vim /lib/systemd/system/docker.service |
1 | # ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock |
或者
1 | sed -i 's/^ExecStart=.*/ExecStart=\/usr\/bin\/dockerd --icc=false -H unix:\/\/\/var\/run\/docker.sock -H tcp:\/\/127.0.0.1:2375 -H fd:\/\/ --containerd=\/run\/containerd\/containerd.sock/' /lib/systemd/system/docker.service |
配置镜像加速器&私有镜像仓库的非安全访问
1 | mkdir -p /etc/docker |
重新加载服务配置
1 | systemctl daemon-reload |
启用docker
1 | systemctl enable docker |
启动docker
1 | systemctl restart docker |
查看docker状态
1 | systemctl status docker |
下载convoy
1 | wget https://github.com/rancher/convoy/releases/download/v0.5.2/convoy.tar.gz |
解压
1 | tar xvzf convoy.tar.gz |
复制
1 | \cp -f convoy/convoy convoy/convoy-pdata_tools /usr/local/bin/ |
创建docker插件目录
1 | mkdir -p /etc/docker/plugins/ |
写入插件信息
1 | echo "unix:///var/run/convoy/convoy.sock" > /etc/docker/plugins/convoy.spec |
创建convoy服务
创建卷存储目录
1 | mkdir -p /storage/data/docker/volumes |
编辑
1 | vim /usr/lib/systemd/system/convoy.service |
内容
1 | [Unit] |
启动服务
启用服务
1 | systemctl enable convoy |
启动服务
1 | systemctl start convoy |
查看状态
1 | systemctl status convoy |
重启docker服务
1 | systemctl restart docker |
编辑crontab
1 | mkdir -p /etc/cron.d/ |
启用crond
1 | systemctl enable crond |
启动crond
1 | systemctl restart crond |
查看crond状态
1 | systemctl status crond |
查看cron日志
1 | cat /var/log/cron |
退出root账户
1 | exit |
将当前用户加入到docker组
1 | sudo gpasswd -a ${USER} docker |
1 | sudo newgrp docker |
配置DOCKER_HOST环境变量【可选】
1 | vim ~/.bashrc |
追加
1 | export DOCKER_CONTENT_TRUST=1 |
审核Docker文件和目录
编辑
1 | vim /etc/audit/audit.rules |
追加
1 | -w /var/lib/docker -k docker |
编辑
1 | vim /etc/audit/rules.d/audit.rules |
追加
1 | -w /var/lib/docker -k docker |
启用
1 | systemctl enable auditd |
启动
1 | systemctl restart auditd |
查看
1 | systemctl enable auditd |