Ubuntu Google Cloud
Introduction
Ubuntu is one of the most popular Linux distributions for cloud environments, and Google Cloud Platform (GCP) provides robust infrastructure for hosting Ubuntu instances. This guide will walk you through the basics of deploying, configuring, and managing Ubuntu on Google Cloud, making it accessible for beginners while providing practical knowledge for real-world scenarios.
What is Ubuntu on Google Cloud?
Ubuntu on Google Cloud refers to running Ubuntu operating system instances on Google's cloud infrastructure. Google Cloud officially supports Ubuntu images, providing a reliable, secure, and up-to-date environment for your applications and services.
Benefits of Ubuntu on Google Cloud
- Reliability: Google's infrastructure combined with Ubuntu's stability
- Security: Regular security updates from both Ubuntu and Google
- Scalability: Easily scale resources up or down based on demand
- Cost-effective: Pay only for what you use with flexible pricing options
- Integration: Seamless integration with other Google Cloud services
Getting Started
Prerequisites
Before you begin, make sure you have:
- A Google Cloud account
- The Google Cloud SDK installed on your local machine
- Basic knowledge of Linux commands
- A billing account linked to your Google Cloud project
Setting Up Your Google Cloud Project
First, you'll need to create a project in Google Cloud Console:
- Go to the Google Cloud Console
- Click on "Select a project" at the top of the page
- Click "New Project"
- Enter a project name and select a billing account
- Click "Create"
Installing Google Cloud SDK
The Google Cloud SDK allows you to manage your Google Cloud resources from the command line:
# For Ubuntu/Debian
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates gnupg
# Add the Google Cloud SDK distribution URI as a package source
echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
# Import the Google Cloud public key
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key --keyring /usr/share/keyrings/cloud.google.gpg add -
# Update and install the SDK
sudo apt-get update
sudo apt-get install google-cloud-sdk
Initialize the SDK and authenticate:
gcloud init
Deploying Ubuntu on Google Cloud
Creating an Ubuntu VM Instance
You can create an Ubuntu virtual machine instance using either the Google Cloud Console (GUI) or the gcloud
command-line tool.
Using the Google Cloud Console:
- Navigate to Compute Engine > VM instances
- Click "Create Instance"
- Configure your instance:
- Name: Give your instance a unique name
- Region and Zone: Select based on your target audience location
- Machine type: Select based on your resource needs
- Boot disk: Click "Change" and select "Ubuntu" (typically Ubuntu 20.04 LTS or newer)
- Firewall: Check "Allow HTTP traffic" if needed
- Click "Create"
Using the gcloud CLI:
gcloud compute instances create ubuntu-instance \
--project=your-project-id \
--zone=us-central1-a \
--machine-type=e2-medium \
--network-interface=network=default,subnet=default \
--maintenance-policy=MIGRATE \
--image-family=ubuntu-2004-lts \
--image-project=ubuntu-os-cloud \
--boot-disk-size=10GB \
--boot-disk-type=pd-balanced
Connecting to Your Ubuntu Instance
After your instance is created, you can connect to it using SSH:
Using the Google Cloud Console:
Click the "SSH" button next to your instance in the VM instances list.
Using the gcloud CLI:
gcloud compute ssh ubuntu-instance --zone=us-central1-a
Managing Your Ubuntu Instance
Updating Your Ubuntu System
Once connected, it's important to keep your system updated:
sudo apt update
sudo apt upgrade -y
Installing Basic Software
Install some commonly used software:
sudo apt install -y \
nginx \
python3-pip \
git
Configuring the Firewall
To allow traffic to your web server, configure the Google Cloud firewall:
gcloud compute firewall-rules create allow-http \
--direction=INGRESS \
--priority=1000 \
--network=default \
--action=ALLOW \
--rules=tcp:80 \
--source-ranges=0.0.0.0/0
Monitoring Your Instance
Google Cloud provides monitoring tools to track your instance's performance:
- Navigate to Monitoring > Dashboards
- Select "VM Instances" to view CPU, memory, and disk metrics
Practical Example: Deploying a Web Application
Let's go through a complete example of deploying a simple Flask web application on Ubuntu Google Cloud.
Step 1: Set Up Your Instance
Create an Ubuntu instance as described earlier, making sure to allow HTTP traffic.
Step 2: Install Required Software
Connect to your instance and run:
sudo apt update
sudo apt install -y python3-pip python3-venv nginx
# Create a directory for your application
mkdir -p ~/myapp
cd ~/myapp
# Create a virtual environment
python3 -m venv venv
source venv/bin/activate
# Install Flask
pip install flask gunicorn
Step 3: Create a Simple Flask Application
Create a file named app.py
:
cat > app.py << 'EOF'
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return '<h1>Hello from Ubuntu on Google Cloud!</h1>'
if __name__ == '__main__':
app.run(host='0.0.0.0')
EOF
Step 4: Create a systemd Service
Create a systemd service file to run your application:
sudo bash -c 'cat > /etc/systemd/system/myapp.service << EOF
[Unit]
Description=Gunicorn instance to serve myapp
After=network.target
[Service]
User=$USER
Group=www-data
WorkingDirectory=/home/$USER/myapp
Environment="PATH=/home/$USER/myapp/venv/bin"
ExecStart=/home/$USER/myapp/venv/bin/gunicorn --workers 3 --bind unix:myapp.sock -m 007 app:app
[Install]
WantedBy=multi-user.target
EOF'
Step 5: Configure Nginx
Create an Nginx configuration file:
sudo bash -c 'cat > /etc/nginx/sites-available/myapp << EOF
server {
listen 80;
server_name _;
location / {
include proxy_params;
proxy_pass http://unix:/home/$USER/myapp/myapp.sock;
}
}
EOF'
# Create symbolic link
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled
# Test Nginx configuration
sudo nginx -t
# Restart Nginx
sudo systemctl restart nginx
Step 6: Start the Application
# Start and enable the service
sudo systemctl start myapp
sudo systemctl enable myapp
# Check status
sudo systemctl status myapp
You should now be able to access your application by visiting your instance's public IP address.
Scaling and Advanced Features
Creating Instance Templates
For scalable deployments, create an instance template:
gcloud compute instance-templates create ubuntu-template \
--machine-type=e2-medium \
--image-family=ubuntu-2004-lts \
--image-project=ubuntu-os-cloud \
--boot-disk-size=10GB \
--tags=http-server
Setting Up Managed Instance Groups
Create a managed instance group for automatic scaling:
gcloud compute instance-groups managed create ubuntu-group \
--zone=us-central1-a \
--template=ubuntu-template \
--size=2
# Set up autoscaling
gcloud compute instance-groups managed set-autoscaling ubuntu-group \
--zone=us-central1-a \
--max-num-replicas=5 \
--min-num-replicas=1 \
--target-cpu-utilization=0.7
Using Cloud Storage with Ubuntu
Mount a Google Cloud Storage bucket to your Ubuntu instance:
# Install gcsfuse
export GCSFUSE_REPO=gcsfuse-`lsb_release -c -s`
echo "deb http://packages.cloud.google.com/apt $GCSFUSE_REPO main" | sudo tee /etc/apt/sources.list.d/gcsfuse.list
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
sudo apt-get update
sudo apt-get install -y gcsfuse
# Create mount directory
mkdir -p ~/my-bucket
# Mount bucket
gcsfuse my-bucket ~/my-bucket
Optimizing Costs
Preemptible Instances
For non-critical workloads, use preemptible instances to save costs:
gcloud compute instances create ubuntu-preemptible \
--preemptible \
--machine-type=e2-medium \
--image-family=ubuntu-2004-lts \
--image-project=ubuntu-os-cloud
Resource Monitoring and Budgeting
Set up budget alerts to monitor your spending:
- Navigate to Billing > Budgets & alerts
- Click "Create Budget"
- Enter a budget amount and set up email notifications
Troubleshooting Common Issues
Instance Not Starting
Check the serial port output:
gcloud compute instances get-serial-port-output ubuntu-instance
SSH Connection Issues
If you can't connect via SSH:
- Check your firewall rules
- Verify your SSH keys are properly set up
- Use the browser-based SSH from Google Cloud Console
Disk Space Issues
Check disk usage and extend if needed:
# Check disk usage
df -h
# Extend disk
gcloud compute disks resize ubuntu-instance \
--size=20GB \
--zone=us-central1-a
Security Best Practices
Keep Your System Updated
Regularly update your system:
sudo apt update && sudo apt upgrade -y
Use Strong Firewall Rules
Limit access to your instances:
gcloud compute firewall-rules create allow-ssh-from-trusted \
--direction=INGRESS \
--priority=1000 \
--network=default \
--action=ALLOW \
--rules=tcp:22 \
--source-ranges=YOUR_TRUSTED_IP_RANGE
Enable OS Login
Use OS Login for better SSH key management:
gcloud compute instances add-metadata ubuntu-instance \
--metadata enable-oslogin=TRUE
Summary
In this guide, we've explored how to deploy, configure, and manage Ubuntu instances on Google Cloud Platform. We've covered:
- Setting up a Google Cloud project
- Creating Ubuntu VM instances
- Managing your Ubuntu instances
- Deploying a web application
- Scaling and advanced features
- Cost optimization
- Troubleshooting and security best practices
Ubuntu on Google Cloud provides a powerful, flexible, and reliable platform for a wide range of applications, from simple websites to complex distributed systems.
Additional Resources
Exercises
- Deploy an Ubuntu instance on Google Cloud and install a LAMP (Linux, Apache, MySQL, PHP) stack.
- Create a load-balanced setup with two Ubuntu instances serving a static website.
- Set up automatic backups for your Ubuntu instance using Google Cloud Storage.
- Configure a firewall to restrict access to your instance to only your IP address.
- Create a monitoring dashboard for your Ubuntu instance that tracks CPU, memory, and disk usage.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)