1.系统配置
环境信息:
- 系统:CentOS Linux release 7.6.1810 (Core)
- k8s版本:1.26.0 (可自己选择)
IP | 主机名 | 规划角色 |
192.168.223.123 | auto-inspaction-1 | master |
192.168.223.68 | auto-inspaction-0 | node |
192.168.223.73 | auto-inspaction-2 | node |
在各个主机上完成下面的系统配置。
如果各个主机启用了防火墙或安全组策略,需要开放Kubernetes各个组件所需要的端口,可以查看 Ports and Protocols中的内容, 开放相关端口或者关闭主机的防火墙。
禁用SELINUX:
创建/etc/modules-load.d/containerd.conf配置文件:
执行以下命令使配置生效:
创建/etc/sysctl.d/99-kubernetes-cri.conf配置文件:
执行以下命令使配置生效:
配置服务器支持开启ipvs的前提条件:
由于ipvs已经加入到了内核的主干,所以为kube-proxy开启ipvs的前提需要加载以下的内核模块:
在各个服务器节点上执行以下脚本:
赋权:
上面脚本创建了的/etc/sysconfig/modules/ipvs.modules文件,保证在节点重启后能自动加载所需模块。
使用lsmod | grep -e ip_vs -e nf_conntrack_ipv4命令查看是否已经正确加载所需的内核模块。
接下来还需要确保各个节点上已经安装了ipset软件包,为了便于查看ipvs的代理规则,最好安装一下管理工具ipvsadm。
如果不满足以上前提条件,则即使kube-proxy的配置开启了ipvs模式,也会退回到iptables模式。
2.部署容器运行时Containerd
在各个服务器节点上安装容器运行时Containerd。
下载Containerd的二进制包,可先在网络可达的机器上下载好,再上传到服务器:
cri-containerd-cni-1.6.14-linux-amd64.tar.gz压缩包中已经按照官方二进制部署推荐的目录结构布局好。 里面包含了systemd配置文件,containerd以及cni的部署文件。 将解压缩到系统的根目录/中:
注意 经测试cri-containerd-cni-1.6.4-linux-amd64.tar.gz包中包含的runc在CentOS 7下的动态链接有问题,这里从runc的github上单独下载runc,并替换上面安装的containerd中的runc:
接下来生成containerd的配置文件:
根据文档 Container runtimes中的内容,对于使用systemd作为init system的Linux的发行版,使用systemd作为容器的cgroup driver可以确保服务器节点在资源紧张的情况更加稳定,因此这里配置各个节点上containerd的cgroup driver为systemd。
修改前面生成的配置文件/etc/containerd/config.toml:
再修改/etc/containerd/config.toml中的sandbox_image镜像源:
配置containerd开机启动,并启动containerd:
使用crictl测试一下,确保可以打印出版本信息并且没有错误信息输出:
3.使用kubeadm部署Kubernetes
下面在各节点安装kubeadm和kubelet,创建yum源:
跟根据后缀修改自己想要的版本,如kubelet-1.28.0* kubeadm-1.28.0* kubectl-1.28.0*
,不指定默认则下载的是yum源里面最新的kubelet kubeadm kubectl。
运行kubelet --help可以看到原来kubelet的绝大多数命令行flag参数都被DEPRECATED了,官方推荐我们使用–config指定配置文件,并在配置文件中指定原来这些flag所配置的内容。具体内容可以查看这里 Set Kubelet parameters via a config file
最初Kubernetes这么做是为了支持动态Kubelet配置(Dynamic Kubelet Configuration),但动态Kubelet配置特性从k8s 1.22中已弃用,并在1.24中被移除。如果需要调整集群汇总所有节点kubelet的配置,还是推荐使用ansible等工具将配置分发到各个节点。
kubelet的配置文件必须是json或yaml格式,具体可查看 这里 Kubernetes 1.8开始要求关闭系统的Swap,如果不关闭,默认配置下kubelet将无法启动。 关闭系统的Swap方法如下:
修改/etc/fstab文件,注释掉 SWAP 的自动挂载,使用free -m确认swap已经关闭。
swappiness参数调整,修改/etc/sysctl.d/99-kubernetes-cri.conf添加下面一行,再执行sysctl -p /etc/sysctl.d/99-kubernetes-cri.conf
使修改生效。
4.使用kubeadm init初始化集群
在各节点开机启动kubelet服务:
使用kubeadm config print init-defaults --component-configs KubeletConfiguration
可以打印集群初始化默认的使用的配置,这里不在贴默认配置。
从默认的配置中可以看到,可以使用imageRepository
定制在集群初始化时拉取k8s所需镜像的地址。以下配置是基于默认的配置定制出本次使用kubeadm初始化集群所需的配置文件kubeadm.yaml,特别注意修改advertiseAddress
为你的master节点主机地址。
kubernetesVersion: 1.26.0 需要与上面 yum install -y kubelet-1.26.0* kubeadm-1.26.0* kubectl-1.26.0*安装的软件包版本保持一直
这里定制了imageRepository为阿里云的registry,避免因gcr被墙,无法直接拉取镜像。criSocket设置了容器运行时为containerd。 同时设置kubelet的cgroupDriver为systemd,设置kube-proxy代理模式为ipvs
在开始初始化集群之前,先在各个服务器节点上拉取所k8s需要的容器镜像。
接下来使用kubeadm初始化集群,执行下面的命令:
上面记录了完成的初始化输出的内容,根据输出的内容基本上可以看出手动初始化安装一个Kubernetes集群所需要的关键步骤。 其中有以下关键内容:
-
[certs]
生成相关的各种证书 -
[kubeconfig]
生成相关的kubeconfig文件 -
[kubelet-start]
生成kubelet的配置文件/var/lib/kubelet/config.yaml -
[control-plane]
使用/etc/kubernetes/manifests目录中的yaml文件创建apiserver、controller-manager、scheduler的静态pod -
[bootstraptoken]
生成token记录下来,后边使用kubeadm join往集群中添加节点时会用到 -
[addons]
安装基本插件:CoreDNS, kube-proxy
下面的命令是配置常规用户如何使用kubectl访问集群:
最后给出了将另外其它节点加入集群的命令:
查看一下集群状态,确认个组件都处于healthy状态:
集群初始化如果遇到问题,可以使用kubeadm reset命令进行清理。
5.安装包管理器helm
Helm是Kubernetes的包管理器,后续流程也将可以使用Helm安装Kubernetes的常用组件。 这里先在master节点上安装helm。
执行helm list确认没有错误输出:
6.在线部署flannel网络(flannel与Calico选其中一个部署)
7.部署Calico网络(flannel与Calico选其中一个部署)
选择calico作为k8s的Pod网络组件,下面使用helm在k8s集群中安装calico。
下载tigera-operator的helm chart:
可以执行查看这个chart的中可定制的默认配置:helm show values tigera-operator-v3.24.5.tgz
这里不再贴默认配置
本环境定制的values.yaml如下:
使用helm安装calico:
等待并确认所有pod处于Running状态:
查看一下calico向k8s中添加的api资源:
这些api资源是属于calico的,因此不建议使用kubectl来管理,推荐按照calicoctl来管理这些api资源。 将calicoctl安装为kubectl的插件:
验证插件正常工作:
8.验证k8s DNS是否可用
首次验证:
后续进入相同的容器可继续执行命令:
9.使用Helm部署ingress-nginx
了便于将集群中的服务暴露到集群外部,需要使用Ingress。接下来使用Helm将ingress-nginx部署到Kubernetes上。 Nginx Ingress Controller被部署在Kubernetes的边缘节点上。
这里将主机 auto-inspaction-0 作为边缘节点,打上Label:
下载ingress-nginx的helm chart:
执行helm show values ingress-nginx-4.4.2.tgz
查看ingress-nginx-4.4.2.tgz这个chart的m默认配置,这里不再贴出默认配置。
对ingress-values.yaml配置定制如下,可以直接用:
nginx ingress controller的副本数replicaCount为1,将被调度到node1这个边缘节点上。这里并没有指定nginx ingress controller service的externalIPs,而是通过hostNetwork: true设置nginx ingress controller使用宿主机网络。 因为k8s.gcr.io被墙,这里替换成unreachableg/registry.k8s.io_ingress-nginx_controller提前拉取一下镜像:
部署:
查看部署结果
创建deployment service ingress测试:
执行创建测试资源,并检查相应资源状态。
浏览器测试访问nginx,出现Welcome to nginx!则成功,访问前提电脑要配置域名地址解析。
https://k8s.example.com/app
10.在线部署kubernetes dashbord