电脑装配网

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

 人阅读 | 作者lilintao | 时间:2023-09-17 14:28

目录

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

k8s高可用架构解析

静态ip设置

打开目录https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cd /etc/sysconfig/network-scripts

修改文件 ifcfg-ens33https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> #修改BOOTPROTO为static#BOOTPROTO=dhcpBOOTPROTO=static#修改ONBOOT为yesONBOOT=yesIPADDR=192.168.232.128GATEWAY=192.168.232.2NETMASK=255.255.255.0DNS1=114.114.114.114DNS2=8.8.8.8

重启网络服务https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> service network restart

查看当前IP地址https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ip a

根据集群规划分别设置五台虚拟机静态ip,设置完成后使用 Xshell 7 连接到五台虚拟机https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

Xshell 7 切换tab快捷键:ctrl + tabhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

节点配置

所有节点配置hosts,修改/etc/hosts如下:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> vim /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.232.128 k8s-master01192.168.232.129 k8s-master02192.168.232.130 k8s-master03192.168.232.236 k8s-master-lb # 如果不是高可用集群,该IP为Master01的IP192.168.232.131 k8s-node01192.168.232.132 k8s-node02

CentOS 7安装yum源如下:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repoyum install -y yum-utils device-mapper-persistent-data lvm2yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repocat <<EOF > /etc/yum.repos.d/kubernetes.repo[kubernetes]name=Kubernetesbaseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/enabled=1gpgcheck=1repo_gpgcheck=1gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpgEOFsed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo

必备工具安装https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> yum install wget jq psmisc vim net-tools telnet yum-utils device-mapper-persistent-data lvm2 git -y

所有节点关闭防火墙、selinux、dnsmasq、swap。服务器配置如下:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> systemctl disable --now firewalld systemctl disable --now dnsmasqsystemctl disable --now NetworkManagersetenforce 0sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinuxsed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

