Deploying a 3 tier app in GCP from Ansible

Ansible is an open-source automation tool that allows you to automate configuration management, application deployment, and task automation. In this blog post, we will explore how to deploy a three-tier application in Google Cloud Platform (GCP) using Ansible.

GCP Overview Google Cloud Platform (GCP) is a suite of cloud computing services offered by Google. It provides a wide range of services such as virtual machines, storage, and networking, among others, that can be used to deploy and run applications in the cloud. One of the advantages of using GCP is its high availability and scalability, which makes it an excellent option for deploying enterprise-grade applications.

Ansible Overview Ansible is an open-source automation tool that enables you to automate configuration management, application deployment, and task automation. It uses a simple, human-readable YAML syntax for defining tasks and supports many cloud providers, including GCP.

Deploying a Three-Tier Application using Ansible in GCP To deploy a three-tier application using Ansible in GCP, we will use an Ansible playbook. A playbook is a series of tasks that are executed on a set of hosts defined in an inventory file. In this example, we will use an Ansible playbook to deploy a simple three-tier application that consists of a web server, application server, and database server.

The following are the steps required to deploy the application:

  1. Set up a GCP project and enable the Compute Engine API.
  2. Create a service account in GCP and assign it the necessary roles.
  3. Install Ansible and the GCP Ansible modules.
  4. Write an Ansible playbook that defines the components and dependencies of the application.
  5. Run the Ansible playbook to create the infrastructure.
  6. Monitor the infrastructure in GCP.

Let’s go through each step in detail.

Step 1: Set up a GCP project and enable the Compute Engine API. To set up a GCP project and enable the Compute Engine API, follow these steps:

  1. Go to the GCP Console (console.cloud.google.com).
  2. Click on the project drop-down menu in the top left corner of the screen.
  3. Click on the “New Project” button.
  4. Enter a name for the project and click on the “Create” button.
  5. Once the project is created, select it from the project drop-down menu.
  6. Enable the Compute Engine API by navigating to APIs & Services > Library and searching for Compute Engine. Click on the “Enable” button.

Step 2: Create a service account in GCP and assign it the necessary roles. To create a service account in GCP, follow these steps:

  1. Go to the GCP Console (console.cloud.google.com).
  2. Navigate to the IAM & Admin > Service Accounts page.
  3. Click on the “Create Service Account” button.
  4. Enter a name for the service account and click on the “Create” button.
  5. On the “Create Key” tab, select “JSON” as the key type and click on the “Create” button. This will download a JSON file containing the service account key.
  6. Assign the service account the necessary roles by navigating to IAM & Admin > IAM and adding the roles “Compute Instance Admin (v1)” and “Service Account User” to the service account.

Step 3: Install Ansible and the GCP Ansible modules. To install Ansible and the GCP Ansible modules, follow these steps:

  1. Install Ansible on your local machine using the appropriate method for your operating system.
  2. Install the Google Cloud SDK by following the instructions on the Google Cloud SDK documentation page.
  3. Install the GCP Ansible modules by running the command “pip install requests google -auth google-auth google-auth-httplib2 google-api-python-client google-cloud-ansible” in a terminal window.

Step 4: Write an Ansible playbook that defines the components and dependencies of the application. To write an Ansible playbook that defines the components and dependencies of the application, follow these steps:

  1. Create a new directory for the Ansible playbook.
  2. Create an inventory file that lists the GCP instances that will be created.
  3. Create a playbook YAML file that defines the tasks required to create the infrastructure for the three-tier application.
  4. Define any necessary variables for the playbook.
  5. Use the GCP Ansible modules to manage the GCP resources.

Here’s an example Ansible playbook that deploys a three-tier application:

