Vagrantfile

1 개요[ | ]

Vagrantfile
베이그런트파일
  • 프로젝트 단위로 Vagrant 설정하는 ruby 파일
  • 루비 문법이지만, 루비 프로그래밍 지식이 많이 요구되지는 않음

2 예시 1[ | ]

offerings = {
	'1c0.5g' => { :cpus => 1, :memory => 512 },
	'1c1g' => { :cpus => 1, :memory => 1024 }
}
default = {
	:box => 'ubuntu/xenial64',
	:offering => '1C0.5G',
	:inline => <<-SCRIPT
sudo sed -i 's/^PermitRootLogin .*/PermitRootLogin yes/g' /etc/ssh/sshd_config
sudo sed -i 's/^PasswordAuthentication .*/PasswordAuthentication yes/g' /etc/ssh/sshd_config
sudo systemctl restart sshd
sudo echo 'root:root' | chpasswd
SCRIPT,
	:inline_extra => ''
}
nodes = [
	{ :hostname => 'host1', :ip => '192.168.0.11' :box => 'centos/7' :offerings => '1c1g' },
	{ :hostname => 'host2', :ip => '192.168.0.12' },
	{ :hostname => 'host3', :ip => '192.168.0.13' },
	{ :hostname => 'host4', :ip => '192.168.0.14' }
]
Vagrant.configure("2") do |config|
	nodes.each do |node|
		config.vm.define node[:hostname] do |c|
			box = node[:box] ? node[:box] : default[:box]
			offering = node[:offering] ? node[:offering] : default[:offering]
			inline = node[:inline] ? node[:inline] : default[:inline]
			inline_extra = node[:inline_extra] ? node[:inline_extra] : default[:inline_extra]

			c.ssh.shell = "bash -c 'BASH_ENV=/etc/profile exec bash'"
			c.vm.hostname = node[:hostname]
			c.vm.box = box
			c.vm.box_check_update = false
			c.vm.network :private_network, ip: node[:ip]
			c.vm.provider :virtualbox do |v|
				v.cpus = offerings[offering][:cpus]
				v.memory = offerings[offering][:memory]
			end
			c.vm.provision "shell", inline: inline + "\n" + inline_extra
		end
	end
end

3 예시 2[ | ]

# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant::Config.run do |config|
  # All Vagrant configuration is done here. The most common configuration
  # options are documented and commented below. For a complete reference,
  # please see the online documentation at vagrantup.com.

  # Every Vagrant virtual environment requires a box to build off of.
  config.vm.box = "base"

  # The url from where the 'config.vm.box' box will be fetched if it
  # doesn't already exist on the user's system.
  # config.vm.box_url = "http://domain.com/path/to/above.box"

  # Boot with a GUI so you can see the screen. (Default is headless)
  # config.vm.boot_mode = :gui

  # Assign this VM to a host-only network IP, allowing you to access it
  # via the IP. Host-only networks can talk to the host machine as well as
  # any other machines on the same network, but cannot be accessed (through this
  # network interface) by any external networks.
  # config.vm.network :hostonly, "192.168.33.10"

  # Assign this VM to a bridged network, allowing you to connect directly to a
  # network using the host's network device. This makes the VM appear as another
  # physical device on your network.
  # config.vm.network :bridged

  # Forward a port from the guest to the host, which allows for outside
  # computers to access the VM, whereas host only networking does not.
  # config.vm.forward_port 80, 8080

  # Share an additional folder to the guest VM. The first argument is
  # an identifier, the second is the path on the guest to mount the
  # folder, and the third is the path on the host to the actual folder.
  # config.vm.share_folder "v-data", "/vagrant_data", "../data"

  # Enable provisioning with Puppet stand alone.  Puppet manifests
  # are contained in a directory path relative to this Vagrantfile.
  # You will need to create the manifests directory and a manifest in
  # the file base.pp in the manifests_path directory.
  #
  # An example Puppet manifest to provision the message of the day:
  #
  # # group { "puppet":
  # #   ensure => "present",
  # # }
  # #
  # # File { owner => 0, group => 0, mode => 0644 }
  # #
  # # file { '/etc/motd':
  # #   content => "Welcome to your Vagrant-built virtual machine!
  # #               Managed by Puppet.\n"
  # # }
  #
  # config.vm.provision :puppet do |puppet|
  #   puppet.manifests_path = "manifests"
  #   puppet.manifest_file  = "base.pp"
  # end

  # Enable provisioning with chef solo, specifying a cookbooks path (relative
  # to this Vagrantfile), and adding some recipes and/or roles.
  #
  # config.vm.provision :chef_solo do |chef|
  #   chef.cookbooks_path = "cookbooks"
  #   chef.add_recipe "mysql"
  #   chef.add_role "web"
  #
  #   # You may also specify custom JSON attributes:
  #   chef.json = { :mysql_password => "foo" }
  # end

  # Enable provisioning with chef server, specifying the chef server URL,
  # and the path to the validation key (relative to this Vagrantfile).
  #
  # The Opscode Platform uses HTTPS. Substitute your organization for
  # ORGNAME in the URL and validation key.
  #
  # If you have your own Chef Server, use the appropriate URL, which may be
  # HTTP instead of HTTPS depending on your configuration. Also change the
  # validation key to validation.pem.
  #
  # config.vm.provision :chef_client do |chef|
  #   chef.chef_server_url = "https://api.opscode.com/organizations/ORGNAME"
  #   chef.validation_key_path = "ORGNAME-validator.pem"
  # end
  #
  # If you're using the Opscode platform, your validator client is
  # ORGNAME-validator, replacing ORGNAME with your organization name.
  #
  # IF you have your own Chef Server, the default validation client name is
  # chef-validator, unless you changed the configuration.
  #
  #   chef.validation_client_name = "ORGNAME-validator"
