实时警报通知:微信告警通知的重要性解析
953
2022-11-05
通过kubeadm安装K8s集群
kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。这个工具能通过两条指令快速完成一个kubernetes集群的部署。
一、环境准备:
主机名 | IP | 节点类型 | 配置 |
k8s-master1 | 192.168.15.215 | master1 | 1G2CPU |
k8s-master2 | 192.168.15.216 | master2 | 1G2CPU |
k8s-master3 | 192.168.15.217 | master2 | 1G2CPU |
k8s-worker1 | 192.168.15.218 | node1 | 1G2CPU |
k8s-woker2 | 192.168.15.219 | node2 | 1G2CPU |
k8s-woker3 | 192.168.15.221 | node3 | 1G2CPU |
vip | 192.168.15.253 |
#注意:master节点至少要2个cpu,否则报错error execution phase preflight: [preflight] Some fatal errors occurred:[ERROR NumCPU]: the number of available CPUs 1 is less than the required 2
二、系统初始化:
1、修改/etc/hosts,设置主机名,如下:
192.168.15.215 k8s-master01192.168.15.216 k8s-master02192.168.15.217 k8s-master03192.168.15.218 k8s-worker1192.168.15.219 k8s-worker2192.168.15.253 k8s-master-lb #前端负载IP
2、关闭防火墙:
systemctl stop firewalldsystemctl disable firewalldsystemctl stop NetworkManagersystemctl disable NetworkManager
3、关闭selinux:
sed -i 's/enforcing/disabled/' etc/selinux/config # 永久关闭setenforce 0 # 临时关闭
4、关闭swap:
#swap开启会影响k8s编排和调度应用程序运行的效果,会降低性能swapoff -a # 临时关闭sed -ri '/^[^#]*swap/s@^@#@' etc/fstab# 永久关闭
5、将桥接的IPV4流量传递到iptables链:
cat > etc/sysctl.d/k8s.conf << EOFnet.bridge.bridge-nf-call-ip6tables = 1net.bridge.bridge-nf-call-iptables = 1EOFsysctl --system # 生效#有一些ipv4的流量不能走iptables链,会导致流量丢失,iptables链为linux内核的一个过滤器,每个流量都会经过他,然后再匹配是否可进入当前应用进程去处理
6、同步时间服务器:
#各个节点需要通过时间服务器保持时间同步yum install ntpdate -yntpdate time.windows.com # 可添加到定时任务中
7、修改内核参数文件,添加内容如下:
cat >> etc/security/limits.conf < 8、升级一下内核,执行命令如下: yum update -y --exclude=kernel* && reboot #排除内核,下面会单独升级内核 9、升级内核,至少达到4.18以上,如下: #需要提前下载好内核文件,下载地址:https://kernel.org/yum -y localinstall kernel-ml*更改内核启动顺序:grub2-set-default 0 && grub2-mkconfig -o etc/grub2.cfggrubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"grubby --default-kernel #检查默认内核reboot #最后重启即可 10、安装ipvsadm: yum -y install ipvsadm ipset sysstat conntrack libseccomp内核配置ipvs模块vim etc/modules-load.d/ipvs.conf 加入如下内容:cat > etc/modules-load.d/ipvs.conf << EOFip_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_REJECTipipEOF#执行命令启动服务,注意,上一步升级内核如果不重启此处会不成功systemctl enable --now systemd-modules-load.service#查看是否加载lsmod | grep -e ip_vs -e nf-conntrack 11、开启k8s集群中必需的内核参数: cat >> etc/sysctl.d/k8s.conf < 三、docker安装: 1、下载二进制文件包,下载地址: https://download.docker.com/linux/static/stable/x86_64/ 2、执行下面命令,将二进制包解压,如下: #解压二进制包tar zxvf docker-19.03.9.tgz#移动二进制文件到环境变量路径下mv docker/* usr/bin 3、修改docker启动配置文件 ,执行命令如下: cat > etc/docker/daemon.json << EOF{"exec-opts": ["native.cgroupdriver=systemd"],"max-concurrent-downloads": 10,"max-concurrent-uploads": 5,"log-driver": "json-file","storage-driver": "overlay2","data-root": "/data/docker","storage-opts": ["overlay2.override_kernel_check=true" ],"log-opts":{"max-size": "300m","max-file": "2" },"live-restore": true}EOF 4、通过systemd 来管理 docker,执行命令如下: cat > usr/lib/systemd/system/docker.service << EOF[Unit]Description=Docker Application Container EngineDocumentation=https://docs.docker.comAfter=network-online.target firewalld.serviceWants=network-online.target[Service]Type=notifyExecStart=/usr/bin/dockerdExecReload=/bin/kill -s HUP $MAINPIDLimitNOFILE=infinityLimitNPROC=infinityLimitCORE=infinityTimeoutStartSec=0Delegate=yesKillMode=processRestart=on-failureStartLimitBurst=3StartLimitInterval=60s[Install]WantedBy=multi-user.targetEOF 5、启动并设置开机启动 : systemctl daemon-reloadsystemctl start dockersystemctl enable docker 四、安装keepalived和nginx: 注意:如果是单master,那么就不需要安装 keepalived和nginx keepalived直接yum即可,keepalived配置文件可参考二进制安装,nginx需要编译安装,因为需要stream模块,nginx配置文件内容如下: events {worker_connections 1024;}stream {log_format main '$remote_addr $upstream_addr - [$time_local] $status $upstream_bytes_sent';access_log var/log/nginx/k8s-access.log main;upstream k8s-apiserver {server 192.168.15.215:6443; # Master1 APISERVER IP:PORTserver 192.168.15.216:6443; # Master2 APISERVER IP:PORTserver 192.168.15.217:6443 # Master3 APISEVER IP:PORT } server {listen 16443; # 由于nginx与master节点复用,这个监听端口不能是6443,否则会冲突proxy_pass k8s-apiserver; }}http {include mime.types;default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '# '$status $body_bytes_sent "$http_referer" '# '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;#gzip on;server {listen 80;server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;location {root html;index index.html index.htm; }#error_page 404 404.html;# redirect server error pages to the static page 50x.html#error_page 500 502 503 504 50x.html;location = 50x.html {root html; } }} keepalived中配置nginx健康状态检测脚本,如下: ! Configuration File for keepalivedglobal_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.200.1smtp_connect_timeout 30router_id LVS_DEVELvrrp_skip_check_adv_addrvrrp_garp_interval 0vrrp_gna_interval 0}vrrp_script check_nginx {script "/etc/keepalived/check_nginx.sh"interval 2weight -20 #脚本返回状态码为1,那么权重下降20,实现VIP飘逸}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.15.253/24}track_script {check_nginx}} check_nginx脚本内容如下: #!/bin/bashcount=$(ss -antp |grep 16443 |egrep -cv "grep|$$")if [ "$count" -eq 0 ];thenexit 1elseexit 0fi 五、安装etcd: 本教程中etcd采用单独安装的方式,安装方法参考二进制部署章节 https://ywdevops.cn/index.php/2021/06/29/k8s-2/ 六、安装k8s组件: 1、下载k8s组件: 我们需要安装kubeadm, kubelet, kubectl,版本需要一致。在可以连外网的机器上下载组件。 添加kubernetes yum源: cat > etc/yum.repos.d/kubernetes.repo < 查看kubeadm版本: yum list kubeadm --showduplicates 我们下载1.20.8版本(也可根据需要下载任何版本) yum install --downloadonly --downloaddir ~/k8s/kubernetes kubeadm-1.20.8-0 在根据上述方式查找kubelet、kubectl版本,以及依赖包,并下载: yum install --downloadonly --downloaddir ~/k8s/kubernetes kubelet-1.20.8-0yum install --downloadonly --downloaddir ~/k8s/kubernetes kubectl-1.20.8-0yum install --downloadonly --downloaddir ~/k8s/kubernetes kubernetes-cni-0.8.7-0yum install --downloadonly --downloaddir ~/k8s/kubernetes cri-tools-1.13.0-0 2、安装k8s组件: 将上图中的rpm包拷贝到每一个节点,执行如下命令安装: yum install ~/k8s/kubernetes/*.rpm 3、设置kubelet的开机启动: 我们并不需要启动kubelet,就算启动,也是不能成功的。执行kubeadm命令,会生成一些配置文件 ,这时才会让kubelet启动成功的。 systemctl enable kubelet 七、部署k8s集群: 1、获取默认配置文件 : kubeadm config print init-defaults > kubeadm-config.yaml kubeadm-config.yaml文件的内容如下: apiVersion: kubeadm.k8s.io/v1beta2bootstrapTokens:- groups: - system:bootstrappers:kubeadm:default-node-token token: abcdef.0123456789abcdef ttl: 24h0m0s usages: - signing - authenticationkind: InitConfigurationlocalAPIEndpoint: advertiseAddress: 192.168.15.215 bindPort: 6443nodeRegistration: criSocket: /var/run/dockershim.sock name: k8s-master1 taints: - effect: NoSchedule key: node-role.kubernetes.io/master---apiServer: certSANs: - k8s-master1 - k8s-master2 - k8s-master3 - 192.168.15.215 - 192.168.15.216 - 192.168.15.217 - 192.168.15.253 - 127.0.0.1 extraArgs: authorization-mode: Node,RBAC timeoutForControlPlane: 4m0sapiVersion: kubeadm.k8s.io/v1beta2certificatesDir: /etc/kubernetes/pkiclusterName: kubernetescontrolPlaneEndpoint: "192.168.15.253:16443" #vipcontrollerManager: {}dns: type: CoreDNSetcd: external: endpoints: - https://192.168.15.215:2379 - https://192.168.15.216:2379 - https://192.168.15.217:2379 caFile: /opt/etcd/ssl/etcd-ca.pem certFile: /opt/etcd/ssl/server.pem keyFile: /opt/etcd/ssl/server-key.pem imageRepository: registry.aliyuncs.com/google_containers #阿里云仓库kind: ClusterConfigurationkubernetesVersion: v1.20.8networking: dnsDomain: cluster.local podSubnet: 172.168.0.0/12 #pod网段 serviceSubnet: 10.96.0.0/12 #server网段scheduler: {} 2、初始化集群: kubeadm init --config kubeadm-config.yaml 注意:如果初始化失败,可执行下面命令后再次重新初始化,如下: kubeadm reset -fiptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X 如果初始化成功,可以看到内容如下: 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 control-plane nodes by copying certificate authoritiesand service account keys on each node and then running the following as root: kubeadm join 192.168.15.253:16443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:9363682fcdbfedd6e059d7428d859d5b0ae5388641dcd098b6876d89461b0625 \ --control-plane Then you can join any number of worker nodes by running the following on each as root:kubeadm join 192.168.15.253:16443 --token abcdef.0123456789abcdef \ --discovery-token-ca-cert-hash sha256:9363682fcdbfedd6e059d7428d859d5b0ae5388641dcd098b6876d89461b0625 注意:master节点加入命令和Node节点加入命令的唯一区别就是master节点加入命令多了一个参数 --control-plane,其余都是一摸一样的 注意:如果初始化卡在这个位置:[kubelet-check] Initial timeout of 40s passed., 则说明是初始化配置文件中的controlPlaneEndpoint不通,可排查下 3、在其它两个master节点创建以下目录 : mkdir -p /etc/kubernetes/pki/ 4、 把主master节点证书分别复制到从master节点 : scp -r /etc/kubernetes/pki/* root@192.168.15.216:/etc/kubernetes/pki/scp -r /etc/kubernetes/pki/* root@192.168.15.217:/etc/kubernetes/pki/scp -r /etc/kubernetes/admin.conf root@192.168.15.216:/etc/kubernetes/scp -r /etc/kubernetes/admin.conf root@192.168.15.217:/etc/kubernetes/ 5、其他master节点加入集群执行以下命令 : kubeadm join 10.88.15.253:8443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:9363682fcdbfedd6e059d7428d859d5b0ae5388641dcd098b6876d89461b0625 --control-plane 6、所有master节点执行以下命令(管理集群,查看集群等),node节点随意 : #root用户执行以下命令:echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profilesource .bash_profile#非root用户可以执行以下命令:mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config 7、node节点加入集群执行以下命令(要安装kubeadm、kubelet、kubectl) : kubeadm join 10.88.15.253:8443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:9363682fcdbfedd6e059d7428d859d5b0ae5388641dcd098b6876d89461b0625 注意:如果加入时卡在了这个命令位置,[preflight] Running pre-flight checks,说明token已经过期,此时可以执行下面命令重新生成token和hash的值 kubeadm token createkubeadm token listopenssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //' 生成后使用新的token和hash值再次执行命令加入集群即可 如果加入node节点,出现如下错误: error execution phase kubelet-start: error uploading crisocket: timed out waiting for the conditionTo see the stack trace of this error execute with –v=5 or higher,此时可以按照如下步骤执行,如下: swapoff -akubeadm resetsystemctl daemon-reloadsystemctl restart kubeletiptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X#执行完成后重新加入集群即可 8、加入完成后,在master节点执行命令查看集群资源,如下: 从上图可以看出,STATUS显示NotReady,因为还没有安装网络组件 9、安装calico网络插件:下载文件,执行命令如下: #注意:此calico是针对50个节点之内的curl https://docs.projectcalico.org/manifests/calico.yaml -O 修改pod CIDR地址,默认是192.168.0.0/16,需要修改成我们自己的,先打开注释,修改自定义的pod网段地址,如图: 里面的镜像地址根据需要修改为自己的仓库地址,如图: 在镜像拉取策略位置要设置仓库认证名称和拉取策略,如图: 注意:需要创建仓库认证文件secret.yml,然后配置在calico中,这样才可以顺利将镜像拉去下来,参考二进制部署中的配置说明https://ywdevops.cn/index.php/2021/06/29/k8s-2/ 最后执行命令kubectl apply -f calico.yaml即可,执行完成后可以看到coredns也恢复正常了,如图: 各个节点状态都变成了Ready,如图: 注意:如果出现如下错误calico/node is not ready: BIRD is not ready: BGP not established,表示calico没有 发现实际真正的网卡 ,可用如下方式解决,如图: 至此,通过kubeadm部署k8s集群完成!!! 更多内容可参考链接:https://ywdevops.cn/index.php/2021/07/21/k8s-3/
发表评论
暂时没有评论,来抢沙发吧~