约 3 分钟阅读
云计算

使用Vagrant搭建本地K8S集群

前端时间利用Vagrant搭建K8S集群由于Vagrant eth0网卡的问题没有成功,这里重新尝试使用Vagrant安装。

使用Vagrant搭建本地K8S集群
SYSTEM更新于 Apr 9, 2024
74 次阅读约 3 分钟阅读
本文目录大纲

环境准备

一、虚拟机

IPCPU核心内存操作系统hostname节点类型
172.17.1.1122GCentOS 7k8s-c1Control-plane
172.17.1.1222GCentOS 7k8s-c2Control-plane
172.17.1.1322GCentOS 7k8s-c3Control-plane
172.17.1.2122GCentOS 7k8s-w1Node
172.17.1.2222GCentOS 7k8s-w2Node
172.17.1.2324GCentOS 7k8s-w3Node

这里使用 Vagrantfile 如下

BASH
DOCKER_VER = "19.03.11"
K8S_VER = "1.20.2"

init_script = <<SCRIPT
#!/usr/bin/env bash

sudo yum install -y net-tools wget

# 打开password登录root:vagrant
sudo sed -i 's|PasswordAuthentication no|#PasswordAuthentication no|' /etc/ssh/sshd_config
sudo sed -i 's|#PasswordAuthentication yes|PasswordAuthentication yes|' /etc/ssh/sshd_config
sudo systemctl restart sshd
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.box_check_update = false

  config.vm.provider "virtualbox" do |vb|
    vb.cpus = 2
    vb.memory = "2048"
  end

  (1..3).each do |n|
    config.vm.define "k8s-c#{n}" do |master|
      master.vm.network "private_network", ip: "172.17.1.1#{n}"
      master.vm.hostname = "k8s-c#{n}"
    end
  end

  (1..3).each do |n|
    config.vm.define "k8s-w#{n}" do |node|
      node.vm.network "private_network", ip: "172.17.1.2#{n}"
      node.vm.hostname = "k8s-w#{n}"
    end
  end

  config.ssh.forward_agent = true
  config.vm.provision "shell", inline: init_script

  config.vm.provision "shell", path: "prepare.sh", args: [DOCKER_VER, K8S_VER]

end

二、配置K8S安装基本环境及安装 kubeadm

BASH
#!/usr/bin/env bash

##################################################
## prepare.sh {DOCKER_VER} {K8S_VER}
#################################################

DOCKER_VER=${1}
K8S_VER=${2}

# 切换到root
sudo su

# 修改Hostname
# 配置/etc/hosts根据hostname通信

# 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld

# 关闭SeLinux
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

# 关闭Swap
swapoff -a
sed -i '/swap/s/^/#/g' /etc/fstab

# 配置br_netfilter模块
cat > /etc/rc.sysinit << EOF
#!/bin/bash
for file in /etc/sysconfig/modules/*.modules ; do
[ -x $file ] && $file
done
EOF
cat > /etc/sysconfig/modules/br_netfilter.modules << EOF
modprobe br_netfilter
EOF
chmod 755 /etc/sysconfig/modules/br_netfilter.modules

cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness = 0
EOF
sysctl --system

# 配置IPVS
# kube-proxy 使用 ipvs 方式负载 ,所以需要内核加载 ipvs 模块, 否则只会使用 iptables 方式
cat > /etc/sysconfig/modules/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack_ipv4
EOF
# 授权可执行权限
chmod 755 /etc/sysconfig/modules/ipvs.modules 
# 加载模块
bash /etc/sysconfig/modules/ipvs.modules
# 查看加载
lsmod | grep -e ip_vs -e nf_conntrack_ipv4

# Docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum update -y && yum install -y containerd.io-1.2.13 docker-ce-19.03.11 docker-ce-cli-19.03.11

mkdir /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ],
  "registry-mirrors": ["https://2nfdeci6.mirror.aliyuncs.com"]
}
EOF

mkdir -p /etc/systemd/system/docker.service.d
systemctl daemon-reload
systemctl restart docker
systemctl enable docker

# 安装kubeadm,kubelet,kubectl
# cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
# [kubernetes]
# name=Kubernetes
# baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-\$basearch
# enabled=1
# gpgcheck=1
# repo_gpgcheck=1
# gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
# exclude=kubelet kubeadm kubectl
# EOF

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-\$basearch
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
exclude=kubelet kubeadm kubectl
EOF

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
systemctl enable --now kubelet

# 配置 kubectl 命令补全
# 安装 bash-completion
yum -y install bash-completion

# Linux 默认脚本路径为 /usr/share/bash-completion/bash_completion
# 配置 bashrc
# vi ~/.bashrc
# 添加如下:
# kubectl
# source /usr/share/bash-completion/bash_completion
# source <(kubectl completion bash)
echo 'source /usr/share/bash-completion/bash_completion' >> ~/.bashrc
echo 'source <(kubectl completion bash)' >> ~/.bashrc
# 生效配置
source ~/.bashrc

评论交流

暂无评论,欢迎分享您的见解