k8s安装
安装虚拟机,软路由
软路由参考链接: 虚拟机(VM)安装openwrt-koolshare软路由.
虚拟机设置yum源: CentOS镜像使用帮助
设置系统主机名以及Host文件的相互解析(以下以k8s-master01为例)
hostnamectl set-hostname k8s-master01
修改 /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.66.10 k8s-master01
192.168.66.20 k8s-node01
192.168.66.21 k8s-node02
安装依赖包
yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net tools git
设置防火墙为Iptables 并设置空规则
- 关闭firewalld并取消自启动
systemctl stop firewalld && systemctl disable firewalld
- 安装iptables,启动iptables,设置开机自启,清空iptables规则,保存当前规则到默认规则
yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && service iptables save
关闭 swap,关闭 SELINUX
- 关闭swap分区
# 关闭swap分区【虚拟内存】并且永久关闭虚拟内存。
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
**kubeadm初始化Kubernetes时的过程中会检测swap分区到底有没有关闭,因为如果开启虚拟内存的话,kubernetes的容器【pod】就有可能会运行在虚拟内存上,会大大的降低容器的工作效率,因此Kubernetes会要求强制关闭,可以通过kubelet的启动参数–fail-swap-on=false更改这个限制。推荐关闭以防止容器运行在虚拟内存的情况出现。
- 关闭SELinux
setenforce 0 && sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
相对于Kubernetes调整内核参数
cat > kubernetes.conf <<EOF
# 开启网桥模式【重要】
net.bridge.bridge-nf-call-iptables=1
# 开启网桥模式【重要】
net.bridge.bridge-nf-call-ip6tables=1
net.ipv4.ip_forward=1
net.ipv4.tcp_tw_recycle=0
# 禁止使用swap空间,只有当系统OOM时才允许使用它
vm.swappiness=0
# 不检查物理内存是否够用
vm.overcommit_memory=1
# 开启OOM
vm.panic_on_oom=0
fs.inotify.max_user_instances=8192
fs.inotify.max_user_watches=1048576
fs.file-max=52706963
fs.nr_open=52706963
# 关闭ipv6【重要】
net.ipv6.conf.all.disable_ipv6=1
net.netfilter.nf_conntrack_max=2310720
EOF
cp kubernetes.conf /etc/sysctl.d/kubernetes.conf
sysctl -p /etc/sysctl.d/kuberbetes.conf
调整系统时区
# 设置系统时区为 中国/上海
timedatectl set-timezone Asia/Shanghai
# 将当前的 UTC 时间写入硬件时钟
timedatectl set-local-rtc 0
# 重启依赖于系统时间的服务
systemctl restart rsyslog
systemctl restart crond
关闭系统不需要服务
#关闭及禁用邮件服务
systemctl stop postfix && systemctl disable postfix
设置 rsyslogd 和 systemd journald
在Centos7以后,因为引导方式改为了system.d,所以有两个日志系统同时在工作,默认的是rsyslogd,以及systemd journald
使用systemd journald更好一些,因此我们更改默认为systemd journald,只保留一个日志的保存方式。
- 创建保存日志的目录
mkdir /var/log/journal
- 创建配置文件存放目录
mkdir /etc/systemd/journald.conf.d
- 创建配置文件
cat > /etc/systemd/journald.conf.d/99-prophet.conf << EOF
[Journal]
# 持久化保存到磁盘
Storage=persistent
# 压缩历史日志
Compress=yes
SyncIntervalSec=5m
RateLimitInterval=30s
RateLimitBurst=1000
# 最大占用空间 10G
SystemMaxUse=10G
# 单日志文件最大 200M
SystemMaxFileSize=200M
# 日志保存时间 2 周
MaxRetentionSec=2week
# 不将日志转发到 syslog
ForwardToSyslog=no
EOF
- 重启systemd journald的配置
systemctl restart systemd-journald
升级系统内核为 4.44
CentOS 7.x 系统自带的3.10.x内核存在一些Bugs.导致运行的Docker.Kubernetes不稳定。
- 获取源
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
- 开始安装,安装完成后检查 /boot/grub2/grub.cfg中对应内核menuentry中是否包含 initrd16 配置,如果没有,再安装一次!
yum --enablerepo=elrepo-kernel install -y kernel-lt
- 查看系统中的全部内核【可忽略】
rpm -qa | grep kernel
- 设置开机从新内核启动
grub2-set-default 'CentoS Linux(4.4.202-1.el7.elrepo.×86_64) 7 (Core)'
- 重启启动使配置生效
reboot
- 查看正在使用的内核【可忽略】
uname -a