Kubernetes快速部署
1. 安装要求
在开始之前,部署Kubernetes集群机器需要满足以下几个条件:
至少3台机器,操作系统 CentOS7+
- 硬件配置:2GB或更多RAM,2个CPU或更多* CPU,硬盘20GB或更多
- 集群中所有机器之间网络互通
- 可以访问外网,需要拉取镜像
- 禁止swap分区
2. 学习目标
- 在所有节点上安装Docker和kubeadm
- 部署Kubernetes Master
- 部署容器网络插件
- 部署 Kubernetes Node,将节点加入Kubernetes集群中
3. 准备环境
系统环境 | 角色 | IP |
---|---|---|
centos8 | master | 192.168.8.120 |
centos7 | node1 | 192.168.8.128 |
centos7 | node2 | 192.168.8.152 |
//更改主机名
//master主机上操作
[root@localhost ~]# cat /etc/redhat-release
CentOS Stream release 8
[root@localhost ~]# hostnamectl set-hostname master.example.com
[root@localhost ~]# bash
[root@master ~]# hostname
master.example.com
[root@master ~]#
//node1主机上操作
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@localhost ~]# hostnamectl set-hostname node1.example.com
[root@localhost ~]# bash
[root@node1 ~]# hostname
node1.example.com
[root@node1 ~]#
//node2主机上操作
[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@localhost ~]# hostnamectl set-hostname node2.example.com
[root@localhost ~]# bash
[root@node2 ~]# hostname
node2.example.com
[root@node2 ~]#
//关闭防火墙
//master主机上操作
[root@master ~]# systemctl disable --now firewalld
[root@master ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
//node1主机上操作
[root@node1 ~]# systemctl disable --now firewalld
[root@node1 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
//node2主机上操作
[root@node2 ~]# systemctl disable --now firewalld
[root@node2 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
//master主机上操作
// 关闭swap
注释掉或删掉swap分区(我这里是注释掉的)
[root@master ~]# vim /etc/fstab
#/dev/mapper/cs-swap none swap defaults 0 0
//node1主机上操作
[root@node1 ~]# vim /etc/fstab
#/dev/mapper/centos-swap swap swap defaults 0 0
//node2主机上操作
[root@node2 ~]# vim /etc/fstab
#/dev/mapper/cs-swap none swap defaults 0 0
//在master添加hosts
//master主机上操作
[root@master ~]# vim /etc/hosts
[root@master ~]# sed -n '1,5p' /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.8.120 master master.example.com
192.168.8.128 node1 node1.example.com
192.168.8.152 node2 node2.example.com
//测试
[root@master ~]# ping master
PING master (192.168.8.120) 56(84) bytes of data.
64 bytes from master (192.168.8.120): icmp_seq=1 ttl=64 time=0.047 ms
64 bytes from master (192.168.8.120): icmp_seq=2 ttl=64 time=0.021 ms
^C
--- master ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1046ms
rtt min/avg/max/mdev = 0.021/0.034/0.047/0.013 ms
[root@master ~]# ping node1
PING node1 (192.168.8.128) 56(84) bytes of data.
64 bytes from node1 (192.168.8.128): icmp_seq=1 ttl=64 time=0.358 ms
64 bytes from node1 (192.168.8.128): icmp_seq=2 ttl=64 time=1.69 ms
^C
--- node1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1034ms
rtt min/avg/max/mdev = 0.358/1.024/1.690/0.666 ms
[root@master ~]# ping node2
PING node2 (192.168.8.152) 56(84) bytes of data.
64 bytes from node2 (192.168.8.152): icmp_seq=1 ttl=64 time=1.67 ms
64 bytes from node2 (192.168.8.152): icmp_seq=2 ttl=64 time=1.44 ms
64 bytes from node2 (192.168.8.152): icmp_seq=3 ttl=64 time=0.983 ms
^C
--- node2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 0.983/1.365/1.674/0.286 ms
//将桥接的IPv4流量传递到iptables的链
//master主机上操作
[root@master ~]# cat > /etc/sysctl.d/k8s.conf << EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@master ~]# sysctl --system
//配置时间同步
//所有主机上操作
[root@master ~]# yum -y install chrony
[root@master ~]# vim /etc/chrony.conf
3 pool time1.aliyun.com iburst
[root@master ~]# systemctl enable --now chronyd
[root@node1 ~]# yum -y install chrony
[root@node1 ~]# vim /etc/chrony.conf
3 server time1.aliyun.com iburst
4 #server 0.centos.pool.ntp.org iburst
5 #server 1.centos.pool.ntp.org iburst
6 #server 2.centos.pool.ntp.org iburst
7 #server 3.centos.pool.ntp.org iburst
[root@node1 ~]# systemctl enable --now chronyd
[root@node2 ~]# yum -y install chrony
[root@node2 ~]# vim /etc/chrony.conf
3 pool time1.aliyun.com iburst
[root@node2 ~]# systemctl enable --now chronyd
//配置免密认证
//master主机上操作
[root@master ~]# ssh-keygen -t rsa
[root@master ~]# ssh-copy-id master
[root@master ~]# ssh-copy-id node1
[root@master ~]# ssh-copy-id node2
//测试
[root@master ~]# for i in master node1 node2;do ssh $i 'date';done
2021年 12月 18日 星期六 13:44:45 CST
2021年 12月 18日 星期六 13:44:45 CST
2021年 12月 18日 星期六 13:44:45 CST
[root@master ~]#
//重启让刚刚的操作生效
[root@master ~]# reboot
[root@node1 ~]# reboot
[root@node2 ~]# reboot
4. 所有节点安装Docker/kubeadm/kubelet
Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。
4.1 安装Docker
//所有主机上操作
// 下载docker源
[root@master ~]# cd /etc/yum.repos.d/
[root@master yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
[root@node1 ~]# cd /etc/yum.repos.d/
[root@node1 yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
[root@node2 ~]# cd /etc/yum.repos.d/
[root@node2 yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
//安装docker
[root@master ~]# yum -y install docker-ce
[root@node1 ~]# yum -y install docker-ce
[root@node2 ~]# yum -y install docker-ce
//设置docker开机自启
[root@master ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@node1 ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@node2 ~]# systemctl enable --now docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
//查看docker版本号
[root@master ~]# docker --version
Docker version 20.10.12, build e91ed57
[root@node1 ~]# docker --version
Docker version 20.10.12, build e91ed57
[root@node2 ~]# docker --version
Docker version 20.10.12, build e91ed57
//配置加速器
//所有主机上操作