冲天香阵透长安,满城尽带黄金甲。这篇文章主要讲述kuberneteskubeadm安装多master节点的k8s集群相关的知识,希望能为你提供帮助。
1、概述
K8s主要分为master节点(控制节点)和node节点(运行容器pod),master节点中有apiserver、controller manager、scheduler和etcd几个主要组件,node节点一般有kubelet、kube-proxy、pod、还有网络插件等等。
K8s简单的工作流程:
k8s核心功能:
2、基础配置2.1、服务器配置
server-IP
server-node
os
system
备注
192.168.2.80
VIP
-
-
浮动在master01-03服务器其中之一
192.168.2.81
master01
centos-7.9
8VCPU16G
第一次启动keepalived和nginx没有发生故障转移情况下,VIP在master01上
192.168.2.82
master02
centos-7.9
8VCPU16G
192.168.2.83
master03
centos-7.9
8VCPU16G
192.168.2.84
node01
centos-7.9
8VCPU16G
192.168.2.85
node02
【kuberneteskubeadm安装多master节点的k8s集群】centos-7.9
8VCPU16G
修改所有主机hostname如上表,参考即可,根据自己实际情况处理
hostnamectl set-hostname master01
配置IP地址,参考即可,根据自己实际情况处理
# vim/etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="yes"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="8d2ff878-454e-48ca-8cf4-9ef5f987a3d2"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.2.81"唯一修改的地方
PREFIX="24"
GATEWAY="192.168.2.2"
DNS1="114.114.114.114"
IPV6_PRIVACY="no"
2.2、在master01--免密其他服务器和配置hosts
由于很多操作在master01节点进行操作,方便传输文件,建议master01节点免密其他节点,由于是一些初始化操作,在本次搭建中都是使用的root用户,搭建完成后,在使用过程中建议使用普通用户sudo使用。
[root@master01 ~]# ssh-keygen
[root@master01 ~]# ssh-copy-id192.168.2.82
[root@master01 ~]# ssh-copy-id192.168.2.83
[root@master01 ~]# ssh-copy-id192.168.2.84
[root@master01 ~]# ssh-copy-id192.168.2.85
[root@master01 ~]# vim /etc/hosts
192.168.2.81 master01
192.168.2.82 master02
192.168.2.83 master03
192.168.2.84 node01
192.168.2.85 node02
2.3、所有节点时间同步
# yum install -y ntp ntpdate
# crontab-e
*/5 * * * * ntpdate cn.pool.ntp.org
2.4、所有节点修改内核参数
[root@master01 ~]# modprobe br_netfilter
[root@master01 ~]# echo "modprobe br_netfilter" >
>
/etc/profile
[root@master01 ~]# cat >
/etc/sysctl.d/k8s.conf <
>
net.bridge.bridge-nf-call-ip6tables = 1
>
net.bridge.bridge-nf-call-iptables = 1
>
net.ipv4.ip_forward = 1
>
EOF
[root@master01 ~]# sysctl -p /etc/sysctl.d/k8s.conf
2.5、所有节点关闭防火墙和selinux(记得重启一下)
[root@master01 ~]#systemctl stop firewalld ;
systemctl disable firewalld
[root@master01 ~]# sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config
2.6、所有节点加载docker和kubeletYUM源
配置YUM需要的命令,一般情况下,最小化安装系统是没有这个命令的
[root@master01 ~]# yum -y install yum-utils
配置dockerYUM源
[root@master01 ~]# yum-config-manager --add-repohttp://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
配置kubeadmYUM源
[root@master01 ~]# vi /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
刷新YUM源
[root@master01 ~]# yum clean all
[root@master01 ~]# yum repolist
2.7、开启IPVS
安装rz命令,用于上传配置
[root@master01 modules]# yum install -y lrzsz
[root@master01 ~]# cd /etc/sysconfig/modules/
[root@master01 ~]# chmod 755 /etc/sysconfig/modules/ipvs.modules &
&
bash /etc/sysconfig/modules/ipvs.modules &
&
lsmod | grep ip_vs
3、基础包部署-docker3.1、所有节点-基础依赖包
[root@master01 ~]#yum install -y yum-utils device-mapper-persistent-data lvm2wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel curlcurl-devel unzip sudolibaio-devel wget vim ncurses-devel autoconf automake zlidevel python-devel epel-release openssh-server socat ipvsadm conntracktelnet ipvsadm
3.2、docker部署
[root@master01 ~]# yum install docker-ce docker-ce-cli containerd.io
[root@master01 ~]# systemctl start docker
[root@master01 ~]# systemctl enable docker
3.3、配置docker镜像加速器和驱动
[root@master01 ~]#vim /etc/docker/daemon.json
[root@master01 ~]# systemctl daemon-reload &
&
systemctl restart docker
3.4、部署kubeadm相关软件包
[root@master01 ~]# yum install -y kubelet-1.23.5 kubeadm-1.23.5 kubectl-1.23.5
(特别注意1.24.0之后版本无法docker)
[root@master01 ~]# systemctl enable kubelet &
&
systemctl start kubelet
4、高可用设计keepalived+nginx4.1、架构概述
keeplived主要实现功能高可用和VIP,nginx主要实现api-server负载均衡,所以根据master节点,设置三个节点,并且和kubernetes的master节点部署在同样三台服务器上。
4.2、部署nginx(所有master节点)
[root@master01 ~]# tar -xf nginx-1.16.1.tar.gz
[root@master01 ~]# cd nginx-1.16.1
[root@master01 nginx-1.16.1]# ./configure--prefix=/usr/local/nginx --with-stream
[root@master01 nginx-1.16.1]# make &
&
make install
[root@master01 ~]# cd /usr/local/nginx/conf/
[root@master01 conf]# cp nginx.conf nginx.confbak
[root@master01 conf]# vim nginx.conf(三个master节点一致)
[root@master01 ~]# mkdir /var/log/nginx/&
&
useradd nginx
[root@master01 ~]# /usr/local/nginx/sbin/nginx-t
启动脚本
[root@master01 ~]# vim /etc/init.d/nginx
[root@master01 ~]# chmod +x /etc/init.d/nginx
[root@master01 ~]# /etc/init.d/nginx
4.3、部署keepalived(所有master节点)
[root@master01 ~]# tar -xf keepalived-2.2.7.tar.gz
[root@master01 ~]# cd keepalived-2.2.7
[root@master01 keepalived-2.2.7]# ./configure --prefix=/usr/local/keepalived
[root@master01 keepalived-2.2.7]# make &
&
make install
启动脚本
[root@master01 ~]# vim /etc/init.d/keepalived
前方高能特别注意:VIP地址根据自己实际情况填写
配置文件特别注意:
[root@master01 ~]# vim /usr/local/keepalived/etc/keepalived/keepalived.conf
[root@master02 ~]# vim /usr/local/keepalived/etc/keepalived/keepalived.conf
[root@master03 ~]# vim /usr/local/keepalived/etc/keepalived/keepalived.conf
[root@master01 ~]# /etc/init.d/keepalived
查看VIP情况,一般情况下在master01节点查询即可
[root@master01 ~]# ip addr
5、kubeadm部署kubernetes5.1、在master01初始化
在master01进行初始化操作
[root@master01 ~]# more kubeadm-config.yaml
下图中的红框部分根据实际情况填写,kubernetes版本号可以根据:
[root@master01 ~]# kubectlversion
Client Version: version.InfoMajor:"1", Minor:"23", GitVersion:"v1.23.5", GitCommit:"c285e781331a3785a7f436042c65c5641ce8a9e9", GitTreeState:"clean", BuildDate:"2022-03-16T15:58:47Z", GoVersion:"go1.17.8", Compiler:"gc", Platform:"linux/amd64"
Server Version: version.InfoMajor:"1", Minor:"23", GitVersion:"v1.23.5", GitCommit:"c285e781331a3785a7f436042c65c5641ce8a9e9", GitTreeState:"clean", BuildDate:"2022-03-16T15:52:18Z", GoVersion:"go1.17.8", Compiler:"gc", Platform:"linux/amd64"
[root@master01 ~]# kubeadm init --config kubeadm-config.yaml --ignore-preflight-errors=SystemVerification
如下图表示正确完成初始化:
5.2、添加相应的master节点和node节点
将master节点相关证书拷贝至master02-03
[root@master01 ~]# scp /etc/kubernetes/pki/ca.* 192.168.2.82:/etc/kubernetes/pki/
[root@master01 ~]# scp /etc/kubernetes/pki/sa.* 192.168.2.82:/etc/kubernetes/pki/
[root@master01 ~]# scp /etc/kubernetes/pki/front-proxy-ca.* 192.168.2.82:/etc/kubernetes/pki/
[root@master01 ~]# scp /etc/kubernetes/pki/etcd/ca.* 192.168.2.82:/etc/kubernetes/pki/etcd/
[root@master01 ~]# scp /etc/kubernetes/pki/ca.* 192.168.2.83:/etc/kubernetes/pki/
[root@master01 ~]# scp /etc/kubernetes/pki/sa.* 192.168.2.83:/etc/kubernetes/pki/
[root@master01 ~]# scp /etc/kubernetes/pki/front-proxy-ca.* 192.168.2.83:/etc/kubernetes/pki/
[root@master01 ~]# scp /etc/kubernetes/pki/etcd/ca.* 192.168.2.83:/etc/kubernetes/pki/etcd/
在master02-03节点执行以下命令
[root@master01 ~]# kubeadm join 192.168.2.80:16443 --token eeu9yc.v4uaf0zyrm2qi7qf \\
--discovery-token-ca-cert-hash sha256:683094c96357c5b47adcacf6937465347f1bd6286b7b6ba364ef7e5e4763beef \\
--control-plane
在node01-02节点执行以下命令
[root@master01 ~]# kubeadm join 192.168.2.80:16443 --token eeu9yc.v4uaf0zyrm2qi7qf \\
--discovery-token-ca-cert-hash sha256:683094c96357c5b47adcacf6937465347f1bd6286b7b6ba364ef7e5e4763beef
打上work节点标记
[root@master01 ~]#kubectl label node node01 node-role.kubernetes.io/worker=worker
node/node01 labeled
[root@master01 ~]#kubectl label node node02 node-role.kubernetes.io/worker=worker
node/node02 labeled
[root@master01 ~]# kubectlget nodes
NAMESTATUSROLESAGEVERSION
master01Readycontrol-plane,master3d5hv1.23.5
master02Readycontrol-plane,master3d4hv1.23.5
master03Readycontrol-plane,master3d4hv1.23.5
node01Readyworker3d4hv1.23.5
node02Readyworker3d4hv1.23.5
执行完成如下图:
5.3、添加calico网络插件
缺少网络插件,在master01节点执行如下命令
[root@master01 ~]# kubectlapply -f calico.yaml
执行成功后,如下图:
5.4、检测dns
[root@master01 ~]#kubectl run busybox --image busybox:1.28 --restart=Never --rm-it busybox -- sh
5.5、检查集群可用性---tomcat服务
将tomcat服务镜像上传至node01-02节点
[root@node01 ~]# docker load -i tomcat.tar.gz
[root@node02 ~]# docker load -i tomcat.tar.gz
在master01节点执行
[root@master01 ~]# kubectlapply-f tomcat.yaml
pod/demo-pod created
[root@master01 ~]# kubectlapply -f tomcat-service.yaml
service/tomcat created
在浏览器访问,特别注意:因为所有节点都部署kube-proxy组件,所以所有节点IP地址+端口30080都是可以访问tomcat这个主页的,也包括VIP
推荐阅读
- 卷积神经网络中的CNN是啥()
- MySQL数据库(19)(高级数据操作-查询数据)
- Flannel 同节点通信
- 虚拟化技术KVMkvm虚拟机创建-生成脚本
- 没有JDK和Maven,用Docker也能构建Maven工程
- JavaWeb+MySQL实现课程管理系统
- kudu表数据备份还原说明
- linux-ext4格式文件误删除恢复
- CentOS 备份实战