虚拟机集群搭建

作为热身, 首先构建一个单台的centos8虚拟机:

step1:

$ 初始化方式1: 直接在线下载box文件并初始化

vagrant init centos8 https://mirrors.ustc.edu.cn/centos-cloud/centos/8/vagrant/x86_64/images/CentOS-8-Vagrant-8.0.1905-1.x86_64.vagrant-virtualbox.box

$ 初始化方式2: box文件下载到本地, 初始化

vagrant init CentOS-8-Vagrant-8.0.1905-1.x86_64.vagrant-virtualbox.box

step2:

vagrant up

step3:

修改ip地址,在该文件夹下找到vagrantfile,此处的ip地址对应VirtualBox的"主机网络管理器"的ip网段

config.vm.network "private_network", ip: "192.168.56.10"

该ip地址要和宿主机在同一个网段

接下来尝试为k8s构建一个集群

  1. 在.box镜像文件当前目录中执行如下命令,把下载的镜像文件添加到vagrant box列表中
vagrant box add CentOS-8-Vagrant-8.0.1905-1.x86_64.vagrant-virtualbox.box --name centos/8
  1. Vagrantfile的内容说明
Vagrant.configure("2") do |config| #最外层的集群实例
  config.vm.define "k8s-master-01" do |master_01| #集群中的节点实例
    master_01.vm.box = "centos/8" #指定使用哪个镜像包
    master_01.vm.hostname = "k8s-master-01"
    master_01.vm.network "private_network", ip: "192.168.56.31"
    master_01.vm.provider "virtualbox" do |v| #分配资源
      v.memory = 2048
      v.cpus = 2
    end
  end
end
  1. 完整的Vagrantfile:
Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: "echo Hello"
  config.vm.network "public_network", bridge: "en0: Wi-Fi (Wireless)"

  config.vm.define "k8s-master-01" do |master_01|
    master_01.vm.box = "centos/8"
    master_01.vm.hostname = "k8s-master-01"
    master_01.vm.network "private_network", ip: "192.168.56.31"
    master_01.vm.provider "virtualbox" do |v|
      v.memory = 2048
      v.cpus = 2
    end
  end

  config.vm.define "k8s-worker-01" do |worker_01|
    worker_01.vm.box = "centos/8"
    worker_01.vm.hostname = "k8s-worker-01"
    worker_01.vm.network "private_network", ip: "192.168.56.32"
    worker_01.vm.provider "virtualbox" do |v|
      v.memory = 2048
      v.cpus = 2
    end
  end

  config.vm.define "k8s-worker-02" do |worker_02|
    worker_02.vm.box = "centos/8"
    worker_02.vm.hostname = "k8s-worker-02"
    worker_02.vm.network "private_network", ip: "192.168.56.33"
    worker_02.vm.provider "virtualbox" do |v|
      v.memory = 2048
      v.cpus = 2
    end
  end

  config.vm.define "k8s-docker-register" do |docker|
    docker.vm.box = "centos/8"
    docker.vm.hostname = "k8s-docker-register"
    docker.vm.network "private_network", ip: "192.168.56.30"
    docker.vm.provider "virtualbox" do |v|
      v.memory = 1024
      v.cpus = 2
    end
  end
end
  1. 在c:\Windows\System32\drivers\etc\hosts 文件中添加
    192.168.56.31 k8s-master-01
    192.168.56.32 k8s-worker-01
    192.168.56.33 k8s-worker-02
    192.168.56.30 k8s-docker-register

5.shh登入集群中的节点

vagrant ssh k8s-master-01
vagrant ssh k8s-worker-01
vagrant ssh k8s-worker-02
vagrant ssh k8s-docker-register

然后用ping命令检查节点之间是否能够联通

安装各个节点

未完…

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