- hosts: all
  gather_facts: no
  tasks:
  - name: create network
    gcp_compute_network:
      name: my-network
      auto_create_subnetworks: false
      auth_kind: serviceaccount
      service_account_file: "{{ lookup('env','GOOGLE_APPLICATION_CREDENTIALS') }}"

  - name: create subnet
    gcp_compute_subnetwork:
      name: my-subnetwork
      network: my-network
      region: us-central1
      ip_cidr_range: "10.0.0.0/24"
      auth_kind: serviceaccount
      service_account_file: "{{ lookup('env','GOOGLE_APPLICATION_CREDENTIALS') }}"

  - name: create web server
    gcp_compute_instance:
      name: web-server
      machine_type: f1-micro
      zone: us-central1-a
      disks:
      - auto_delete: true
        boot: true
        initialize_params:
          source_image: projects/centos-cloud/global/images/family/centos-7
      network_interfaces:
      - network: my-network
        subnetwork: my-subnetwork
      metadata:
        startup-script: "#!/bin/bash\nyum install -y httpd\nsystemctl enable httpd\nsystemctl start httpd\n"
      auth_kind: serviceaccount
      service_account_file: "{{ lookup('env','GOOGLE_APPLICATION_CREDENTIALS') }}"

  - name: create app server
    gcp_compute_instance:
      name: app-server
      machine_type: n1-standard-1
      zone: us-central1-a
      disks:
      - auto_delete: true
        boot: true
        initialize_params:
          source_image: projects/centos-cloud/global/images/family/centos-7
      network_interfaces:
      - network: my-network
        subnetwork: my-subnetwork
      metadata:
        startup-script: "#!/bin/bash\nyum install -y java-1.8.0-openjdk\ncurl -L https://tomcat.apache.org/download-80.cgi#8.5.38 -o /tmp/tomcat.tar.gz\nmkdir /opt/tomcat\ntar xzvf /tmp/tomcat.tar.gz -C /opt/tomcat --strip-components=1\nsystemctl enable tomcat\nsystemctl start tomcat\n"
      auth_kind: serviceaccount
      service_account_file: "{{ lookup('env','GOOGLE_APPLICATION_CREDENTIALS') }}"

  - name: create database server
    gcp_compute_instance:
      name: database-server
      machine_type: n1-standard-2
      zone: us-central1-a
      disks:
      - auto_delete: true
        boot: true
        initialize_params:
          source_image: projects/centos-cloud/global/images/family/centos-7
      network_interfaces:
      - network: my-network
        subnetwork: my-subnetwork
      metadata:
        startup-script: "#!/bin/bash\nyum install -y mariadb-server\nsystemctl enable mariadb\nsystemctl start mariadb\n"
      auth_kind: serviceaccount
      service_account_file: "{{ lookup('env','GOOGLE_APPLICATION_CREDENTIALS') }}"

  - name: wait for web server
    wait_for:
      host: "{{ hostvars['web-server']['networkInterfaces'][0]['accessConfigs'][0]['natIP'] }}"
      port: 80
      delay: 10
      timeout: 120

  - name: wait for app server
    wait_for:
      host: "{{ hostvars['app-server']['networkInterfaces'][0]['accessConfigs'][0]['natIP'] }}"
      port: 8080
      delay: 10
      timeout: 120

Step 5: Run the Ansible playbook to create the infrastructure. To run the Ansible playbook to create the infrastructure, follow these steps:

  1. Open a terminal window and navigate to the directory where you saved the Ansible playbook.
  2. Run the command “export GOOGLE_APPLICATION_CREDENTIALS=path/to/credentials.json” to set the path to the service account key file.
  3. Run the command “ansible-playbook playbook.yml” to run the playbook and create the infrastructure.

Step 6: Monitor the infrastructure in GCP. To monitor the infrastructure in GCP, follow these steps:

  1. In GCP, navigate to the Compute Engine > Instances page to view the deployed VM instances.
  2. Monitor the VM instance status and any associated logs or metrics.

Conclusion Deploying a three-tier application using Ansible in GCP can be a powerful and flexible way to provision infrastructure. By using Ansible, you can automate the deployment of infrastructure and manage it as code. GCP provides many services that can be used to deploy and run applications in the cloud, and by combining Ansible and GCP, you can create a robust and scalable application infrastructure.


Leave a Reply