Create a 3 node docker mysql database which will be orchestrated by GKE
1. Get the credential of the GKE Cluster
Welcome to Cloud Shell! Type "help" to get started.
Your Cloud Platform project in this session is set to qwiklabs-gcp-04-302f12498b68.
Use “gcloud config set project [PROJECT_ID]” to change to a different project.
parwezgcp01@cloudshell:~ (qwiklabs-gcp-04-302f12498b68)$ gcloud container clusters get-credentials cluster-1 --zone us-central1-c --project qwiklabs-gcp-04-302f12498b68
Fetching cluster endpoint and auth data.
kubeconfig entry generated for cluster-1.
parwezgcp01@cloudshell:~ (qwiklabs-gcp-04-302f12498b68)$
parwezgcp01@cloudshell:~ (qwiklabs-gcp-04-302f12498b68)$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
gke-cluster-1-default-pool-854f2c03-4jw5 Ready 5m15s v1.25.8-gke.1000
gke-cluster-1-default-pool-854f2c03-5gxq Ready 5m13s v1.25.8-gke.1000
gke-cluster-1-default-pool-854f2c03-hh4k Ready 5m15s v1.25.8-gke.1000
parwezgcp01 @cloudshell:~ (qwiklabs-gcp-04-302f12498b68)$ mkdir mysql-gke
parwezgcp01 @cloudshell:~ cd mysql-gke
Task 2. Deploy MySQL on the cluster
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ ls -ltr
total 12
-rw-r--r-- 1 parwezgcp01 parwezgcp01 163 Jun 24 09:18 volume.yaml
-rw-r--r-- 1 parwezgcp01 parwezgcp01 902 Jun 24 09:18 deployment.yaml
-rw-r--r-- 1 parwezgcp01 parwezgcp01 156 Jun 24 09:20 service.yaml
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ cat volume.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-data-disk
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
labels:
app: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
ports:
- containerPort: 3306
volumeMounts:
- mountPath: "/var/lib/mysql"
subPath: "mysql"
name: mysql-data
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secrets
key: ROOT_PASSWORD
- name: MYSQL_USER
value: testuser
- name: MYSQL_PASSWORD
value: password
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data-disk
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ cat service.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
selector:
app: mysql
ports:
protocol: TCP
port: 3306
targetPort: 3306
Apply the the conf to the GKE cluster
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$
kubectl apply -f volume.yaml
kubectl apply -f deployment.yaml
kubectl apply -f service.yaml
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl apply -f volume.yaml
persistentvolumeclaim/mysql-data-disk created
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl apply -f deployment.yaml
deployment.apps/mysql-deployment created
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl apply -f service.yaml
service/mysql-service created
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-deployment-75b578b6b5-lks8r 1/1 Running 0 60s
kubectl exec -it mysql-deployment-75b578b6b5-lks8r /bin/bash
Run exec command to login into mysql docket=r image environment
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl exec -it mysql-deployment-75b578b6b5-lks8r /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
bash-4.4#
bash-4.4#
bash-4.4# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
mysql> create database pets;
Query OK, 1 row affected (0.01 sec)
mysql> use pets ;
Database changed
mysql>
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| pets |
| sys |
+--------------------+
5 rows in set (0.01 sec)
Exit and delete conf now, so that we can create a automatics conf file using HELM
kubectl delete -f service.yaml
kubectl delete -f deployment.yaml
kubectl delete -f volume.yaml
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl delete -f service.yaml
service "mysql-service" deleted
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl delete -f deployment.yaml
kubectl delete -f volume.yaml
deployment.apps "mysql-deployment" deleted
3. Use Helm to deploy MySQL on the cluster
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ helm repo update
Hang tight while we grab the latest from your chart repositories…
…Successfully got an update from the "bitnami" chart repository
Update Complete. ⎈Happy Helming!⎈
Install a MySQL named mydb using Helm:
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ helm install mydb bitnami/mysql
NAME: mydb
LAST DEPLOYED: Sat Jun 24 09:38:53 2023
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: mysql
CHART VERSION: 9.10.4
APP VERSION: 8.0.33
** Please be patient while the chart is being deployed **
Tip:
Watch the deployment status using the command: kubectl get pods -w --namespace default
Services:
echo Primary: mydb-mysql.default.svc.cluster.local:3306
Execute the following to get the administrator credentials:
echo Username: root
MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mydb-mysql -o jsonpath="{.data.mysql-root-password}" | base64 -d)
To connect to your database:
Run a pod that you can use as a client:
kubectl run mydb-mysql-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mysql:8.0.33-debian-11-r17 --namespace default --env MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD --command -- bash
To connect to primary service (read/write):
mysql -h mydb-mysql.default.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"
list the helm running
parwezgcp01 @cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ helm ls
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
mydb default 1 2023-06-24 09:38:53.632603173 +0000 UTC deployed mysql-9.10.4 8.0.33
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ echo Username: root
Username: root
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl get secret --namespace default mydb-mysql -o jsonpath="{.data.mysql-root-password}" | base64 -d
z103dZeJdH
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl run mydb-mysql-client --rm --tty -i --restart='Never' --image docker.io/bitnami/mysql:8.0.33-debian-11-r17 --namespace default --env MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD --command -- bash
If you don't see a command prompt, try pressing enter.
I have no name!@mydb-mysql-client:/$
I have no name!@mydb-mysql-client:/$
I have no name!@mydb-mysql-client:/$ mysql -h mydb-mysql.default.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"mysql -h mydb-mysql.default.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"^C
I have no name!@mydb-mysql-client:/$
I have no name!@mydb-mysql-client:/$
I have no name!@mydb-mysql-client:/$ mysql -h mydb-mysql.default.svc.cluster.local -uroot -p"$MYSQL_ROOT_PASSWORD"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 96
Server version: 8.0.33 Source distribution
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| my_database |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
mysql>
mysql> create database pets ;
Query OK, 1 row affected (0.01 sec)
mysql> use pets ;
Database changed
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl get pods ;
NAME READY STATUS RESTARTS AGE
mydb-mysql-0 1/1 Running 0 11m
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$
To see your Helm deployment, enter the following command:
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ helm ls
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
mydb default 1 2023-06-24 09:38:53.632603173 +0000 UTC deployed mysql-9.10.4 8.0.33
To delete the depoyment, enter the following command:
helm delete mydb
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl get pods ;
NAME READY STATUS RESTARTS AGE
mydb-mysql-0 1/1 Running 0 11m
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ helm delete mydb
release "mydb" uninstalled
parwezgcp01@cloudshell:~/mysql-gke (qwiklabs-gcp-04-302f12498b68)$ kubectl get pods ;
No resources found in default namespace.
931 total views, 2 views today
