主机基础配置
主机名配置
因为 K8S 或者 FQDN
的规定,主机名仅支持包含 -
或/和 .
(中横线和点)两种特殊符号,并且一个集群中主机名不能重复。
Hosts
- Linux 系统安装完成后,在 hosts(/etc/hosts) 中应该有
localhost 指向 127.0.0.1
,如果没有则手动添加上。 - 配置每台主机的 hosts(/etc/hosts),添加
host_ip $hostname
到 /etc/hosts
文件中。
CentOS 关闭 selinux
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
关闭防火墙(可选)或者放行相应端口
对于刚刚接触 Rancher 的用户,建议在测试环境关闭防火墙,以避免出现网络通信问题。
- 关闭防火墙
systemctl stop firewalld.service && systemctl disable firewalld.service
ufw disable
- 端口放行
端口放行请查看端口需求
配置主机时间、时区、系统语言
- 查看时区
date -R
或者 timedatectl
- 修改时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
|
- 修改系统语言环境
sudo echo 'LANG="en_US.UTF-8"' >> /etc/profile; source /etc/profile
|
- 配置主机 NTP 时间同步
配置主机 DNS 服务器
对于类似 Ubuntu 18 这类默认使用 systemd-resolve
管理 DNS 服务器的系统,建议禁用 systemd-resolved
服务,然后手动配置 DNS。
因为 systemd-resolve
会使用 127.0.x.x
这样的 IP 来作为默认的 DNS 服务器地址,这个是主机的环回地址。这个地址传递到容器环境中后,容器应用无法访问这个地址。
操作方法:
- 禁用 systemd-resolved.service
systemctl disable systemd-resolved.service
systemctl stop systemd-resolved.service
rm -rf /etc/resolv.conf; touch /etc/resolv.conf
|
- 接着编辑
/etc/resolv.conf
添加 DNS 服务器 - 重启 docker 服务
systemctl daemon-reload; systemctl restart docker
|
Kernel 调优
cat >> /etc/sysctl.conf<<EOF
net.ipv4.ip_forward=1
net.ipv4.conf.all.forwarding=1
# 参考 https://github.com/prometheus/node_exporter#disabled-by-default
kernel.perf_event_paranoid=-1
kernel.watchdog_thresh=30
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
# 存在于 ARP 高速缓存中的最少层数,如果少于这个数,垃圾收集器将不会运行,默认值:128。
net.ipv4.neigh.default.gc_thresh1=128
# 保存在 ARP 高速缓存中的最大记录软限制。垃圾收集器在开始收集前,允许记录数超过这个数字 5 秒。默认值:512。
net.ipv4.neigh.default.gc_thresh2=6144
# 保存在 ARP 高速缓存中的最大记录的硬限制,一旦高速缓存中的数目高于此,垃圾收集器将马上运行。默认值:1024。
net.ipv4.neigh.default.gc_thresh3=8192
# 决定检查一次相邻层记录的有效性的周期。当相邻层记录失效时,将在给它发送数据前,再解析一次。默认值:60 秒。
# 系统中所有进程能够同时打开的文件句柄数量
fs.file-max=2097152
# 设置每个用户可以运行的 inotifywait 或 inotifywatch 命令的进程数。
fs.inotify.max_user_instances=8192
# inotify 用于监控文件系统事件,该文件中的值为调用 inotify_init 函数时分配给 inotify 队列的事件数目的最大值,超出这个值得事件被丢弃,但会触发 IN_Q_OVERFLOW 事件,文件系统变化越频繁,这个值就应该越大
fs.inotify.max_queued_events=16384
# IO 复用 epoll 监听文件句柄的数量最大值
fs.inotify.max_user_watches=524288
vm.swappiness=0
net.ipv4.tcp_syncookies=1
net.ipv4.tcp_fin_timeout=30
net.ipv4.tcp_synack_retries=2
net.ipv4.tcp_max_syn_backlog=8096
net.ipv4.tcp_tw_recycle=0
fs.aio-max-nr = 1048576
kernel.randomize_va_space = 0
net.core.somaxconn = 1024
EOF
|
数值根据实际环境自行配置,最后执行 sysctl -p
保存配置。
连接数调整
cat >> /etc/security/limits.conf <<EOF
root soft nofile 655350
root hard nofile 655350
* soft nofile 655350
* hard nofile 655350
* soft nproc unlimited
* hard nproc unlimited
EOF
|
模块列表
如果要使用 ceph 存储相关功能,需保证 worker 节点加载 RBD 模块
以下模块需要在主机上加载
模块名称
|
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
|
nfnetlink
|
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
|
提示
模块查询: lsmod | grep <模块名>
模块加载: modprobe <模块名>
ETCD 集群容错表
建议在 ETCD 集群中使用奇数个成员,通过添加额外成员可以获得更高的失败容错。具体详情可以查阅optimal-cluster-size。
集群大小
| MAJORITY
| 失败容错
|
1
| 1
| 0 |
2
| 2
| 0 |
3
| 2
| 1 |
4
| 3
| 1 |
5
| 3
| 2 |
6
| 4
| 2 |
7
| 4
| 3 |
8
| 5
| 3 |
9
| 5
| 4 |
Docker 安装与配置
Docker 安装
修改系统源
- Ubuntu
cp /etc/apt/sources.list /etc/apt/sources.list.bak
sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
apt-get update
|
- Centos 7.x
yum install ca-certificates ;
update-ca-trust;
cp -rf /etc/yum.repos.d /etc/yum.repos.d.backup
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.ustc.edu.cn/centos|g' \
-i.bak /etc/yum.repos.d/CentOS-Base.repo
yum makecache
|
- centos8.x
cp -rf /etc/yum.repos.d /etc/yum.repos.d.backup
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://mirrors.ustc.edu.cn/centos|g' \
-i.bak \
/etc/yum.repos.d/CentOS-Linux-AppStream.repo \
/etc/yum.repos.d/CentOS-Linux-BaseOS.repo \
/etc/yum.repos.d/CentOS-Linux-Extras.repo \
/etc/yum.repos.d/CentOS-Linux-PowerTools.repo \
/etc/yum.repos.d/CentOS-Linux-Plus.repo
yum makecache
|
Docker-ce 安装
- Ubuntu
# 定义用户名
NEW_USER=rancher
# 添加用户(可选)
sudo adduser $NEW_USER
# 为新用户设置密码
sudo passwd $NEW_USER
# 为新用户添加 sudo 权限
sudo echo "$NEW_USER ALL=(ALL) ALL" >> /etc/sudoers
# 定义安装版本
export docker_version=18.09.9;
# 删除旧的 docker 组件
sudo apt-get remove docker docker-engine docker.io containerd runc -y;
# 更新 apt 源
sudo apt-get update;
# 对系统进行全面的更新升级,推荐升级一下(可选)
sudo apt-get -y upgrade;
# 安装必要的一些系统工具
sudo apt-get -y install apt-transport-https ca-certificates \
curl software-properties-common bash-completion gnupg-agent;
# 安装 GPG 证书
sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | \
sudo apt-key add -;
# 添加 Docker APT 源
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable";
# 更新并安装 Docker-CE
sudo apt-get -y update;
install_version=$( apt-cache madison docker-ce | grep ${docker_version} | awk '{print $3}' );
# --allow-downgrades 允许降级安装
sudo apt-get -y install docker-ce=${install_version} docker-ce-cli=${install_version} --allow-downgrades;
# 把当前用户加入 docker 组
sudo usermod -aG docker $NEW_USER;
# 设置开机启动
sudo systemctl enable docker;
# 清理不需要的依赖
apt-get autoremove -y
|
Docker-engine
Docker-Engine Docker 官方已经不推荐使用,请安装 Docker-CE。
- Centos
注意:在安装的时候如果提示 containerd 版本过低,则需要访问 https://download.docker.com/linux/centos/7/x86_64/stable/Packages 下载最新版本的 containerd 到本地,然后运行 yum localinstall xxx.rpm
进行安装。
因为 CentOS 的安全限制,通过 RKE 安装 K8S 集群时候无法使用 root
账户。所以,建议 CentOS
用户使用非 root
用户来运 docker,不管是 RKE
还是 custom
安装 k8s,详情查看无法为主机配置 SSH 隧道 。
# 定义用户名
NEW_USER=rancher
# 添加用户(可选)
sudo adduser $NEW_USER
# 为新用户设置密码
sudo passwd $NEW_USER
# 为新用户添加 sudo 权限
sudo echo "$NEW_USER ALL=(ALL) ALL" >> /etc/sudoers
# 卸载旧版本 Docker 软件
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine \
container*
# 定义安装版本
export docker_version=18.06.3
# 对系统进行全面的更新升级,推荐升级一下(可选)
sudo yum update -y;
# 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data \
lvm2 bash-completion;
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo \
http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo;
# Step 3: 更新并安装 Docker-CE
sudo yum makecache all;
version=$(yum list docker-ce.x86_64 --showduplicates | sort -r|grep ${docker_version}|awk '{print $2}');
sudo yum -y install --setopt=obsoletes=0 docker-ce-${version} docker-ce-selinux-${version};
# 如果已经安装高版本 Docker,可进行降级安装(可选)
yum downgrade --setopt=obsoletes=0 -y docker-ce-${version} docker-ce-selinux-${version};
# 把当前用户加入 docker 组
sudo usermod -aG docker $NEW_USER;
# 设置开机启动
sudo systemctl enable docker;
|
Docker-engine
Docker-Engine Docker 官方已经不推荐使用,请安装 Docker-CE。
锁定 Docker 版本
可能因为某些原因无意间执行了 yum update
或者 apt-get -y upgrade;
导致 Docker 版本升级。为了避免此类问题发生,建议在安装好 Docker 后对 Docker 软件进行锁定,防止 Docker 意外更新。
centos
- 安装 yum-plugin-versionlock 插件
yum install yum-plugin-versionlock
|
- 锁定软件包
yum versionlock add docker-ce docker-ce-cli
|
[root@izwz969o7lu6t9lh4ta6m5z ~]# yum versionlock add docker-ce docker-ce-cli
已加载插件:fastestmirror, versionlock
Adding versionlock on: 0:docker-ce-17.06.2.ce-3.el7.centos
versionlock added: 1
[root@izwz969o7lu6t9lh4ta6m5z ~]#
|
- 查看已锁定的软件包
- 解锁指定的软件包
yum versionlock delete <软件包名称>
|
- 解锁所有的软件包
Ubuntu
- 锁定软件:
sudo apt-mark hold docker-ce docker-ce-cli
|
- 查看已锁定的软件
软件包被锁定后,再次执行 apt-get upgrade
可以看到以下日志提示
root@alihost-03:/home/docker# apt-get upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
docker-ce docker-ce-cli linux-generic linux-headers-generic linux-image-generic ubuntu-minimal
0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
root@alihost-03:/home/docker#
|
被锁定的软件不会再升级。
- 解除锁定:
sudo apt-mark unhold docker-ce docker-ce-cli
|
Docker 配置
对于通过 systemd 来管理服务的系统(比如 CentOS7.X、Ubuntu16.X),Docker 有两处可以配置参数: 一个是 docker.service
服务配置文件,一个是 Docker daemon 配置文件 daemon.json。
docker.service
对于 CentOS 系统,docker.service
默认位于 /usr/lib/systemd/system/docker.service
;对于 Ubuntu 系统,docker.service
默认位于 /lib/systemd/system/docker.service
daemon.json
daemon.json 默认位于 /etc/docker/daemon.json
,如果没有可手动创建,基于 systemd 管理的系统都是相同的路径。通过修改 daemon.json
来改过 Docker 配置,也是 Docker 官方推荐的方法。
以下说明均基于 systemd,并通过 /etc/docker/daemon.json
来修改配置。
配置镜像下载和上传并发数
从 Docker1.12 开始,支持自定义下载和上传镜像的并发数,默认值上传为 3 个并发,下载为 5 个并发。通过添加”max-concurrent-downloads”和”max-concurrent-uploads”参数对其修改:
"max-concurrent-downloads": 3,
"max-concurrent-uploads": 5
|
配置镜像加速地址
Rancher 从 v1.6.15 开始到 v2.x.x,Rancher 系统相关的所有镜像(包括 1.6.x 上的 K8S 镜像)都托管在 Dockerhub 仓库。Dockerhub 节点在国外,国内直接拉取镜像会有些缓慢。为了加速镜像的下载,可以给 Docker 配置国内的镜像地址。
编辑 /etc/docker/daemon.json
加入以下内容
{
"registry-mirrors": ["https://7bezldxe.mirror.aliyuncs.com/","https://IP:PORT/"]
}
|
可以设置多个 registry-mirrors
地址,以数组形式书写,地址需要添加协议头(https 或者 http)。
配置 insecure-registries
私有仓库
Docker 默认只信任 TLS 加密的仓库地址(https),所有非 https 仓库默认无法登陆也无法拉取镜像。insecure-registries
字面意思为不安全的仓库,通过添加这个参数对非 https 仓库进行授信。可以设置多个 insecure-registries
地址,以数组形式书写,地址不能添加协议头(http)。
编辑 /etc/docker/daemon.json
加入以下内容:
{
"insecure-registries": ["192.168.1.100","IP:PORT"]
}
|
配置 Docker 存储驱动
OverlayFS 是一个新一代的联合文件系统,类似于 AUFS,但速度更快,实现更简单。Docker 为 OverlayFS 提供了两个存储驱动程序:旧版的 overlay
,新版的 overlay2
(更稳定)。
先决条件:
overlay2
: Linux 内核版本 4.0 或更高版本,或使用内核版本 3.10.0-514+的 RHEL 或 CentOS。overlay
: 主机 Linux 内核版本 3.18+- 支持的磁盘文件系统
- ext4(仅限 RHEL 7.1)
- xfs(RHEL7.2 及更高版本),需要启用 d_type=true。
具体详情参考 Docker Use the OverlayFS storage driver
编辑 /etc/docker/daemon.json
加入以下内容
{
"storage-driver": "overlay2",
"storage-opts": ["overlay2.override_kernel_check=true"]
}
|
配置日志驱动
容器在运行时会产生大量日志文件,很容易占满磁盘空间。通过配置日志驱动来限制文件大小与文件的数量。
限制单个日志文件为 100M
,最多产生 3
个日志文件
{
"log-driver": "json-file",
"log-opts": {
"max-size": "100m",
"max-file": "3"
}
}
|
(可选)修改 Docker0 默认 网络接口 IP 地址
Docker 第一次运行时会自动创建名为 docker0 的网络接口,默认接口地址为 172.17.0.1/16。在一些企业中,可能已经使用了这个网段的地址,或者规划以后会使用这个网段的地址。所以,建议在安装好 docker 服务后,第一时间修改 docker0 接口地址,避免后期出现网段冲突。
- 停止 docker 运行
systemctl stop docker.service
- 删除已有的 docker0 接口
sudo ip link del docker0
- 修改 docker 配置文件
在 /etc/docker/daemon.json
中添加 "bip": "192.168.100.1/24",
- 重启启动 docker
systemctl enable docker; systemctl daemon-reload; systemctl restart docker;
docker.service 配置
- 对于 CentOS 系统,docker.service 默认位于
/usr/lib/systemd/system/docker.service
; - 对于 Ubuntu 系统,docker.service 默认位于
/lib/systemd/system/docker.service
。
编辑 docker.service
,添加以下参数。
- 防止 docker 服务 OOM:
OOMScoreAdjust=-1000
- 开启 iptables 转发链:
ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT
Ubuntu\Debian 系统 ,docker info 提示 WARNING: No swap limit support
Ubuntu\Debian 系统下,默认 cgroups 未开启 swap account 功能,这样会导致设置容器内存或者 swap 资源限制不生效。可以通过以下命令解决:
# 统一网卡名称为 ethx
sudo sed -i 's/en[[:alnum:]]*/eth0/g' /etc/network/interfaces;
sudo sed -i 's/GRUB_CMDLINE_LINUX="\(.*\)"/GRUB_CMDLINE_LINUX="net.ifnames=0 cgroup_enable=memory swapaccount=1 biosdevname=0 \1"/g' /etc/default/grub;
sudo update-grub;
|
注意 通过以上命令可自动配置参数,如果 /etc/default/grub
非默认配置,需根据实际参数做调整。 提示 以上配置完成后,建议重启一次主机