查看config文件,SELINUX被设为disabledhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> [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分区https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> swapoff -a && sysctl -w vm.swappiness=0

注释后重启服务器,swap分区就不会再打开https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab

安装ntpdate,保证五台服务器时间一致,云服务器不需要https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpmyum install ntpdate -y

所有节点同步时间。时间同步配置如下:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtimeecho 'Asia/Shanghai' >/etc/timezonentpdate time2.aliyun.com# 查看一下时间date# 加入到crontabcrontab -e*/5 * * * * /usr/sbin/ntpdate time2.aliyun.com

所有节点配置limit:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ulimit -SHn 65535

设置limit永久生效https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> vim /etc/security/limits.conf# 末尾添加如下内容* soft nofile 655360* hard nofile 131072* soft nproc 655350* hard nproc 655350* soft memlock unlimited* hard memlock unlimited

取消发送键输入到所有会话https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

Master01节点免密钥登录其他节点,安装过程中生成配置文件和证书均在Master01上操作,集群管理也在Master01上操作,阿里云或者AWS上需要单独一台kubectl服务器。密钥配置如下:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> 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

下载安装所有的源码文件:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cd /root/ ; git clone https://github.com/dotbalo/k8s-ha-install.git

无法下载的可以通过本地拉取压缩再上传到服务器https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

yum安装ziphttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> yum install -y unzip zip

解压文件https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> unzip k8s-ha-install.zip

所有节点(发送键输入到所有会话)升级系统并重启,此处升级没有升级内核,下节会单独升级内核:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> yum update -y --exclude=kernel* && reboot #CentOS7需要升级,CentOS8可以按需升级系统

推荐centos7,因为CentOS8在2021年停止维护,而centos7到2024年才停止维护https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

重启完成之后,查看版本(CentOS Linux release 7.9.2009 (Core))https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cat /etc/redhat-release

Kubeadm系统及内核升级

查看内核版本https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> uname -a

内核3.10版本使用docker会有一些bug,需要升级https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

CentOS7 需要升级内核至4.18+,本地升级的版本为4.19https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

在master01节点(取消发送键输入到所有会话)下载内核:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cd /rootwget http://193.49.22.109/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpmwget http://193.49.22.109/elrepo/kernel/el7/x86_64/RPMS/kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm

从master01节点传到其他节点:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> for i in k8s-master02 k8s-master03 k8s-node01 k8s-node02;do scp kernel-ml-4.19.12-1.el7.elrepo.x86_64.rpm kernel-ml-devel-4.19.12-1.el7.elrepo.x86_64.rpm $i:/root/ ; done

所有节点(发送键输入到所有会话)安装内核https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cd /root && yum localinstall -y kernel-ml*

所有节点更改内核启动顺序,因为默认是3.10的https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> grub2-set-default 0 && grub2-mkconfig -o /etc/grub2.cfggrubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"

检查默认内核是不是4.19https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> grubby --default-kernel

所有节点重启,然后检查内核是不是4.19https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> rebootuname -a

所有节点安装ipvsadm:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> yum install ipvsadm ipset sysstat conntrack libseccomp -y

所有节点配置ipvs模块,在内核4.19+版本nf_conntrack_ipv4已经改为nf_conntrack, 4.18以下使用nf_conntrack_ipv4即可:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> vim /etc/modules-load.d/ipvs.conf# 加入以下内容ip_vsip_vs_lcip_vs_wlcip_vs_rrip_vs_wrrip_vs_lblcip_vs_lblcrip_vs_dhip_vs_ship_vs_foip_vs_nqip_vs_sedip_vs_ftpip_vs_shnf_conntrackip_tablesip_setxt_setipt_setipt_rpfilteript_REJECTipip

加载配置https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> systemctl enable --now systemd-modules-load.service

开启一些k8s集群中必须的内核参数,所有节点配置k8s内核:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cat <<EOF > /etc/sysctl.d/k8s.confnet.ipv4.ip_forward = 1net.bridge.bridge-nf-call-iptables = 1net.bridge.bridge-nf-call-ip6tables = 1fs.may_detach_mounts = 1vm.overcommit_memory=1vm.panic_on_oom=0fs.inotify.max_user_watches=89100fs.file-max=52706963fs.nr_open=52706963net.netfilter.nf_conntrack_max=2310720net.ipv4.tcp_keepalive_time = 600net.ipv4.tcp_keepalive_probes = 3net.ipv4.tcp_keepalive_intvl =15net.ipv4.tcp_max_tw_buckets = 36000net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_max_orphans = 327680net.ipv4.tcp_orphan_retries = 3net.ipv4.tcp_syncookies = 1net.ipv4.tcp_max_syn_backlog = 16384net.ipv4.ip_conntrack_max = 65536net.ipv4.tcp_max_syn_backlog = 16384net.ipv4.tcp_timestamps = 0net.core.somaxconn = 16384EOFsysctl --system

所有节点配置完内核后,重启服务器,保证重启后内核依旧加载https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> reboot

检查是否加载https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> lsmod | grep --color=auto -e ip_vs -e nf_conntrack

Kubeadm基本组件安装

所有节点安装Docker-ce 19.03,不需要太新,这是官方已经经过验证的版本https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> yum install docker-ce-19.03.* docker-cli-19.03.* -y

由于新版kubelet建议使用systemd,所以可以把docker的CgroupDriver改成systemdhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> mkdir /etc/dockercat > /etc/docker/daemon.json <<EOF{ "exec-opts": ["native.cgroupdriver=systemd"]}EOF

所有节点设置开机自启动Docker:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> systemctl daemon-reload && systemctl enable --now docker

查看docker版本(Server Version: 19.03.15,Cgroup Driver: systemd)https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> docker info

查看k8s最新版本(取消发送键输入到所有会话):https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> yum list kubeadm.x86_64 --showduplicates | sort -r

最新版本是1.21.2-0,但是推荐小版本大于5才使用,所以安装1.20版本https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

所有节点(发送键输入到所有会话)安装最新版本kubeadm:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> yum install kubeadm-1.20* kubelet-1.20* kubectl-1.20* -y

默认配置的pause镜像使用gcr.io仓库,国内可能无法访问,所以这里配置Kubelet使用阿里云的pause镜像:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cat >/etc/sysconfig/kubelet<<EOFKUBELET_EXTRA_ARGS="--cgroup-driver=systemd --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause-amd64:3.2"EOF

设置Kubelet开机自启动:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> systemctl daemon-reloadsystemctl enable --now kubelet

Kubeadm高可用组件安装

如果不是高可用集群,haproxy和keepalived无需安装https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

公有云要用公有云自带的负载均衡,比如阿里云的SLB,腾讯云的ELB,用来替代haproxy和keepalived,因为公有云大部分都是不支持keepalived的https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

如果用阿里云的话,kubectl控制端不能放在master节点,因为阿里云的slb有回环的问题,也就是slb代理的服务器不能反向访问SLB,推荐使用腾讯云,腾讯云修复了这个问题。https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

所有Master节点(node节点取消发送键输入到所有会话)通过yum安装HAProxy和KeepAlived:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> yum install keepalived haproxy -y

所有Master节点配置HAProxy(详细配置参考HAProxy文档,所有Master节点的HAProxy配置相同):https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> vim /etc/haproxy/haproxy.cfg

删除所有内容https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ggdG

添加以下内容,注意首行global是否复制完整https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> global maxconn 2000 ulimit-n 16384 log 127.0.0.1 local0 err stats timeout 30sdefaults log global mode http option httplog timeout connect 5000 timeout client 50000 timeout server 50000 timeout http-request 15s timeout http-keep-alive 15sfrontend monitor-in bind *:33305 mode http option httplog monitor-uri /monitorfrontend k8s-master bind 0.0.0.0:16443 bind 127.0.0.1:16443 mode tcp option tcplog tcp-request inspect-delay 5s default_backend k8s-masterbackend k8s-master mode tcp option tcplog option tcp-check balance roundrobin default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100 server k8s-master01192.168.232.128:6443 check server k8s-master02192.168.232.129:6443 check server k8s-master03192.168.232.130:6443 check

所有Master节点配置KeepAlived,配置不一样,注意每个节点的IP和网卡(interface参数)https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

查看网卡名称(ens33)https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ip a

如果网卡名称不是ens33,不要把配置文件中的网卡配置替换https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

Master01节点的配置:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> mkdir /etc/keepalivedvim /etc/keepalived/keepalived.conf

删除所有内容https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ggdG

取消发送键输入到所有会话https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

添加以下内容,注意首行是否复制完整https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ! Configuration File for keepalivedglobal_defs { router_id LVS_DEVELscript_user root enable_script_security}vrrp_script chk_apiserver { script "/etc/keepalived/check_apiserver.sh" interval 5 weight -5 fall 2 rise 1}vrrp_instance VI_1 { state MASTER interface ens33 mcast_src_ip 192.168.232.128 virtual_router_id 51 priority 101 advert_int 2 authentication { auth_type PASS auth_pass K8SHA_KA_AUTH } virtual_ipaddress { 192.168.232.236 } track_script { chk_apiserver }}

Master02节点的配置:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

添加以下内容,注意首行是否复制完整https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ! Configuration File for keepalivedglobal_defs { router_id LVS_DEVELscript_user root enable_script_security}vrrp_script chk_apiserver { script "/etc/keepalived/check_apiserver.sh" interval 5 weight -5 fall 2 rise 1}vrrp_instance VI_1 { state BACKUP interface ens33 mcast_src_ip 192.168.232.129 virtual_router_id 51 priority 100 advert_int 2 authentication { auth_type PASS auth_pass K8SHA_KA_AUTH } virtual_ipaddress { 192.168.232.236 } track_script { chk_apiserver }}

Master03节点的配置:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

添加以下内容,注意首行是否复制完整https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ! Configuration File for keepalivedglobal_defs { router_id LVS_DEVELscript_user root enable_script_security}vrrp_script chk_apiserver { script "/etc/keepalived/check_apiserver.sh" interval 5 weight -5 fall 2 rise 1}vrrp_instance VI_1 { state BACKUP interface ens33 mcast_src_ip 192.168.232.130 virtual_router_id 51 priority 100 advert_int 2 authentication { auth_type PASS auth_pass K8SHA_KA_AUTH } virtual_ipaddress { 192.168.232.236 } track_script { chk_apiserver }}

所有master节点(发送键输入到所有会话,取消node节点)配置KeepAlived健康检查文件:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> vim /etc/keepalived/check_apiserver.sh

添加以下内容,注意首行是否复制完整https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> #!/bin/basherr=0for k in $(seq 1 3)do check_code=$(pgrep haproxy) if [[ $check_code == "" ]]; then err=$(expr $err + 1) sleep 1 continue else err=0 break fidoneif [[ $err != "0" ]]; then echo "systemctl stop keepalived" /usr/bin/systemctl stop keepalived exit 1else exit 0fi

我们通过KeepAlived虚拟出来一个VIP,VIP会配置到一个master节点上面,它会通过haproxy暴露的16443的端口反向代理到我们的三个master节点上面,所以我们可以通过VIP的地址加上16443访问到我们的API serverhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

健康检查会检查haproxy的状态,三次失败就会将KeepAlived停掉,停掉之后KeepAlived会跳到其他的节点https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

添加权限https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> chmod +x /etc/keepalived/check_apiserver.sh

启动haproxyhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> systemctl daemon-reloadsystemctl enable --now haproxy

查看端口(16443)https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> netstat -lntp

启动keepalivedhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> systemctl enable --now keepalived

查看系统日志(Sending gratuitous ARP on ens33 for 192.168.232.236)https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> tail -f /var/log/messagescat /var/log/messages | grep 'ens33' -5

查看iphttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ip a

可以看到192.168.232.236绑定到了master01,其他两个节点是没有的https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

测试VIPhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ping 192.168.232.236 -c 4telnet 192.168.232.236 16443

如果ping不通且telnet没有出现 ] ,则认为VIP不可以,不可在继续往下执行,需要排查keepalived的问题https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> 比如防火墙和selinux,haproxy和keepalived的状态,监听端口等所有节点查看防火墙状态必须为disable和inactive:systemctl status firewalld所有节点查看selinux状态,必须为disable:getenforcemaster节点查看haproxy和keepalived状态:systemctl status keepalived haproxymaster节点查看监听端口:netstat -lntp

