title | description |
---|---|
GKE Storage with GCP Cloud SQL - MySQL Public Instance |
Use GCP Cloud SQL MySQL DB for GKE Workloads |
- Verify if GKE Cluster is created
- Verify if kubeconfig for kubectl is configured in your local terminal
# Configure kubeconfig for kubectl
gcloud container clusters get-credentials <CLUSTER-NAME> --zone <ZONE> --project <PROJECT>
# Replace Values CLUSTER-NAME, ZONE, PROJECT
gcloud container clusters get-credentials standard-cluster-1 --zone us-central1-c --project kdaida123
- GKE Private Cluster
- GCP Cloud SQL with Public IP and Authorized Network for DB as entire internet (0.0.0.0/0)
- Go to SQL -> Choose MySQL
- Instance ID: ums-db-public-instance
- Password: KalyanReddy13
- Database Version: MYSQL 8.0
- Choose a configuration to start with: Development
- Choose region and zonal availability
- Region: US-central1(IOWA)
- Zonal availability: Single Zone
- Primary Zone: us-central1-a
- Customize your instance
- Machine Type
- Machine Type: LightWeight (1 vCPU, 3.75GB)
- STORAGE
- Storage Type: HDD
- Storage Capacity: 10GB
- Enable automatic storage increases: CHECKED
- CONNECTIONS
- Instance IP Assignment:
- Private IP: UNCHECKED
- Public IP: CHECKED
- Authorized networks
- Name: All-Internet
- Network: 0.0.0.0/0
- Click on DONE
- Instance IP Assignment:
- DATA PROTECTION
- Automatic Backups: UNCHECKED
- Enable Deletion protection: UNCHECKED
- Maintenance: Leave to defaults
- Flags: Leave to defaults
- Labels: Leave to defaults
- Click on CREATE INSTANCE
# Telnet Test
telnet <MYSQL-DB-PUBLIC-IP> 3306
# Replace Public IP
telnet 35.184.228.151 3306
## SAMPLE OUTPUT
Kalyans-Mac-mini:25-GKE-Storage-with-GCP-Cloud-SQL kalyanreddy$ telnet 35.184.228.151 3306
Trying 35.184.228.151...
Connected to 151.228.184.35.bc.googleusercontent.com.
Escape character is '^]'.
Q
8.0.26-google?h'Sxcr+?nd'h<a(X`z=mysql_native_password2#08S01Got timeout reading communication packetsConnection closed by foreign host.
Kalyans-Mac-mini:25-GKE-Storage-with-GCP-Cloud-SQL kalyanreddy$
- Go to SQL -> ums-db-public-instance -> Databases -> CREATE DATABASE
- Database Name: webappdb
- Character set: utf8
- Collation: Default collation
- Click on CREATE
- Update Cloud SQL MySQL DB
Public IP
in ExternalName Service
apiVersion: v1
kind: Service
metadata:
name: mysql-externalname-service
spec:
type: ExternalName
externalName: 35.184.228.151
apiVersion: v1
kind: Secret
metadata:
name: mysql-db-password
type: Opaque
data:
db-password: S2FseWFuUmVkZHkxMw==
# Base64 of KalyanReddy13
# https://www.base64encode.org/
# Base64 of KalyanReddy13 is S2FseWFuUmVkZHkxMw==
apiVersion: apps/v1
kind: Deployment
metadata:
name: usermgmt-webapp
labels:
app: usermgmt-webapp
spec:
replicas: 1
selector:
matchLabels:
app: usermgmt-webapp
template:
metadata:
labels:
app: usermgmt-webapp
spec:
initContainers:
- name: init-db
image: busybox:1.31
command: ['sh', '-c', 'echo -e "Checking for the availability of MySQL Server deployment"; while ! nc -z mysql-externalname-service 3306; do sleep 1; printf "-"; done; echo -e " >> MySQL DB Server has started";']
containers:
- name: usermgmt-webapp
image: stacksimplify/kube-usermgmt-webapp:1.0.0-MySQLDB
imagePullPolicy: Always
ports:
- containerPort: 8080
env:
- name: DB_HOSTNAME
value: "mysql-externalname-service"
- name: DB_PORT
value: "3306"
- name: DB_NAME
value: "webappdb"
- name: DB_USERNAME
value: "root"
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-db-password
key: db-password
apiVersion: v1
kind: Service
metadata:
name: usermgmt-webapp-lb-service
labels:
app: usermgmt-webapp
spec:
type: LoadBalancer
selector:
app: usermgmt-webapp
ports:
- port: 80 # Service Port
targetPort: 8080 # Container Port
# Deploy Kubernetes Manifests
kubectl apply -f kube-manifests/
# List Deployments
kubectl get deploy
# List Pods
kubectl get pods
# List Services
kubectl get svc
# Verify Pod Logs
kubectl get pods
kubectl logs -f <USERMGMT-POD-NAME>
kubectl logs -f usermgmt-webapp-6ff7d7d849-7lrg5
# List Services
kubectl get svc
# Access Application
http://<ExternalIP-from-get-service-output>
Username: admin101
Password: password101
## Verify from Kubernetes Cluster, we are able to connect to MySQL DB
# Template
kubectl run -it --rm --image=mysql:8.0 --restart=Never mysql-client -- mysql -h <Kubernetes-ExternalName-Service> -u <USER_NAME> -p<PASSWORD>
# MySQL Client 8.0: Replace External Name Service, Username and Password
kubectl run -it --rm --image=mysql:8.0 --restart=Never mysql-client -- mysql -h mysql-externalname-service -u root -pKalyanReddy13
mysql> show schemas;
mysql> use webappdb;
mysql> show tables;
mysql> select * from user;
mysql> exit
# Access Application
http://<ExternalIP-from-get-service-output>
Username: admin101
Password: password101
# Create New User (Used for testing `allowVolumeExpansion: true` Option)
Username: admin102
Password: password102
First Name: fname102
Last Name: lname102
Email Address: admin102@stacksimplify.com
Social Security Address: ssn102
# MySQL Client 8.0: Replace External Name Service, Username and Password
kubectl run -it --rm --image=mysql:8.0 --restart=Never mysql-client -- mysql -h mysql-externalname-service -u root -pKalyanReddy13
mysql> show schemas;
mysql> use webappdb;
mysql> show tables;
mysql> select * from user;
mysql> exit
# Delete Kubernetes Objects
kubectl delete -f kube-manifests/
# Delete Cloud SQL MySQL Instance
1. Go to SQL -> ums-db-public-instance -> DELETE
2. Instance ID: ums-db-public-instance
3. Click on DELETE