目前所在的公司,因为业务原因,有的业务需要把集群部署到客户提供的机器上,客户提供的机器配置又容不得我们自己做主,对此综合考虑选择使用kubernetes轻量版:k3s
K3s将kbernetes所需的一切都打包到一个仅有60MB的二进制文件中,为了轻量,k3s去除了许多不必要的驱动程序,kubernetes的功能都有。 接下来将演示一master一node节点K3S集群快速安装。
1、环境准备
修改hosts(/etc/hosts)
cat >> /etc/hosts <<EOF 10.30.18.80 node1-80 10.30.18.81 node2-81 EOF
所有节点时间同步:
#master安装chrony: yum -y install chrony #注释默认ntp服务器 sed -i 's/^server/#&/' /etc/chrony.conf#指定上游公共 ntp 服务器,并允许其他节点同步时间 cat >> /etc/chrony.conf << EOF server 0.asia.pool.ntp.org iburst server 1.asia.pool.ntp.org iburst server 2.asia.pool.ntp.org iburst server 3.asia.pool.ntp.org iburst allow all EOF #重启chronyd服务并设为开机启动: systemctl enable chronyd && systemctl restart chronyd #开启网络时间同步功能 timedatectl set-ntp true #node设置时间同步#安装chrony: yum -y install chrony #注释默认服务器sed -i 's/^server/#&/' /etc/chrony.conf #指定内网 master节点为上游NTP服务器 echo 'server 10.1.10.128 iburst' >> /etc/chrony.conf #重启服务并设为开机启动: systemctl enable chronyd && systemctl restart chronyd
关闭selinux:
setenforce 0 && sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
关闭防火墙:
systemctl disable firewalld && systemctl stop firewalld
修改内核参数:
cat << EOF > /etc/sysctl.d/k3s.conf net.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward=1 vm.swappiness=0 EOF # sysctl -p /etc/sysctl.d/k3s.conf
关闭swap:
swapoff -a && sed -i 's/\(.*swap.*\)/#\1/' /etc/fstab
安装IPVS:
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vsmodprobe -- ip_vs_rrmodprobe -- ip_vs_wrrmodprobe -- ip_vs_shmodprobe -- nf_conntrack_ipv4
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
yum install ipset ipvsadm -y
k3s 的默认网络方案是 flannel的VXLAN,跨云建议使用 wireguard 模式,
WG 对内核的要求比较高,需要升级更高版本的内核。
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.orgrpm -Uvh http://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm yum --disablerepo=\* --enablerepo=elrepo-kernel repolistyum --disablerepo=\* --enablerepo=elrepo-kernel install kernel-ml.x86_64 -y yum remove kernel-tools-libs.x86_64 kernel-tools.x86_64 -y yum --disablerepo=\* --enablerepo=elrepo-kernel install kernel-ml-tools kernel-ml-devel kernel-ml-headers -y grep "^menuentry" /boot/grub2/grub.cfg | cut -d "'" -f2 grub2-editenv list grub2-set-default 'CentOS Linux (5.8.2-1.el7.elrepo.x86_64) 7 (Core)'
更新好内核后需要reboot一下。
接下来安装WG:
yum install epel-release https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm -yyum install yum-plugin-elrepo -yyum install kmod-wireguard wireguard-tools -y
2、安装K3s server节点
好的,到目前为止,我们已经做好的集群所需要的基础环境,接下来就是部署集群有一个内嵌 SQLite 数据库的单节点 K3s server
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn INSTALL_K3S_SKIP_START=true K3S_KUBECONFIG_MODE=644 INSTALL_K3S_VERSION=v1.18.9+k3s1 sh -
我们上面的命令是使用官方提供的一个安装脚本,INSTALL_K3S_VERSION指定我们所需要的版本;INSTALL_K3S_SKIP_START=true指的是安装完节点不自启,后续需要手动。
k3s支持后端存储有:etcd、mysql、SQLite等。如果需要使用mysql存储;则使用以下格式
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn INSTALL_K3S_SKIP_START=true K3S_KUBECONFIG_MODE=644 INSTALL_K3S_VERSION=v1.18.9+k3s1 K3S_DATASTORE_ENDPOINT='mysql://k3s_cluster:k3s_cluster@tcp(xxxxxx.mysql.rds.aliyuncs.com:3306)/k3s_cluster' sh -s - server
修改/etc/systemd/system/k3s.service 启动文件:
ExecStart=/usr/local/bin/k3s \ server \ --no-deploy servicelb,traefik \ --flannel-backend wireguard \ --kube-proxy-arg "proxy-mode=ipvs" "masquerade-all=true" \ --kube-proxy-arg "metrics-bind-address=0.0.0.0"
重启k3s server:
systemctl daemon-reload && systemctl restart k3s.service
3、node节点加入集群
在node节点上安装k3s-agent服务,加入集群需要两个配置:
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn INSTALL_K3S_VERSION=v1.18.9+k3s1 INSTALL_K3S_SKIP_START=true K3S_URL=https://node1-80:6443 K3S_TOKEN=K10701b2edee1ef8a2ff7139f43a5e7b906d3e126a36c169dcfa76a44302924391a::server:87cd858b5b63e4e320dd6b4e5fd2978f sh -
K3S_URL=https://node1-80:6443
K3S_TOKEN=K10701b2edee1ef8a2ff7139f43a5e7b906d3e126a36c169dcfa76a44302924391a::server:87cd858b5b63e4e320dd6b4e5fd2978f
K3S_TOKEN可以在/var/lib/rancher/k3s/server/node-token里找到。
修改agent启动文件:
ExecStart=/usr/local/bin/k3s \ agent \ --kube-proxy-arg "proxy-mode=ipvs" "masquerade-all=true" \ --kube-proxy-arg "metrics-bind-address=0.0.0.0"
重启k3s-agent服务:
systemctl daemon-reload && systemctl restart k3s-agent.service
最后需要安装下命令补全工具,方便使用:
yum install bash-completion -y source /usr/share/bash-completion/bash_completion echo "source <(kubectl completion bash)" >> /etc/profilesource /etc/profile