Kubeadm集群初始化

官方初始化文档:https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/high-availability/https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

在生产环境中有些配置需要修改,因为使用默认的配置可能会导致网段冲突,所以我们使用配置文件的形式初始化https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

发送键输入到所有会话https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

Master01节点创建 kubeadm-config.yaml 配置文件如下:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> vim kubeadm-config.yaml

Master01:(# 注意,如果不是高可用集群,192.168.232.236:16443改为master01的地址,16443改为apiserver的端口,默认是6443,注意更改v1.18.5自己服务器kubeadm的版本:kubeadm version)https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

以下文件内容,宿主机网段、podSubnet网段、serviceSubnet网段不能重复,具体看前面的高可用Kubernetes集群规划https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> apiVersion: kubeadm.k8s.io/v1beta2bootstrapTokens:- groups: - system:bootstrappers:kubeadm:default-node-token token: 7t2weq.bjbawausm0jaxury ttl: 24h0m0s usages: - signing - authenticationkind: InitConfigurationlocalAPIEndpoint: advertiseAddress: 192.168.232.128 bindPort: 6443nodeRegistration: criSocket: /var/run/dockershim.sock name: k8s-master01 taints: - effect: NoSchedule key: node-role.kubernetes.io/master---apiServer: certSANs: - 192.168.232.236 timeoutForControlPlane: 4m0sapiVersion: kubeadm.k8s.io/v1beta2certificatesDir: /etc/kubernetes/pkiclusterName: kubernetescontrolPlaneEndpoint: 192.168.232.236:16443controllerManager: {}dns: type: CoreDNSetcd: local: dataDir: /var/lib/etcdimageRepository: registry.cn-hangzhou.aliyuncs.com/google_containerskind: ClusterConfigurationkubernetesVersion: v1.20.0networking: dnsDomain: cluster.local podSubnet: 172.168.0.0/12 serviceSubnet: 10.96.0.0/12scheduler: {}

更新kubeadm文件https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm config migrate --old-config kubeadm-config.yaml --new-config new.yaml

查看kubeadm版本(GitVersion:"v1.20.8")https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm version

将配置文件中的 kubernetesVersion: v1.20.0 改为一致的 kubernetesVersion: v1.20.8https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

node节点取消发送键输入到所有会话https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

将new.yaml文件复制到其他master节点,之后所有Master节点提前下载镜像,可以节省初始化时间:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm config images pull --config /root/new.yaml

因为配置了阿里云镜像(imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers),所以下载速度比默认使用的gcr镜像快,国内访问不了gcr镜像https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

因为配置了token过期时间(ttl: 24h0m0s),所以可能出现今天生成token,明天加入不了集群的问题https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

同时master节点为我们配置了一个污点(taints),这个污点可以让我们的mater不部署容器https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

criSocket就是通过哪一个socket连接我们的docker,dockershim在k8s 1.20版本废弃,官方不维护,后期可能有人会维护,也可以改成其他cri的runtimehttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

Master01节点(取消发送键输入到所有会话)初始化,初始化以后会在/etc/kubernetes目录下生成对应的证书和配置文件,之后其他Master节点加入Master01即可:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm init --config /root/new.yaml --upload-certs

kubeadm 的配置管理是通过 pod 管理的,所有的组件都是通过容器启动的,通过 /etc/kubernetes/manifests 目录下面的 yaml 文件启动,这就是 kubelet 生命周期管理的目录,在这里面配置一个 pod 的 yaml 文件,它就会为你管理 pod 的生命周期https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

进入到该目录中https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cd /etc/kubernetes/manifests

可以看到以下文件https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> etcd.yaml kube-apiserver.yaml kube-controller-manager.yaml kube-scheduler.yaml

kubeadm 与二进制安装不一样的地方在于它的配置管理都在 yaml 文件中,可以编辑文件查看,二进制是一个单独的server文件,如果更改了配置,千万不要手动让它生效,kubelet 会自动帮我们加载配置,重启容器https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

如果初始化失败,重置后再次初始化,命令如下:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm reset -f ; ipvsadm --clear ; rm -rf ~/.kube

初始化成功以后,会产生Token值,用于其他节点加入时使用,因此要记录下初始化成功生成的token值(令牌值):https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> Your Kubernetes control-plane has initialized successfully!To start using your cluster, you need to run the following as a regular user: mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/configAlternatively, if you are the root user, you can run: export KUBECONFIG=/etc/kubernetes/admin.confYou should now deploy a pod network to the cluster.Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at: https://kubernetes.io/docs/concepts/cluster-administration/addons/You can now join any number of the control-plane node running the following command on each as root: kubeadm join 192.168.232.236:16443 --token 7t2weq.bjbawausm0jaxury \ --discovery-token-ca-cert-hash sha256:3b304187585d91d18d33d171592d4d37ec237d68af4837efebbb5c9ec86072d8 \ --control-plane --certificate-key 7fc2e6005c32476d189b5b4763e2131404df187f7601b01d33370c7485ed2c53Please note that the certificate-key gives access to cluster sensitive data, keep it secret!As a safeguard, uploaded-certs will be deleted in two hours; If necessary, you can use"kubeadm init phase upload-certs --upload-certs" to reload certs afterward.Then you can join any number of worker nodes by running the following on each as root:kubeadm join 192.168.232.236:16443 --token 7t2weq.bjbawausm0jaxury \ --discovery-token-ca-cert-hash sha256:3b304187585d91d18d33d171592d4d37ec237d68af4837efebbb5c9ec86072d8

Master01节点配置环境变量,用于访问Kubernetes集群:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cat <<EOF >> /root/.bashrcexport KUBECONFIG=/etc/kubernetes/admin.confEOFsource /root/.bashrc

管理集群的命令 kubectl 只需要在一个节点上面有就可以,这个节点可以是 k8s 节点,也可以不是,它就是通过 admin.conf 文件和 k8s 通讯的,文件中定义了一个变量 KUBECONFIG,指定了文件的地址,然后我们就可以操作我们的集群了https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

查看节点状态:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get nodes

可以看到它添加了一个规则 control-planehttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> NAME STATUS ROLES AGE VERSIONk8s-master01 NotReady control-plane,master 3m54s v1.20.8

查看server:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get svc

可以看到以下的serverhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5m32s

采用初始化安装方式,所有的系统组件均以容器的方式运行并且在kube-system命名空间内,生产环境建议创建一个namespaceshttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

此时可以查看Pod状态:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get pods -n kube-system -o wide

可以看到以下的podhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATEScoredns-54d67798b7-lrvm8 0/1 Pending 0 6m40s <none> <none> <none> <none>coredns-54d67798b7-tkfrx 0/1 Pending 0 6m40s <none> <none> <none> <none>etcd-k8s-master01 1/1 Running 0 6m34s 192.168.232.128 k8s-master01 <none> <none>kube-apiserver-k8s-master01 1/1 Running 0 6m34s 192.168.232.128 k8s-master01 <none> <none>kube-controller-manager-k8s-master01 1/1 Running 0 6m34s 192.168.232.128 k8s-master01 <none> <none>kube-proxy-4gwlb 1/1 Running 0 6m40s 192.168.232.128 k8s-master01 <none> <none>kube-scheduler-k8s-master01 1/1 Running 0 6m34s 192.168.232.128 k8s-master01 <none> <none>

高可用Master及Token过期处理

注意:以下步骤是上述init命令产生的Token过期了才需要执行以下步骤,如果没有过期不需要执行https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

Token过期后生成新的token:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm token create --print-join-command

Master需要生成--certificate-keyhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm init phase upload-certs --upload-certs

Token没有过期直接执行Joinhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

初始化master02加入集群https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm join 192.168.232.236:16443 --token 7t2weq.bjbawausm0jaxury \--discovery-token-ca-cert-hash sha256:3b304187585d91d18d33d171592d4d37ec237d68af4837efebbb5c9ec86072d8 \--control-plane --certificate-key 7fc2e6005c32476d189b5b4763e2131404df187f7601b01d33370c7485ed2c53

在master01查看其他节点https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get node

可以看到master02节点https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> NAME STATUS ROLES AGE VERSIONk8s-master01 NotReady control-plane,master 25m v1.20.8k8s-master02 NotReady control-plane,master 91s v1.20.8

尝试重新生成tokenhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm token create --print-join-commandkubeadm init phase upload-certs --upload-certs

替换参数,初始化master03加入集群https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm join 192.168.232.236:16443 --token rff9me.bhgzm7d3j2uoq5fv --discovery-token-ca-cert-hash sha256:3b304187585d91d18d33d171592d4d37ec237d68af4837efebbb5c9ec86072d8 \--control-plane --certificate-key d3fb9602f75f4f879d4eea083129bd110734824ef8721c956be95ae481022992

可以在master01查看新生成的tokenhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get secret -n kube-system

这就是新生成的tokenhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> NAME TYPE DATA AGEbootstrap-token-rff9me bootstrap.kubernetes.io/token 6 3m31s

查看token内容:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get secret -n kube-system bootstrap-token-rff9me -oyaml

可以看到过期时间(这是通过base64加密的):https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> expiration: MjAyMS0wNy0wOFQxNzo0MjoyMiswODowMA==

解密一下:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> echo "MjAyMS0wNy0wOFQxNzo0MjoyMiswODowMA==" | base64 -d

可以看到解密后的时间https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> 2021-07-08T22:42:22+08:00

Kubeadm Node及Calico节点配置

Kubeadm Node

Node节点上主要部署公司的一些业务应用,生产环境中不建议Master节点部署系统组件之外的其他Pod,测试环境可以允许Master节点部署Pod以节省系统资源。https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

初始化node01,node02加入集群(与master相比,不需要control-plane)https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubeadm join 192.168.232.236:16443 --token 7t2weq.bjbawausm0jaxury \ --discovery-token-ca-cert-hash sha256:3b304187585d91d18d33d171592d4d37ec237d68af4837efebbb5c9ec86072d8

所有节点初始化完成后,查看集群状态https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get node

可以看到所有节点https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> NAME STATUS ROLES AGE VERSIONk8s-master01 NotReady control-plane,master 39m v1.20.8k8s-master02 NotReady control-plane,master 16m v1.20.8k8s-master03 NotReady control-plane,master 9m38s v1.20.8k8s-node01 NotReady <none> 22s v1.20.8k8s-node02 NotReady <none> 11s v1.20.8

Calico节点配置

以下步骤只在master01执行https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cd /root/k8s-ha-install && git checkout manual-installation-v1.20.x && cd calico/

如果是本地下载上传的话需要现在本地切换分支再上传https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> unzip k8s-ha-install.zipcd k8s-ha-install/calico/

修改calico-etcd.yaml的以下位置:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

修改etcd的节点https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> sed -i 's#etcd_endpoints: "http://<ETCD_IP>:<ETCD_PORT>"#etcd_endpoints: "https://192.168.232.128:2379,https://192.168.232.129:2379,https://192.168.232.130:2379"#g' calico-etcd.yaml

使用默认配置https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> ETCD_CA=`cat /etc/kubernetes/pki/etcd/ca.crt | base64 | tr -d '\n'`ETCD_CERT=`cat /etc/kubernetes/pki/etcd/server.crt | base64 | tr -d '\n'`ETCD_KEY=`cat /etc/kubernetes/pki/etcd/server.key | base64 | tr -d '\n'`sed -i "s@# etcd-key: null@etcd-key: ${ETCD_KEY}@g; s@# etcd-cert: null@etcd-cert: ${ETCD_CERT}@g; s@# etcd-ca: null@etcd-ca: ${ETCD_CA}@g" calico-etcd.yaml

把 etcd_key 放到 secret 里面,secret 会挂载到 calico 容器的 pod 里面,挂载的名称就是 ETCD_CA,这样 calico 就能找到证书,就可以连接到 etcd,就可以把 pod 信息存储到 etcd 里面https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> sed -i 's#etcd_ca: ""#etcd_ca: "/calico-secrets/etcd-ca"#g; s#etcd_cert: ""#etcd_cert: "/calico-secrets/etcd-cert"#g; s#etcd_key: "" #etcd_key: "/calico-secrets/etcd-key" #g' calico-etcd.yaml

修改 pod 网段https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> POD_SUBNET=`cat /etc/kubernetes/manifests/kube-controller-manager.yaml | grep cluster-cidr= | awk -F= '{print $NF}'`

注意下面的这个步骤是把calico-etcd.yaml文件里面的CALICO_IPV4POOL_CIDR下的网段改成自己的Pod网段,也就是把192.168.x.x/16改成自己的集群网段,并打开注释,所以更改的时候请确保这个步骤的这个网段没有被统一替换掉,如果被替换掉了,还请改回来:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> sed -i 's@# - name: CALICO_IPV4POOL_CIDR@- name: CALICO_IPV4POOL_CIDR@g; s@# value: "192.168.0.0/16"@ value: '"${POD_SUBNET}"'@g' calico-etcd.yaml

检查文件:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> vim calico-etcd.yaml

可以看到 etcd-key 已经导入进来,它就是把证书 /etc/kubernetes/pki/etcd/ca.crt 读取出来,再经过 base64 加密,再填到这个位置https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

安装 calicohttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl apply -f calico-etcd.yaml

查看容器状态https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get po -n kube-system

成功运行https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> NAME READY STATUS RESTARTS AGEcalico-kube-controllers-5f6d4b864b-f2ssh 1/1 Running 0 2m54scalico-node-78wg7 1/1 Running 0 2m54scalico-node-8hxxj 1/1 Running 0 2m54scalico-node-8t4c9 1/1 Running 0 2m54scalico-node-fgwdv 1/1 Running 0 2m54scalico-node-jzh8w 1/1 Running 0 2m54s

目前用的是阿里云的镜像,生产环境需要推荐使用自己的镜像仓库,这样速度更快https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

Dashboard&Metrics Server安装

Metrics Server

在新版的Kubernetes中系统资源的采集均使用Metrics-server,可以通过Metrics采集节点和Pod的内存、磁盘、CPU和网络的使用率。https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

github 地址:https://github.com/kubernetes-sigs/metrics-serverhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

查看yaml文件配置https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cd metrics-server-0.4.x-kubeadm/vim comp.yaml

添加了证书,不然可能导致获取不到度量指标https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> - --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt # change to front-proxy-ca.crt for kubeadm

镜像地址也修改为阿里云https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> image: registry.cn-beijing.aliyuncs.com/dotbalo/metrics-server:v0.4.1

将Master01节点的front-proxy-ca.crt复制到所有Node节点https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> scp /etc/kubernetes/pki/front-proxy-ca.crt k8s-node01:/etc/kubernetes/pki/front-proxy-ca.crtscp /etc/kubernetes/pki/front-proxy-ca.crt k8s-node02:/etc/kubernetes/pki/front-proxy-ca.crt

安装metrics serverhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cd /root/k8s-ha-install/metrics-server-0.4.x-kubeadm/kubectl create -f comp.yaml

查看状态https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl top node

显示CPU状态,内存使用量https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% k8s-master01 204m 10% 1114Mi 59% k8s-master02 128m 6% 1133Mi 60% k8s-master03 113m 5% 1056Mi 56% k8s-node01 54m 2% 809Mi 43% k8s-node02 71m 3% 834Mi 44%

Dashboard部署

Dashboard用于展示集群中的各类资源,同时也可以通过Dashboard实时查看Pod的日志和在容器中执行一些命令等。https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

github 地址:https://github.com/kubernetes/dashboardhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

安装指定版本dashboard

查看yaml文件配置https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> cd /root/k8s-ha-install/dashboard/grep "image" dashboard.yaml

可以看到只修改了镜像地址https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> image: registry.cn-beijing.aliyuncs.com/dotbalo/dashboard:v2.0.4imagePullPolicy: Alwaysimage: registry.cn-beijing.aliyuncs.com/dotbalo/metrics-scraper:v1.0.4

注意:所有的镜像包括caclico, coredns, etcd等等都要放到自己公司内部的镜像仓库,这样发布、更新、故障恢复的速度更快https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

安装https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl create -f .

安装最新版

如果需要访问最新版本可以访问官方github获取连接,但是没必要安装最新https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml

创建管理员用户vim admin.yamlhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> apiVersion: v1kind: ServiceAccountmetadata: name: admin-user namespace: kube-system---apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBinding metadata: name: admin-user annotations: rbac.authorization.kubernetes.io/autoupdate: "true"roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-adminsubjects:- kind: ServiceAccount name: admin-user namespace: kube-system

应用https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl apply -f admin.yaml -n kube-system

登录dashboard

在谷歌浏览器(Chrome)启动文件中加入启动参数,用于解决无法访问Dashboard的问题https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> --test-type --ignore-certificate-errors

右键--属性--快捷方式--目标https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> "C:\Program Files\Google\Chrome\Application\chrome.exe" --test-type --ignore-certificate-errors

更改dashboard的svc为NodePort:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl edit svc kubernetes-dashboard -n kubernetes-dashboard

将ClusterIP更改为NodePort(如果已经为NodePort忽略此步骤)https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

查看端口号:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get svc kubernetes-dashboard -n kubernetes-dashboard

端口号为10.99.156.65https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes-dashboard NodePort 10.99.156.65 <none> 443:32272/TCP 9m58s

查看容器是否启动完成https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get po -A

根据自己的实例端口号,通过任意安装了kube-proxy的宿主机或者VIP的IP+端口即可访问到dashboard:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

访问Dashboard:https://192.168.232.236:32272(请更改32272为自己的端口),选择登录方式为令牌(即token方式)https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

也可以通过宿主机的ip访问:https://192.168.232.128:32272https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

查看端口占用https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> netstat -lntp

占用情况https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> Active Internet connections (only servers)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:32272 0.0.0.0:* LISTEN 31552/kube-proxy

可以看到 NodePort 所做的事情就是在宿主机上启动一个端口号 32272,这个端口号会对应到 dashboard,每一台服务器都会启动这个端口,都可以访问到 dashboardhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> https://192.168.232.129:32272https://192.168.232.130:32272https://192.168.232.131:32272https://192.168.232.132:32272

查看token值:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

得到token值:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> token: eyJhbGciOiJSUzI1NiIsImtpZCI6IkxReUpVOGk4b0FrUUlLVDgyb1hTb0U1Uzg4cHZYRzBMRXJqOERHRDJMQ0EifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLXJ2bW1oIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiIxMDkwNTc5MS1mMmJmLTRlMmQtYjJiNy1hYTRlNjU5YTM1MjQiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06YWRtaW4tdXNlciJ9.UEayfCEyeAx-wrguqqyZIKTr1O9OzFyksowng6J54BhdonBhF3JEcbJlykjRfP4DZzpniorWdwE6AhuE-VQ8xiyKcMEl8mnnovHM3T65v1DsD3KSdfFnbdFXLyEnA7zeS7-7oh7-fQkEHcPvEWJGO7OJq3aVVxxVqgvecuxkC0v43Bp3yVlZZ94bmRpy3VeEtAJ1KYgDH33oSOzBqRJ5GOWqCicepQFlwsrERu8slwa1yazu7upcR7mn2H-bXZxZY09HPG3gM0xypfULhdV8xd7ZI1wovm29na_PjcPB8w3b3tMbqF8ee5BKQdZGORZWYdAZhActT33rYqhZs_1ieg

将token值输入到令牌后,单击登录即可访问Dashboardhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

切换 namespace 到 kube-systemhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

一些必须的配置更改

将Kube-proxy改为ipvs模式,因为在初始化集群的时候注释了ipvs配置,所以需要自行修改一下:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

在master01节点执行https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl edit cm kube-proxy -n kube-system

搜索定位到modehttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> /mode 回车

修改为 ipvshttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> mode: “ipvs”

保存退出:shift + z + zhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

更新 Kube-Proxy 的 Pod:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl patch daemonset kube-proxy -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}" -n kube-system

