一、前期体系情况准备
1、关闭防火墙与selinux
- [root@k8s-master ~]# systemctl stop firewalld
- [root@k8s-master ~]# systemctl disable firewalld
- Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
- Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
- [root@k8s-master ~]# setenforce 0
- [root@k8s-master ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
- [root@k8s-master ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
复制代码 2、设置主机映射
- [root@k8s-master ~]# vim /etc/hosts
- 10.0.0.11 k8s-master
- 10.0.0.22 k8s-node01
- 10.0.0.33 k8s-node02
- [root@k8s-master ~]# scp /etc/hosts root@10.0.0.22:/etc/hosts
- [root@k8s-master ~]# scp /etc/hosts root@10.0.0.22:/etc/hosts
复制代码 3、测试映射结果
- [root@k8s-master ~]# ping k8s-node01
- PING k8s-node01 (10.0.0.22) 56(84) bytes of data.
- 64 bytes from k8s-node01 (10.0.0.22): icmp_seq=1 ttl=64 time=0.346 ms
- 64 bytes from k8s-node01 (10.0.0.22): icmp_seq=2 ttl=64 time=0.265 ms
- ^C
- --- k8s-node01 ping statistics ---
- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms
- rtt min/avg/max/mdev = 0.265/0.305/0.346/0.044 ms
- [root@k8s-master ~]# ping k8s-node02
- PING k8s-node02 (10.0.0.33) 56(84) bytes of data.
- 64 bytes from k8s-node02 (10.0.0.33): icmp_seq=1 ttl=64 time=0.306 ms
- 64 bytes from k8s-node02 (10.0.0.33): icmp_seq=2 ttl=64 time=0.193 ms
- ^C
- --- k8s-node02 ping statistics ---
- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms
复制代码 4、设置主机间免密登录
- [root@k8s-master ~]# ssh-keygen
- Generating public/private rsa key pair.
- Enter file in which to save the key (/root/.ssh/id_rsa):
- Enter passphrase (empty for no passphrase):
- Enter same passphrase again:
- Your identification has been saved in /root/.ssh/id_rsa.
- Your public key has been saved in /root/.ssh/id_rsa.pub.
- The key fingerprint is:
- SHA256:pJNP7Nx9pi00P7w8nBNECxdAyHyKPnc6UNaLdXYs6b8 root@k8s-master
- The key's randomart image is:
- +---[RSA 2048]----+
- | o oo... |
- | + o o |
- | .. + + + |
- | =. + o B o|
- | +.So o * o |
- | *+.o.= o |
- | ++.+.=o+ |
- | o .*O .|
- | ...+E.|
- +----[SHA256]-----+
- [root@k8s-master ~]# ssh-copy-id root@10.0.0.22
- /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed:
- "/root/.ssh/id_rsa.pub"
- /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to
- filter out any that are already installed
- /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are
- prompted now it is to install the new keys
- root@10.0.0.22's password:
- Number of key(s) added: 1
- Now try logging into the machine, with: "ssh 'root@10.0.0.22'"
- and check to make sure that only the key(s) you wanted were added.
- [root@k8s-master ~]# ssh-copy-id root@10.0.0.33
复制代码 5、设置yum源
6、安装必备工具
- [root@k8s-master ~]# yum install wget jq psmisc vim net-tools telnet yum-utils
- device-mapper-persistent-data lvm2 git -y
复制代码 7、关闭swap 分区
- [root@k8s-master ~]# swapoff -a && sysctl -w vm.swappiness=0
- vm.swappiness = 0
- [root@k8s-master ~]# sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
复制代码 8、同步时间
- [root@k8s-master ~]# yum -y install ntpdate
- [root@k8s-master ~]# ntpdate time2.aliyun.com
- 4 Sep 10:08:59 ntpdate[1897]: adjust time server 203.107.6.88 offset 0.007780
- sec
- [root@k8s-master ~]# which ntpdate
- /usr/sbin/ntpdate
- [root@k8s-master ~]# crontab -e
- * 5 * * * /usr/sbin/ntpdate time2.aliyun.com
复制代码 9、设置 limit
- # 单个进程可以打开的⽂件数量将被限制为 65535
- [root@k8s-master ~]# ulimit -SHn 65535
- [root@k8s-master ~]# vim /etc/security/limits.conf
- # 末尾添加如下内容
- * soft nofile 65536
- * hard nofile 131072
- * soft nproc 65535
- * hard nproc 655350
- * soft memlock unlimited
- * hard memlock unlimited
复制代码 10、安装 k8s ⾼可⽤性 Git 仓库并重启
- # 在 /root/ ⽬录下克隆⼀个名为 k8s-ha-install.git 的 Git 仓库
- [root@k8s-master ~]# git clone https://gitee.com/dukuan/k8s-ha-install.git
- 正克隆到 'k8s-ha-install'...
- remote: Enumerating objects: 920, done.
- remote: Counting objects: 100% (8/8), done.
- remote: Compressing objects: 100% (6/6), done.
- remote: Total 920 (delta 1), reused 0 (delta 0), pack-reused 912
- 接收对象中: 100% (920/920), 19.74 MiB | 1.51 MiB/s, done.
- 处理 delta 中: 100% (388/388), done.
- [root@k8s-master ~]# cd k8s-ha-install/
- [root@k8s-master k8s-ha-install]# ls
- calico.yaml krm.yaml LICENSE metrics-server-0.3.7 metrics-server-3.6.1
- README.md
- [root@k8s-master k8s-ha-install]# reboot
复制代码 二、设置内核模块
1、设置ipvs模块
2、设置k8s内核
三、基本组件安装
1、安装 Containerd
1)安装 Docker
- # 卸载之前的containerd
- [root@k8s-master ~]# yum remove -y podman runc containerd
- # 安装Docker和containerd
- [root@k8s-master ~]# yum install containerd.io docker-ce dockerce-cli -y
复制代码 2)设置 Containerd 所需模块
- [root@k8s-master ~]# cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
- > overlay
- > br_netfilter
- > EOF
- overlay # ⽤于⽀持Overlay⽹络⽂件系统的模块,它可以在现有的⽂件系统之上创建叠加层,以实现
- 虚拟化、隔离和管理等功能。
- br_netfilter # ⽤于containerd的⽹络过滤模块,它可以对进出容器的⽹络流量进⾏过滤和管理。
- [root@k8s-master ~]# cat /etc/modules-load.d/containerd.conf
- overlay
- br_netfilter
- [root@k8s-master ~]# modprobe -- overlay
- [root@k8s-master ~]# modprobe -- br_netfilter
复制代码 3)设置 Containerd 所需内核
- [root@k8s-master ~]# vim /etc/sysctl.d/99-kubernetes-cri.conf
- net.bridge.bridge-nf-call-iptables = 1
- # ⽤于控制⽹络桥接是否调⽤iptables进⾏包过滤和转发。
- net.ipv4.ip_forward = 1
- # 路由转发,1为开启
- net.bridge.bridge-nf-call-ip6tables = 1
- # 控制是否在桥接接⼝上调⽤IPv6的iptables进⾏数据包过滤和转发。
- [root@k8s-master ~]# sysctl --system
复制代码 4)Containerd 设置⽂件
- [root@k8s-master ~]# mkdir -p /etc/containerd
- # 读取containerd的配置并保存到/etc/containerd/config.toml
- [root@k8s-master ~]# containerd config default | tee
- /etc/containerd/config.toml
- [root@k8s-master ~]# vim /etc/containerd/config.toml
- # 找到第63行修改为sandbox_image = "registry.cnhangzhou.aliyuncs.com/google_containers/pause:3.9"
- #
- 找到containerd.runtimes.runc.options模块,添加SystemdCgroup = false,如果已经存
- 在则直接修改(在第127行)
- # 添加sandbox_image = "registry.cnhangzhou.aliyuncs.com/google_containers/pause:3.9"(第128行)
复制代码
- # 加载systemctl控制脚本
- [root@k8s-master ~]# systemctl daemon-reload
- # 启动containerd并设置开机启动
- [root@k8s-master ~]# systemctl start containerd.service
- [root@k8s-master ~]# systemctl enable containerd.service
- Created symlink from /etc/systemd/system/multiuser.target.wants/containerd.service
- to
- /usr/lib/systemd/system/containerd.service.
复制代码 5)设置 crictl 客户端连接的运⾏位置
- # 配置容器运⾏环境的crictl.yml⽂件
- [root@k8s-master ~]# vim /etc/crictl.yaml
- runtime-endpoint: unix:///run/containerd/containerd.sock
- # 指定了容器运⾏时的地址为:unix://...
- image-endpoint: unix:///run/containerd/containerd.sock
- # 指定了镜像运⾏时的地址为:unix://...
- timeout: 10
- # 设置了超时时间为10秒
- debug: false
- # 关闭调试模式
复制代码 2、安装 Kubernetes 组件
- # 安装 Kubeadm、Kubelet 和 Kubectl
- # 查询最新的Kubernetes版本号
- [root@k8s-master ~]# yum list kubeadm.x86_64 --showduplicates | sort -r
- # 安装1.28最新版本kubeadm、kubelet和kubectl
- [root@k8s-master ~]# yum install kubeadm-1.28* kubelet-1.28* kubectl-1.28* -y
- [root@k8s-master ~]# systemctl daemon-reload
- # 允许开机⾃启kubelet
- [root@k8s-master ~]# systemctl enable --now kubelet
- # 查看当前安装的kubeadm版本号
- [root@k8s-master ~]# kubeadm version
- kubeadm version: &version.Info{Major:"1", Minor:"28", GitVersion:"v1.28.2",
- GitCommit:"89a4ea3e1e4ddd7f7572286090359983e0387b2f", GitTreeState:"clean",
- BuildDate:"2023-09-13T09:34:32Z", GoVersion:"go1.20.8", Compiler:"gc",
- Platform:"linux/amd64"}
复制代码 3、Kubernetes 集群初始化
1)Kubeadm 设置⽂件
2)下载组件镜像
- # 通过新的配置⽂件new.yaml从指定的阿⾥云仓库拉取kubernetes组件镜像
- [root@k8s-master ~]# kubeadm config images pull --config /root/new.yaml
- [config/images] Pulled registry.cnhangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.28.2
- [config/images]
- Pulled registry.cnhangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.28.2
- [config/images]
- Pulled registry.cnhangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.28.2
- [config/images]
- Pulled registry.cnhangzhou.aliyuncs.com/google_containers/kube-proxy:v1.28.2
- [config/images]
- Pulled registry.cnhangzhou.aliyuncs.com/google_containers/pause:3.9
- [config/images]
- Pulled registry.cnhangzhou.aliyuncs.com/google_containers/etcd:3.5.9-0
- [config/images]
- Pulled registry.cnhangzhou.aliyuncs.com/google_containers/coredns:v1.10.1
复制代码 3)集群初始化
- [root@k8s-master ~]# kubeadm init --config /root/new.yaml --upload-certs
- # 等待初始化后保存这些命令
- # 当需要加⼊新node节点时,只复制这执行即可
- [root@k8s-master ~]# vim token.txt
- kubeadm join 10.0.0.200:6443 --token 7t2weq.bjbawausm0jaxury --discoverytoken-ca-cert-hash
- sha256:92191cb8741805ac561c5781d936f60a44a3233740209abf6e64738bfecd4c5e
- # 当需要⾼可⽤master集群时,将整个token复制下来
- --control-plane --certificate-key
- f9984be15f98141b212efa176c7a49fcda982888f8869b7cc668e661982cbcc0
复制代码 4)初始化错误解决
错误信息表现本机内存不够,cpu数量不够,我们现在将本机内存提到4个G,cpu数量提到4个 注意要关闭本主机然后进行修改主机设置的操作
- [root@k8s-master ~]# kubeadm init --config /root/new.yaml --upload-certs
复制代码
- 错误信息显示需要修改配置文件/proc/sys/net/ipv4/ip_forward
- [root@k8s-master ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
- [root@k8s-master ~]# kubeadm init --config /root/new.yaml --upload-certs
复制代码
- # 检查kubelet为运行状态
- [root@master ~]# systemctl status kubelet
- Active: active (running) since 五 2024-09-06 17:33:30 CST; 5min ago
- # 可能是配置文件的地址没有改,所以找不到主机,所以超时
- [root@k8s-master ~]# vim new.yaml
- # 修改第12行、24行、29行的ip地址为自己本机的ip地址
- # 初始化重置
- [root@k8s-master ~]# kubeadm reset -f ; ipvsadm --clear ; rm -rf ~/.kube
- [root@k8s-master ~]# kubeadm init --config /root/new.yaml --upload-certs
复制代码 5)加载情况变量
- [root@k8s-master ~]# vim /root/.bashrc
- export KUBECONFIG=/etc/kubernetes/admin.conf
- [root@k8s-master ~]# source /root/.bashrc
复制代码 6)查看组件容器状态
之前采⽤初始化安装⽅式,所有的体系组件均以容器的⽅式运⾏ 并且在 kube-system 定名空间内, 此时可以查看 Pod(容器 组)状态
- [root@k8s-master ~]# kubectl get po -n kube-system
- NAME READY STATUS RESTARTS AGE
- coredns-6554b8b87f-2jslr 0/1 Pending 0 10m
- coredns-6554b8b87f-mmgbd 0/1 Pending 0 10m
- etcd-k8s-master 1/1 Running 0 10m
- kube-apiserver-k8s-master 1/1 Running 0 10m
- kube-controller-manager-k8s-master 1/1 Running 3 10m
- kube-proxy-tvk64 1/1 Running 0 10m
- kube-scheduler-k8s-master 1/1 Running 3 10m
- # kubectl:k8s控制命令
- # get:获取参数
- # po:pod缩写
- # -n:指定命名空间
- # kube-system:命名空间
复制代码 4、Token 过期处理
注意:以下步骤是上述初始化下令产⽣的 Token 过期了才需要执 ⾏以下步骤,如果没有过期不需要 执⾏,直接 join 即可。
Token 过期后⽣成新的 token
- kubeadm token create --print-join-command
复制代码 Master 需要⽣成 --certificate-key:
- kubeadm init phase upload-certs --upload-certs
复制代码 5、Node 节点设置
Node 节点上主要摆设公司的⼀些业务应⽤,⽣产情况中不建议 Master 节点摆设体系组件之外的其 他 Pod,测试情况可以答应 Master 节点摆设 Pod 以节流体系资源。
1)查看集群信息
- [root@k8s-master ~]# kubectl get node
- NAME STATUS ROLES AGE VERSION
- k8s-master NotReady control-plane 25s v1.28.2
复制代码 2)node 结点根本情况设置
- [root@node01 ~]# systemctl stop firewalld
- [root@node01 ~]# systemctl disable firewalld
- Removed symlink /etc/systemd/system/multiuser.target.wants/firewalld.service.
- Removed
- symlink /etc/systemd/system/dbusorg.fedoraproject.FirewallD1.service.
- [root@node01
- ~]# setenforce 0
- [root@node01 ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g'
- /etc/sysconfig/selinux
- [root@node01 ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#g'
- /etc/selinux/config
- [root@node01 ~]# vim /etc/hosts
- [root@node01 ~]# ls /etc/yum.repos.d/
- CentOS-Base.repo epel.repo hh.repo repo.tar.gz
- docker-ce.repo epel-testing.repo kubernetes.repo
- [root@node01 ~]# yum clean all && yum makecache
- [root@node01 ~]# yum install wget jq psmisc vim net-tools telnet yum-utils
- device-mapper-persistent-data lvm2 git -y
- [root@k8s-master ~]# scp /etc/modules-load.d/containerd.conf
- root@10.0.0.250:/etc/modules-load.d/containerd.conf
- [root@node01 ~]# vim /etc/modules-load.d/containerd.conf
- [root@node01 ~]# modprobe -- br_netfilter
- [root@node01 ~]# modprobe -- overlay
- [root@k8s-master ~]# scp /etc/sysctl.d/99-kubernetes-cri.conf
- root@10.0.0.250:/etc/sysctl.d/99-kubernetes-cri.conf
- [root@node01 ~]# vim /etc/sysctl.d/99-kubernetes-cri.conf
- [root@node01 ~]# sysctl --system
- [root@k8s-master ~]# scp /etc/containerd/config.toml
- root@10.0.0.250:/etc/containerd/config.toml
- [root@node01 ~]# vim /etc/containerd/config.toml
- [root@node01 ~]# systemctl enable --now containerd
- [root@node01 ~]# cat > /etc/crictl.yaml <<EOF
- > runtime-endpoint: unix:///run/containerd/containerd.sock
- > image-endpoint: unix:///run/containerd/containerd.sock
- > timeout: 10
- > debug: false
- > EOF
- root@node01 ~]# systemctl status containerd
- Active: active (running) since 五 2024-09-06 21:41:46 CST; 2min 13s ago
- [root@node01 ~]# yum install kubeadm-1.28* kubelet-1.28* kubectl-1.28* -y
- [root@node01 ~]# systemctl daemon-reload
- [root@node01 ~]# systemctl enable --now kubelet
- [root@node01 ~]# systemctl status kubelet
- [root@node01 ~]# yum -y install ntpdate
- [root@node01 ~]# ntpdate time2.aliyun.com
- [root@node01 ~]# crontab -e
- * 5 * * * /usr/sbin/ntpdate time2.aliyun.com
- [root@node01 ~]# ulimit -SHn 65535
- [root@k8s-master ~]# scp /etc/security/limits.conf
- root@10.0.0.250:/etc/security/limits.conf
- [root@node01 ~]# vim /etc/security/limits.conf
- [root@k8s-master ~]# scp /etc/sysctl.d/k8s.conf
- root@10.0.0.250:/etc/sysctl.d/k8s.con
- [root@node01 ~]# vim /etc/sysctl.d/k8s.conf
- [root@node01 ~]# yum install docker-ce docker-ce-cli containerd.io -y
复制代码
- # node01通过复制master初始化⽣成的token来加⼊集群
- [root@node01 ~]# kubeadm join 10.0.0.200:6443 --token 7t2weq.bjbawausm0jaxury
- --discovery-token-ca-cert-hash
- sha256:92191cb8741805ac561c5781d936f60a44a3233740209abf6e64738bfecd4c5e
复制代码
- [root@node01 ~]# swapoff -a && sysctl -w vm.swappiness=0
- [root@node01 ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
复制代码
- [root@k8s-master ~]# kubeadm token create --print-join-command
- kubeadm join 10.0.0.200:6443 --token mwq6bb.h4eqdzp5vjyhe4iy --discoverytoken-ca-cert-hashsha256:92191cb8741805ac561c5781d936f60a44a3233740209abf6e64738bfecd4c5e [root@node01 ~]# kubeadm join 10.0.0.200:6443 --token mwq6bb.h4eqdzp5vjyhe4iy --discovery-token-ca-cert-hash sha256:92191cb8741805ac561c5781d936f60a44a3233740209abf6e64738bfecd4c5e
复制代码
- [root@k8s-master ~]# kubectl get node
- NAME STATUS ROLES AGE VERSION
- k8s-master NotReady control-plane 24h v1.28.2
- node01 NotReady <none> 68s v1.28.2
复制代码 3)查看集群状态
master 上查看集群状态(NotReady 不影响)
- # 获取所有节点信息
- [root@k8s-master ~]# kubectl get node
- NAME STATUS ROLES AGE VERSION
- k8s-master NotReady control-plane 24h v1.28.2
- node01 NotReady <none> 68s v1.28.2
复制代码 到此建议打快照
6、Calico 组件安装
1)切换 git 分⽀
- [root@k8s-master ~]# cd k8s-ha-install/
- [root@k8s-master k8s-ha-install]# ls
- calico.yaml krm.yaml LICENSE metrics-server-0.3.7 metrics-server-3.6.1
- README.md
- [root@k8s-master k8s-ha-install]# git checkout manual-installation-v1.28.x
- 分支 manual-installation-v1.28.x 设置为跟踪来自 origin 的远程分支 manualinstallation-v1.28.x。
- 切换到一个新分支
- 'manual-installation-v1.28.x'
复制代码 2)修改 Pod ⽹段
- [root@k8s-master k8s-ha-install]# cd calico/
- # 获取已定义的Pod⽹段
- [root@k8s-master calico]# POD_SUBNET=`cat /etc/kubernetes/manifests/kubecontroller-manager.yaml
- | grep cluster-cidr= | awk -F= '{print $NF}'`
- [root@k8s-master calico]# echo $POD_SUBNET
- 172.16.0.0/16
- # 修改calico.yml⽂件中的pod⽹段
- [root@k8s-master calico]# sed -i "s#POD_CIDR#${POD_SUBNET}#g" calico.yaml
- # 创建calico的pod
- [root@k8s-master calico]# kubectl apply -f calico.yaml
复制代码
3)查看容器和节点状态
- [root@k8s-master calico]# kubectl get po -n kube-system
复制代码
- [root@k8s-master calico]# kubectl get node
- NAME STATUS ROLES AGE VERSION
- k8s-master NotReady control-plane 24h v1.28.2
- node01 NotReady <none> 20m v1.28.2
- [root@k8s-master calico]# kubectl describe po -n kube-system calico
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |