1、环境准备

  • master01: 192.168.10.25
  • master02: 192.168.10.26
  • master03: 192.168.10.27
  • node01: 192.168.10.28
  • node02: 192.168.10.29
  • 负载均衡器1:192.168.10.30
  • 负载均衡器2:192.168.10.31
//所有节点,关闭防火墙规则,关闭selinux,关闭swap交换
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
swapoff -a						#交换分区必须要关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab		#永久关闭swap分区,&符号在sed命令中代表上次匹配的结果
#加载 ip_vs 模块
for i in $(ls /usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs|grep -o "^[^.]*");do echo $i; /sbin/modinfo -F filename $i >/dev/null 2>&1 && /sbin/modprobe $i;done
 
 
//修改主机名
hostnamectl set-hostname master01
hostnamectl set-hostname master02
hostnamectl set-hostname master03
hostnamectl set-hostname node01
hostnamectl set-hostname node02
vim /etc/hosts  #所有主机操作   除了两个负载均衡器
192.168.10.25 master01
192.168.10.26 master02
192.168.10.27 master03
192.168.10.28 node01
192.168.10.29 node02
 
 
//调整内核参数
cat > /etc/sysctl.d/kubernetes.conf << EOF
#开启网桥模式,可将网桥的流量传递给iptables链
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
#关闭ipv6协议
net.ipv6.conf.all.disable_ipv6=1
net.ipv4.ip_forward=1
EOF
 
//生效参数
sysctl --system  
 
 
vim /etc/security/limits.conf  内核限制配置
尾部添加
*  soft  nofile  65536
*  hard  nofile  65536
*  soft  nproc   65536
*  hard  nproc   65536
*  soft  memlock  unlimited
*  hard  memlock  unlimited

2、升级内核

#配置elrepo镜像源   所有服务器操作    除了两个负载均衡器
vim /etc/yum.repos.d/elrepo.repo
[elrepo]
name=elrepo
baseurl=https://mirrors.aliyun.com/elrepo/archive/kernel/el7/x86_64
gpgcheck=0
enabled=1
 
cd /etc/yum.repos.d/
mv local.repo repo.bak/
上传阿里云在线云
 
yum clean all
 
#集群升级内核
yum install -y kernel-lt kernel-lt-devel
 
#设置默认启动内核
grub2-set-default 0
 
#重启操作系统
reboot
 
#查看生效版本
hostnamectl

 

3、所有节点安装docker

//所有 node 节点部署docker引擎
yum install -y yum-utils device-mapper-persistent-data lvm2 
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 
yum install -y docker-ce docker-ce-cli containerd.io
 
mkdir /etc/docker
cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": ["https://6ijb8ubo.mirror.aliyuncs.com"],
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "500m", "max-file": "3"
  }
}
EOF
 
systemctl start docker.service
systemctl enable docker.service 
 
docker info | grep "Cgroup Driver"
Cgroup Driver: systemd

4、安装kubeadm,kubelet和kubectl

所有服务器操作   除了两个负载均衡器
//定义kubernetes源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
 
yum install -y kubelet-1.20.15 kubeadm-1.20.15 kubectl-1.20.15
 
//开机自启kubelet
systemctl enable kubelet.service
#K8S通过kubeadm安装出来以后都是以Pod方式存在,即底层是以容器方式运行,所以kubelet必须设置开机自启
 

5、高可用配置

部署nginx做负载均衡器

192.168.10.30、192.168.10.31两台服务器做负载均衡
初始化操作
systemctl disable --now firewalld
setenforce 0
 
两台服务器安装nginx
cd /etc/yum.repos.d/
上传nginx.repo包
yum install -y nginx

192.168.10.30服务器
cd /etc/nginx/
vim nginx.conf 
#10行添加     use epoll;
#14行开始添加
stream {
      upstream k8s-apiservers {
         server 192.168.10.25:6443;
         server 192.168.10.26:6443;
         server 192.168.10.27:6443;
      }
      server {
         listen 6443;
         proxy_pass k8s-apiservers;
      }
  }
 
 
nginx -t
systemctl enable --now nginx
netstat -lntp | grep nginx
 
scp nginx.conf 192.168.10.15:`pwd`

部署keepalived做高可用

192.168.10.30、192.168.10.31两台服务器做高可用
yum install -y keepalived
 
192.168.10.14服务器
cd /etc/keepalived/
cp keepalived.conf keepalived.conf.bak
touch nginx_check.sh
vim nginx_check.sh
#!/bin/bash
 
if ! killall -0 nginx &> /dev/null
    then
    systemctl stop keepalived
fi
 
chmod +x nginx_check.sh

 

vim keepalived.conf
#10行修改
smtp_server 127.0.0.1
#12行修改
router_id NGINX_01
删除原本13-16行
 
#15行添加
vrrp_script nginx_check {
     script "/etc/keepalived/nginx_check.sh"
     weight 2
     interval 2
}
 
#23行修改为本机网卡
interface ens33
#32行修改为指定vip         
192.168.9.100
#34行添加
track_script {
         nginx_check
}
 
 
scp keepalived.conf nginx_check.sh 192.168.10.31:`pwd`

192.168.10.31服务器
cd /etc/keepalived/
vim keepalived.conf
#12行修改
router_id NGINX_02
#22行修改
state BACKUP
#25行修改
priority 90
 
两台keepalived服务器开启服务
systemctl enable --now keepalived.service

6、部署K8S集群

master01节点操作
 
cd /opt
mkdir k8s/
kubeadm config print init-defaults > /opt/kubeadm-config.yaml
 