查看 pod 滚动更新https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl get po -n kube-system -owide

可以看到新起的是在 master03https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESkube-proxy-47pww 1/1 Running 0 2m33s 192.168.232.129 k8s-master02 <none> <none>kube-proxy-4mlnm 1/1 Running 0 2m21s 192.168.232.128 k8s-master01 <none> <none>kube-proxy-6h8mx 1/1 Running 0 2m8s 192.168.232.132 k8s-node02 <none> <none>kube-proxy-6p7hg 1/1 Running 0 2m12s 192.168.232.131 k8s-node01 <none> <none>kube-proxy-fzgws 1/1 Running 0 118s 192.168.232.130 k8s-master03 <none> <none>

在 master03 验证 Kube-Proxy 模式,接着可以在所有服务器验证一下https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> curl 127.0.0.1:10249/proxyMode

注意事项

kubeadm安装的集群,证书有效期默认是一年。master节点的kube-apiserver、kube-scheduler、kube-controller-manager、etcd都是以容器运行的。可以通过kubectl get po -n kube-system查看。https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

启动和二进制的区别:

kubelet的配置文件在/etc/sysconfig/kubelet和/var/lib/kubelet/config.yaml,修改后需要重启kubelet进程https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

其他组件的配置文件在/etc/kubernetes/manifests目录下,比如kube-apiserver.yaml,该yaml文件更改后,kubelet会自动刷新配置,也就是会重启pod。不能再次创建该文件https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

