k8s集群安装(kubeadm方式)
一、准备三台虚拟机,系统CentOS7.9;
192.168.1.221 master1
192.168.1.189 node1
192.168.1.60 node2
二、.对三台虚拟机初始化
1.关闭防火墙,关闭开机自启
systemctl stop firewalld #临时
systemctl disable firewalld #永久
2.关闭selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config #永久 setenforce 0 #临时
3.关闭swap
swapoff -a #临时 sed -ri 's/.*swap.*/#&/' /etc/fstab #永久
4.根据规划设置主机名,
hostnamectl set-hostname <hostname>
5.在master添加hosts
cat >> /etc/hosts << EOF 192.168.1.221 master1 192.168.1.189 node1 192.168.1.60 node2 EOF
6.将桥接的IPV4流量传递到iptables的链
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 sysctl --system #重新加载配置
#加载网桥过滤模块
modprobe br_netfilter
#查看是否加载成功
lsmod | grep br_netfilter
7.时间同步
yum install ntpdate -y ntpdate time.aliyun.com
8.安装ipset和ipvsadm
yum install ipset ipvsadm -y cat > /etc/sysconfig/modules/ipvs.modules << EOF modprobe -- ip_vs modprobe -- ip_vs_rr modprobe -- ip_vs_wrr modprobe -- ip_vs_sh modprobe -- nf_conntrack_ipv4 EOF chmod +x /etc/sysconfig/modules/ipvs.modules /bin/bash /etc/sysconfig/modules/ipvs.modules lsmod | grep -e ip_vs -e nf_conntrack_ipv4
三、所有节点安装Docker/kubeadm/kubelet
1.安装Docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo #切换镜像源 yum list docker-ce --showduplicates #查看docker版本列表 yum install docker-ce-20.10.8-3.el7 -y #安装指定版本 systemctl enable docker && systemctl start docker #启动并开机自启 docker --version #查看docker版本
2.配置阿里云容器镜像加速
mkdir /etc/docker
cat > /etc/docker/daemon.json << EOF {
"exec-opts": ["native.cgroupdriver=systemd"], "registry-mirrors": ["https://jukg1w30.mirror.aliyuncs.com"] } EOF systemctl restart docker
3.添加阿里云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
4.安装kubeadm,kubelet和kubectl
yum install -y --nogpgcheck kubelet-1.22.5 kubeadm-1.22.5 kubectl-1.22.5
#编辑/etc/sysconfig/kubelet
KUBELET_CGROUP_ARGS="--cgroup-driver=systemd"
KUBE_PROXY_MODE="ipvs"
#启动
systemctl enable kubelet
systemctl start kubelet
四、部署Kubernetes Master
1. 在master节点执行
kubeadm init --apiserver-advertise-address=192.168.1.221 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.22.5 --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
kubectl get nodes
2.加入Kubernetes Node( 在node节点执行)
kubeadm join 192.168.1.221:6443 --token 2cyong.smy0f3dsnzeq7jly --discovery-token-ca-cert-hash sha256:9333a1bf2eaa80e31e61f49e8996aa787e14e20cf851ba6544322d8972b9fec7
ps: token默认有效期24小时,过期不可用,需要在master重新创建token
kubeadm token create --print-join-command
五、部署CNI网络插件(Master节点执行)
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml kubectl apply -f kube-flannel.yml
六、测试kubernetes集群
kubectl create deployment nginx --image=nginx kubectl expose deployment nginx --port=80 --type=NodePort kubectl get pod,svc #访问地址: http://NodeIP:Port