k8s集群搭建
前期准备
1.关闭selinux
永久关闭selinux
sed -i 's/enforcing/disabled/g' /etc/selinux/config
更改后需重启系统
reboot
2.关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
3.关闭swap内存分区
永久关闭swap分区
echo vm.swappiness=0 >> /etc/sysctl.conf && sysctl -p
vim /etc/fstab
注释掉带有swap分区这行 (如下最后一行)
# /dev/mapper/centos-swap swap swap defaults 0 0#
# /etc/fstab
# Created by anaconda on Thu Dec 14 09:33:15 2023
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos_geendata04-root / xfs defaults 0 0
UUID=3e1fd9d5-dc1a-4d6f-ae48-8912cd4c1cde /boot xfs defaults 0 0
#/dev/mapper/centos_geendata04-swap swap swap defaults 0 0
修改完毕后需要重启系统
reboot
4.修改各个节点hostname
命令
hostnamectl set-hostname [hostname]分别为k8s-master、k8s-node1、k8s-node2
5.为每个节点添加host解析
echo "192.168.182.128 k8s-master" >> /etc/hosts && echo "192.168.182.132 k8s-node1" >> /etc/hosts && echo "192.168.182.133 k8s-node2" >> /etc/hosts
6.将桥接的 IPv4 流量传递到 iptables 的链
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
使配置生效
sysctl --system
7.同步时间
timedatectl set-timezone Asia/Shanghai
# 查看是否设置成功
timedatectl
8.添加kubernetes阿里云yum源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpgEOF
开始安装
1、安装docker(公开版20.10.17)
1、安装软件依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
2、设置仓库(阿里云)
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3、安装docker engine-community
yum install -y docker-ce docker-ce-cli containerd.io
4、启动docker(添加开机自启动)
systemctl start docker && systemctl enable docker
5、更改docker启动参数cgroups为systemd
vim /etc/docker/daemon.json内容如下
{"max-concurrent-downloads": 10,"max-concurrent-uploads": 20,"registry-mirrors": ["https://docker.mirrors.ustc.edu.cn","https://registry.cn-hangzhou.aliyuncs.com","https://registry.docker-cn.com"],"exec-opts": ["native.cgroupdriver=systemd"]
}
更改后需重启docker
systemctl daemon-reload && systemctl restart docker
2、安装kubeadm、kubectl、kubelet(1.20.2)
yum install -y kubeadm-1.20.2 kubelet-1.20.2 kubectl-1.20.2
3、初始化master节点(192.168.31.200节点执行)
kubeadm init \
--apiserver-advertise-address=192.168.182.128 \
--image-repository registry.aliyuncs.com/google_containers \
--kubernetes-version v1.20.2 \
--service-cidr=10.96.0.0/12 \
--pod-network-cidr=10.244.0.0/16
执行完毕后,导入配置
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
4、工作节点加入集群
在master节点init完毕之后会有一行join命令,复制到各个节点去加入即可
kubeadm join 192.168.182.128:6443 --token 6f5p4j.ind40kpbnyu1b2pp --discovery-token-ca-cert-hash sha256:876d50340929a551a4d7f9e882c7db76bda76ec6cb9e6ecf3e064445c4b95519
此时因为没有添加网络,所以执行kubectl get nodes 是节点状态异常,需要添加网络
5、安装网络插件(calico 3.18.0)
curl https://docs.projectcalico.org/v3.18/manifests/calico.yaml >> calico.yamlkubectl apply -f calico.yaml
至此k8s集群部署完毕,使用kubectl get nodes可以看到k8s节点状态都是就绪状态