centos8 通过 rancher2 部署 k8s


环境:

  • 192.168.145.150 rancher 2核4G
  • 192.168.145.151 k8s-dn1 4核8G
  • 192.168.145.152 k8s-dn2 4核8G
  • 192.168.145.153 k8s-dn3 4核8G

域名:

192.168.145.150 rancher.dev

1、虚拟机安装centos

https://www.cnblogs.com/wcwen1990/p/7630545.html

2、先设置为NAT保证可以访问外网:

http://baijiahao.baidu.com/s?id=1597809303775176940&wfr=spider&for=pc

1
2
3
4
5
6
7
8
9
su root
vi /etc/sysconfig/network-scripts/ifcfg-ensXXXX

ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.145.150
NETMASK=255.255.255.0
GATEWAY=192.168.145.2
DNS1=192.168.145.2

image-20200723225333027

1
2
3
4
5
6
7
8
9
10
# centos 7
service network restart
# centos 8
nmcli networking off && nmcli networking on
ifdown eth0 && ifup eth0
nmcli con down eth0 && nmcli con up eth0
systemctl restart NetworkManager

reboot
ping www.baidu.com

image-20200723230040796

image-20200723230020587

image-20200723225130402

image-20200723225202276

3、安装ifconfig

https://www.cnblogs.com/jtnote/p/6103754.html

1
yum install -y net-tools.x86_64

4、安装ssh

https://www.cnblogs.com/liuhouhou/p/8975812.html

1
2
3
4
5
6
7
8
9
yum install -y openssl openssh-serverifcon

vi /etc/ssh/sshd_config

RSAAuthentication yes
PubkeyAuthentication yes

systemctl restart sshd.service
systemctl enable sshd.service

img

5、用 xshell 连接 contos7

6、配置 hosts 系统文件

1
2
3
4
5
6
7

192.168.145.150 rancher
192.168.145.151 k8s-dn1
192.168.145.152 k8s-dn2
192.168.145.153 k8s-dn3

192.168.145.150 rancher.dev

7、修改时区

1
2
3
4
5
6
7
\cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

vi /etc/sysconfig/clock
# 添加如下内容
ZONE="Asia/Shanghai"
UTC=false
ARC=false

8、安装 ntp,设置 节点间 ntp 同步

1
2
3
4
5
6
7
8
# 查看是否安装:
rpm -q ntp
# centos 7 安装
yum install ntpdate ntp -y
# centos 8 安装
rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
yum install wntp -y
ntpdate ntp1.aliyun.com

9、安装 ssh,配置节点间免密登录

设置主机名

1
2
3
4
hostnamectl set-hostname rancher
hostnamectl set-hostname k8s-dn1
hostnamectl set-hostname k8s-dn2
hostnamectl set-hostname k8s-dn3

依次在每台服务器上执行

1
2
3
4
5
6
7
su root
cd ~
ssh-keygen -t rsa
ssh-copy-id -i .ssh/id_rsa.pub root@rancher
ssh-copy-id -i .ssh/id_rsa.pub root@k8s-dn1
ssh-copy-id -i .ssh/id_rsa.pub root@k8s-dn2
ssh-copy-id -i .ssh/id_rsa.pub root@k8s-dn3

测试

1
2
3
4
ssh 'root@k8s-dn1'
ssh 'root@k8s-dn2'
ssh 'root@k8s-dn3'
ssh 'root@k8s-rancher'

10、关闭防火墙

1
2
systemctl stop firewalld
systemctl disable firewalld

11、kernel调优

vi /etc/security/limits.conf 在文件末尾添加以下内容:

1
2
3
4
root soft nofile 655350
root hard nofile 655350
* soft nofile 655350
* hard nofile 655350

vi /etc/sysctl.conf 添加如下内容:

1
2
3
4
5
net.ipv4.ip_forward=1
net.bridge.bridge-nf-call-iptables=1
net.bridge.bridge-nf-call-ip6tables=1
vm.swappiness=0
vm.max_map_count=655360

12、关闭swap

vi /etc/fstab 注释或删除swap交换分区:

1
2
3
/dev/mapper/cl-root     /                       xfs     defaults        0 0
UUID=5fe3b563-4639-47d5-ab24-8161c324f532 /boot ext4 defaults 1 2
# /dev/mapper/cl-swap swap swap defaults 0 0

13、关闭 selinux

SELINUX值设置为disabled:

1
2
vim /etc/selinux/config
SELINUX=disabled

14、yum 源更新

阿里云 yum 源 https://developer.aliyun.com/mirror/centos?spm=a2c6h.13651102.0.0.3e221b11HiFxxt

各种源:https://developer.aliyun.com/mirror/

1
2
3
4
5
6
cd /etc/yum.repos.d/
rm -f CentOS-Base.repo CentOS-AppStream.repo CentOS-PowerTools.repo CentOS-centosplus.repo CentOS-Extras.repo

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo

yum clean all && yum makecache

15、docker 安装配置

每个节点执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-engine

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y https://download.docker.com/linux/fedora/30/x86_64/stable/Packages/containerd.io-1.2.6-3.3.fc30.x86_64.rpm
yum install -y docker-ce docker-ce-cli containerd.io

systemctl start docker
docker run hello-world

systemctl enable docker

配置docker源

1
2
3
4
5
6
7
8
mkdir -p /etc/docker
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://k8spv7nq.mirror.aliyuncs.com"]
}

systemctl daemon-reload
systemctl restart docker

16、docker安装rancher2

在rancher节点执行

1
2
3
docker run -d --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher

docker logs -f ec91d23fb68b

http://192.168.145.150

admin/123456

image-20200724004156391

访问 rancher

在主机hosts添加

1
2
3
> C:\Windows\System32\drivers\etc\hosts
> 192.168.145.150 rancher.dev
>

https://rancher.dev

admin/123456

17、安装k8s

image-20200724004944667

在 worker节点机器中配置hosts地址

1
2
vi /etc/hosts
192.168.145.150 rancher.dev
1
2
3
docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run rancher/rancher-agent:v2.4.5 --server https://rancher.dev --token vpcq7clf5r5v7vcck8wd759xjdb87bj966hzl5tdfjm7mgmdzc5zwq --ca-checksum 9e76ce060a22f87140fca47d350ccbf99d8d7790f76915db6dba7c50d2b95be6 --etcd --controlplane --worker

docker run -d --privileged --restart=unless-stopped --net=host -v /etc/kubernetes:/etc/kubernetes -v /var/run:/var/run rancher/rancher-agent:v2.4.5 --server https://rancher.dev --token vpcq7clf5r5v7vcck8wd759xjdb87bj966hzl5tdfjm7mgmdzc5zwq --ca-checksum 9e76ce060a22f87140fca47d350ccbf99d8d7790f76915db6dba7c50d2b95be6 --worker

耗时10分钟左右,集群可用

image-20200724005836365

image-20200724010046927

18、安装 kubectl 并配置 kubeconfig

在4台机器上安装

1
2
3
4
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl
chmod +x ./kubectl
mv ./kubectl /usr/local/bin/kubectl
kubectl version --client

配置kubeconfig

image-20200725202349013

1
2
3
4
5
mkdir -p ~/.kube
vi ~/.kube/config
# 将上图中的文件内容,复制到文件中

kubectl get all

image-20200725204124834