<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cloud SQL Archives - Codersee blog- Kotlin on the backend</title>
	<atom:link href="https://blog.codersee.com/tag/cloud-sql/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>Kotlin &#38; Backend Tutorials - Learn Through Practice.</description>
	<lastBuildDate>Wed, 16 Apr 2025 04:50:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://blog.codersee.com/wp-content/uploads/2025/04/cropped-codersee_logo_circle_2-32x32.png</url>
	<title>Cloud SQL Archives - Codersee blog- Kotlin on the backend</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Spring Boot on Google GKE with Cloud SQL and Kotlin</title>
		<link>https://blog.codersee.com/spring-boot-on-google-gke-with-cloud-sql-and-kotlin/</link>
					<comments>https://blog.codersee.com/spring-boot-on-google-gke-with-cloud-sql-and-kotlin/#comments</comments>
		
		<dc:creator><![CDATA[Piotr]]></dc:creator>
		<pubDate>Thu, 15 Oct 2020 09:50:39 +0000</pubDate>
				<category><![CDATA[Spring]]></category>
		<category><![CDATA[Cloud SQL]]></category>
		<category><![CDATA[Google Cloud]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Spring Boot]]></category>
		<guid isPermaLink="false">http://codersee.com/?p=1495</guid>

					<description><![CDATA[<p>In this article, I'd like to show you how to deploy Spring Boot Kotlin application to the GKE with Cloud SQL using sidecar pattern.</p>
<p>The post <a href="https://blog.codersee.com/spring-boot-on-google-gke-with-cloud-sql-and-kotlin/">Spring Boot on Google GKE with Cloud SQL and Kotlin</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2 class="article-heading-introduction">1. Introduction</h2>
<p>In <a href="http://codersee.com/deploy-spring-boot-application-to-gke/" target="_blank" rel="noopener noreferrer">one of my past articles</a>, we&#8217;ve learned how to deploy the <strong>Spring Boot Kotlin</strong> application to the <strong>Google Kubernetes Engine</strong>. According to your feedback, you really enjoyed it and some of you asked me to write something more about connecting to the databases in such an environment.</p>
<p>In the beginning, I&#8217;d like to thank you so much for the inspiration. You can be sure, that in the future I will prepare more articles on this topic.</p>
<p>In this guide, I&#8217;d like to show you how to deploy <strong>Spring Boot Kotlin</strong> application to the <strong>GKE with Cloud SQL</strong> using a sidecar pattern. It will be the continuation of <a href="http://codersee.com/deploy-spring-boot-application-to-gke/" target="_blank" rel="noopener noreferrer">this post</a>, so I highly recommend spending some time checking it.</p>
<h2 class="article-heading-main">2. Create Cloud SQL Instance</h2>
<p>As the first step, let&#8217;s head to the <a href="https://console.cloud.google.com/sql/" target="_blank" rel="noopener noreferrer">GCP Cloud SQL console</a> and click the <strong>Create Instance</strong> button.</p>
<p><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-1514" src="http://blog.codersee.com/wp-content/uploads/2020/10/1-1.png" alt="This image cotnains the screenshot from cloud sql create instance page" width="537" height="301" srcset="https://blog.codersee.com/wp-content/uploads/2020/10/1-1.png 537w, https://blog.codersee.com/wp-content/uploads/2020/10/1-1-300x168.png 300w" sizes="(max-width: 537px) 100vw, 537px" /></p>
<p>On the next page, let&#8217;s select the <strong>MySQL database</strong>:</p>
<p><img decoding="async" class="alignnone size-full wp-image-1515" src="http://blog.codersee.com/wp-content/uploads/2020/10/2-1.png" alt="Photo shows the screenshot from cloud sql select database page" width="483" height="315" srcset="https://blog.codersee.com/wp-content/uploads/2020/10/2-1.png 483w, https://blog.codersee.com/wp-content/uploads/2020/10/2-1-300x196.png 300w" sizes="(max-width: 483px) 100vw, 483px" /></p>
<p>Let&#8217;s provide the <em>instance ID</em> and<em> root password</em> for our database. As you can see on the below screenshot, we can also specify other things, like <em>region</em>, <em>zone</em>, or <em>database</em> version:</p>
<p><img decoding="async" class="alignnone size-full wp-image-1516" src="http://blog.codersee.com/wp-content/uploads/2020/10/3-3.png" alt="The photo shows the screenshot from configure instance info page" width="462" height="515" srcset="https://blog.codersee.com/wp-content/uploads/2020/10/3-3.png 462w, https://blog.codersee.com/wp-content/uploads/2020/10/3-3-269x300.png 269w" sizes="(max-width: 462px) 100vw, 462px" /></p>
<p>Finally, let&#8217;s click the <strong>Create</strong> button and start the process of creating the instance.</p>
<h2 class="article-heading-main">3. Create a Service Account</h2>
<p>As the next step, let&#8217;s switch to the <a href="https://console.cloud.google.com/iam-admin/serviceaccounts" target="_blank" rel="noopener noreferrer">service accounts panel</a> of the IAM management. We need to prepare the service account, which will be used later to establish the connection between the <strong>GKE</strong> and <strong>Cloud SQL</strong>.</p>
<p>Let&#8217;s click the <strong>Create Service Account</strong> button. Then, let&#8217;s specify the service account name and click <strong>Create</strong>:</p>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-1517" src="http://blog.codersee.com/wp-content/uploads/2020/10/5-1.png" alt="The image contains the screenshot showing how to provide the service account name for the cloud sql service account" width="620" height="426" srcset="https://blog.codersee.com/wp-content/uploads/2020/10/5-1.png 620w, https://blog.codersee.com/wp-content/uploads/2020/10/5-1-300x206.png 300w" sizes="auto, (max-width: 620px) 100vw, 620px" /></p>
<p>Nextly, let&#8217;s grant the <em><strong>Cloud SQL Admin Role</strong></em> to our service account:</p>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-1518" src="http://blog.codersee.com/wp-content/uploads/2020/10/6-1.png" alt="The image shows the screenshot from the console showing how to add Cloud SQL Admin role to the service account" width="533" height="313" srcset="https://blog.codersee.com/wp-content/uploads/2020/10/6-1.png 533w, https://blog.codersee.com/wp-content/uploads/2020/10/6-1-300x176.png 300w" sizes="auto, (max-width: 533px) 100vw, 533px" /></p>
<p>Please note: for the tutorial, we are using the admin role, but in the real-life scenarios we should always remember about the <strong>Principle of Least Privilege (POLP)</strong>.</p>
<h2 class="article-heading-main">4. Enable Cloud SQL Admin API</h2>
<p>Before we will be able to connect using the Cloud SQL Proxy, we need to enable the Cloud SQL Admin API. To do that, let&#8217;s head to the <a href="https://console.developers.google.com/apis/library/sqladmin.googleapis.com" target="_blank" rel="noopener noreferrer">API management page</a> and click the Enable button:</p>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-1520" src="http://blog.codersee.com/wp-content/uploads/2020/10/api-enable.png" alt="The image shows how to enable the Cloud SQL Admin API" width="515" height="256" srcset="https://blog.codersee.com/wp-content/uploads/2020/10/api-enable.png 515w, https://blog.codersee.com/wp-content/uploads/2020/10/api-enable-300x149.png 300w" sizes="auto, (max-width: 515px) 100vw, 515px" /></p>
<h2 class="article-heading-main">5. Connecting Using the Cloud SQL Proxy</h2>
<p>There are a few approaches on how to connect to our SQL instance. For a better understanding of the topic, I highly recommend checking out <a href="https://cloud.google.com/sql/docs/mysql/connect-kubernetes-engine" target="_blank" rel="noopener noreferrer">this Google guide</a>.</p>
<p>In this tutorial, we will add the proxy to our application&#8217;s pod using the <em>sidecar container pattern</em>. If you would be interested in tutorials covering other ways of connection, please let me know about it.</p>
<h3 class="article-heading-sub">5.1. Add Proxy Container</h3>
<p>Let&#8217;s start by adding the proxy to the deployment file. Let&#8217;s head to the previously created <strong>deployment.yaml</strong> file and put the following config under the <em><strong>containers</strong></em>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="yaml">- image: gcr.io/cloudsql-docker/gce-proxy:1.18.0
  name: cloud-sql-proxy
  command:
    - "/cloud_sql_proxy"
    - "-instances=&lt;PROJECT_ID&gt;:&lt;ZONE&gt;:&lt;INSTANCE_NAME&gt;=tcp:3306"
  securityContext:
    runAsNonRoot: true
</pre>
<p>The second command in our configuration requires us to specify the list of comma-separated fully qualified instances connections names. You can check your connection name in the <a href="https://console.cloud.google.com/sql/" target="_blank" rel="noopener noreferrer">Cloud SQL console</a>.</p>
<h3 class="article-heading-sub">5.2. Connect Service Account</h3>
<p>As the next step, we will provide the service account for our proxy. We will do it using <strong>GKE&#8217;s Workload Identity feature</strong>, which is the preferred method. To put it simply, we will bind the <strong>Kubernetes Service Account (KSA)</strong> to a <strong>Google Service Account (GSA)</strong>.</p>
<p>Let&#8217;s start by enabling the Workload Identity on our existing cluster:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">gcloud container clusters update  \
  --workload-pool=&lt;PROJECT-ID&gt;.svc.id.goog
  --zone=&lt;ZONE&gt;
</pre>
<p>We will need to wait for some time for the changes to be applied.</p>
<p>Nextly, let&#8217;s create the <strong>service-account.yaml</strong> file and put there the configuration for the KSA:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="yaml">apiVersion: v1
kind: ServiceAccount
metadata:
  name: cloud-sql-sa
</pre>
<p>And create the KSA in our cluster:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">kubectl apply -f service-account.yaml</pre>
<p>With that being done, we can enable the IAM binding between the<strong> &lt;GSA-NAME&gt;</strong> and<strong> &lt;KSA-NAME&gt;</strong>:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">gcloud iam service-accounts add-iam-policy-binding \
  --role roles/iam.workloadIdentityUser \
  --member "serviceAccount:&lt;PROJECT-ID&gt;.svc.id.goog[&lt;K8S-NAMESPACE&gt;/&lt;KSA-NAME&gt;]" \
  &lt;GSA-NAME&gt;@&lt;PROJECT-ID&gt;.iam.gserviceaccount.com
</pre>
<p>As the next step, let&#8217;s add an annotation to <strong>&lt;KSA-NAME&gt;</strong> to complete the binding:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">kubectl annotate serviceaccount \
   &lt;KSA-NAME&gt; \
   iam.gke.io/gcp-service-account=&lt;GSA-NAME&gt;@&lt;PROJECT-ID&gt;.iam.gserviceaccount.com
</pre>
<p>Finally, please make sure to specify the service account for the Kubernetes object:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="yaml">spec:
  serviceAccountName: cloud-sql-sa
</pre>
<h2 class="article-heading-main">6. Configure Spring Boot App</h2>
<p>After all the above steps, we can finally switch to our <strong>Spring Boot</strong> project to set up the connection.</p>
<h3 class="article-heading-sub">6.1. Imports</h3>
<p>Let&#8217;s start by adding necessary imports:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="yaml">    runtimeOnly("mysql:mysql-connector-java")
</pre>
<p>As you can see, we are adding <strong>MySQL Connector dependency</strong>. If we had chosen another database in step 2, we would have to import the appropriate driver here.</p>
<h3 class="article-heading-sub">6.2. Edit Application Properties</h3>
<p>As the next step, let&#8217;s edit the <strong>application.yaml</strong> file and put the following configuration there:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="yaml">spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/${DB_NAME}
    username: ${DB_USERNAME}
    password: ${DB_PASSWORD}
</pre>
<p>Please notice, that the environment variables should be set to, or replaced with the correct values. For the learning purposes, as the user, we can use root with the password we&#8217;ve set in step 2. Also, to create the database, we can head to the <a href="https://console.cloud.google.com/sql/instances/mysql-instance1/databases" target="_blank" rel="noopener noreferrer">Cloud SQL Databases console</a>.</p>
<h2 class="article-heading-main">7. Deploy Spring Boot App to GKE</h2>
<p>As the last step, let&#8217;s deploy our application to the<strong> Google Kubernetes Engine</strong>. Let&#8217;s build and push our container with the following commands:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">docker build -t spring-boot-example .
docker tag spring-boot-example gcr.io//spring-boot-example:0.0.2
docker push gcr.io//spring-boot-example:0.0.2
</pre>
<p>Please notice, that you can use the tag whatever you like. I&#8217;m just using <em><strong>0.0.2</strong></em> to keep it concise with the previous article.</p>
<p>Finally, let&#8217;s update our deployment file:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="yaml">containers:
  - image: gcr.io//spring-boot-example:0.0.2
</pre>
<p>And submit our changes to the cluster:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">kubectl apply -f deployment.yaml</pre>
<p>After all, we can check the status of changes, for instance with the get pods command:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="raw">kubectl get pods

//example response
NAME                                   READY   STATUS    RESTARTS   AGE
spring-boot-example-799bb5dd97-whwdb   2/2     Running   0          55s
</pre>
<p>As you can see, the Pod is running and both containers are ready.</p>
<h2 class="article-heading-main">8. Summary</h2>
<p>And that would be all for this article. We&#8217;ve covered step by step how to deploy the <strong>Spring Boot application</strong> to the<strong> Google Kubernetes Engine</strong> with <strong>Cloud SQL</strong>. I believe, that some of the steps may seem pretty difficult, but I can guarantee you that over time, you will get a much better understanding of this topic.</p>
<p>Hope you had a great time reading this article. Please remember, that you can always contact me through the <a href="https://www.facebook.com/codersee/" target="_blank" rel="noopener noreferrer"><strong>fan page</strong></a>, <a href="https://www.facebook.com/groups/622361565094117" target="_blank" rel="noopener noreferrer"><strong>group</strong></a>, or <strong><a href="http://codersee.com/contact/" target="_blank" rel="noopener noreferrer">contact</a> </strong>form. Don&#8217;t be afraid to ask any questions- I will be more than happy to answer them.</p>
<p>And of course, you can find the full source code <a href="https://github.com/codersee-blog/kotlin-spring-boot-gke-cloud-sql" target="_blank" rel="noopener noreferrer"><strong>here</strong></a>.</p>
<p>The post <a href="https://blog.codersee.com/spring-boot-on-google-gke-with-cloud-sql-and-kotlin/">Spring Boot on Google GKE with Cloud SQL and Kotlin</a> appeared first on <a href="https://blog.codersee.com">Codersee blog- Kotlin on the backend</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.codersee.com/spring-boot-on-google-gke-with-cloud-sql-and-kotlin/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: blog.codersee.com @ 2026-02-22 22:12:37 by W3 Total Cache
-->