一k8s基础环境搭建

卧疾丰暇豫,翰墨时间作。这篇文章主要讲述一k8s基础环境搭建相关的知识,希望能为你提供帮助。
一、概述
搭建一台机器,配置好防火墙、selinux、swap、内核参数、yum源等,然后克隆出三台机器,再拍快照,方便反复练习
二、步骤
【一k8s基础环境搭建】虚拟机设置

1、配置主机之间免密登录

ssh-keygen ##一路回车就好
cd /root/.ssh & & mv id_rsa.pub authorized_keys ##克隆之后每台虚拟机都存在authorized_keys这个文件

2、关闭firewalld防火墙
systemctl stop firewalld ; systemctl disable firewalld

3、关闭selinux
sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config

4、修改每台机器的/etc/hosts文件
192.168.10.131 k8s01
192.168.10.132 k8s02
192.168.10.133 k8s03
192.168.10.134 k8s04

5、关闭交换分区swap
swapoff -a
永久关闭:备份并注释掉
sed -i.bak s/^.*centos-swap/#& /g /etc/fstab

6、修改内核参数
modprobe br_netfilter ##临时更改
lsmod|grep br_netfilter ##查看

echo "modprobe br_netfilter" > > /etc/profile

cat > /etc/sysctl.d/k8s.conf < < EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sysctl -p /etc/sysctl.d/k8s.conf

7、备份配置yum源
mkdir /root/repo.bak
mv /etc/yum.repos.d/* /root/repo.bak/
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
配置docker源
yum install -y yum-utils
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
配置k8s源
cat < < EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF

8、配置时间同步
yum install ntpdate -y
ntpdate cn.pool.ntp.org
#把时间同步做成计划任务
crontab -e
* */1 * * * /usr/sbin/ntpdatecn.pool.ntp.org
#重启crond服务
service crond restart

9、安装iptables
yum install iptables-services -y
禁用iptables
service iptables stop& & systemctl disable iptables
清空防火墙规则
iptables -F

10、开启ipvs
cat > /etc/sysconfig/modules/ipvs.modules < < EOF
#!/bin/bash
ipvs_modules="ip_vs ip_vs_lc ip_vs_wlc ip_vs_rr ip_vs_wrr ip_vs_lblc ip_vs_lblcr ip_vs_dh ip_vs_sh ip_vs_nq ip_vs_sed ip_vs_ftp nf_conntrack"
for kernel_module in \\${ipvs_modules}; do
/sbin/modinfo -F filename \\${kernel_module} > /dev/null 2> & 1
if [ $? -eq 0 ]; then
/sbin/modprobe \\${kernel_module}
fi
done
EOF
chmod 755 /etc/sysconfig/modules/ipvs.modules & & bash /etc/sysconfig/modules/ipvs.modules & & lsmod | grep ip_vs

11、安装基础软件包
yum install -y yum-utils device-mapper-persistent-data lvm2 wget net-tools nfs-utils lrzsz gcc gcc-c++ make cmake libxml2-devel openssl-devel \\
curl curl-devel unzip sudo ntp libaio-devel wget vim ncurses-devel autoconf automake zlib-develpython-devel epel-release openssh-server socat ipvsadm conntrack

12、安装docker-ce
yum install docker-ce docker-ce-cli containerd.io -y
systemctl start docker & & systemctl enable docker.service

cat > /etc/docker/daemon.json < < EOF
{
"registry-mirrors": ["https://阿里云镜像加速地址"],
"exec-opts": ["native.cgroupdriver=systemd"] #修改docker文件驱动为systemd,默认为cgroupfs,kubelet默认使用systemd,两者必须一致才可以。
}
EOF

systemctl daemon-reload & & systemctl restart docker & & systemctl status docker

13、虚拟机克隆并额外设置

我这里网络使用的是dhcp自动分配ip
挨个启动克隆的虚拟机,更改主机名和测试主机间免密连接
hostnamectl set-hostname k8s01 & & bash
hostnamectl set-hostname k8s02 & & bash
hostnamectl set-hostname k8s03 & & bash
hostnamectl set-hostname k8s04 & & bash
k8s01额外设置###k8s01连接其他可以免密,但是ssh自己还是得输入mima
k8s01额外操作:cd /root/.ssh & & cp authorized_keys id_rsa.pub
ssh root@192.168.10.131
ssh root@192.168.10.132
ssh root@192.168.10.133
ssh root@192.168.10.134
四个服务器做好快照

    推荐阅读