请先参考 Windows10安装VirtualBox及Vagrant 安装好VirtualBox及Vagrant
服务器规划
使用5台服务器部署,ip地址如下:
192.168.99.211 k8s-test1
192.168.99.212 k8s-test2
192.168.99.213 k8s-test3
192.168.99.214 k8s-test4
192.168.99.215 k8s-test5
其中k8s-test1/k8s-test2/k8s-test3作为k8s的主节点,其它2台作为node节点
假定Vagrantfile
文件的路径在D:\vagrant\k8s
Vagrantfile
文件的内容为
Vagrant.configure("2") do |config|
(1..5).each do |i|
config.vm.define "k8s-test#{i}" do |node|
# 设置虚拟机的Box
node.vm.box = "centos/7"
node.vm.box_url = "https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box"
# 设置虚拟机的主机名
node.vm.hostname="k8s-test#{i}"
# 设置虚拟机的IP
node.vm.network "private_network", ip: "192.168.99.#{210+i}", netmask: "255.255.255.0"
# VirtaulBox相关配置
node.vm.provider "virtualbox" do |v|
# 设置虚拟机的名称
v.name = "k8s-test#{i}"
# 设置虚拟机的内存大小。注意:如果电脑内存不够,这里需要设置小一些,否则Vagrant启动时会报错
v.memory = 6000
# 设置虚拟机的CPU个数
v.cpus = 4
end
end
end
end
使用管理员权限打开cmd命令行窗口
cd D:\vagrant\k8s
#启动服务器
vagrant up
连接服务器,配置ssh
vagrant ssh k8s-test1
#切换root用户,默认的密码是vagrant
su
#把PasswordAuthentication no 修改成yes
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
#重启ssh服务
service sshd restart
#保存,执行2次exit退出k8s-test1服务器
其它服务器使用类似命令 vagrant ssh k8s-test2
连接上服务器,做相同的配置
使用Xshell用root用户连接上5台服务器
使用Xshell的工具->发送键输入到所有会话,给5台服务器同时执行命令
vi /etc/hosts
#删除这行127.0.1.1 k8s-test1 k8s-test1
192.168.99.211 k8s-test1
192.168.99.212 k8s-test2
192.168.99.213 k8s-test3
192.168.99.214 k8s-test4
192.168.99.215 k8s-test5
#检查5台机器是否能正常通信
ping k8s-test1
ping k8s-test2
ping k8s-test3
#配置阿里云仓库
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum -y update
CentOS7.8默认内核版本是3.10,需要升级到4.19
查看版本号 uname -a
#设置源
cat > /etc/yum.repos.d/linnux-kernel.repo <
设置启动项并重启
grub2-mkconfig -o /boot/grub2/grub.cfg
grub2-set-default 0
#查看默认内核是不是4.19
grubby --default-kernel
reboot
#再次查看内核版本号
grubby --default-kernel
yum install -y yum-utils device-mapper-persistent-data lvm2 psmisc vim telnet git
yum install -y conntrack ipvsadm ipset jq sysstat curl libseccomp net-tools nc wget htop
#配置Docker安装源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#配置kubernetes安装源
cat < /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
timedatectl set-timezone Asia/Shanghai && date && echo 'Asia/Shanghai' > /etc/timezone
#安装ntpdate
rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
yum install ntpdate -y
#所有节点同步时间
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ntpdate time2.aliyun.com
# 加入到crontab系统定时任务中
crontab -e
*/5 * * * * /usr/sbin/ntpdate time2.aliyun.com
#查看该用户下的crontab服务是否创建成功
crontab -l
配置limit
ulimit -SHn 65535
vi /etc/security/limits.conf
# 末尾添加如下内容
* soft nofile 65536
* hard nofile 131072
* soft nproc 65535
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited
关闭防火墙及swap
#将SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
#关闭交换分区 free -m 其中swap 要为0
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
#允许 iptables 检查桥接流量
cat <
配置k8s集群中的内核参数
cat < /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.may_detach_mounts = 1
net.ipv4.conf.all.route_localnet = 1
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
EOF
sysctl --system
yum install -y ipvsadm ipset sysstat conntrack libseccomp
执行
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
加入到启动配置文件中
vi /etc/modules-load.d/ipvs.conf
ip_vs
ip_vs_lc
ip_vs_wlc
ip_vs_rr
ip_vs_wrr
ip_vs_lblc
ip_vs_lblcr
ip_vs_dh
ip_vs_sh
ip_vs_fo
ip_vs_nq
ip_vs_sed
ip_vs_ftp
ip_vs_sh
nf_conntrack
ip_tables
ip_set
xt_set
ipt_set
ipt_rpfilter
ipt_REJECT
ipip
启动并查看状态
systemctl enable --now systemd-modules-load.service
lsmod | grep --color=auto -e ip_vs -e nf_conntrack
Docker安装文件中已包含了Containerd
安装Docker后,可以无需启动Docker,只需要配置和启动Containerd即可
yum install -y docker-ce-20.10.* docker-ce-cli-20.10.*
配置Containerd所需的模块
cat <
加载模块,执行
modprobe -- overlay
modprobe -- br_netfilter
配置Containerd所需的内核参数
cat <
配置Containerd的配置文件
mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml
#将Containerd的Cgroup改为Systemd
vim /etc/containerd/config.toml
#找到containerd.runtimes.runc.options,修改
SystemdCgroup = true
#sandbox_image的Pause镜像改成registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6
sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6"
启动Containerd
systemctl daemon-reload
systemctl enable --now containerd
配置crictl客户端连接的运行时位置
cat > /etc/crictl.yaml <
查看镜像
ctr image ls
在k8s-test1配置ssh免密钥登录其他节点(只在k8s-test1上执行)
ssh-keygen -t rsa #回车2次
#拷贝公钥到其它服务器,需要输入密码
for i in k8s-test1 k8s-test2 k8s-test3 k8s-test4 k8s-test5;do ssh-copy-id -i .ssh/id_rsa.pub $i;done
#测试ssh
ssh k8s-test5
exit