一.基础环境配置
1.主机命名
hostnamectl set-hostname k8s-master
vim /etc/hosts
ip k8s-master
2.时间同步
dnf -y install chrony
vim /etc/chrony.conf
server ntp.aliyun.com iburst
systemctl start chronyd
systemctl enable chronyd
timedatectl set-timezone Asia/Shanghai
chronyc sources
3.配置防火墙规则
systemctl stop firewalld
systemctl disable firewalld
dnf -y install iptables-services
systemctl start iptables
systemctl enable iptables
iptables -F
service iptables save
4.关闭selinux
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
5.关闭swap分区
swapoff -a
sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
6.内核配置
cat > /etc/sysctl.d/kubernetes.conf << EOF
vm.swappiness = 0
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
sysctl -p /etc/sysctl.d/kubernetes.conf
7.kube-proxy开启ipvs
dnf -y install ipset ipvsadm
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules && bash
#开机添加自动加载模块
echo "/etc/sysconfig/modules/ipvs.modules" >> /etc/rc.local
chmod +x /etc/rc.local
#启用网桥过滤器模块
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
echo 1 > /proc/sys/net/ipv4/ip_forward
8.配置yum源
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
9.安装kubeadm、kubectl、kubelet
dnf install -y kubelet kubeadm kubectl
systemctl enable kubelet
systemctl start kubelet
二.安装kubernets集群(kubernetes 1.28.2版本)
1.配置文件如下
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.2.0.14 #修改为控制节点IP(VIP)
bindPort: 6443
nodeRegistration:
criSocket: unix:///run/containerd/containerd.sock #使用containerd为容器运行时
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.aliyuncs.com/google_containers #修改为阿里镜像地址
kind: ClusterConfiguration
kubernetesVersion: 1.28.2 #版本
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16 #指定Pod网段
serviceSubnet: 10.96.0.0/12 #指定Service网段
scheduler: {}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: ipvs
---
apiVersion: kubelet.config.k8s.io/v1beta1
kind: KubeletConfiguration
cgroupDriver: systemd
2.初始化集群
kubeadm init --config=kubeadm-conf.yaml
3.初始化kubectl
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
4.添加命令补全
yum -y install bash-completion
echo "source <(kubectl completion bash)" >> ~/.bash_profile
source ~/.bash_profile
5.其他节点加入集群
kubeadm join 10.2.0.14:6443 --token abcdef.xxx\
--discovery-token-ca-cert-hash sha256:fb4330969e315252cdc7b5ece6e16362549bc17d57d47e878fa9c29bd7046d4e
6..网络插件安装(flannel)
wget https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml
kubectl apply -f kube-flannel.yml
7.查看集群信息
kubectl get node