This example shows how to connect to Google Cloud SQL from an application running on Google Kubernetes Engine.
The mysql_wordpress_deployment.yaml
manifest file
that consists of two containers:
- A web frontend container running WordPress.
- A sidecar container for Cloud SQL Proxy container providing connectivity to Cloud SQL.
⚠️ NOTE: This example does not provide a WordPress example that is ready to use in production, as it does not configure a persistent disk for the WordPress set up and may yield in data loss. Follow the Using Persistent Disks with WordPress and MySQL for an example that is ready to use.
If you are looking for a PostgreSQL example, see postgres_deployment.yaml
.
Follow the tutorial at Connecting from Google Container Engine.
After you follow the tutorial, you must have Secrets named
cloudsql-instance-credentials
and cloudsql-db-credentials
in your cluster:
$ kubectl get secrets
NAME TYPE DATA AGE
cloudsql-db-credentials Opaque 2 33m
cloudsql-instance-credentials Opaque 1 35m
Open mysql_wordpress_deployment.yaml
and modify <INSTANCE_CONNECTION_NAME>
with
your the connection name of your Cloud SQL instance.
Then apply the manifest:
$ kubectl apply -f mysql_wordpress_deployment.yaml
deployment "wordpress" created
After a while, you should see a Pod with two containers is "Running":
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
wordpress-1615343444-d95v3 2/2 Running 0 1m
This means the WordPress application is able to connect the Cloud SQL proxy. Follow the next step to expose the WordPress container with a load balancer and set up the blog to verify the connection to the Cloud SQL instance.
If the Pod fails to start, consider troubleshooting using commands:
kubectl describe deployment wordpress
kubectl logs -l app=wordpress -c web
kubectl logs -l app=wordpress -c cloudsql-proxy
The following command will create a public IP and load balancer for the WordPress deployment.
$ kubectl expose deployment wordpress --type=LoadBalancer
service "wordpress" exposed
Run kubectl get services
and wait until the wordpress
service gets a value
for EXTERNAL-IP
.
Then visit this IP address and complete setting up the blog. If you succeed, it means the WordPress container is successfully using the Cloud SQL instance.
The following command will delete the Deployment and Service resources:
$ kubectl delete service,deployment -l app=wordpress