kube-proxy的配置在kube-system命名空间下的configmap中,可以通过https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl edit cm kube-proxy -n kube-system

进行更改,更改完成后,可以通过patch重启kube-proxyhttps://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl patch daemonset kube-proxy -p "{\"spec\":{\"template\":{\"metadata\":{\"annotations\":{\"date\":\"`date +'%s'`\"}}}}}" -n kube-system

Kubeadm安装后,master节点默认不允许部署pod,会占用资源,在学习过程中可以通过以下方式打开:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>

查看Taints:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl describe node -l node-role.kubernetes.io/master= | grep Taints

可以看到三个污点https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> Taints: node-role.kubernetes.io/master:NoScheduleTaints: node-role.kubernetes.io/master:NoScheduleTaints: node-role.kubernetes.io/master:NoSchedule

删除Taint:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl taint node -l node-role.kubernetes.io/master node-role.kubernetes.io/master:NoSchedule-

查看Taints:https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> kubectl describe node -l node-role.kubernetes.io/master= | grep Taints

可以看到三个污点https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc> Taints: <none>Taints: <none>Taints: <none>

课程链接(私信我领取专属福利)

http://www.kubeasy.com/https://www.dnzp.com/uploads/allimg/230917/4cjshmrfvmc>


文章标签:

本文链接:『转载请注明出处』