Kubernetes作为容器的编排平台,它是以集群的形式为业务提供服务。所以在日常的工作中,作为Kubernetes平台的维护者,会经常对集群进行管理。
Kubernetes的集群安装分为:kubeadm安装和二进制安装。在这里,只会介绍kubeadm的安装。
安装说明:
集群节点:2个 IP信息: master:192.168.205.128 node:192.168.205.128 Kubernetes版本:v1.24.2 运行时:containerd 系统:centos 7.9 系统内核:3.10.0-1160
这是安装的不是生产级别的集群,只是为了演示使用。
(1)在每个节点添加host信息
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat >> /etc/hosts << EOF</span><br>192.168.205.128 kk-master<br>192.168.205.130 kk-node01<br>EOF<br>
(2)关闭防火墙和SELinux
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl stop firewalld</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl <span style="color: #a6e22e;line-height: 26px;">disable</span> firewalld</span><br><span style="color: #75715e;line-height: 26px;"><br>$</span><span style="line-height: 26px;"> setenforce 0</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat /etc/selinux/config</span><br>SELINUX=disabled<br>
(3)优化内核参数
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat > /etc/sysctl.d/k8s.conf << EOF</span><br>net.bridge.bridge-nf-call-ip6tables = 1<br>net.bridge.bridge-nf-call-iptables = 1<br>net.ipv4.ip_forward = 1<br>vm.swappiness=0<br>EOF<br>
执行以下命令使其生效:
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> modprobe br_netfilter</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> sysctl -p /etc/sysctl.d/k8s.conf</span><br>
(4)关闭swap空间
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> swapoff -a</span><br>
注释/etc/fstab文件中swap挂载。
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat /etc/fstab </span><br><span style="color: #75715e;line-height: 26px;"><br>#</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> /etc/fstab</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> Created by anaconda on Tue Apr 12 17:10:16 2022</span><br><span style="color: #75715e;line-height: 26px;">#</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> Accessible filesystems, by reference, are maintained under <span style="color: #a6e22e;line-height: 26px;">'/dev/disk'</span></span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) <span style="color: #f92672;font-weight: bold;line-height: 26px;">for</span> more info</span><br><span style="color: #75715e;line-height: 26px;">#</span><br>/dev/mapper/centos-root / xfs defaults 0 0<br>UUID=bc73c871-006c-4e24-a7af-6beb9aac06a7 /boot xfs defaults 0 0<br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> /dev/mapper/centos-swap swap swap defaults 0 0</span><br>
(5)安装ipvs软件包
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat > /etc/sysconfig/modules/ipvs.modules <<EOF</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;">!/bin/bash</span><br>modprobe -- ip_vs<br>modprobe -- ip_vs_rr<br>modprobe -- ip_vs_wrr<br>modprobe -- ip_vs_sh<br>modprobe -- nf_conntrack_ipv4<br>EOF<br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4</span><br><span style="color: #75715e;line-height: 26px;"><br>$</span><span style="line-height: 26px;"> yum install ipset ipvsadm -y</span><br>
(6)同步服务器时间
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum install chrony -y</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl <span style="color: #a6e22e;line-height: 26px;">enable</span> chronyd</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl start chronyd</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> chronyc sources</span><br>
(7)安装containerd
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum install -y yum-utils \</span><br> device-mapper-persistent-data \<br> lvm2<br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum-config-manager \</span><br> --add-repo \<br> https://download.docker.com/linux/centos/docker-ce.repo<br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum list | grep containerd</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum install containerd -y</span><br>
创建containerd配置文件。
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> mkdir -p /etc/containerd</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> containerd config default > /etc/containerd/config.toml</span><br><span style="color: #75715e;line-height: 26px;">#</span><span style="line-height: 26px;"> 替换配置文件</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> sed -i <span style="color: #a6e22e;line-height: 26px;">"s#k8s.gcr.io#registry.cn-hangzhou.aliyuncs.com/google_containers#g"</span> /etc/containerd/config.toml</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> sed -i <span style="color: #a6e22e;line-height: 26px;">'s#SystemdCgroup = false#SystemdCgroup = true#g'</span> /etc/containerd/config.toml</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> sed -i <span style="color: #a6e22e;line-height: 26px;">"s#https://registry-1.docker.io#https://registry.cn-hangzhou.aliyuncs.com#g"</span> /etc/containerd/config.toml</span><br>
启动containerd。
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl daemon-reload</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl <span style="color: #a6e22e;line-height: 26px;">enable</span> containerd</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl restart containerd</span><br>
(8)安装Kubernetes组件
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> cat <<EOF > /etc/yum.repos.d/kubernetes.repo</span><br>[kubernetes]<br>name=Kubernetes<br>baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64<br>enabled=1<br>gpgcheck=0<br>repo_gpgcheck=0<br>gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg<br> http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg<br>EOF<br>
安装指定版本的组件。
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> yum install -y kubelet-1.24.2 kubeadm-1.24.2 kubectl-1.24.2</span><br>
设置运行时。
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> crictl config runtime-endpoint /run/containerd/containerd.sock</span><br>
设置kubelet为自启动。
<span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl daemon-reload</span><br><span style="color: #75715e;line-height: 26px;">$</span><span style="line-height: 26px;"> systemctl <span style="color: #a6e22e;line-height: 26px;">enable</span> kubelet && systemctl start kubelet</span><br>
上面把基础环境准备好了,现在开始真正的进行集群初始化。
然后接下来在 master 节点配置 kubeadm 初始化文件,可以通过如下命令导出默认的初始化配置:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。