kubeadm是官方社区推出的一个用于快速部署kubernetes集群的工具。
这个工具能通过两条指令完成一个kubernetes集群的部署:
创建一个Master节点
kubeadm init
将一个Node节点加入到当前集群中
kubeadm join <Master 节点的IP和端口>
安装要求
在开始之前,部署Kubernetes集群机器需要满足以下几个条件:
-至少3台机器,操作系统 CentOS7+
- 硬件配置:2GB或更多RAM,2个CPU或更多CPU,硬盘20GB或更多
- 集群中所有机器之间网络互通
- 可以访问外网,需要拉取镜像
- 禁止swap分区
学习目标
- 在所有节点上安装Docker和kubeadm
- 部署Kubernetes Master
- 部署容器网络插件
- 部署 Kubernetes Node,将节点加入Kubernetes集群中
- 部署Dashboard Web页面,可视化查看Kubernetes资源
准备环境
角色 | IP |
master | 192.168.139.135 |
node01 | 192.168.139.136 |
node02 | 192.168.139.137 |
关闭所有节点的防火墙和selinux且关闭swap分区,然后设置主机名并重启
[root@localhost ~]# systemctl disable --now firewalld
[root@localhost ~]# vim /etc/selinux/config
[root@localhost ~]# vim /etc/fstab (把带有swap的注释掉,或者删除)
[root@localhost ~]# hostnamectl set-hostname
[root@localhost ~]# reboot
在所以节点添加hosts
vim /etc/hosts
192.168.139.135 master
192.168.139.136 node01
192.168.139.137 node0在所有节点将桥接的IPV4流量传递到iptables的链
[root@master ~]# cat > /etc/sysctl.d/k8s.conf << EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.brodge-nf-call-iptables = 1
> net.ipv4.ip_forward = 1
> EOF[root@master ~]# sysctl --system (使文件生效)
在所有节点设置时间同步
yum -y install chrony
systemctl enable --now chronyd
[root@master ~]# vim /etc/chrony.con
pool time1.aliyun.com iburst
[root@master ~]# systemctl restart chronyd
[root@node01 ~]# vim /etc/chrony.conf
pool master iburst
[root@node01 ~]# systemctl restart chronyd
[root@node02 ~]# vim /etc/chrony.conf
pool master iburst
[root@node02 ~]# systemctl restart chronyd
[root@master ~]# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 203.107.6.88 2 6 255 64 -32us[+2385us] +/- 43ms[root@node01 ~]# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? master 0 7 0 - +0ns[ +0ns] +/- 0ns[root@node02 ~]# chronyc sources
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^? master 0 6 0 - +0ns[ +0ns] +/- 0ns在master免密登录
[root@master ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:/7tJF6mXVXDempEvK9d4gxwYvCYLaCLf32QdV8UyEAE root@master
The key's randomart image is:
+---[RSA 3072]----+
| E.++..o|
| . o=+|
| o o++|
| . + .*.|
| . . o .S. = o* o|
| o + ..= +.oO |
| . . +..+oB.o|
| . + o * ..|
| . . =o |
+----[SHA256]-----+[root@master ~]# ssh-copy-id master
[root@master ~]# ssh-copy-id node01
[root@master ~]# ssh-copy-id node02
然后用for循环查看一下时间是否一致
[root@master ~]# for i in master node01 node02;do ssh $i 'date';done
Thu Nov 16 15:03:13 CST 2023
Thu Nov 16 15:03:13 CST 2023
Thu Nov 16 15:03:13 CST 2023
所有节点安装Docker/kubeadm/kubelet
Kubernetes默认CRI(容器运行时)为Docker,因此先安装Docker。
安装Docker
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo 这是一行内容
yum -y install docker-ce
systemctl enable --now docker
docker --version
vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]
}systemctl restart docker
添加kubernetes阿里云YUM软件源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg(这一行和上面一行是一行)
EOF
安装kubeadm,kubelet和kubectl
由于版本更新频繁,这里指定版本号部署:
yum -y install kubectl kubelet kubeadm
systemctl enable kubelet
containerd配置
为确保后面集群初始化及加入集群能够成功执行,需要配置containerd的配置文件/etc/containerd/config.toml,此操作需要在所有节点执行
containerd config default > /etc/containerd/config.toml
sed -i 's#k8s.gcr.io#registry.aliyuncs.com/google_containers#g' /etc/containerd/config.toml
systemctl restart containerd
systemctl enable containerd
部署Kubernetes Master
在Master执行