【k8s】环境搭建一、环境初始化

描述:

    k8s栏目文章是笔者的学习笔记,文章内容深度级别是初级。

    学习的教程是【黑马程序员k8s全套教程】,链接如下,感兴趣的读者可以直接观看原版视频。https://www.bilibili.com/video/BV1Qv41167ck?p=6&vd_source=9ed866526704f42028462f8b3d5ea2b1

搭建步骤:

1、主机规划

    本次环境搭建需要3台Centos服务器(一主两从),然后在每台服务器上安装:

    1)docker:18.06.3

    2)kubeadm:1.17.4

    3)kubelet:1.17.4

    4)kubectl:1.17.4

作用IP地址操作系统配置
Master192.168.254.145CentOS Linux release 7.8.2003 (Core)2颗CPU 2G内存
Node1192.168.254.160CentOS Linux release 7.8.2003 (Core)2颗CPU 2G内存
Node2192.168.254.161CentOS Linux release 7.8.2003 (Core)2颗CPU 2G内存

2、环境初始化

1)检查操作系统的版本

    k8s集群要求操作系统在7.5或以上

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.8.2003 (Core)
[root@localhost ~]# 

2)主机名解析

[root@localhost ~]# echo "192.168.254.145 master" >> /etc/hosts
[root@localhost ~]# echo "192.168.254.160 node1" >> /etc/hosts
[root@localhost ~]# echo "192.168.254.161 node2" >> /etc/hosts
[root@localhost ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.254.145 master
192.168.254.160 node1
192.168.254.161 node2
[root@localhost ~]#

3)时间同步

    k8s要求集群中节点的时间必须一致,这里直接使用chronyd服务从网络同步时间。

    企业中建议配置内部的时间同步服务器。

[root@localhost ~]# systemctl start chronyd
[root@localhost ~]# systemctl enable chronyd
[root@localhost ~]# date
2022年 09月 15日 星期四 22:53:00 CST
[root@localhost ~]# 

4)禁用iptables和firewalld服务

    k8s和docker在运行中会产生大量的iptables规则,为了不让系统规则和他们混淆,直接关闭系统的规则。

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# 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@localhost ~]# systemctl stop iptables
Failed to stop iptables.service: Unit iptables.service not loaded.
[root@localhost ~]# systemctl disable iptables
Failed to execute operation: No such file or directory
[root@localhost ~]# 

5)禁用selinux

    selinux是linux的安全服务,如果不关闭它,在安装集群的过程中会出现各种预想外异常。

[root@localhost ~]# vi /etc/selinux/config


# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

6)禁用swap分区

    swap分区是指虚拟内存分区,它的作用是物理内存使用完成后,将磁盘空间作为内存使用。

    由于k8s集群对内存有要求,不建议使用swap,如果必须使用swap,需要在集群安装过程中通过明确的参数进行配置说明。

    修改完成后,需要重启linux,后续步骤还有需要重启的,待完成后一起重启。

[root@localhost ~]# vi /etc/fstab


#
# /etc/fstab
# Created by anaconda on Sat Aug 13 06:09:45 2022
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=7d5da407-7fbc-4da9-8359-bba8d384673e /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

7)修改linux内核参数

[root@localhost ~]# vi /etc/sysctl.d/kubernetes.conf
#修改linux内核参数,添加网桥过滤和地址转发功能
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
[root@localhost ~]# sysctl -p    #重新加载内核参数
#加载网桥过滤模块
[root@localhost ~]# modprobe br_netfilter
#查看网桥过滤模块是否加载成功
[root@localhost ~]# lsmod|grep br_netfilter
br_netfilter           22256  0 
bridge                151336  1 br_netfilter
[root@localhost ~]#

8)配置ipvs功能

    在k8s中service有两种代理模型,一种是基于iptables的,一种是基于ipvs

    两者比较,ipvs性能要高一些,但是如果要使用它,需要手动加载ipvs模块

#1、安装ipset和ipvsadm
[root@localhost ~]# yum install ipset ipvsadmin -y

#2、添加需要加载的模块写入脚本文件
cat <<EOF > /etc/sysconfig/modules/ipvs.modules
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF

#3、为脚本文件添加执行权限
[root@localhost ~]# chmod +x /etc/sysconfig/modules/ipvs.modules

#4、执行脚本文件
[root@localhost ~]# bash /etc/sysconfig/modules/ipvs.modules

#5、查看对应的模块是否加载成功
[root@localhost ~]# lsmod|grep -e ip_vs -e nf_conntrack_ipv4
nf_conntrack_ipv4      15053  0 
nf_defrag_ipv4         12729  1 nf_conntrack_ipv4
ip_vs_sh               12688  0 
ip_vs_wrr              12697  0 
ip_vs_rr               12600  0 
ip_vs                 145497  6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
nf_conntrack          139264  2 ip_vs,nf_conntrack_ipv4
libcrc32c              12644  3 xfs,ip_vs,nf_conntrac
[root@localhost ~]#

9)重启服务器

    上面步骤完成后,需要重启linux系统

10)重启后检查

#1、检查selinux是否关闭
[root@master ~]# getenforce
Disabled

#2、检查swap是否正常
[root@master ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1819         120        1575           9         123        1559
Swap:             0           0           0
[root@master ~]#

day1学习完P7

问题:

1、nf_conntrack_ipv4不存在

    在网上找到教程安装K8S需要配置ipvs功能,但是在进行配置时会报错modprobe: FATAL: Module nf_conntrack_ipv4 not found.
    这是因为使用了高内核,较如博主就是使用了5.2的内核,一般教程都是3.2的内核。在高版本内核已经把nf_conntrack_ipv4替换为nf_conntrack了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值