Kubernetes全栈架构师(Kubeadm高可用安装k8s集群)--学习笔记

目录

  • k8s高可用架构解析
  • Kubeadm基本环境配置
  • Kubeadm系统及内核升级
  • Kubeadm基本组件安装
  • Kubeadm高可用组件安装
  • Kubeadm集群初始化
  • 高可用Master及Token过期处理
  • Kubeadm Node及Calico节点配置
  • Dashboard&Metrics Server安装

k8s高可用架构解析

  • Etcd Cluster:键值数据库,存放k8s的数据,比如我们创建的资源,所做的变更
  • Master:控制节点,控制整个集群
  • Node:主要用来跑pod和容器
  • Kube-APIServer:它是整个k8s的控制大脑,所有的流量都会经过APIServer
  • ControllerManager:集群的控制器
  • Scheduler:集群的调度器,控制pod调度到哪一个node节点
  • Load Balancer:负载均衡,一般使用nginx + keepalived,或者keepalived + haproxy,如果有硬件资源如f5,就不需要Load Balancer,通过虚拟IP连接

Kubeadm基本环境配置

Kubeadm 是官方推荐的安装方式,但是生产环境推荐使用二进制的方式安装

Kubeadm 证书的有效期是一年,因为官方建议运行一年的过程中必须要升级一次

高可用Kubernetes集群规划

主机名 IP地址 说明
k8s-master01 ~ 03 192.168.232.128 ~ 130 master节点 * 3
k8s-master-lb 192.168.232.236 keepalived虚拟IP
k8s-node01 ~ 02 192.168.232.131 ~ 132 worker节点 * 2
配置信息 备注
Pod网段 172.168.0.0/12
Service网段 10.96.0.0/12

VIP(虚拟IP)不要和公司内网IP重复,首先去ping一下,不通才可用。VIP需要和主机在同一个局域网内

[root@localhost ~]# ping 192.168.232.236
PING 192.168.232.236 (192.168.232.236) 56(84) bytes of data.
From 192.168.232.128 icmp_seq=1 Destination Host Unreachable
From 192.168.232.128 icmp_seq=2 Destination Host Unreachable
From 192.168.232.128 icmp_seq=3 Destination Host Unreachable

公有云上搭建VIP是公有云的负载均衡的IP,比如阿里云的内网SLB的地址,腾讯云内网ELB的地址

基本环境配置

  • 环境搭建
  • 静态ip设置
  • 节点配置

环境搭建

不要使用带中文的服务器和克隆的虚拟机

安装虚拟机:https://www.cnblogs.com/mr-xiong/p/12468280.html

下载centos-7镜像:https://zhuanlan.zhihu.com/p/104118123

三台master节点,两台node节点,每台虚拟机分配2核2G,存储使用20G硬盘

安装完成后启动并通过Xshell 7连接五台虚拟机

Xshell 7下载地址:https://downloadly.net/2020/15/4832/03/xmanager/01/?#/4832-netsaran-122140071106.html

使用Xshell 7可以同时发送命令到所有会话,菜单栏--工具--发送键输入到所有会话

通过 VMware 菜单栏编辑,虚拟网络编辑器查看子网地址,192.168.232.0

静态ip设置

打开目录

cd /etc/sysconfig/network-scripts

修改文件 ifcfg-ens33

#修改BOOTPROTO为static
#BOOTPROTO=dhcp
BOOTPROTO=static

#修改ONBOOT为yes
ONBOOT=yes

IPADDR=192.168.232.128
GATEWAY=192.168.232.2
NETMASK=255.255.255.0
DNS1=114.114.114.114
DNS2=8.8.8.8

重启网络服务

service network restart

查看当前IP地址

ip a

根据集群规划分别设置五台虚拟机静态ip,设置完成后使用 Xshell 7 连接到五台虚拟机

Xshell 7 切换tab快捷键:ctrl + tab

节点配置

所有节点配置hosts,修改/etc/hosts如下:

vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.232.128 k8s-master01
192.168.232.129 k8s-master02
192.168.232.130 k8s-master03
192.168.232.236 k8s-master-lb # 如果不是高可用集群,该IP为Master01的IP
192.168.232.131 k8s-node01
192.168.232.132 k8s-node02

CentOS 7安装yum源如下:

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
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
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
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

必备工具安装

yum install wget jq psmisc vim net-tools telnet yum-utils device-mapper-persistent-data lvm2 git -y

所有节点关闭防火墙、selinux、dnsmasq、swap。服务器配置如下:

systemctl disable --now firewalld 
systemctl disable --now dnsmasq
systemctl disable --now NetworkManager

setenforce 0

sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

查看config文件,SELINUX被设为disabled

[root@localhost ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

关闭swap分区

swapoff -a && sysctl -w vm.swappiness=0

注释后重启服务器,swap分区就不会再打开

sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab

安装ntpdate,保证五台服务器时间一致,云服务器不需要

rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
yum install ntpdate -y

所有节点同步时间。时间同步配置如下:

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo 'Asia/Shanghai' >/etc/timezone
ntpdate time2.aliyun.com

# 查看一下时间
date

# 加入到crontab
crontab -e

*/5 * * * * /usr/sbin/ntpdate time2.aliyun.com

所有节点配置limit:

ulimit -SHn 65535

设置limit永久生效

vim /etc/security/limits.conf
# 末尾添加如下内容
* soft nofile 655360
* hard nofile 131072
* soft nproc 655350
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited

取消发送键输入到所有会话

Master01节点免密钥登录其他节点,安装过程中生成配置文件和证书均在Master01上操作,集群管理也在Master01上操作,阿里云或者AWS上需要单独一台kubectl服务器。密钥配置如下:

ssh-keygen -t rsa

# 把文件传送到五个节点
for i in k8s-master01 k8s-master02 k8s-master03 k8s-node01 k8s-node02;do ssh-copy-id -i .ssh/id_rsa.pub $i;done

下载安装所有的源码文件:

cd /root/ ; git clone https://github.com/dotbalo/k8s-ha-install.git

无法下载的可以通过本地拉取压缩再上传到服务器

yum安装zip

yum install -y unzip zip

解压文件

unzip k8s-ha-install.zip

所有节点(发送键输入到所有会话)升级系统并重启,此处升级没有升级内核,下节会单独升级内核:

yum update -y --exclude=kernel* && reboot #CentOS7需要升级,CentOS8可以按需升级系统

推荐centos7,因为CentOS8在2021年停止维护,而centos7到2024年才停止维护

重启完成之后,查看版本(CentOS Linux release 7.9.2009 (Core))

cat /etc/redhat-release

Kubeadm系统及内核升级

查看内核版本

uname -a

内核3.10版本使用docker会有一些bug,需要升级

CentOS7 需要升级内核至4.18+,本地升级的版本为4.19

在master01节点(取消发送键输入到所有会话)下载内核:

cd /root
wget http://193.49.22.109/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm
wget http://193.49.22.109/elrepo/k
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值