end

4 예시3[ | ]

$init_script = <<SCRIPT

yum -y update
yum -y install wget telnet java-1.8.0-openjdk net-tools
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/#PermitRootLogin no/PermitRootLogin yes/' /etc/ssh/sshd_config
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
systemctl restart sshd
getenforce
systemctl stop NetworkManager && systemctl disable NetworkManager
swapoff -a && sed -i '/ swap / s/^/#/' /etc/fstab
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sysctl -p
sysctl --system
yum install -y yum-utils device-mapper-persistent-data lvm2 
curl -s https://get.docker.com | sudo sh
systemctl enable --now docker
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF


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"
}
EOF

sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker


cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
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
EOF

yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes

systemctl enable --now kubelet

SCRIPT

Vagrant.configure("2") do |config|

        # Define base image
        config.vm.box = "centos/7"

        # Manage /etc/hosts on host and VMs
        config.hostmanager.enabled = false
        config.hostmanager.manage_host = true
        config.hostmanager.include_offline = true
        config.hostmanager.ignore_private_ip = false
		config.vm.synced_folder "./SharedFiles/", "/data/"

    # master01
        config.vm.define :master01 do |master01|
                master01.vm.provider :vmware_desktop do |v|
                        v.vmx["memsize"] = "4096"
						v.vmx["numvcpus"] = "4"
						v.gui = true
                end
                master01.vm.hostname = "master01"
                master01.vm.provision :shell, :inline => $init_script
        end

    # worker01
        config.vm.define :worker01 do |worker01|
                worker01.vm.provider :vmware_desktop do |v|
                        v.vmx["memsize"] = "4096"
						v.vmx["numvcpus"] = "4"
						v.gui = true
                end
                worker01.vm.hostname = "worker01"
                worker01.vm.provision :shell, :inline => $init_script
		end

    # worker02
        config.vm.define :worker02 do |worker02|
                worker02.vm.provider :vmware_desktop do |v|
                        v.vmx["memsize"] = "4096"
						v.vmx["numvcpus"] = "4"
						v.gui = true
                end
                worker02.vm.hostname = "worker02"
                worker02.vm.provision :shell, :inline => $init_script
        end
end

5 예시[ | ]

# -*- mode: ruby -*-
# vi: set ft=ruby :

$script = <<-SCRIPT
	#insert any command you want here
	# SSH Root 접속 허용 , 패스워드 로그인 허용
	sed -i 's/#PermitRootLogin yes/PermitRootLogin yes/g' /etc/ssh/sshd_config
	sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/g' /etc/ssh/sshd_config
	# Root 패스워드 변경
	echo PASSWORD | passwd --stdin root
SCRIPT

N=2

Vagrant.configure("2") do |config|
  config.vm.box = "generic/centos7"

		(1..N).each do |i|
			config.vm.define "ansible#{i}" do |worker|
					worker.vm.provider :vmware_desktop do |v|
							v.vmx["memsize"] = "8192"
							v.vmx["numvcpus"] = "4"
							v.gui = true
					end
					worker.vm.hostname = "ansible#{i}"
					config.vm.provision "shell", inline: $script
			end
		end

end

6 같이 보기[ | ]

7 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}