vim kubeadm-config.yaml
#12行修改为本机地址
advertiseAddress: 192.168.10.25
#22行添加
certSANS:
   - 192.168.10.25
   - 192.168.10.26
   - 192.168.10.27
   - 192.168.10.100
 
#31行添加
controlPlaneEndpoint: "192.168.10.100:6443"
 
#38行
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers
 
#40行
kubernetesVersion: v1.20.15
 
#43行修改   
serviceSubnet: 10.96.0.0/16
 
#44行添加   
podSubnet: "10.244.0.0/16"
 
 
#46行添加
 ---
 apiVersion: kubeproxy.config.k8s.io/v1alpha1
 kind: KubeProxyConfiguration
 mode: ipvs

在线拉取镜像
kubeadm config images pull --config kubeadm-config.yaml

master02、master03节点
cd /opt
mkdir k8s
 
#将master01节点文件进行远程复制
scp kubeadm-config.yaml master02:/opt/k8s/
scp kubeadm-config.yaml master03:/opt/k8s/
 
master02、master03节点
cd k8s
kubeadm config images pull --config kubeadm-config.yaml

master01节点
 
//初始化 master
kubeadm init --config=/opt/kubeadm-config.yaml --upload-certs | tee kubeadm-init.log
#--upload-certs 参数可以在后续执行加入节点时自动分发证书文件
#tee kubeadm-init.log 用以输出日志
 
  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

将其他master加入集群
kubeadm join 192.168.10.100:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:7ec5e3df3ac1d983d9c33ce65c6d08507062b79cf9fde66be32d17f494e3ad7e \
    --control-plane --certificate-key 2b288421f07de419098e2b23325b8d8c40c0ded7029cf49d32cc2943b6df3e97

#将node节点加入集群
kubeadm join 192.168.10.100:6443 --token abcdef.0123456789abcdef \
    --discovery-token-ca-cert-hash sha256:7ec5e3df3ac1d983d9c33ce65c6d08507062b79cf9fde66be32d17f494e3ad7e 

 

 

 

7、所有节点部署网络插件flannel

master01节点
cd /opt/k8s/
上传flannel-v0.21.5.zip压缩包
unzip flannel-v0.21.5.zip
docker load -i flannel.tar
docker load -i flannel-cni-plugin.tar 
 
两个node节点
cd /opt
mkdir k8s
 
master01节点
scp cni-plugins-linux-amd64-v1.3.0.tgz flannel-cni-plugin.tar flannel.tar master02:/opt/k8s/
scp cni-plugins-linux-amd64-v1.3.0.tgz flannel-cni-plugin.tar flannel.tar master03:/opt/k8s/
scp cni-plugins-linux-amd64-v1.3.0.tgz flannel-cni-plugin.tar flannel.tar node01:/opt/k8s/
scp cni-plugins-linux-amd64-v1.3.0.tgz flannel-cni-plugin.tar flannel.tar node02:/opt/k8s/
 

在其余所有节点进入/opt/k8s目录执行
docker load -i flannel.tar
docker load -i flannel-cni-plugin.tar 
所有服务器操作
cd /opt
mv cni/ cni_bak
mkdir -p /opt/cni/bin
cd /opt/k8s/
tar xf cni-plugins-linux-amd64-v1.3.0.tgz -C /opt/cni/bin/
ls !$
 
 
master01节点操作
kubectl apply -f kube-flannel.yml   #创建资源
kubectl get nodes

注:如何解决kubeadm部署k8s集群中状态显示Unhealthy?

cd /etc/kubernetes/manifests
vim kube-controller-manager.yaml
vim kube-scheduler.yaml
将三个master节点中两个文件中的 #- --port=0注释重启即可
 
systemctl restart kubelet.service
kubectl get cs

8、部署 Dashboard可视化面板

//在 master01 节点上操作
#上传 recommended.yaml 文件到 /opt/k8s 目录中
cd /opt/k8s
kubectl apply -f recommended.yaml
 
 
#创建service account并绑定默认cluster-admin管理员集群角色
kubectl create serviceaccount dashboard-admin -n kube-system
kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin
kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')   #查看用户使用的令牌,需复制,后面要用

kubectl get pods -A -o wide  #master01节点上使用,即可查看到dashboard运行在那个节点上进而获取ip
#使用输出的token登录Dashboard
https://192.168.10.28:30001

如何解决kubeadm部署k8s集群中状态显示Unhealthy?

cd /etc/kubernetes/manifests
vim kube-controller-manager.yaml
vim kube-scheduler.yaml
将三个master节点中两个文件中的 #- --port=0注释重启即可
 
systemctl restart kubelet.service
kubectl get cs

9、更新k8s证书有效期

#查看证书有效期
kubeadm alpha certs check-expiration
openssl x509 -noout -dates -in /etc/kubernetes/pki/apiserver.crt

方法一:通过脚本升级证书有效期

master01节点
cd /opt/k8s/
上传脚本update-kubeadm-cert.sh
chmod +x update-kubeadm-cert.sh
./update-kubeadm-cert.sh all

方法二:刷新并重启证书

kubeadm alpha certs renew all --config kubeadm.yaml         #重新刷新证书一年有效期
kubeadm init phase kubeconfig all --config kubeadm.yaml      #更新kubeconfig配置文件
 
随后重启证书
systemctl restart kubelet     二进制安装k8s重启方法
 
kubeadm安装k8s的重启方法
mv /etc/kubernetes/manifests/*.yaml  /tmp/
mv /tmp/*.yaml /etc/kubernetes/manifests/
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