安装Kubernetes:
主机名 | IP地址 | 角色 |
master | 192.168.17.131 | master结点 |
node1 | 192.168.17.132 | node |
node2 | 192.168.17.133 | node |
设置主机名:
[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# hostnamectl set-hostname node1
[root@localhost ~]# hostnamectl set-hostname node2
设置主机名解析:
[root@master ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.17.107 server01 server01.lnmp.com
192.168.17.131 master
192.168.17.132 node1
192.168.17.133 node2
测试:
[root@master ~]# ping node1
PING node1 (192.168.17.132) 56(84) bytes of data.
64 bytes from node1 (192.168.17.132): icmp_seq=1 ttl=64 time=0.419 ms
64 bytes from node1 (192.168.17.132): icmp_seq=2 ttl=64 time=1.11 ms
64 bytes from node1 (192.168.17.132): icmp_seq=3 ttl=64 time=0.965 ms
64 bytes from node1 (192.168.17.132): icmp_seq=4 ttl=64 time=0.988 ms
64 bytes from node1 (192.168.17.132): icmp_seq=5 ttl=64 time=0.667 ms
^C
--- node1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4024ms
rtt min/avg/max/mdev = 0.419/0.830/1.112/0.252 ms
[root@master ~]# ping node2
PING node2 (192.168.17.133) 56(84) bytes of data.
64 bytes from node2 (192.168.17.133): icmp_seq=1 ttl=64 time=0.363 ms
64 bytes from node2 (192.168.17.133): icmp_seq=2 ttl=64 time=0.818 ms
64 bytes from node2 (192.168.17.133): icmp_seq=3 ttl=64 time=0.868 ms
64 bytes from node2 (192.168.17.133): icmp_seq=4 ttl=64 time=1.16 ms
64 bytes from node2 (192.168.17.133): icmp_seq=5 ttl=64 time=0.988 ms
--- node2 ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8080ms
rtt min/avg/max/mdev = 0.363/0.876/1.163/0.236 ms
关闭防火墙、SELinux和交换分区:
防火墙和SELINUX已关闭。
配置内核参数,将桥接的IPv4流量传递到iptables链。
配置所有节点上的网络参数,使得各个节点上的网桥再转发数据包时,也交由iptables的相应链来处理。
[root@master ~]# cat /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
[root@master ~]# sysctl --system
* Applying /usr/lib/sysctl.d/00-system.conf ...
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
* Applying /usr/lib/sysctl.d/10-default-yama-scope.conf ...
kernel.yama.ptrace_scope = 0
* Applying /usr/lib/sysctl.d/50-default.conf ...
kernel.sysrq = 16
kernel.core_uses_pid = 1
kernel.kptr_restrict = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.promote_secondaries = 1
net.ipv4.conf.all.promote_secondaries = 1
fs.protected_hardlinks = 1
fs.protected_symlinks = 1
* Applying /etc/sysctl.d/99-sysctl.conf ...
* Applying /etc/sysctl.d/k8s.conf ...
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
* Applying /etc/sysctl.conf ...
配置国内的软件源:
默认,centos是使用国外的软件源来安装软件包,软件包的下载速度会比较慢,为了加快访问速度,改为国内的镜像站点。
直接使用阿里云:
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
使用阿里云的Kubernetes镜像站点作为安装源。
[root@master yum.repos.d]# cat Kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
两个地址之间是空格,不需要换行。不然会报错。
将阿里云的Docker镜像站点作为安装源。
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
软件安装:
开始安装各种软件工具,主要有docker和Kubernetes的各种管理工具。
在每个节点上执行。
[root@master yum.repos.d]# yum install -y docker-ce
然后启动docker,并启动docker服务。
[root@master yum.repos.d]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@master yum.repos.d]# systemctl start docker
然后查看docker的服务状态:
[root@master yum.repos.d]# systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2023-10-06 10:19:47 CST; 55s ago
Docs: https://docs.docker.com
Main PID: 15776 (dockerd)
Tasks: 8
Memory: 27.7M
CGroup: /system.slice/docker.service
└─15776 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Oct 06 10:19:47 master systemd[1]: Starting Docker Application Container Engine...
Oct 06 10:19:47 master dockerd[15776]: time="2023-10-06T10:19:47.100997019+08:00" level=info ...up"
Oct 06 10:19:47 master dockerd[15776]: time="2023-10-06T10:19:47.131734527+08:00" level=info ...t."
Oct 06 10:19:47 master dockerd[15776]: time="2023-10-06T10:19:47.314521219+08:00" level=info ...e."
Oct 06 10:19:47 master dockerd[15776]: time="2023-10-06T10:19:47.329973530+08:00" level=info ...0.6
Oct 06 10:19:47 master dockerd[15776]: time="2023-10-06T10:19:47.330130484+08:00" level=info ...on"
Oct 06 10:19:47 master dockerd[15776]: time="2023-10-06T10:19:47.357335398+08:00" level=info ...ck"
Oct 06 10:19:47 master systemd[1]: Started Docker Application Container Engine.
Hint: Some lines were ellipsized, use -l to show in full.
安装Kubernetes组件:
安装Kubernetes的管理工具:
yum install -y kubelet kubeadm kubectl
kubelet负责与其他节点通信,并负责管理本节点pod和容器的生命周期。
kubeadm是K8s的自动化部署工具,可以降低部署应用的难度,提高效率。
kubectl是K8s集群管理工具。
以上是在各个节点上都要部署。
部署master节点:
然后是集群的部署,这个过程分两步,首先是master节点的部署,然后进行其余工作节点的部署。