Deploy multi-tier application in GKE using Terraform & Google SDK

Deploy multi-tier application in GKE using Terraform & Google SDK

wordpress+mysql deployment


This article illustrates the process of deploying a WordPress application with a MySQL database server in a containerized environment on a Google Kubernetes Engine (GKE) cluster. To initiate this deployment, Terraform is employed to provision a GKE cluster named "test-gke," utilizing a custom Virtual Private Cloud (VPC) and subnet configuration. Following the successful creation of the GKE cluster, the Google SDK is utilized to establish a connection with Google Cloud Platform (GCP) and validate the provisioning of the GKE using fundamental commands. It's important to note that the kubectl component can be installed on your local machine by executing the command "gcloud components install kubectl."

With kubectl now available, the deployment process begins by utilizing the command to deploy the MySQL:5.6 image as a container. Subsequently, the port is internally exposed to facilitate interaction among other containers and applications within the GKE cluster "test-gke."

Once the MySQL container is established, the next step involves creating a WordPress container. Additionally, a service is generated with the type LoadBalancer to enable access to the application from the internet.

Phase-1 of 3 : provisioning GKE cluster using terraform using five resources.

# 1. VPC
resource "google_compute_network" "vpc" {
  name                    = "${var.project_id}-vpc"
  auto_create_subnetworks = "false"
}

# 2. Subnet
resource "google_compute_subnetwork" "subnet" {
  name          = "${var.project_id}-subnet"
  region        = var.region
  network       = google_compute_network.vpc.name
  ip_cidr_range = "10.10.0.0/24"
}

#3. check GCP to verify 1.27 version exist.
data "google_container_engine_versions" "gke_version" {
  location = var.region
  version_prefix = "1.27"
}
#4. create google container cluster.
resource "google_container_cluster" "example" {
  name = "test-gke"
  location = var.region
  remove_default_node_pool = true
  initial_node_count = 1
  network = google_compute_network.vpc.name
  subnetwork = google_compute_subnetwork.subnet.name
}

#5. create a separately managed node pool
resource "google_container_node_pool" "primary_node" {
  name = google_container_cluster.example.name
  location = var.region
  cluster = google_container_cluster.example.name
  version = data.google_container_engine_versions.gke_version.release_channel_latest_version["STABLE"]
  node_count = var.gke_num_nodes

  node_config {
    oauth_scopes = [
      "https://www.googleapis.com/auth/logging.write",
      "https://www.googleapis.com/auth/monitoring",
    ]
    labels = {
        env = var.project_id
    }
    machine_type = "n1-standard-1"
    tags = ["gke-node", "${var.project_id}-gke", "test-gke"]
    metadata = {
        disable-legacy-endpoints="true"
    }
  }
}        

Phase-2 of 3: Deploy mysql & wordpress applications as containers

# create mysql container using below mentioned sample YAML file
# mysql.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mysql
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql-container
        image: mysql:5.6
        
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        - name: MYSQL_DATABASE
          value: srinidb

        

Command to connect to GCP using google sdk

gcloud container clusters get-credentials test-gke --region us-central1 --project <project-id>        

kubectl commands

kubectl config current-context
kubectl apply -f sql.yaml
kubectl expose deployment mysql --port=3306
kubectl create deploy mywp --image=docker.io/wordpress
kubectl expose deploy mywp --type=LoadBalancer --port=80 --target-port=80
kubectl get services  # external IP may be created for mywp        

Phase 3 of 3: using external IP which was generated using above command, open a browser to launch wp home page. You can follow the steps to configure wordpress.

This comprehensive deployment strategy ensures the successful integration of a WordPress application with a containerized MySQL database server within the Google Kubernetes Engine cluster, providing a scalable and efficient environment for web applications.

Ashutosh S. Bhakare

AWS CB, GDE, Docker Captain, CK {S,A,D}, SCE, LFCE, RHCA XI, SCI, RHCI, JBCI, Google Authorised Trainer, Kubernetes, Openshift, Ansible,SUSE, PC {A, DBE, MLE, NA}

1 年

Excellent !!!

回复

要查看或添加评论,请登录

Srinivasan (Srini) Viswanathan的更多文章

社区洞察

其他会员也浏览了