You can use Photon OS as a virtual machine on Google Compute Engine (GCE). You can download Photon OS, as an OVA or ISO file, and install the Photon OS distribution on vSphere. After you install Photon OS, you can deploy a containerized application in Docker with a single command.
This the multi-page printable view of this section. Click here to print.
Running Photon OS on Google Compute Engine
1 - Prerequisites for Running Photon OS on GCE
Before you use Photon OS within GCE, verify that you have the following resources:
Google Compute Engine Account
Working with GCE requires a Google Compute Engine account with valid payment information. Keep in mind that, if you try the examples in this document, you will be charged by Google. The GCE-ready version of Photon OS is free to use.
GCE Tools
GCE is a service that lets you run virtual machines on Google’s infrastructure. You can customize the virtual machine as much as you want, and you can even install your own custom operating system image. Or, you can adopt one of the public images provided by Google. For any operating system to work with GCE, it must match Google’s infrastructure needs. Google provides tools that VM instances require to work correctly on GCE:
- Google startup scripts: You can provide some startup script to configure your instances at startup.
- Google Daemon: Google Daemon creates new accounts and configures ssh to accept public keys using the metadata server.
- Google Cloud SDK: Command line tools to manage your images, instances and other objects on GCE.
Perform the following tasks to make Photon OS work on GCE:
- Install Google Compute Engine Image packages
- Install Google Cloud SDK
- Change GPT partition table to MBR
- Update the Grub config for new MBR and serial console output
- Update ssh configuration
- Delete ssh host keys
- Set the time zone to UTC
- Use the Google NTP server
- Delete the hostname file.
- Add Google hosts /etc/hosts
- Set MTU to 1460. SSH will not work without it.
- Create
/etc/ssh/sshd_not_to_be_run
with just the contents “GOOGLE\n”.
For more information see Importing Boot Disk Images to Compute Engine.
For information about upgrading the Photon OS Linux kernel see Upgrading the Kernel Version Requires Grub Changes for AWS and GCE Images
Photon OS Image
VMware recommends that administrators use the Photon OS image for Google Compute Engine (GCE) to create Photon OS instances on GCE. Photon OS bundles the Google startup scripts, daemon, and cloud SDK into a GCE-ready image that has been modified to meet the configuration requirements of GCE. You can download the Photon OS image for GCE from the following URL: https://packages.vmware.com/photon/5.0/GA/gce/
For instructions, see Downloading Photon OS.
Optionally you can customize Photon OS to work with GCE.
Creating Photon image for GCE
Perform the following tasks:
Prepare Photon Disk
- Install Photon Minimal on Fusion/Workstation and install some required packages.
mount /dev/cdrom /media/cdrom tdnf install python2-libs ntp sudo wget tar which gptfdisk sed findutils grep gzip -y
Convert GPT to MBR and update Grub
Photon installer installs GPT partition table by default but GCE only accepts an MBR (msdos) type partition table. So, you must convert GPT to MBR and update grub. Use the following commands to update the grub:
# Change partition table to MBR from GPT sgdisk -m 1:2 /dev/sda grub2-install /dev/sda # Enable serial console on grub for GCE. cat << EOF >> /etc/default/grub GRUB_CMDLINE_LINUX="console=ttyS0,38400n8" GRUB_TERMINAL=serial GRUB_SERIAL_COMMAND="serial --speed=38400 --unit=0 --word=8 --parity=no --stop=1" EOF # Create new grub.cfg based on the settings in /etc/default/grub grub2-mkconfig -o /boot/grub2/grub.cfg
Install Google Cloud SDK and GCE Packages
tdnf install -y google-compute-engine google-compute-engine-services cp /usr/lib/systemd/system/google* /lib/systemd/system/ cd /lib/systemd/system/multi-user.target.wants/ # Create links in multi-user.target to auto-start these scripts and services. for i in ../google*; do ln -s $i `basename $i`; done cd /tmp/; wget https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz tar -xf google-cloud-sdk.tar.gz cd google-cloud-sdk ./install.sh
Update /etc/hosts file with GCE values as follows:
echo "169.254.169.254 metadata.google.internal metadata" >> /etc/hosts
Remove all servers from ntp.conf and add Google’s ntp server.
sed -i -e "/server/d" /etc/ntp.conf cat /etc/ntp.conf echo "server 169.254.169.254" >> /etc/ntp.conf # Create ntpd.service to auto starting ntp server. cat << EOF >> /lib/systemd/system/ntpd.service [Unit] Description=Network Time Service After=network.target nss-lookup.target [Service] Type=forking PrivateTmp=true ExecStart=/usr/sbin/ntpd -g -u ntp:ntp Restart=always [Install] WantedBy=multi-user.target EOF # Add link in multi-user.target.wants to auto start this service. cd /lib/systemd/system/multi-user.target.wants/ ln -s ../ntpd.service ntpd.service
Set UTC timezone
ln -sf /usr/share/zoneinfo/UTC /etc/localtime
Update /etc/resolv.conf
echo "nameserver 8.8.8.8" >> /etc/resolv.conf
Remove ssh host keys and add script to regenerate them at boot time.
rm /etc/ssh/ssh_host_* # Depending on the installation, you may need to purge the following keys rm /etc/ssh/ssh_host_rsa_key* rm /etc/ssh/ssh_host_dsa_key* rm /etc/ssh/ssh_host_ecdsa_key* sed -i -e "/exit 0/d" /etc/rc.local echo "[ -f /etc/ssh/ssh_host_key ] && echo 'Keys found.' || ssh-keygen -A" >> /etc/rc.local echo "exit 0" >> /etc/rc.local printf "GOOGLE\n" > /etc/ssh/sshd_not_to_be_run # Edit sshd_config and ssh_config as per instructions on [this link](https://cloud.google.com/compute./tutorials/building-images).
Change MTU to 1460 for network interface.
# Create a startup service in systemd that will change MTU and then exit cat << EOF >> /lib/systemd/system/eth0.service [Unit] Description=Network interface initialization After=local-fs.target network-online.target network.target Wants=local-fs.target network-online.target network.target [Service] ExecStart=/bin/ifconfig eth0 mtu 1460 up Type=oneshot [Install] WantedBy=multi-user.target EOF # Make this service auto-start at boot. cd /lib/systemd/system/multi-user.target.wants/ ln -s ../eth0.service eth0.service
Pack and upload to GCE.
Shut down the Photon VM and copy its disk to THE
tmp
folder.# You will need to install Google Cloud SDK on host machine to upload the image and play with GCE. cp Virtual\ Machines.localized/photon.vmwarevm/Virtual\ Disk.vmdk /tmp/disk.vmdk cd /tmp # GCE needs disk to be named as disk.raw with raw format. qemu-img convert -f vmdk -O raw disk.vmdk disk.raw # ONLY GNU tar will work to create acceptable tar.gz file for GCE. MAC's default tar is BSDTar which will not work. # On Mac OS X ensure that you have gtar "GNU Tar" installed. exmaple: gtar -Szcf photon.tar.gz disk.raw gtar -Szcf photon.tar.gz disk.raw # Upload gsutil cp photon.tar.gz gs://photon-bucket # Create image gcloud compute --project "<project name>" images create "photon-beta-vYYYYMMDD" --description "Photon Beta" --source-uri https://storage.googleapis.com/photon-bucket/photon032315.tar.gz # Create instance on GCE of photon image gcloud compute --project "photon" instances create "photon" --zone "us-central1-f" --machine-type "n1-standard-1" --network "default" --maintenance-policy "MIGRATE" --scopes "https://www.googleapis.com/auth/devstorage.read_only" "https://www.googleapis.com/auth/logging.write" --image "https://www.googleapis.com/compute/v1/projects/photon/global/images/photon" --boot-disk-type "pd-standard" --boot-disk-device-name "photon"
2 - Installing Photon OS on Google Compute Engine
You can use either the Google Cloud Platform or the gcloud CLI to upload the Photon OS GCE tar file to the bucket, and create the Image & Photon OS VM instance.
Setting Up Using the Google Cloud Platform
After you download the Photon OS image for GCE, log into GCE and install Photon OS.
Perform the following steps:
Create a New Bucket
Create a new bucket to store your Photon OS image for GCE.
Upload the Photon OS Image
While viewing the bucket that created, click the
Upload files
button, navigate to your Photon OS image and click theChoose
button.When the upload finishes, you can see the Photon OS compressed image in the file list for the bucket that you created.
Create a New Image
To create a new image, click on
Images
in theCompute
category in the left panel and then click on theNew Image
button.Enter a name for the image in the
Name
field and change theSource
toCloud Storage file
using the pull-down menu. Then, in theCloud Storage file
field, enter the bucket name and filename as the path to the Photon OS image for GCE. In this example, where the bucket was namedphoton_storage,
the path is as follows:`photon_storage/photon-gce-2.0-tar.gz`
The new image form autopopulates the
gs://
file path prefix.*Click the
Create
button to create your image. You must be able to see the Images catalog and your Photon OS image at the top of the list.Create a New Instance
To create an instance, check the box next to the Photon OS image and click the
Create Instance
button.On the
Create a new instance
form, provide a name for this instance, confirm the zone into which this instance is to be deployed and, before clickingCreate,
check theAllow HTTP traffic
andAllow HTTPS traffic
options.Note: The firewall rules in this example are optional. You can configure the ports according to your requirements.
When the instance is created you will be returned to your list of VM instances. If you click on the instance, the status page for the instance will allow you to SSH into your Photon OS environment using the SSH button at the top of the panel.
At this point, your instance is running and you are ready to start the Docker engine and run a container workload. For more information, see Deploying a Containerized Application in Photon OS.
Setting Up Using the gcloud CLI
Example Setup Script:
You can use the following script (create.sh) to upload your tar file programmatically to the bucket and create the VM.
#!/bin/bash
timestamp=$(date +%s)
export PATH=$PATH:/root/gce/google-cloud-sdk/bin
# get branch name in order to determine the machine type.
GCE_VM_NAME=$2
branch=`echo ${GCE_VM_NAME} | cut -d '-' -f 1`
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
GCE_USERNAME=<gce_username>
GCE_BUCKET=<gs://bucket-name>
if [ $# -lt 2 ]
then
echo "Usage is: create.sh <path_to_gce_image.tar.gz> <vm_name> [(optional) <user-data-file-path>]";
exit;
fi
echo "Uploading gce tar.gz to gce bucket...."
/root/gce/google-cloud-sdk/bin/gsutil cp ${1} $GCE_BUCKET/photon-gce-${timestamp}.tar.gz
if [ ! "$?" -eq 0 ]; then
echo "Failed: couldn't upload to gce bucket"
exit 1
fi
echo "GCE tar.gz uploaded successfully, proceeding with image creation"
gcloud compute images create ${2}-image --source-uri $GCE_BUCKET/photon-gce-${timestamp}.tar.gz
if [ ! "$?" -eq 0 ]; then
echo "Failed: couldn't create image successfully"
exit 1
fi
echo "GCE image created successfully. Proceeding with instance creation"
if [[ ( "${branch}" != "one" ) && ( "${branch}" != "two" ) ]];then
machine_type="n1-standard-1"
else
machine_type="n1-standard-2"
fi
echo branch=$branch
echo machine_type=$machine_type
if [ $# -gt 2 ]
then
gcloud compute instances create ${2} --machine-type ${machine_type} --image ${2}-image --metadata-from-file=user-data=${3}
else
gcloud compute instances create ${2} --machine-type ${machine_type} --image ${2}-image
fi
if [ ! "$?" -eq 0 ]; then
echo "Failed: couldn't create instance successfully"
exit 1
fi
echo "Photon Instance created successfully on GCE"
externalip="$(gcloud compute instances list ${2} --format='value(networkInterfaces[].accessConfigs[].natIP)')"
echo $externalip
GCE_VM_IP=$externalip
echo GCE_VM_IP=$GCE_VM_IP