环境
系统:rocky9.4最小安装,单节点
网络规划:
| 网络 | cidr |
|---|---|
| 主机网络 | 10.0.0.0/24 |
| podip | 10.244.0.0/16 |
| serviceip | 10.96.0.0/12 |
| loadbalanceip | 10.0.1.0/24 |
环境准备
# 修改主机名
vim /etc/hostname
k8s-master
#关闭selinux
[root@k8s-master ~]# sed -i 's/^ *SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
#关闭防火墙
[root@k8s-master ~]# systemctl stop firewalld.service ; systemctl disable firewalld添加yum源
KUBERNETES_VERSION=v1.31
CRIO_VERSION=v1.31
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://pkgs.k8s.io/core:/stable:/$KUBERNETES_VERSION/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/core:/stable:/$KUBERNETES_VERSION/rpm/repodata/repomd.xml.key
EOF
cat <<EOF | tee /etc/yum.repos.d/cri-o.repo
[cri-o]
name=CRI-O
baseurl=https://pkgs.k8s.io/addons:/cri-o:/stable:/$CRIO_VERSION/rpm/
enabled=1
gpgcheck=1
gpgkey=https://pkgs.k8s.io/addons:/cri-o:/stable:/$CRIO_VERSION/rpm/repodata/repomd.xml.key
EOF关闭交换分区
[root@k8s-master ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab
[root@k8s-master ~]# swapoff -a加载模块
1. 永久添加模块
[root@k8s-master ~]# vim /etc/modules-load.d/k8s.conf
overlay
br_netfilter2.手动添加
[root@k8s-master ~]# modprobe br_netfilter
[root@k8s-master ~]# modprobe overlay3.查看添加效果
[root@k8s-master ~]# lsmod | grep br_netfilter修改内核参数
#配置 k8s 网络配置
[root@k8s-master ~]# cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
#这个命令的作用是应用 k8s.conf 文件中的内核参数设置,并且开启网络桥接的防火墙功能。其中 k8s.conf 文件中的内容包括以下三个参数设置:
#net.bridge.bridge-nf-call-iptables = 1 表示开启防火墙功能。
#net.bridge.bridge-nf-call-ip6tables = 1 表示开启 IPV6 的防火墙功能。
#net.ipv4.ip_forward = 1 表示开启 IP 转发功能。
[root@k8s-master ~]# sysctl -p /etc/sysctl.d/k8s.conf
#重新加载内核参数配置文件,以确保这些设置生效。
[root@k8s-master ~]# sysctl --system安装ipvs模块
IPVS(IP Virtual Server)是 Linux 内核自带的一种负载均衡技术,它可以将请求分配到一组后端服务器(也称为“真实服务器”),从而提高系统的性能和可靠性。IPVS 采用网络地址转换(NAT)或直接路由模式来处理请求,支持 LVS(Linux Virtual Server)协议、TCP、UDP 和 SCTP 等多种协议。IPVS 能够自动检测后端服务器的状态,如果某个服务器出现故障,IPVS 会自动将其从服务器池中删除,并将请求转发给其他健康的服务器。
IPVS 主要由以下三个组件组成:
调度器(Scheduler):负责根据特定的算法(如轮询、加权轮询、最小连接数等)将请求转发到后端服务器。
控制器(IPVS Control):负责配置和管理 IPVS 的规则和策略。
真实服务器(Real Server):负责处理来自客户端的请求。
IPVS 的优点是实现简单、性能高、可靠性强,并且支持多种负载均衡算法,可以灵活地根据实际需求进行配置和管理。
[root@k8s-master ~]# yum -y install ipset ipvsadm
[root@k8s-master ~]# mkdir -p /etc/sysconfig/modules/
[root@k8s-master ~]# vim /etc/sysconfig/modules/ipvs.modules
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
[root@k8s-master ~]# chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack安装基础包
[root@k8s-master ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curl curl-devel unzip sudo libaio-devel wget vim ncurses-devel autoconf automake python-devel epel-release openssh-server socat ipvsadm conntrack telnet ipvsadm
# 重启一下
[root@k8s-master ~]# reboot安装cri-o
dnf install -y container-selinux
dnf install -y cri-o kubelet kubeadm kubectl
systemctl enable --now crio.service修改cri-o配置
vim /etc/crio/crio.conf.d/02-pause.conf
[crio.image]pause_image="registry.k8s.io/pause:3.6"
systemctl restart crio.service安装kubernetes
#配置开机自启
[root@k8s-master ~]# systemctl enable --now kubelet
#Kubeadm: kubeadm 是一个工具,用来初始化 k8s 集群的
#kubelet: 安装在集群所有节点上,用于启动 Pod 的
#kubectl: 通过 kubectl 可以部署和管理应用,查看各种资源,创建、删除和更新各种组件
#查看版本
[root@k8s-master ~]# kubeadm version初始化k8s
#生成文件
[root@k8s-master ~]# kubeadm config print init-defaults > kubeadm.yaml
[root@k8s-master ~]# vim kubeadm.yaml
apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 10.0.0.3 #master节点ip
bindPort: 6443
nodeRegistration:
criSocket: unix://`/var/run/crio/crio.sock` # cri-o socket
imagePullPolicy: IfNotPresent
name: k8s-master #主节点名
taints: null
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers #替换镜像仓库
kind: ClusterConfiguration
kubernetesVersion: 1.31.0
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16 #pod 网段
serviceSubnet: 10.96.0.0/12
scheduler: {}
--- #新增
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: systemd拉取需要的脚本
#初始化apiserver等依赖镜像
[root@k8s-master ~]# kubeadm config images pull --config=kubeadm.yaml初始化主节点
#初始化k8s 服务器起码2C,否则报错
[root@k8s-master ~]# kubeadm init --config kubeadm.yaml | tee kubeadm-init.log配置 kubectl 的配置文件 config,相当于对 kubectl 进行授权,这样 kubectl 命令可以使用这个证 书对 k8s 集群进行管理
[root@k8s-master ~]# mkdir -p $HOME/.kube
[root@k8s-master ~]# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
[root@k8s-master ~]# sudo chown $(id -u):$(id -g) $HOME/.kube/config
[root@k8s-master ~]# export KUBECONFIG=/etc/kubernetes/admin.conf设置节点标签
[root@k8s-master ~]# kubectl label nodes k8s-master node-role.kubernetes.io/work=work
kubectl taint nodes --all node-role.kubernetes.io/master-安装calico网络插件
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.28.2/manifests/tigera-operator.yamlwget https://raw.githubusercontent.com/projectcalico/calico/v3.28.2/manifests/custom-resources.yamlvim custom-resources.yaml
spec:
# Configures Calico networking.
calicoNetwork:
# Note: The ipPools section cannot be modified post-install.
ipPools:
- blockSize: 26
cidr: 10.244.0.0/16 # 改成podip 子网范围
encapsulation: VXLANCrossSubnet
natOutgoing: Enabled
nodeSelector: all()
kubectl create -f custom-resources.yaml安装multus-cni
wget https://raw.githubusercontent.com/k8snetworkplumbingwg/multus-cni/master/deployments/multus-daemonset-thick.yml
kubectl apply -f multus-daemonset-thick.yml