安装 rancher v2.7.1
[TOC]
1.前期准备
1.1.机器规划清单
- 以下测试环境配置了3台虚拟机,信息如下:
#.节点1:192.168.4.231,配置 4C-8G-40G,CentOS 7.9,官方建议rancher至少4G内存
#.节点2:192.168.4.232,配置 4C-8G-40G,CentOS 7.9,官方建议rancher至少4G内存
#.节点3:192.168.4.233,配置 4C-8G-40G,CentOS 7.9,官方建议rancher至少4G内存
1.2.修改主机名
#.节点1:hostnamectl set-hostname k8s-master
#.节点2:hostnamectl set-hostname k8s-node01
#.节点2:hostnamectl set-hostname k8s-node02
1.3.配置hosts文件
cat /etc/hosts | grep "192.168.4.231" > /dev/null || echo "192.168.4.231 k8s-master" >> /etc/hosts
cat /etc/hosts | grep "192.168.4.232" > /dev/null || echo "192.168.4.232 k8s-node01" >> /etc/hosts
cat /etc/hosts | grep "192.168.4.233" > /dev/null || echo "192.168.4.233 k8s-node02" >> /etc/hosts
cat /etc/hosts | grep "k8s"
2.初始化工作
2.1.初始化工作
#.1.配置dns并安装基础软件
sed -i 's/^nameserver.*/nameserver 223.5.5.5/g' /etc/resolv.conf
cat /etc/resolv.conf | grep "^nameserver" > /dev/null || echo "nameserver 223.5.5.5" > /etc/resolv.conf
yum install -y -q curl wget ntp ntpdate lrzsz telnet zip unzip net-tools
#.2.修改时区并同步时间
timedatectl set-timezone Asia/Shanghai
/usr/sbin/ntpdate -u ntp.aliyun.com
#.3.建议禁用firewalld否则重启后需要iptable -F清除防火墙策略
/usr/sbin/iptables -F
systemctl stop firewalld.service
systemctl disable firewalld.service
systemctl status firewalld.service
#.4.建议关闭selinux否则会限制服务
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
setenforce 0
#.5.设置ssh端口为1618
sed -i "s/#Port/Port/" /etc/ssh/sshd_config
sed -i '/^Port/c Port 1618' /etc/ssh/sshd_config
grep "^Port" /etc/ssh/sshd_config
systemctl restart sshd.service
#.6.启用rc.local开机启动
chmod +x /etc/rc.d/rc.local
systemctl enable -q rc-local.service
systemctl start rc-local
systemctl status rc-local | grep Active
2.2.安装docker ce v24.0.1
curl -sL 'http://iso.sqlfans.cn/docker/install_docker_2401.sh' | bash
sudo docker --version
- 注:确认docker默认目录,若最大分区为 /data 则需要修改docker路径为 /data/docker/lib
#.请参考:curl http://iso.sqlfans.cn/docker/docker-change-rootdir.txt
sudo docker info | grep Root
2.3.安装docker-compose v2.18.1
curl -sL 'http://iso.sqlfans.cn/docker/install_docker_compose_2181.sh' | bash
sudo docker-compose --version
3.安装rancher
3.1.master节点:安装rancher v2.7.1
mkdir -p /data/{images,docker}
docker pull rancher/rancher:v2.7.1
docker run --name rancher -d --privileged --restart=unless-stopped -p 80:80 -p 8443:443 -v /etc/localtime:/etc/localtime:ro -v /data/docker/rancher:/var/lib/rancher/ rancher/rancher:v2.7.1
docker exec -it rancher /bin/bash -c '/usr/bin/rancher --version'
3.2.登录rancher控制台,配置k8s集群
- 第1步,打开 rancher 控制台,地址:
https://192.168.4.231:8443
- 第2步,初始密码可在master节点执行
docker logs rancher 2>&1 | grep Password
来获取,比如:
[root@k8s-master ~]# docker logs rancher 2>&1 | grep Password
2023/04/04 09:37:56 [INFO] Bootstrap Password: r4b6b49twljrv2xq96264jwswp8n2b596nk6vb9lpj9vdkt2tvh8fj
- 第3步,设置新的控制台
admin
登录密码
- 第4步,在欢迎页,点
Create
创建集群
- 第5步,选择
Custom
,即:使用现有节点并使用 RKE 创建集群
- 第6步,输入集群名称,比如
k8s
- 第7步,关于角色选择,若 Master 请勾选 Etcd + Control,若 Worker 请勾选 Worker,若 Master 也充当 Worker 则请全选。示例:
k8s-master 全选,k8s-node01 和 k8s-node02 只勾选 Worker
- 第8步,复制上一步的命令去相应节点的SSH终端运行,示例如下:
[root@k8s-master ~]# sudo docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run rancher/rancher-agent:v2.7.1 --server https://192.168.4.231:8443 --token 8pmg8z645jwvh2ptr6rnwwkdbt6dwhqwlwcqcpjjb7tbzbcts4cqs7 --ca-checksum 7c73825483e6f9e1d3288710c892fd71320812fd01af5d10619b0c528857f998 --etcd --controlplane --worker
5740fb7cb265d6246ad10e8f8cce41e509f74b7107310145c1fbcb62ceb57ffa
[root@k8s-node01 ~]# sudo docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run rancher/rancher-agent:v2.7.1 --server https://192.168.4.231:8443 --token 8pmg8z645jwvh2ptr6rnwwkdbt6dwhqwlwcqcpjjb7tbzbcts4cqs7 --ca-checksum 7c73825483e6f9e1d3288710c892fd71320812fd01af5d10619b0c528857f998 --worker
ef14c36c78be540b449c6b6a46433e85266e03255b5d552e891db2e7e1622afb
[root@k8s-node02 ~]# sudo docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run rancher/rancher-agent:v2.7.1 --server https://192.168.4.231:8443 --token 8pmg8z645jwvh2ptr6rnwwkdbt6dwhqwlwcqcpjjb7tbzbcts4cqs7 --ca-checksum 7c73825483e6f9e1d3288710c892fd71320812fd01af5d10619b0c528857f998 --worker
67f6c7c95aeeb9ed801216cdbdbb7cdebaedfc5bca0b62a73a4d60ad62aeec89
3.3.集群初始化
- 待复制上一步的命令并去对应节点运行之后,再点最下面的 Done,然后等待集群初始化 ......
3.4.master节点:确认k8s集群信息
- 确认 k8s 版本为 v1.24.10
docker exec -it rancher /bin/bash -c '/usr/bin/kubectl version --short'
docker exec -it rancher /bin/bash -c '/usr/bin/kubectl get componentstatuses'
docker exec -it rancher /bin/bash -c '/usr/bin/kubectl get nodes'
- 刷新rancher控制台,查看集群- Nodes 查看节点状态
4.后期优化
4.1.安装kubectl v1.26.3
sudo curl -L http://iso.sqlfans.cn/k8s/kubectl.v1.26.3 -o /usr/bin/kubectl
sudo chmod +x /usr/bin/kubectl
sudo kubectl version --short
4.2.将kubectl关联k8s资源
mkdir ~/.kube
touch ~/.kube/config
#.集群首页右上角,点击 Copy KubeConfig to Clipboard,并将内容覆盖到~/.kube/config
kubectl get nodes
附录
- 如果为了测试,需要铲掉重来的话,可以参考如下脚本:
#.如果一切可以重来
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker volume rm $(docker volume ls -q)
docker rmi -f $(docker images | awk '{print $3}')
rm -rf /data/docker/rancher
rm -rf /etc/ceph /etc/cni /etc/kubernetes /opt/cni /opt/rke /run/secrets/kubernetes.io /run/calico /run/flannel
rm -rf /var/run/calico /var/log/containers /var/log/pods /var/lib/calico /var/lib/etcd /var/lib/cni /var/lib/kubelet /var/lib/rancher/rke/log