You can either choose
Start Script or Instance Template or Custom Images to reduce your manual VM setup process.
Let's start step by step:
Startup Script
Let's do it in GCP. Head over to VM Instances as we have no VM created.
Allow the HTTP Traffic and choose this one.
Here under the management, you can find the startup script
#!/bin/bash
apt update
apt -y install apache2
echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html
This is what we are going to use.
We will first update the os and then install apache. The write this simple Hello world and hostname with its ip address.
Here hostname's ip address is actually the internal IP.
and press create.
Our instance is launched.
Press this external Ip (Publicly available server)
Here you can find the web page with Internal IP mentioned as previously mentioned.
Done!
Instance Template
When you want to launch multiple instances, its tough to launch instances all time manually. Thus you can create a template and use it to launch others.
Note:you can not change a instance template after creating that one.
Go to instance templates
Give it a name:
Note: You don't need to provide regions and zones here as it can be used to create Vm instances in any region and zones.
Remain others in default and allow the HTTP traffic.
Follow the management option and then startup script
#!/bin/bash
apt update
apt -y install apache2
echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html
Add this one .
Then press create.
Tips: There is no cost for creating an instance template. But you will pay once you use instance templates to create a VM.
Let's create one using Instance template .
You can see most of the details provided from earlier.
Although you can customize it once creating the VM.
This is the VM we created using the VM instances.
Press the external IP to see our script doing magic for us.
Custom Images
The problem for Instance templates is that, every time one instance is created, the script starts working and it takes time.
But to solve this issue, we can create an instance and make it an image.
Then we can use this image for all of our instances.
Now go to disks.
Disks are the OS attached with your VM instances. We have this disk as we have 1 vm instances already created. That time , this disk got created as well.
As we created an instance previously and got this disk with it. We will create a copy of this disk to make our custom image.
After pressing the create image, you can see this one
Note: You should not create an image from a disk which is connected with a running instance.
Once you create the image, you can us it to create instances.
Also you can use instance template using this image. And again create instances.
Copy the instance template we have and then modify it with image we just created.
Change this boot disk and choose your custom image.
You can also modify the script and remove the line causing install in our instances.
Press create now!
Now you can create instances using this instance templates.
Done!
Top comments (0)