k8s 1.25学习1 - 环境准备

服务器准备

请先参考 Windows10安装VirtualBox及Vagrant 安装好VirtualBox及Vagrant

服务器规划
使用5台服务器部署,ip地址如下:

192.168.99.211  k8s-test1
192.168.99.212  k8s-test2
192.168.99.213  k8s-test3
192.168.99.214  k8s-test4
192.168.99.215  k8s-test5

其中k8s-test1/k8s-test2/k8s-test3作为k8s的主节点,其它2台作为node节点


假定Vagrantfile文件的路径在D:\vagrant\k8s
Vagrantfile文件的内容为

Vagrant.configure("2") do |config|
   (1..5).each do |i|
        config.vm.define "k8s-test#{i}" do |node|
            # 设置虚拟机的Box
            node.vm.box = "centos/7"
            node.vm.box_url = "https://mirrors.ustc.edu.cn/centos-cloud/centos/7/vagrant/x86_64/images/CentOS-7.box"

            # 设置虚拟机的主机名
            node.vm.hostname="k8s-test#{i}"

            # 设置虚拟机的IP
            node.vm.network "private_network", ip: "192.168.99.#{210+i}", netmask: "255.255.255.0"

            # VirtaulBox相关配置
            node.vm.provider "virtualbox" do |v|
                # 设置虚拟机的名称
                v.name = "k8s-test#{i}"
                # 设置虚拟机的内存大小。注意:如果电脑内存不够,这里需要设置小一些,否则Vagrant启动时会报错
                v.memory = 6000
                # 设置虚拟机的CPU个数
                v.cpus = 4
            end
        end
   end
end

启动服务器

使用管理员权限打开cmd命令行窗口

cd D:\vagrant\k8s

#启动服务器
vagrant up

连接服务器,配置ssh

vagrant ssh k8s-test1

#切换root用户,默认的密码是vagrant
su

#把PasswordAuthentication no 修改成yes
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config

#重启ssh服务
service sshd restart

#保存,执行2次exit退出k8s-test1服务器

其它服务器使用类似命令 vagrant ssh k8s-test2连接上服务器,做相同的配置


使用Xshell连接5台服务器

使用Xshell用root用户连接上5台服务器
使用Xshell的工具->发送键输入到所有会话,给5台服务器同时执行命令

vi /etc/hosts
#删除这行127.0.1.1 k8s-test1 k8s-test1
192.168.99.211  k8s-test1
192.168.99.212  k8s-test2
192.168.99.213  k8s-test3
192.168.99.214  k8s-test4
192.168.99.215  k8s-test5


#检查5台机器是否能正常通信
ping k8s-test1
ping k8s-test2
ping k8s-test3


#配置阿里云仓库
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
yum -y update

升级操作系统内核

CentOS7.8默认内核版本是3.10,需要升级到4.19
查看版本号 uname -a

#设置源
cat > /etc/yum.repos.d/linnux-kernel.repo <<EOF
[kernel-longterm-4.19]
name=kernel-longterm-4.19
baseurl=https://copr-be.cloud.fedoraproject.org/results/kwizart/kernel-longterm-4.19/epel-7-x86_64/
enabled=1
gpgcheck=0
EOF

#安装
yum install -y kernel-longterm kernel-longterm-devel kernel-longterm-modules-extra

设置启动项并重启

grub2-mkconfig -o /boot/grub2/grub.cfg
grub2-set-default 0
#查看默认内核是不是4.19
grubby --default-kernel
reboot


#再次查看内核版本号
grubby --default-kernel

安装基础软件

yum install -y yum-utils device-mapper-persistent-data lvm2 psmisc vim telnet git
yum install -y conntrack ipvsadm ipset jq sysstat curl libseccomp net-tools nc wget htop


#配置Docker安装源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

#配置kubernetes安装源
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
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

更新时区

timedatectl set-timezone Asia/Shanghai && date && echo 'Asia/Shanghai' > /etc/timezone

#安装ntpdate
rpm -ivh http://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
yum install ntpdate -y

#所有节点同步时间
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ntpdate time2.aliyun.com

# 加入到crontab系统定时任务中
crontab -e
*/5 * * * * /usr/sbin/ntpdate time2.aliyun.com

#查看该用户下的crontab服务是否创建成功
crontab -l 

配置操作系统内核参数

配置limit

ulimit -SHn 65535

vi /etc/security/limits.conf
# 末尾添加如下内容
* soft nofile 65536
* hard nofile 131072
* soft nproc 65535
* hard nproc 655350
* soft memlock unlimited
* hard memlock unlimited

关闭防火墙及swap

#将SELinux 设置为 permissive 模式(相当于将其禁用)
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

#关闭交换分区 free -m 其中swap 要为0
swapoff -a  
sed -ri 's/.*swap.*/#&/' /etc/fstab

#允许 iptables 检查桥接流量
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
br_netfilter
EOF

#使以上配置生效
sudo sysctl --system

配置k8s集群中的内核参数

cat <<EOF > /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
fs.may_detach_mounts = 1
net.ipv4.conf.all.route_localnet = 1
vm.overcommit_memory=1
vm.panic_on_oom=0
fs.inotify.max_user_watches=89100
fs.file-max=52706963
fs.nr_open=52706963
net.netfilter.nf_conntrack_max=2310720

net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_max_tw_buckets = 36000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_max_orphans = 327680
net.ipv4.tcp_orphan_retries = 3
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.ip_conntrack_max = 65536
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_timestamps = 0
net.core.somaxconn = 16384
EOF

sysctl --system

安装ipvsadm查看路由配置

yum install -y ipvsadm ipset sysstat conntrack libseccomp

执行

modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack

加入到启动配置文件中

vi /etc/modules-load.d/ipvs.conf
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_fo
ip_vs_nq
ip_vs_sed
ip_vs_ftp
ip_vs_sh
nf_conntrack
ip_tables
ip_set
xt_set
ipt_set
ipt_rpfilter
ipt_REJECT
ipip

启动并查看状态

systemctl enable --now systemd-modules-load.service
lsmod | grep --color=auto -e ip_vs -e nf_conntrack

安装Containerd

Docker安装文件中已包含了Containerd
安装Docker后,可以无需启动Docker,只需要配置和启动Containerd即可
yum install -y docker-ce-20.10.* docker-ce-cli-20.10.*

配置Containerd所需的模块

cat <<EOF | sudo tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF

加载模块,执行

modprobe -- overlay
modprobe -- br_netfilter

配置Containerd所需的内核参数

cat <<EOF | sudo tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables  = 1
net.ipv4.ip_forward                 = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF

#加载内核参数
sysctl --system

配置Containerd的配置文件

mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml


#将Containerd的Cgroup改为Systemd
vim /etc/containerd/config.toml
#找到containerd.runtimes.runc.options,修改
SystemdCgroup = true

#sandbox_image的Pause镜像改成registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6
sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.6"

启动Containerd

systemctl daemon-reload
systemctl enable --now containerd

配置crictl客户端连接的运行时位置

cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF

查看镜像

ctr image ls

配置ssh免密钥登录

在k8s-test1配置ssh免密钥登录其他节点(只在k8s-test1上执行)

ssh-keygen -t rsa		#回车2次

#拷贝公钥到其它服务器,需要输入密码
for i in k8s-test1 k8s-test2 k8s-test3 k8s-test4 k8s-test5;do ssh-copy-id -i .ssh/id_rsa.pub $i;done

#测试ssh
ssh k8s-test5
exit
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GalenZhang888

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值