环境准备

准备虚拟机

本文教程采用虚拟机方式搭建各节点。
准备搭建一个master节点,两个工作节点的集群。

virtual machinehostnameiptype
ubuntu server 22.04k8s-master192.168.1.81master-node
ubuntu server 22.04k8s-node1192.168.1.82work-node
ubuntu server 22.04k8s-node2192.168.1.83work-node

虚拟机配置

每台机器按照以下步骤进行配置:

  1. 时间同步
apt-get install ntpdate -y

ntpdate time.windows.com
  1. 关闭防火墙服务(虚拟机默认未安装防火墙)
systemctl disable firewalld
  1. 关闭swap分区(虚拟机默认未开启)
...
  1. 关闭selinux(需重启)
echo "SELINUX = disabled" >> /etc/selinux/semanage.conf
  1. 启用overlay(若使用calico作为cni插件则不需要)br_netfilter模块
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf

overlay
br_netfilter

EOF

显式加载模块

modprobe overlay
modprobe br_netfilter
  1. 允许iptables桥接和转发流量
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1

EOF

sudo sysctl --system

安装k8s-v1.24

每台机器按照以下步骤操作:

  1. 安装相关支持
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg \
lsb-release
  1. 设置阿里云k8s软件源镜像
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -

cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt kubernetes-xenial main
EOF

apt update
  1. 安装containerd
wget https://github.com/containerd/containerd/releases/download/v1.6.6/containerd-1.6.6-linux-amd64.tar.gz

tar Cxzvf /usr/local containerd-1.6.6-linux-amd64.tar.gz

通过systemd启动containerd

wget https://raw.githubusercontent.com/containerd/containerd/v1.6.6/containerd.service

mv containerd.service /usr/lib/systemd/system/

systemctl daemon-reload

systemctl enable --now containerd

创建containerd配置,并配置阿里云镜像加速

mkdir -p /etc/containerd

containerd config default | sudo tee /etc/containerd/config.toml

sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml && \
grep 'SystemdCgroup' -B 11 /etc/containerd/config.toml

sed -i 's#endpoint = ""#endpoint = "https://xx4bwyg2.mirror.aliyuncs.com"#g' /etc/containerd/config.toml && \
grep 'endpoint' -B 5 /etc/containerd/config.toml

sed -i 's#sandbox_image = "k8s.gcr.io/pause:3.6"#sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.6"#g' /etc/containerd/config.toml && \
grep 'sandbox_image' /etc/containerd/config.toml

systemctl daemon-reload

systemctl restart containerd.service

需要额外安装runc和cni-plugins,参考

https://github.com/containerd/containerd/blob/main/docs/getting-started.md
  1. 安装kubeadm、kubectl、kubelet
apt install kubeadm=1.24.2-00 kubelet=1.24.2-00 kubectl=1.24.2-00
  1. 所有节点必须部署一个基于 Pod 网络插件的容器网络接口(CNI),以便Pod可以相互通信。 在安装网络之前,集群 DNS (CoreDNS) 将不会启动。本教程部署flannel作为cni插件实现。教程参考安装flannel插件。当部署完flannel后,master节点处于pending状态的coredns将会创建容器并启动。node节点需要保证/opt/bin目录下存在flanneld

master节点额外执行以下步骤:
创建初始化配置文件(也可以不创建文件,通过初始化参数指定),并修改镜像源为阿里云

kubeadm config print init-defaults > kubeadm-config.yaml

修改为以下配置:

advertiseAddress: 192.168.1.81
name: k8s-master
imageRepository: registry.aliyuncs.com/google_containers
kubernetesVersion: 1.24.2
networking:
  serviceSubnet: 10.96.0.0/12
  podSubnet: 10.244.0.0/16

如果使用calico作为cni插件,修改podSubnet网段:

networking:
  serviceSubnet: 10.96.0.0/12
  podSubnet: 192.168.0.0/16

追加以下配置,修改kubelet的cgroup驱动

---
kind: KubeletConfiguration
apiVersion: kubelet.config.k8s.io/v1beta1
cgroupDriver: systemd

使用kubeadm初始化

kubeadm init --config kubeadm-config.yaml

将master节点中的【/etc/kubernetes/admin.conf】文件拷贝到node节点相同目录下,然后配置环境变量:

echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile

立即生效

source ~/.bash_profile

工作节点join到控制平面管理参考添加node节点

至此,就已经搭建好了一个master节点,两个node节点的k8s集群。下一章将通过web-ui的方式管理k8s集群

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