mmumshad / kubernetes-the-hard-way

Bootstrap Kubernetes the hard way on Vagrant on Local Machine. No scripts.
Apache License 2.0
4.7k stars 4.54k forks source link

Proposed solution for Vagrant support on Mac M1 #273

Closed akinsella closed 8 months ago

akinsella commented 2 years ago

Posting here some solution to get Vagrant work on Mac M1 (As more and more Mac machines are M1 CPUs based). Virtualbox is not supported at the time on M1 chips. Options are WMWare Fusion or Parallels.

Introduction notice: Provided instructions may not be fully exhaustive, however all required elements should be there to manage to get a working solution for Mac M1 ... It works on my machine ;) ...

I tried with success an installation based on Vagrant + WMWare Fusion: There is a WMWare Fusion Tech Preview available that you can install as a replacement of VirtualBox ( https://blogs.vmware.com/teamfusion/2021/09/fusion-for-m1-public-tech-preview-now-available.html ). As Parallels now supports Mac M1 chips, and that a updated provided also supports Parallels on Mac M1, you should be able to get it work with Parallels.

After VMWare Fusion installation, you have to install Vagrant with brew ( brew install vagrant / At time v2.2.19 has a defect, therefore, you have to install Vagrant 2.2.18 ).

Then, you also have to install the Vagrant vmware provider:

vagrant plugin install vagrant-vmware-desktop

Finally, I adapted Vagrantfile from [https://github.com/mmumshad/certified-kubernetes-administrator-course](Certified Kubernetes Administrator course) as an example of a working configuration:

# -*- mode: ruby -*-
# vi:set ft=ruby sw=2 ts=2 sts=2:

# Define the number of master and worker nodes
# If this number is changed, remember to update setup-hosts.sh script with the new hosts IP details in /etc/hosts of each VM.
NUM_MASTER_NODE = 1
NUM_WORKER_NODE = 2

IP_NW = "192.168.56."
MASTER_IP_START = 1
NODE_IP_START = 2

# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
  # The most common configuration options are documented and commented below.
  # For a complete reference, please see the online documentation at
  # https://docs.vagrantup.com.

  # Every Vagrant development environment requires a box. You can search for
  # boxes at https://vagrantcloud.com/search.
  # config.vm.box = "base"
  config.vm.box = "spox/ubuntu-arm"

  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  config.vm.box_check_update = false

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  # config.vm.network "public_network"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  # config.vm.synced_folder "../data", "/vagrant_data"

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  # config.vm.provider "virtualbox" do |vb|
  #   # Customize the amount of memory on the VM:
  #   vb.memory = "1024"
  # end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  # Provision Master Nodes
  (1..NUM_MASTER_NODE).each do |i|
      config.vm.define "kubemaster" do |node|
        # Name shown in the GUI
        node.vm.provider "vmware_desktop" do |vb|
#            vb.name = "kubemaster"
#            vb.memory = 2048
#            vb.cpus = 2
          vb.vmx["displayName"] = "kubemaster"
          vb.vmx["memsize"] = "2048"
          vb.vmx["numvcpus"] = "2"
          vb.vmx["ethernet0.pcislotnumber"] = "160"
        end
        node.vm.hostname = "kubemaster"
        node.vm.network :private_network, ip: IP_NW + "#{MASTER_IP_START + i}"
        node.vm.network "forwarded_port", guest: 22, host: "#{2710 + i}"

        node.vm.provision "setup-hosts", :type => "shell", :path => "ubuntu/vagrant/setup-hosts.sh" do |s|
          s.args = ["enp0s8"]
        end

        node.vm.provision "setup-dns", type: "shell", :path => "ubuntu/update-dns.sh"

      end
  end

  # Provision Worker Nodes
  (1..NUM_WORKER_NODE).each do |i|
    config.vm.define "kubenode0#{i}" do |node|
        node.vm.provider "vmware_desktop" do |vb|
          vb.vmx["displayName"] = "kubenode0#{i}"
          vb.vmx["memsize"] = "2048"
          vb.vmx["numvcpus"] = "2"

          # Fix
          vb.vmx["ethernet0.pcislotnumber"] = "160"
        end
        node.vm.hostname = "kubenode0#{i}"
        node.vm.network :private_network, ip: IP_NW + "#{NODE_IP_START + i}"
        node.vm.network "forwarded_port", guest: 22, host: "#{2720 + i}"

        node.vm.provision "setup-hosts", :type => "shell", :path => "ubuntu/vagrant/setup-hosts.sh" do |s|
          s.args = ["enp0s8"]
        end

        node.vm.provision "setup-dns", type: "shell", :path => "ubuntu/update-dns.sh"
    end
  end
end

Then, you can run:

vagrant up

A great resource to fix any issue at installation:

https://gist.github.com/sbailliez/f22db6434ac84eccb6d3c8833c85ad92

You may have to run Vagran up/ Vagran down/ Vagran destroy a few times to overcome some blocking situation.

Thanks to @sbailliez and @fatso83 that paved the way to the solution.

akinsella commented 2 years ago

I created a Gist for easier reference and with few improvements:

fireflycons commented 2 years ago

Hi @akinsella

This is great work! We have now merged a major change to bring this course to v1.24. Would you please verify this against the new version, make any changes required, then ping me back so that we can see how this may be incorporated into this repo (for which of course we'll name all involved in a list of credits on the main README for this site).

robel-yemane commented 1 year ago

Hi @akinsella thanks for the workaround. Is this an option that would work for the M2 and is there any update for v1.24? Much appreaciated.

fireflycons commented 8 months ago

There is an Apple Silicon version of this coming soon