未经允许,不得转载
本人使用的系统是:fedora 38
该系统与centos
rockylinux
almalinux
同理,本人更倾向于使用fedora
。废话不多说,直接开始正题。
准备两台机器
主机名 | IP |
---|---|
master | 192.168.101 |
node | 192.168.91 |
设置主机名
# 192.168.101 机器
hostnamectl set-hostname master
# 192.168.91 机器
hostnamectl set-hostname node
准备好之后,下面所有的操作这两台机器都要执行,这很重要!!!
先保证机器可以通过主机名相互访问
# 修改 /etc/hosts 文件
vim /etc/hosts
# Loopback entries; do not change.
# For historical reasons, localhost precedes localhost.localdomain:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# 添加 IP 主机名
192.168.2.101 master
192.168.2.91 node
# See hosts(5) for proper format and other examples:
# 192.168.1.10 foo.mydomain.org foo
# 192.168.1.13 bar.mydomain.org bar
更新一下系统包
dnf -y upgrade
或者
yum -y upgrade
如果更新慢,下载包网速慢,可以更换为国内源。以阿里云为例
选中系统,找到自己对应的系统。按照步骤替换即可。
关闭防火墙
# 关闭防火墙
systemctl stop firewalld
# 禁止开机自启
systemctl disable firewalld
禁用 selinux
# 永久关闭
sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# 临时关闭,规避重启
setenforce 0
关闭 swap
# fedora系统永久关闭 swap
dnf -y remove zram-generator-defaults
# 不是fedora系统永久关闭 swap
sed -ri 's/.*swap.*/#&/' /etc/fstab
# 临时关闭,规避重启
swapoff -a
转发 IPv4 并让 iptables 看到桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# 设置所需的 sysctl 参数,参数在重新启动后保持不变
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# 应用 sysctl 参数而不重新启动
sudo sysctl --system
通过运行以下指令确认 br_netfilter
和 overlay
模块被加载
lsmod | grep br_netfilter
lsmod | grep overlay
通过运行以下指令确认 net.bridge.bridge-nf-call-iptables
、net.bridge.bridge-nf-call-ip6tables
和 net.ipv4.ip_forward
系统变量在你的 sysctl 配置中被设置为 1
sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward
安装podman
containerd
。podman
也可以替换为docker
dnf -y install podman containerd
安装kubelet
kubeadm
kubectl
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
setenforce 0
dnf -y install kubelet kubeadm kubectl
创建默认配置文件
containerd config default > /etc/containerd/config.toml
修改配置
vim /etc/containerd/config.toml
# 注意修改以下配置
# sandbox_image = "registry.k8s.io/pause:3.6"
sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6"
# SystemdCgroup = false
SystemdCgroup = true
变更podman镜像源
vim /etc/containers/registries.conf
# 注意修改以下配置
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
location = "docker.io"
[[registry.mirror]]
location = "registry.docker-cn.com"
[[registry.mirror]]
location = "hub-mirror.c.163.com"
insecure = true
加入开机自启并重启podman
、containerd
systemctl daemon-reload
systemctl enable podman
systemctl restart podman
systemctl enable containerd
systemctl restart containerd
生成crictl
配置文件
# 生成`crictl`配置文件
crictl config runtime-endpoint
# 修改配置
vim /etc/crictl.yaml
runtime-endpoint: "unix:///run/containerd/containerd.sock"
image-endpoint: "unix:///run/containerd/containerd.sock"
timeout: 10
debug: false
pull-image-on-create: false
disable-pull-on-run: false
启动kubelet
systemctl enable kubelet && systemctl restart kubelet
到此为止,所有的准备工作都完成了。现在准备开始初始化k8s集群,下面操作只在主节点上执行,也就是我的这台IP是:192.168.2.101 的 master 机器
开始初始化k8s集群
# 执行初始化
kubeadm init --apiserver-advertise-address=192.168.2.101 --pod-network-cidr=10.224.0.0/16 --image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers --token-ttl=0
# --apiserver-advertise-address 主节点机器IP
# --pod-network-cidr pod 网络可以使用的 IP 地址段
# --image-repository 镜像的容器仓库
# --token-ttl token过期时长,如果设置为 '0',则令牌将永不过期
初始化完成之后如下图:
执行k8s要求我们要执行的命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 如果是 root 账号执行
export KUBECONFIG=/etc/kubernetes/admin.conf
在 node 节点执行下面的 join 命令,我这边就是对应的IP为:192.168.2.91 的这台 node 机器
# 执行 join 命令
kubeadm join 192.168.2.101:6443 --token xncjvy.89mlmaiju51ckock --discovery-token-ca-cert-hash sha256:b251743c2ccad45d3be0e2c90fbb4a84adf05f0cf45b9d9161f44aa47c1d58c6
安装calico
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.1/manifests/tigera-operator.yaml
wget https://raw.githubusercontent.com/projectcalico/calico/v3.25.1/manifests/custom-resources.yaml
kubectl create -f custom-resources.yaml
未经允许,不得转载