I am attempting to complete the following tutorial for deploying Wordpress on GKE: https://cloud.google.com/kubernetes-engine/docs/tutorials/persistent-disk
I have used terraform for provisioning the gcp resources, instead of gcp as the tutorial recommends. Here is the deployment that is resulting in a CrashLoopBackOff state.
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
replicas: 1
selector:
matchLabels:
app: wordpress
template:
metadata:
labels:
app: wordpress
spec:
containers:
- image: wordpress
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: 127.0.0.1:3306
# These secrets are required to start the pod.
- name: WORDPRESS_DB_USER
valueFrom:
secretKeyRef:
name: cloudsql-db-credentials
key: username
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: cloudsql-db-credentials
key: password
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
# Change archtek-wordpress:us-west1:archtek-wordpress-postgres-instance here to include your GCP
# project, the region of your Cloud SQL instance and the name
# of your Cloud SQL instance. The format is
# ::
- name: cloudsql-proxy
image: gcr.io/cloudsql-docker/gce-proxy:1.11
command: ["/cloud_sql_proxy",
"-instances=archtek-wordpress:us-west1:archtek-wordpress-mysql-instance=tcp:3306",
# If running on a VPC, the Cloud SQL proxy can connect via Private IP. See:
# https://cloud.google.com/sql/docs/mysql/private-ip for more info.
# "-ip_address_types=PRIVATE",
"-credential_file=/secrets/cloudsql/key.json"]
securityContext:
runAsUser: 2 # non-root user
allowPrivilegeEscalation: false
volumeMounts:
- name: cloudsql-instance-credentials
mountPath: /secrets/cloudsql
readOnly: true
imagePullPolicy: Always
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: wordpress-volumeclaim
- name: cloudsql-instance-credentials
secret:
secretName: cloudsql-instance-credentials
When I describe the pod, I see the following in the logs:
wordpress-54c68dbf59-5djfx wordpress MySQL Connection Error: (2002) Connection refused
To rule out the idea that the credentials are invalid, I took the username and password used to create cloudsql-db-credentials
, the k8s secret referenced in my deployment yaml, and ran this.
$: gcloud sql connect archtek-wordpress-mysql-instance -u wordpress
I can connect, no problem. But what I discovered I also cannot do is this:
$: mysql -u wordpress -p'$CLOUD_SQL_PASSWORD' \ ()
-h 35.197.7.98 -P 3306 \
-D archtek-wordpress:us-west1:archtek-wordpress-mysql-instance -v
which returns:
ERROR 2003 (HY000): Can't connect to MySQL server on '35.197.7.98' (60)
I know that when using the gcloud
client to connect to a cloudsql database, it whitelists for ip for a 5 minute period prior to authentication, which might explain why the mysql
client fails to authenticate. However, I'm not sure if this rationale holds up for my deployment in the cluster. Does it also need to be whitelisted for cloudsql to accept auth requests?
Here is the terraform file for provisioning the cloudsql instance:
resource "google_sql_database_instance" "postgres" {
name = "archtek-wordpress-mysql-instance"
database_version = "MYSQL_5_7"
settings {
tier = "db-f1-micro"
availability_type = "ZONAL"
}
}
kubectl exec <pod name> -- mysql -u wordpressb -p '$CLOUD_SQL_PASSWORD' -h 35.197.7.98 -P 3306 -D archtek-wordpress:us-west1:archtek-wordpress-mysql-instance -v
– Patrick WERROR 2003 (HY000): Can't connect to MySQL server on '35.197.7.98' (110)
– Kyle Greencloudsql
instance with proxy from wordpress pod without any additional reconfiguration, gcloud command andmysql
command (after adding the IP you are connecting from). Could you show the Terraform file responsible for creating thiscloudsql
instance? Also you have a typo inmysql
command: "wordpressb". – Dawid Kruk