Terriermon - Digimon

16. Connect an App to a Cloud SQL for PostgreSQL Instance

2024. 11. 24. 12:11클라우드/GCP

Cloud SQL for PostgreSQL 인스턴스에 앱 연결

(애플리케이션이 데이터베이스에 쓰고 읽을 수 있는지 확인)

 

Task 1. Initialize APIs and create a Cloud IAM service account

1.In Cloud Shell, run the following command to enable the Artifact Registry API:
gcloud services enable artifactregistry.googleapis.com

 

Create a Service Account for Cloud SQL

1.In Cloud Shell, create a Service Account and bind it to the Cloud SQL admin role in the lab project:

 

gcloud iam service-accounts keys create $CLOUDSQL_SERVICE_ACCOUNT.json \
    --iam-account=$CLOUDSQL_SERVICE_ACCOUNT@$PROJECT_ID.iam.gserviceaccount.com \
    --project=$PROJECT_ID

 

Task 2. Deploy a lightweight GKE application

Create a Kubernetes cluster

ZONE=us-east4-a
gcloud container clusters create postgres-cluster \
--zone=$ZONE --num-nodes=2

 

Create Kubernetes secrets for database access

kubectl create secret generic cloudsql-instance-credentials \
--from-file=credentials.json=$CLOUDSQL_SERVICE_ACCOUNT.json
   
kubectl create secret generic cloudsql-db-credentials \
--from-literal=username=postgres \
--from-literal=password=supersecret! \
--from-literal=dbname=gmemegen_db

 

Download and build the GKE application container

gsutil -m cp -r gs://spls/gsp919/gmemegen .
cd gmemegen

 

 

export REGION=us-east4
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
export REPO=gmemegen

 

gcloud auth configure-docker ${REGION}-docker.pkg.dev

 

gcloud artifacts repositories create $REPO \
    --repository-format=docker --location=$REGION

 

docker build -t ${REGION}-docker.pkg.dev/${PROJECT_ID}/gmemegen/gmemegen-app:v1 .

 

 

Configure and deploy the GKE application

수정

image: us-east4-docker.pkg.dev/qwiklabs-gcp-04-55cc834f5ad9/gmemegen/gmemegen-app:v1

 

수정

-instances=qwiklabs-gcp-04-55cc834f5ad9:us-east4:postgres-gmemegen=tcp:5432

 

 

kubectl create -f gmemegen_deployment.yaml
kubectl get pods

 

 

(kubectl apply -f gmemegen_deployment.yaml

오류가 나서 수동으로 다시 적용)

 

Task 3. Connect the GKE application to an external load balancer

 

Create a load balancer to make your GKE application accessible from the web

kubectl expose deployment gmemegen \
    --type "LoadBalancer" \
    --port 80 --target-port 8080

 

 

Test the application to generate some data

In this step you will access the gMemegen application from your web browser.

The application has a very simple interface. It launches to the application home page, which displays 6 candidate images for making memes. You can select an image by clicking on it.

The Create Meme page is displayed, where you enter two items of text, to be displayed at the top and bottom of the image. Clicking Submit renders the meme and displays it. The interface provides no navigation from the completed meme page. You will have to use the browser's back button to return to the home page.

There are two other pages, Recent and Random, which display a set of recently generated memes and a random meme, respectively. Generating memes and navigating the UI will generate database activity which you can view in the logs as described below.

Wait for the load balancer to expose an external IP, which you can retrieve as follows:

 

kubectl describe service gmemegen

 

export LOAD_BALANCER_IP=$(kubectl get svc gmemegen \
-o=jsonpath='{.status.loadBalancer.ingress[0].ip}' -n default)
echo gMemegen Load Balancer Ingress IP: http://$LOAD_BALANCER_IP

해당 주소의 웹사이트 (밈 모음..인 듯)

 

POD_NAME=$(kubectl get pods --output=json | jq -r ".items[0].metadata.name")
kubectl logs $POD_NAME gmemegen | grep "INFO"

 

Task 4. Verify full read/write capabilities of application to database

 

Connect to the database and query an application table

 

 

1.Run the auto-populated command in Cloud Shell.
2.When prompted, enter the password: supersecret!

 

\c gmemegen_db
select * from meme;

 

참고:  https://www.cloudskillsboost.google/paths/22/course_templates/652/labs/471757?locale=ko

반응형