好久没有操作过k8s了,自从离开了大厂也没有接触k8s的机会了,正好最近有朋友打听k8s相关的事情,这个文章也是自己根据自己脑子里面的逻辑来安装部署的,其实k8s非常简单的部署,比较坑的地方都是一些细节方面的问题,,比如说swap没关,kubelet怎么也启动不起来等等问题,在这里声明一下哈,k8s我也是只懂了一些皮毛,再加上自己还会那么一点点英文,看的懂官方文档,其实就这了。这里将用二进制的方式来进行安装和部署。自己也是一步一步摸索,更新的饿可能有点慢,大家不要介意哈。
集群角色 | 系统版本 | cpu | mem | ip |
---|---|---|---|---|
k8s-master01 | centos7.5 | 2c | 2G | 192.168.100.13 |
k8s-node01 | centos7.5 | 2c | 2G | 192.168.100.14 |
k8s-node02 | centos7.5 | 2c | 2G | 192.168.100.15 |
基础环境这边,稍微的做一下介绍哈,首先集群角色也就是集群中的各个角色的主机名,为什么有个master01,那么02跑哪里去了,这个是为了之后扩容master02做准备的。还有就是操作系统的话,用centos7.x就行了,我也是看了好多的文章,上面都是要升级内核啥的,但是这个好像关系不是很大,基本上centos7.x的内核基本上就够用了,还有就是我们有了最进出的系统,还得yum -y upodate,让他升级一下系统。cpu和mem没有上限,当然是越多越好,但是至少的2c+和2G+,对服务器是物理服务器还是虚拟服务器,这个也是没有要求的,小编不可能为了写这个文章,自己还得掏腰包去买三台服务器是吧,还有就是后面的ip地址,最好是静态的ip,因为这个小编就吃过亏,为什么呢,因为小编为了省劲,直接用的是dhcp,本来想着,只要虚拟机不关机,哪怕它关机了,短时间内也是没事的,但是就偏偏
##所有参与集群的机器都要升级一下哈,如果没有k8s二进制安装包的话,建议让服务器可以上网下载,等操练熟练了,有相应的二进制包了,再离线进行安装
[root@localhost ~]# yum -y update
##master上操作修改主机名
[root@localhost ~]# hostnamectl set-hostname k8s-master01
[root@localhost ~]# bash
bash
[root@k8s-master01 ~]#
##node01上面执行修改主机名
[root@localhost ~]# hostnamectl set-hostname k8s-node01
[root@localhost ~]# bash
bash
[root@k8s-node01 ~]#
##node02上面执行修改主机名
[root@localhost ~]# hostnamectl set-hostname k8s-node02
[root@localhost ~]# bash
[root@k8s-node02 ~]#
## 修改master网络配置文件,更改192.168.100.13静态地址,修改完需要重启一下网卡
[root@k8s-master01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.13
GATEWAY=192.168.100.2
NETMASK=255.255.255.0
DNS1=192.168.100.2 #这里用的是网关做的dns,如果用联通什么之类的dns,会在解析上耗时间,给人的感觉就是反应慢
[root@k8s-master01 ~]# systemctl restart network #重启网卡,注意:这边重启完之后xshell等远程工具会卡住,那是因为你更改了ip地址,你需要重新进行xshell连接
##node01上操作
[root@k8s-node01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.14
GATEWAY=192.168.100.2
NETMASK=255.255.255.0
DNS1=192.168.100.2
[root@k8s-node01 ~]# systemctl restart network #重启网卡,注意:这边重启完之后xshell等远程工具会卡住,那是因为你更改了ip地址,你需要重新进行xshell连接
##node02上操作
[root@k8s-node02 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=none
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.15
GATEWAY=192.168.100.2
NETMASK=255.255.255.0
DNS1=192.168.100.2
[root@k8s-node02 ~]# systemctl restart network #重启网卡,注意:这边重启完之后xshell等远程工具会卡住,那是因为你更改了ip地址,你需要重新进行xshell连接
咱们这里用的是master01做的时间同步服务器,集群里面其他的服务器都找master来同步时间,如果之后要加入新的服务器的话,也是需要配置时间同步服务器的。
[root@k8s-master01 ~]# rpm -qa|grep ntp ##查看服务器上面是否安装了ntp服务,返回为空就代表着没有
[root@k8s-master01 ~]# yum install ntp ntpdate -y ##通过yum安装所需服务,注意:如果你之后还要离线安装的话,建议你用yum将ntp ntpdate这两个包给偷下来进行安装,还有他们的依赖服务哈
[root@k8s-master01 ~]# systemctl start ntpd #启动,这里少了一步,加入开机启动,这个自己百度吧,很简单的哟
[root@k8s-master01 ~]# systemctl status ntpd
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2021-04-09 16:58:50 CST; 6s ago
Process: 9882 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 9883 (ntpd)
CGroup: /system.slice/ntpd.service
└─9883 /usr/sbin/ntpd -u ntp:ntp -g
4月 09 16:58:50 k8s-master01 ntpd[9883]: Listen and drop on 0 v4wildcard 0.0.0.0 UDP 123
4月 09 16:58:50 k8s-master01 ntpd[9883]: Listen and drop on 1 v6wildcard :: UDP 123
4月 09 16:58:50 k8s-master01 ntpd[9883]: Listen normally on 2 lo 127.0.0.1 UDP 123
4月 09 16:58:50 k8s-master01 ntpd[9883]: Listen normally on 3 ens33 192.168.100.13 UDP 123
4月 09 16:58:50 k8s-master01 ntpd[9883]: Listen normally on 4 lo ::1 UDP 123
4月 09 16:58:50 k8s-master01 ntpd[9883]: Listen normally on 5 ens33 fe80::20c:29ff:fefb:d7e0 UDP 123
4月 09 16:58:50 k8s-master01 ntpd[9883]: Listening on routing socket on fd #22 for interface updates
4月 09 16:58:50 k8s-master01 ntpd[9883]: 0.0.0.0 c016 06 restart
4月 09 16:58:50 k8s-master01 ntpd[9883]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
4月 09 16:58:50 k8s-master01 ntpd[9883]: 0.0.0.0 c011 01 freq_not_set
[root@k8s-master01 ~]# rpm -qa | grep ntp ##这次再看一下就会发现有这两个包了就
ntp-4.2.6p5-29.el7.centos.2.x86_64
ntpdate-4.2.6p5-29.el7.centos.2.x86_64
##同样在node01上安装ntp服务,不一样的是,它需要配置nto配置文件,告诉它找谁同步时间
[root@k8s-node01 ~]# yum install ntp ntpdate -y
##修改配置文件
[root@k8s-node01 ~]# vim /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst #注释掉
#server 1.centos.pool.ntp.org iburst #注释掉
#server 2.centos.pool.ntp.org iburst #注释掉
#server 3.centos.pool.ntp.org iburst #注释掉
server 192.168.100.13 iburst #新添加,告诉主机找谁同步时间
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
[root@k8s-node01 ~]# systemctl start ntpd #开启服务,这里也忘了加入开机自启动,小伙伴自己添加吧
[root@k8s-node01 ~]# ntpq -p #测试一下同步
remote refid st t when poll reach delay offset jitter
==============================================================================
192.168.100.13 .INIT. 16 u - 64 0 0.000 0.000 0.000
##node02上面与node01上面同样的步骤
[root@k8s-node02 ~]# yum install ntp ntpdate -y
[root@k8s-node02 ~]# vim /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default nomodify notrap nopeer noquery
# Permit all access over the loopback interface. This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1
restrict ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server 192.168.100.13 iburst
#broadcast 192.168.1.255 autokey # broadcast server
#broadcastclient # broadcast client
#broadcast 224.0.1.1 autokey # multicast server
#multicastclient 224.0.1.1 # multicast client
#manycastserver 239.255.254.254 # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
[root@k8s-node02 ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
192.168.100.13 .INIT. 16 u - 64 0 0.000 0.000 0.000
##master01上面操作,与node01和node02上面是一样的
[root@k8s-master01 ~]# systemctl stop firewalld #关闭防火墙
[root@k8s-master01 ~]# systemctl disable firewalld #关闭防火墙开机自启动
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@k8s-master01 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config #永久性的关闭selinux,但是这个需要重启,下面的命令可以不重启服务器实现关闭selinux
[root@k8s-master01 ~]# setenforce 0 #临时关闭selinux
[root@k8s-master01 ~]# swapoff -a #临时关闭swap分区,这个是k8s一直依赖要求的
[root@k8s-master01 ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab #删除fstab文件里面的挂载,已达到永久关闭的特效
##在node01上面操作,和上面master01一样
[root@k8s-node01 ~]# systemctl stop firewalld
[root@k8s-node01 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@k8s-node01 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
[root@k8s-node01 ~]# setenforce 0
[root@k8s-node01 ~]# swapoff -a
[root@k8s-node01 ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab
##在node02上操作,和上面的一致
[root@k8s-node02 ~]# systemctl stop firewalld
[root@k8s-node02 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@k8s-node02 ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
[root@k8s-node02 ~]# setenforce 0
[root@k8s-node02 ~]# swapoff -a
[root@k8s-node02 ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab
#修改其中的一个,然后通过scp命令进行下发到其他的服务器上面,已达到修改目的
[root@k8s-master01 ~]# cat > /etc/hosts << EOF
> 192.168.100.13 k8s-master01
> 192.168.100.14 k8s-node01
> 192.168.100.15 k8s-node02
> EOF
[root@k8s-master01 ~]# cat /etc/hosts
192.168.100.13 k8s-master01
192.168.100.14 k8s-node01
192.168.100.15 k8s-node02
[root@k8s-master01 ~]# scp /etc/hosts k8s-node01:/etc/hosts #scp给node01
The authenticity of host 'k8s-node01 (192.168.100.14)' can't be established.
ECDSA key fingerprint is SHA256:o98cQWSKlxj3FYKpIcckFsAsb3+hRJ9w+DQThSbUUks.
ECDSA key fingerprint is MD5:9d:ee:d4:8e:1d:02:be:c9:ba:5f:15:51:99:3a:ed:97.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'k8s-node01,192.168.100.14' (ECDSA) to the list of known hosts.
root@k8s-node01's password:
hosts 100% 80 107.0KB/s 00:00
[root@k8s-master01 ~]# scp /etc/hosts k8s-node02:/etc/hosts #scp给node02
The authenticity of host 'k8s-node02 (192.168.100.15)' can't be established.
ECDSA key fingerprint is SHA256:o98cQWSKlxj3FYKpIcckFsAsb3+hRJ9w+DQThSbUUks.
ECDSA key fingerprint is MD5:9d:ee:d4:8e:1d:02:be:c9:ba:5f:15:51:99:3a:ed:97.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'k8s-node02,192.168.100.15' (ECDSA) to the list of known hosts.
root@k8s-node02's password:
hosts 100% 80 76.3KB/s 00:00
[root@k8s-master01 ~]#
#集群中所有的服务器都要操作哈
[root@k8s-master01 ~]# cat > /etc/sysctl.d/k8s.conf << EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@k8s-master01 ~]# sysctl --system
* Applying /usr/lib/sysctl.d/00-system.conf ...
* 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 ...
* Applying /etc/sysctl.conf ...
[root@k8s-node01 ~]# cat > /etc/sysctl.d/k8s.conf << EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@k8s-node01 ~]# sysctl --system
* Applying /usr/lib/sysctl.d/00-system.conf ...
* 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 ...
* Applying /etc/sysctl.conf ...
[root@k8s-node02 ~]# cat > /etc/sysctl.d/k8s.conf << EOF
> net.bridge.bridge-nf-call-ip6tables = 1
> net.bridge.bridge-nf-call-iptables = 1
> EOF
[root@k8s-node02 ~]# sysctl --system
* Applying /usr/lib/sysctl.d/00-system.conf ...
* 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 ...
* Applying /etc/sysctl.conf ...
到此,基础环境算是部署完毕了,等着小编下篇文章吧
下一篇:kubernetes v1.20项目之部署etcd集群