<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kadir Emre Ozcan on Medium]]></title>
        <description><![CDATA[Stories by Kadir Emre Ozcan on Medium]]></description>
        <link>https://medium.com/@kadiremreozcan?source=rss-b61c237af83b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*5hIinAVYWaknVvTdjv4EYg.jpeg</url>
            <title>Stories by Kadir Emre Ozcan on Medium</title>
            <link>https://medium.com/@kadiremreozcan?source=rss-b61c237af83b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 17:57:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kadiremreozcan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Deploy Simple Spring Boot CRUD App to AWS Elastic Beanstalk and Connect AWS Relational Database…]]></title>
            <link>https://kadiremreozcan.medium.com/deploy-simple-spring-boot-crud-app-to-aws-elastic-beanstalk-and-connect-aws-relational-database-1a2de074043e?source=rss-b61c237af83b------2</link>
            <guid isPermaLink="false">https://medium.com/p/1a2de074043e</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[amazon-web-services]]></category>
            <category><![CDATA[elastic-beanstalk]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[spring-boot]]></category>
            <dc:creator><![CDATA[Kadir Emre Ozcan]]></dc:creator>
            <pubDate>Mon, 15 Mar 2021 15:42:45 GMT</pubDate>
            <atom:updated>2021-03-15T15:42:45.142Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deploy Simple Spring Boot CRUD App to AWS Elastic Beanstalk and Connect AWS Relational Database Service(RDS)</h3><p>Simple Deployment Example of Spring Boot CRUD Application to AWS Elastic Beanstalk and Connection example of AWS RDS</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*O8QG7ghox_EgmDEslfkfJA.png" /></figure><h3>👋Introduction</h3><p>This is a <strong><em>Beginner Level</em> <em>Tutorial</em></strong> about How to deploy a Spring Boot CRUD Application on AWS Elastic Beanstalk and Connect to AWS Relational Database Service (RDS). I created a simple Todos application with Spring Boot. I am going to use Postman to emulate CRUD operations.</p><p>You can download the application <a href="https://github.com/emreozcan3320/Simple-Spring-Boot-Todos-App-For-AWS-BeanStalk-RDS.git">source code</a>.</p><h3>👈What This Post is NOT About👉</h3><ol><li>Not an advanced AWS tutorial</li><li>Not an Elastic Beanstalk tutorial</li><li>Not a Spring Boot CRUD web application tutorial</li></ol><h3>👉What This Post is About👈</h3><ol><li>To show how you can deploy an application to Elastic Beanstalk.</li><li>To show how you can connect AWS RDS</li><li>To show how you can check logs for simple errors.</li></ol><h3>🚨Prerequisites</h3><ol><li>Spring Boot knowledge</li><li>Jar creation knowledge</li><li>Basic AWS knowledge</li></ol><h3>📖 TLTR;</h3><p>1- Download Source Code of CRUD Spring Boot Application<br>2- Create A Database on Local MySql<br>3- Connect Spring Boot to Local MySql DB<br>4- Test Local Endpoint with Postman<br>5- Create a MySql Database on AWS<br>6- Configure AWS RDS for Local Connection<br>7- Configure Local to Connect AWS RDS<br>8- Create and Configure an Elastic Beanstalk Application<br>9- Deploy Spring Boot Application to AWS Elastic Beanstalk<br>10- Check Logs for Error<br>11- Test Deployed Application<br>12- Resources</p><h3>Download Source Code of CRUD Spring Boot Application</h3><p>I created a very simple Spring Boot todo application which we will use in this post. We are going to use Postman to emulate front-end actions. You can download the source code via this <a href="https://github.com/emreozcan3320/Simple-Spring-Boot-Todos-App-For-AWS-BeanStalk-RDS.git">link</a>.</p><h3>Create A Database on Local MySql</h3><p>I am going to create a local database to test before deploying my application to AWS. I am using PhpMyAdmin as GUI. You can use GUI or terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/206/0*bgpefIcFIlpZDBtn" /></figure><pre>DB Name =&gt; aws_crud_demo<br>User Name =&gt; root<br>Password =&gt; password</pre><p>We are going to use the above information to configure the spring application.</p><h3>Connect Spring Boot to Local MySql DB</h3><p>We need to add a couple of dependencies to connect MySql.</p><pre><em>&lt;</em>dependency<em>&gt;<br>    &lt;</em>groupId<em>&gt;</em>org.springframework.boot<em>&lt;/</em>groupId<em>&gt;<br>    &lt;</em>artifactId<em>&gt;</em>spring-boot-starter-data-jpa<em>&lt;/</em>artifactId<em>&gt;<br>&lt;/</em>dependency<em>&gt;<br>&lt;</em>dependency<em>&gt;<br>    &lt;</em>groupId<em>&gt;</em>mysql<em>&lt;/</em>groupId<em>&gt;<br>    &lt;</em>artifactId<em>&gt;</em>mysql-connector-java<em>&lt;/</em>artifactId<em>&gt;<br>    &lt;</em>scope<em>&gt;</em>runtime<em>&lt;/</em>scope<em>&gt;<br>&lt;/</em>dependency<em>&gt;</em></pre><p>Finally, we need to add the following properties to the application.properties file.</p><pre>spring.jpa.hibernate.ddl-auto=update<br>spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/aws_crud_demo<br>spring.datasource.username=root<br>spring.datasource.password=password</pre><p>All set.</p><h3>Test Local Endpoint with Postman</h3><p>All CRUD operations are working.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GVVSklY71UG2JkWuFyyEZw.png" /></figure><h3>Create a MySql Database on AWS</h3><p>First, find the RDS service on the AWS console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oVv7fGAtXa2OQRuDJvUYmQ.png" /></figure><p>There will be a configuration page. So I am going to show you my configurations.</p><p>Choose MySql as an Engine option. I also choose Dev/Test environment for minimum resource usage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KdNgR6YG93iIeldCmSPSPw.png" /></figure><p>We have to name our DB instance. Don&#39;t forget it is not the name of the Database, it is the name of the DB instance. I give the same username and password as Local for convenience.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u6t-SvS1hpd14bzzUTRhCg.png" /></figure><p>I give 20GiB volume, you can give more it is up to you.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ctVFAv3zldx5Wf4XyhGA_g.png" /></figure><p>We want to connect this DB from our local too. Therefore we need to open it for public access (<strong><em>NOT PRODUCTION RECOMMENDED</em></strong>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iV_rJYX_pYAi2zOMqtoQpg.png" /></figure><p>Now it is time to create the Database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*exeNC0QB6dwPpoZZ6-6-Aw.png" /></figure><p>AWS gives an estimated monthly cost.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LxOrQcsp4JByGC8jUqfuUA.png" /></figure><p>instance creation takes a while.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*f8ZiV5BXHrZf00WK" /></figure><p>And created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i-GI2TIFoJnIRrnkaOxQhA.png" /></figure><p>If you click instance name, you could find the URL of Database</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2zIa-M3UThyavXk09Mz1mA.png" /></figure><h3>Configure AWS RDS for Local Connection</h3><p>As I said, we want to connect this DB instance from our local. Therefore, some additional configuration is needed.</p><p>If You click the DB instance, AWS will take you to the details page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i-GI2TIFoJnIRrnkaOxQhA.png" /></figure><p>There is a security part on the detail page. When you click it, it will take you Security Groups and when you click security groups, it will take you inbound rules which we need.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IjbEgjrZbj1DZQpXxjoRUA.png" /></figure><p>We are going to add a new rule for Local connection. As you notice, <strong>it is only for the sake of this tutorial, I <em>do not recommend this kind of rule</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FGTHlxbimri1f2s-y_1AAg.png" /></figure><p>Rule successfully added.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3g0EzRCLZUNdoUXtlgWybw.png" /></figure><h3>Configure Local to Connect AWS RDS</h3><p>It is very simple actually. Just replace local URL with AWS RDS URL on application properties. I comment out the old one and added the new one.</p><pre>#spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/aws_crud_demo</pre><pre>spring.datasource.url=jdbc:mysql://aws-demo-instance.cluuww5tunoz.eu-north-1.rds.amazonaws.com:3306/aws_crud_demo</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yiitr4hafaSkWO1hBya4jA.png" /></figure><p>It is connected and I check all CRUD operations are working.</p><h3>Create and Configure an Elastic Beanstalk Application</h3><p>Choose Elastic Beanstalk service from the console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O2oOAqwVBmY997XYRJLfuA.png" /></figure><p>There will be a welcoming page. Choose Create Application</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CLUkUNDCWK3jgiUCkNwzeQ.png" /></figure><p>We will configure Elastic Beanstalk</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vzMqZQAif4gFb2p_RIFSzA.png" /></figure><p>we are going to connect our already created AWS RDS instance to the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8lGA3Vz8mtQ746CKWr2wEw.png" /></figure><p>Fill in the required fields and save.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wjKgDS_Qm8IRh0aJwkSGoQ.png" /></figure><p>save all configurations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*P0W0aUVd_rWrZcpp" /></figure><p>Application creation takes a while.</p><h3>Deploy Spring Boot Application to AWS Elastic Beanstalk</h3><p>After creation completed, the application page will be opened. There is an Upload and deploy button. We are going to upload our application&#39;s Jar file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*V26Au2IXzBT4c-NnlRRLgA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1014/1*FjZX2DTQ-y4QS6dD6s7ZCA.png" /></figure><p>Upload finished AWS deploys our application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*i39h-nm-fr6fMn2w" /></figure><p>Application deployed, there is a URL that we can use for CRUD operations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BOFdfHx0UlxYQd236i66UA.png" /></figure><p>But it is not working.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*R3n6kmABLiSWCqIu" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vud5Y3YwQqqx4bOu" /></figure><h3>Check Logs for Error</h3><p>There is a Logs a menu item at the left sidebar on the same page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/727/1*NFusGTOEjFbq_0IKRCnPVw.png" /></figure><p>It let you download all logs related to the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g8bLOp5d4jRVD3tq5JYNzA.png" /></figure><p>And When I examine it, notice that it is a port error. Our application listens 8080 but Nginx listens 5000 .</p><p>There are 2 solutions.</p><ol><li>Change application port on application.properties file. Rebuild application and upload jar.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*i7_pxDh9BpVQUgPwoI86zA.png" /></figure><p>2. There is configurationon the left sidebar. It can take you to software configurations and you can add a new environment variable .</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*O2sfNeQ-JJ41Ymkzkjs2WQ.png" /></figure><h3>Test Deployed Application</h3><p>Now, It is working</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pJVSrAweqRgbEywiirARzA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QN6-2gFXyD5sp7CeuwDv1A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7Xv8RzTjNnIuUuWBIO5Y5Q.png" /></figure><p>Everythings are working.</p><h4>Resources</h4><ul><li><a href="https://aws.amazon.com/blogs/devops/deploying-a-spring-boot-application-on-aws-using-aws-elastic-beanstalk/">Deploying a Spring Boot Application on AWS Using AWS Elastic Beanstalk | Amazon Web Services</a></li><li><a href="https://spring.io/projects/spring-boot">Spring Boot</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1a2de074043e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Deploy a Stateful CRUD Web Application to Minikube(Local Cluster) and Google Cloud…]]></title>
            <link>https://kadiremreozcan.medium.com/how-to-deploy-a-stateful-crud-web-application-to-minikube-local-cluster-and-google-cloud-69b8181ed36b?source=rss-b61c237af83b------2</link>
            <guid isPermaLink="false">https://medium.com/p/69b8181ed36b</guid>
            <category><![CDATA[gke]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[spring-cloud]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[minikube]]></category>
            <dc:creator><![CDATA[Kadir Emre Ozcan]]></dc:creator>
            <pubDate>Tue, 29 Dec 2020 12:41:02 GMT</pubDate>
            <atom:updated>2020-12-29T13:17:12.650Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Deploy a Stateful CRUD Web Application to Minikube(Local Cluster) and Google Cloud Kubernetes Engine(GKE)</h3><p>Multi-container stateful web application deployment to different Kubernetes environments</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1BF_eIkV6wkuqsziZ_hf7Q.png" /></figure><h3>👋Introduction</h3><p>Kubernetes is very helpful to understand the Cloud architecture and how we got where we are today. When you start to work with Kubernetes without understanding why we need it, it may be felt overwhelming. But if you know due to what requirements this product appeared and you actually need to use it, Kubernetes is fun to understand.</p><p>I created a CRUD application and used Docker/Docker-compose for local development in <a href="https://kadiremreozcan.medium.com/how-to-use-docker-and-docker-compose-for-mysql-phpmyadmin-spring-boot-angular-stack-crud-web-b7b868a7c69e">my previous post</a>. In that post, I told “I am also planning to use Kubernetes for this project in the future” so this is the day🎉. We are going to deploy <em>Angular 9</em>,<em> Spring Boot</em>, and <em>MySql</em> stack Stateful CRUD web application to Google Cloud Kubernetes Engine(GKE) and local Minikube cluster for testing and development purposes.</p><p>The final GitHub repository is below. Frontend and Backend repositories were added as git submodules therefore run git submodule update --init --recursive command to download them too.</p><p><a href="https://github.com/emreozcan3320/quote-kubernetes-application-deployment-files.git">emreozcan3320/quote-kubernetes-application-deployment-files</a></p><h4>👈What This Post is NOT About👉</h4><ol><li>Not step by step guide. I am just going to explain some important parts, some code snippets, some screenshots, and a GitHub repository for the entire project which you can dive into.</li><li>Not Kubernetes 101</li><li>Not a deep down explanation of Kubernetes, just an example of its usage</li><li>Not a Spring Boot CRUD web application tutorial. There are enough of them out there.</li><li>Not a production-ready code but definitely helps.</li></ol><h4>👉What This Post is About👈</h4><ol><li>To show how you can deploy an application to Kubernetes.</li><li>To show how you can test an application on local with Kubernetes.</li><li>To give you a reference codebase.</li></ol><h4>🚨Prerequisites</h4><ol><li>Docker knowledge</li><li>Basic Kubernetes knowledge</li><li>Basic Google Cloud knowledge (like project creation, service activation)</li><li>This project uses <a href="https://github.com/emreozcan3320/star-wars-quote-web-application.git">the codes</a> of <a href="https://kadiremreozcan.medium.com/how-to-use-docker-and-docker-compose-for-mysql-phpmyadmin-spring-boot-angular-stack-crud-web-b7b868a7c69e">the previous post</a> so it is helpful to check it too.</li></ol><h3>📖TLTR;</h3><p>1- What is Minikube<br>2- Install and Run Minikube<br>3- What is Api Server<br>4- What is kubectl<br> — UI Interaction Example<br> — REST API Interaction Example<br>5- Usage of Google Container Registry as Private Container Registry<br> — How to Pull/Push Images to Google Container Registry<br> — Use gcloud Credential Helper to Configure Docker<br>6- Configure Minikube to Access Private Container Registry<br> — Download GCP Project Default Account Private Key<br> — Create Kubernetes Secret to Pull Images <br>7- Create Deployment YAML File for Kubernetes<br> — Convert Docker-compose YAML to Kubernetes YAML<br> — — Kompose Version<br> — — kubectl Version<br> — Angular Deployment YAML <br> — Spring Boot Deployment YAML<br>8- Create Internal Services<br> — Service Pod Relation (label, Endpoint Object)<br> — Angular Service YAML<br> — Spring Boot Service YAML<br>9- Expose Application to Outside World<br> — Create Load Balancer as External Service<br> — Configure Angular to Communicate Backend Over Kubernetes Network<br>10- Create a Deployment to Minikube<br> — Deploy to Minikube<br>11- Create a Deployment to Google Cloud<br> — Activate and Connect Google Cloud SQL<br> — Connect Spring Boot to Google Cloud SQL<br> — Create a Kubernetes Cluster at Google Cloud with GKE<br> — Deploy to Google Kubernetes Engine(GKE)<br>References</p><h3>What is Minikube</h3><p>So we are developing or want to test an application that is going to or already works with Kubernetes. If we want something like that may need to look for a local solution. Therefore, the questions that we should ask are <em>“How can I create a Kubernetes setup on the local machine?” </em>or<em> “do I have to create a whole Kubernetes cluster myself</em>😟<em>?”</em>.</p><p>Thank god <strong>NO</strong>🎊. What we need is <strong>Minikube a ONE Node cluster</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/137/1*7dHEJo8sPi9nId-j7wgFRg.png" /></figure><blockquote>minikube is local Kubernetes, focusing on making it easy to learn and develop for Kubernetes.</blockquote><blockquote>All you need is Docker (or similarly compatible) container or a Virtual Machine environment, and Kubernetes is a single command away: minikube start</blockquote><h3>Install and Run Minikube</h3><p>I recommend <a href="https://minikube.sigs.k8s.io/docs/start/">Minikube website installation documentation</a>, it covers the installation process for all operating systems.</p><p><em>🧠One more important note; We need a container or a virtual machine manager, such as </em><a href="https://minikube.sigs.k8s.io/docs/drivers/docker/"><em>Docker</em></a><em>, </em><a href="https://minikube.sigs.k8s.io/docs/drivers/hyperkit/"><em>Hyperkit</em></a><em>, </em><a href="https://minikube.sigs.k8s.io/docs/drivers/hyperv/"><em>Hyper-V</em></a><em>, </em><a href="https://minikube.sigs.k8s.io/docs/drivers/kvm2/"><em>KVM</em></a><em>, </em><a href="https://minikube.sigs.k8s.io/docs/drivers/parallels/"><em>Parallels</em></a><em>, </em><a href="https://minikube.sigs.k8s.io/docs/drivers/podman/"><em>Podman</em></a><em>, </em><a href="https://minikube.sigs.k8s.io/docs/drivers/virtualbox/"><em>VirtualBox</em></a><em>, or </em><a href="https://minikube.sigs.k8s.io/docs/drivers/vmware/"><em>VMWare</em></a><em> in order to run Minikube.</em></p><p>I have docker on my machine therefore problem fixed😏</p><pre>$minikube start --driver=docker</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BlAIPd4FTUZeUp4R" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/694/1*26DzzmEwZ6AaqyXDu_L-tg.png" /></figure><p><em>🧠Note: If you are using Docker with </em><strong><em>root privileges</em></strong><em>, you are probably going to get errors. You can check the following page for a solution.</em></p><p><a href="https://github.com/kubernetes/minikube/issues/7903">The &quot;docker&quot; driver should not be used with root privileges. · Issue #7903 · kubernetes/minikube</a></p><h3>What is Api Server</h3><p>We created a Kubernetes cluster on our local with the help of Minikube so how we can communicate with it.</p><p>There are <strong><em>worker nodes</em></strong> and <strong><em>master nodes</em></strong><em> </em>on Kubernetes. As the name indicates our application run by worker nodes so what does the master node responsible for?</p><blockquote>A master node is a node which controls and manages a set of worker nodes (workloads runtime) and resembles a cluster in Kubernetes</blockquote><p><strong><em>4 processes</em></strong> must be installed on every Master Node; <em>Api Server</em>, <em>Scheduler</em>, <em>Controller Manager,</em> and <em>etcd</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*H4fXV861eDAQASkkw7pt1g.png" /><figcaption>Processes of Master Node</figcaption></figure><p>They all have particular responsibilities but our focus is <strong><em>Api Server</em></strong>.</p><blockquote>The API Server services REST operations and provides the frontend to the cluster’s shared state through which all other components interact.</blockquote><p>This means the Api Server is our connection with the cluster. So what kind of client we can use to interact with it.</p><h3>What is kubectl</h3><blockquote>The kubectl command line tool lets you control Kubernetes clusters.</blockquote><p><a href="https://kubernetes.io/docs/tasks/tools/install-kubectl/">Follow the link to install and set up kubectl;</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/906/1*KvzD_KIZDKr_Obaiiap4eg.png" /></figure><p>We can interact with Kubernetes now🙂</p><p>But kubectl is not the only way. You also interact with the Kubernetes cluster through a <strong><em>UI</em></strong> or <strong><em>API</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/1*oqxHqmVfVIqCLftgXTHr2A.png" /></figure><h4>UI Interaction Example</h4><p>We already installed Minikube and it provides a dashboard.</p><pre>$minkube dashboard</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N49vXu5Pp6CRL6OTFc8tyw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XnFYy8vOuDIWtM5GFzQZBQ.png" /></figure><p>Google Cloud Kubernetes Engine also provides a dashboard too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tx1KBJpHBC2wcFqN4GStHg.png" /></figure><h4>REST API Interaction Example</h4><p>If we want to directly access the REST API, there are steps we have to follow for locating and authenticating. I am going to use an HTTP Proxy to Access the Kubernetes API which is shown in <a href="https://kubernetes.io/docs/tasks/extend-kubernetes/http-proxy-access-api/"><em>the Kubernetes Documentation</em></a>. Also, you can find more detail at <a href="https://kubernetes.io/docs/tasks/access-application-cluster/access-cluster/#directly-accessing-the-rest-api"><em>the Kubernetes official documentation</em></a> about <strong>Directly accessing the REST API</strong>.</p><pre>$kubectl create deployment node-hello --image=gcr.io/google-samples/node-hello:1.0 --port=8080<br>deployment.apps/node-hello created</pre><pre>$kubectl proxy --port=8080<br>Starting to serve on 127.0.0.1:8080<br></pre><p>Now, we can see all API endpoints,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*g0fCwKiz_3Mp0P1eV3NRag.png" /></figure><p>Don&#39;t forget to delete node-hello deployment.</p><pre>$kubectl delete deployment node-hello</pre><h3>Usage of Google Container Registry as Private Container Registry</h3><p>We have to pass our docker images somehow to Kubernetes deployment. There are 3 options;</p><ol><li>Use Docker Hub (Default)</li><li>Use Private Registry</li><li>Use Local image(just works for the local cluster)</li></ol><p>I am going to choose the <strong>Private Registry</strong> option and <strong><em>Google Container Registry</em></strong> as a private container registry.</p><p><em>🧠Note: If you don&#39;t have a Google Cloud account don&#39;t worry you can sign-up right away and google gives you 300$ worth of credit to spend.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G-gZy6Gij65sD3lQn4h-kQ.png" /></figure><h4>How to Pull/Push Images to Google Container Registry</h4><ol><li>You have to install Google Cloud SDK to interact with Google Cloud. It is very simple just follow <a href="https://cloud.google.com/sdk/docs/install#deb">the instruction at Google Documentation</a>.</li></ol><p>2. We have to push our image to Google Container Registry. We can use Docker for that but we have to configure it.</p><p><em>🧠Note: you can also use </em><em>gcloud builds submit which handle build and push steps.</em></p><h4><strong>Use gcloud Credential Helper to Configure Docker</strong></h4><p>First, we have to configure docker. Check <a href="https://cloud.google.com/container-registry/docs/advanced-authentication#gcloud-helper">the official documentation</a>.</p><pre>$gcloud auth login<br>$gcloud auth configure-docker</pre><p>Docker is configured to interact with Google Container Registry. We are going to simply push our images to Google Container Registry now. You can check <a href="https://cloud.google.com/container-registry/docs/pushing-and-pulling">the official documentation</a> for details.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/481/1*3VTubhNLjPkB1tBZcW1IHA.png" /></figure><p><em>HOSTNAME</em> → gcr.io</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*1eDQVM9W6YWsyPiF8GpzZA.png" /></figure><p><em>PROJECT-ID</em> → bionic-trilogy-298608 (the project you created for container registry)</p><p><em>IMAGE</em> → It is up to you. We are going to give <em>quote-application-api</em> for BE and <em>quote-application-web</em> for FE.</p><p><em>TAG</em> → default is the latest so we just leave it.</p><pre>$docker push gcr.io/bionic-trilogy-298608/quote-application-api</pre><h3>Configure Minikube to Access Private Container Registry</h3><p>We have to configure Minikube to pull Docker images from Google Container Registry because it is a private container registry.</p><h4>Download GCP Project Default Account Private Key</h4><p>Follow the steps in the image below for this section.</p><p>⛔️<strong><em>DO NOT SHARE(commit/push) ACCOUNT PRIVATE KEY FILE!!!</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vympQfJUrxr1kBKLRizUPg.png" /></figure><p>We downloaded the project default account private key. I will put it under the resource folder of the Spring Boot project. <strong>Because <em>we are also going to use it for interacting with Google Cloud SQL</em></strong>.</p><h4>Create Kubernetes Secret to Pull Images</h4><p>We need to create a secret for Minikube to pull images from Google Cloud Container Registry and going to assign the secret as imagePullSecrets.</p><pre>$kubectl create secret docker-registry bionic-trilogy-key \<br>&gt; --docker-server=<a href="https://gcr.io/">https://gcr.io</a> \<br>&gt; --docker-username=_json_key \<br>&gt; --docker-password=&quot;$(cat bionic-trilogy-key.json)&quot; \<br>&gt; --<a href="/service/mailto:docker-email=emre@codeshake.io">docker-email=[</a>valid@email.com]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gro4uFL1VFYvFGNHhYPtDw.png" /></figure><pre>$kubectl patch serviceaccount default \<br>&gt; -p &#39;{&quot;imagePullSecrets&quot;:[{&quot;name&quot;:&quot;bionic-trilogy-key&quot;}]}&#39;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sEMWNt4keyAkYegTX_hmow.png" /></figure><pre>$kubectl get serviceaccount default -o yaml</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZxEiVYxIX4XuLdurlOjscA.png" /></figure><p>Now, Minikube can download Docker images from Google Container Registry</p><h3>Create Deployment YAML File for Kubernetes</h3><p>We, as developers, don’t create a Pod, Service, etc. Kubernetes has abstraction layers for components so we just create a Deployment and Kubernetes handles the rest.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*sf2WJCGkl86vLSkyYORAjw.png" /></figure><p>I am not going to give the detail of a deployment yaml file because Kubernetes has very detailed documentation about it. You can find it on this <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/">link</a>.</p><h4>Convert Docker-compose YAML to Kubernetes YAML</h4><p>There is a tool called <a href="https://kompose.io/"><em>Kompose</em></a>. Its installation is very simple, and you may use it to convert the docker-compose deployment yaml file into Kubernetes deployment yaml files. But I don’t like the end result, It seems too verbose to me. On the other, you may want it to use to scaffold a template, but kubectl already covers that part and I like the kubectl version.</p><p>Let’s see how can you use both of them;</p><h4><strong>Kompose Version</strong></h4><p>I had a docker-compose deployment file that I created in <a href="https://kadiremreozcan.medium.com/how-to-use-docker-and-docker-compose-for-mysql-phpmyadmin-spring-boot-angular-stack-crud-web-b7b868a7c69e"><em>my previous post</em></a><em>.</em> Also, you can find its <a href="https://github.com/emreozcan3320/star-wars-quote-web-application"><em>Github repository</em></a> too. I will use the following command to convert it;</p><pre>$kompose convert -f docker-compose-deployment.yaml</pre><p>And the terminal output;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/898/1*6-Cit9gYLoDM7-5xIvZkbw.png" /></figure><p>if we look into springboot-app-deployment.yaml;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*5guR6Yo3d4Nr0c3WgI-euA.png" /></figure><h4><strong>kubectl Version</strong></h4><p>If we want to use kubectl to create a deployment, we are only gonna need a docker image. You can follow<a href="https://spring.io/guides/gs/spring-boot-kubernetes/"><em> the spring official example Spring Boot Kubernetes example</em></a> for this part. I am going to use the project mentioned there.</p><pre>$kubectl create deployment demo --image=springguides/demo --dry-run -o=yaml &gt; deployment.yaml</pre><p>and output deployment.yaml file is;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*yK0_YgrMivVrmcDGpXJ0wQ.png" /></figure><h4>Angular Deployment YAML</h4><p>We are going to use this deployment file for the Angular application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e94783e1de1b9df69f9cdcd5919d7f38/href">https://medium.com/media/e94783e1de1b9df69f9cdcd5919d7f38/href</a></iframe><h4>Spring Boot Deployment YAML</h4><p>We are going to use this deployment file for the Spring Boot application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c3d0d037cdcbdb23f3485de3f27804c/href">https://medium.com/media/4c3d0d037cdcbdb23f3485de3f27804c/href</a></iframe><h3>Create Internal Services</h3><h4>Service Pod Relation (label, Endpoint Object)</h4><p>I am going to explain very generally. Pods are ephemeral which means they can die very easily and every time they created a new IP address is given to them. So imagine you have a FE pod and a BE pod.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/795/1*LGyCF2xgnPuXbB9VWA0TTQ.png" /></figure><p>FE needs to BE’s IP address to communicate it, and you somehow pass BE’s IP address to FE and FE achieve to connect to BE.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/796/1*zYwp20J-aBWlmPfWwXJgBQ.png" /></figure><p>But what if the BE pod dies and a new one is created instead. How will FE know BE’s IP?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*_R1gjBiS_u3Lgj5vgxphuQ.png" /></figure><p>So we put an internal service. Service has a permanent IP address and FE can communicate service. We almost solve the problem. <em>So how does the service knows the pod&#39;s IP?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*vdjUrwCWPUbbcWQ7WePHig.png" /></figure><p>When you deploy a service you set a <em>label selector</em> value. The service interacts with pods that have the same label. Also, the service gets help from the endpoint object to find the labeled pod’s IP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*kuHKEkFcut51MnP9O0Mf5w.png" /></figure><p>So this is why we need internal services to establish communication between pods.</p><h4>Angular Service YAML</h4><p>We are going to use this service deployment to connect the Angular application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66c397daf3e09bdb3168e3560fab8cb6/href">https://medium.com/media/66c397daf3e09bdb3168e3560fab8cb6/href</a></iframe><h4>Spring Boot Service YAML</h4><p>We are going to use this service deployment to connect the Spring Boot application.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56c73535212f2da5c489a519d9230703/href">https://medium.com/media/56c73535212f2da5c489a519d9230703/href</a></iframe><h3>Expose Application to Outside World</h3><p>When we want to expose the application to the outside, we encounter 4 choices. They are<strong> <em>ClusterIP, NodePort, LoadBalancer, and Ingress</em></strong><em>.</em> I am not going to give detail but you should read about them. you can easily find a detailed explanation on the internet, also I leave these links here(<a href="https://blog.getambassador.io/kubernetes-ingress-nodeport-load-balancers-and-ingress-controllers-6e29f1c44f2d">link1</a>,<a href="https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0">link2</a>). If you look at <a href="https://spring.io/guides/gs/spring-boot-kubernetes/">the Spring Kubernetes tutorial</a>, they are using <strong><em>port-forward</em></strong> but it is not ideal. In this post, I am going to use <em>LoadBalancer</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*XGtCVjBX63fElAsgywejPQ.png" /></figure><h4>Create Load Balancer as External Service</h4><blockquote>Using a <em>LoadBalancer</em> service type automatically deploys an external load balancer. This external load balancer is associated with a specific IP address and routes external traffic to a Kubernetes service in your cluster.</blockquote><p>This means if I deploy the LoadBalancer service to GKE, Google Cloud LoadBalancer going to take control.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ac9f8046a4755c127e52bb8a5dd1800/href">https://medium.com/media/3ac9f8046a4755c127e52bb8a5dd1800/href</a></iframe><h3>Configure Angular to Communicate Backend Over Kubernetes Network</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/343/1*xaLaCBgn_cenKz5pBITBkw.png" /></figure><p>If you know docker networking, the idea is very similar. FE should not communicate with BE service by IP address. We can directly give the service name to Angular and Kubernetes handle redirection. We are adding the following line to the Nginx configuration file. It is a reverse proxy and instead of localhost, we give application-api-service which is the name of <em>Spring Boot Service</em>.</p><pre>location /api/v1{<br>            proxy_pass <a href="http://application-api-service:8080/api/v1">http://application-api-service:8080/api/v1</a>;        }</pre><h3>Create a Deployment to Minikube</h3><p>Currently, we have 5 deployment files.</p><ol><li>LoadBlancer service deployment</li><li>Front-end deployment</li><li>Front-end service deployment</li><li>Back-end deployment</li><li>Back-end service deployment</li></ol><p>and we are going to add 2 more for the Database. There is a very good <a href="https://kubernetes.io/docs/tasks/run-application/run-single-instance-stateful-application/">Kubernetes official example</a> and I am going to use their yaml because it fits just perfectly.</p><p>6. mysql-deployment.yaml</p><pre>$ kubectl apply -f https://k8s.io/examples/application/mysql/mysql-deployment.yaml</pre><p>7. mysql-pv.yaml</p><pre>$kubectl apply -f https://k8s.io/examples/application/mysql/mysql-pv.yaml</pre><p>Having 5 separate deployment files is a pain, so let&#39;s bundle them and create one huge deployment file. I named it kubernetes-deployment.yaml</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/932f331db0db3129d558b905bff1cb3a/href">https://medium.com/media/932f331db0db3129d558b905bff1cb3a/href</a></iframe><h4>Deploy to Minikube</h4><p>Initial minikube status,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*wUwSJJW6aTuj_Y5THK3XCA.png" /></figure><ol><li>Create PersistentVolume and PersistentVolumeClaim</li></ol><pre>$kubectl apply -f https://k8s.io/examples/application/mysql/mysql-pv.yaml</pre><p>2. Create MySql deployment, MySql Service</p><pre>$ kubectl apply -f https://k8s.io/examples/application/mysql/mysql-deployment.yaml</pre><p>3.Connect MySql and Create Database Manually(there are other ways too but for now I will create it manually)</p><pre>$kubectl run -it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql -ppassword</pre><pre>mysql&gt; CREATE DATABASE kubernetes_application;<br>mysql&gt; SHOW DATABASES;<br>+------------------------+<br>| Database               |<br>+------------------------+<br>| information_schema     |<br>| kubernetes_application |<br>| mysql                  |<br>| performance_schema     |<br>+------------------------+<br>4 rows in set (0.00 sec)</pre><pre>mysql&gt; exit</pre><p>4. Deploy kubernetes-deployment.yaml (which contains <em>LoadBalancer</em>, <em>AngularService</em>, <em>AngularDepoyment</em>,<em> SpringBootService</em>, <em>SpringBootDeployment</em>)</p><pre>$kubectl apply -f kubernetes-deployment.yaml</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/584/1*vSa8U66VA-Y7VxUEMIW2Rw.png" /></figure><p>Let’s see the current status</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SYl_KsHqznIl9i0TVIiFEQ.png" /></figure><p>All is fine but LoadBalancer is pending, The solution is the <a href="https://minikube.sigs.k8s.io/docs/handbook/accessing/#example">minikube tunnel</a></p><blockquote>A LoadBalancer service is the standard way to expose a service to the internet. With this method, each service gets its own IP address.Services of type LoadBalancer can be exposed via the minikube tunnel command.</blockquote><pre>$minikube tunnel</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*NI9poS9ZLiRnSXKcfqF1vg.png" /></figure><p>We are going to use LoadBlancer IP and Port to connect the application so in this case, it is 10.108.150.246:4200</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o9PXZ7vVm7nvSLE9dBs8vQ.png" /></figure><p>and perfect, it is working🎉🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xK667kkHDgBce7Qr6tQjFg.png" /></figure><p>If you want to stop all the part related to the application, you should use the following commands;</p><pre>$kubectl delete deployment,svc mysql<br>$kubectl delete pvc mysql-pv-claim<br>$kubectl delete pv mysql-pv-volume<br>$kubectl delete -f kubernetes-deployment.yaml</pre><h3>Create a Deployment to Google Cloud</h3><p>There is a <a href="https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-app">Google Tutorial</a> too, you can check it for a better understanding of the concepts. When we are deploying Google Cloud most of the things will be the same with the Minikube deployment. <strong>The only difference we are going to use Google Cloud SQL. </strong>To connect Google Cloud SQL, we are going to use the Spring Cloud GCP library’s SQL package.</p><h4>Activate and Connect Google Cloud SQL</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*02V-qrdEFxSlKWY4iyR2QA.png" /></figure><p>We created an SQL instance</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0TNfwwDeOo_jC7UEYW7lUg.png" /></figure><p>It is time to create kubernates_applicationdatabase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XzEpBZfQuAXyAt5R95KPgA.png" /></figure><p><strong>Connect Spring Boot to Google Cloud SQL</strong></p><p>We were using classic mysql-connector-java</p><pre><em>&lt;</em>dependency<em>&gt;<br>    &lt;</em>groupId<em>&gt;</em>mysql<em>&lt;/</em>groupId<em>&gt;<br>    &lt;</em>artifactId<em>&gt;</em>mysql-connector-java<em>&lt;/</em>artifactId<em>&gt;<br>    &lt;</em>scope<em>&gt;</em>runtime<em>&lt;/</em>scope<em>&gt;<br>&lt;/</em>dependency<em>&gt;</em></pre><p>and its aplication.properties configurations</p><pre>spring.jpa.hibernate.ddl-auto=update<br><br>spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/${MYSQL_DATABASE:kubernetes_application}<br>spring.datasource.username=${MYSQL_USER:root}<br>spring.datasource.password=${MYSQL_PASSWORD:password}</pre><p>We can continue to use this classic method if we create a <a href="https://cloud.google.com/sql/docs/mysql/sql-proxy">Cloud SQL Proxy</a>. But it is more appropriate for local testing, not for production. Instead, <strong><em>we are going to use the </em></strong><a href="https://spring.io/projects/spring-cloud-gcp"><strong><em>Spring Cloud GCP</em></strong></a><strong><em> library</em></strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/899/1*cWt0JwHkEFH02Bsb1w-9eA.png" /></figure><p>There are code sample;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/871/1*5x6IqHqsRZtVYU3p85J4og.png" /></figure><p>and it takes us to <a href="https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-samples/spring-cloud-gcp-sql-mysql-sample"><em>a GitHub repository</em></a> that gives a code sample and configuration instructions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SH0l3zECYLfl-J5-fhhO0A.png" /></figure><ol><li>We are going to add the new dependency to pom.xml and turn a comment into the old one.</li></ol><pre>&lt;!--        &lt;dependency&gt;--&gt;<br>&lt;!--            &lt;groupId&gt;mysql&lt;/groupId&gt;--&gt;<br>&lt;!--            &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;--&gt;<br>&lt;!--            &lt;scope&gt;runtime&lt;/scope&gt;--&gt;<br>&lt;!--        &lt;/dependency&gt;--&gt;<br>&lt;!--         https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-gcp-starter-sql-mysql --&gt;<br>        <em>&lt;</em>dependency<em>&gt;<br>            &lt;</em>groupId<em>&gt;</em>org.springframework.cloud<em>&lt;/</em>groupId<em>&gt;<br>            &lt;</em>artifactId<em>&gt;</em>spring-cloud-gcp-starter-sql-mysql<em>&lt;/</em>artifactId<em>&gt;<br>            &lt;</em>version<em>&gt;</em>1.2.6.RELEASE<em>&lt;/</em>version<em>&gt;<br>        &lt;/</em>dependency<em>&gt;</em></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iIJn8kyP0etMgsDrYjnNwQ.png" /></figure><p>2. we are going to update theapplication.properties</p><pre>spring.jpa.hibernate.ddl-auto=update<br><br>#spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/${MYSQL_DATABASE:kubernetes_application}<br>spring.datasource.username=${MYSQL_USER:root}<br>spring.datasource.password=${MYSQL_PASSWORD:password}<br><br>spring.cloud.gcp.sql.database-name=kubernetes_application<br>spring.cloud.gcp.sql.instance-connection-name=bionic-trilogy-298608:us-central1:kubernetesdemo</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*qvV5mVdIYbIVLSpPBRfkbQ.png" /></figure><p><em>database-name</em> → the database we created in kubernetesdemo</p><p><em>instance-connection-name </em>→ you can find the name at “GCP SQL Overview page” under the <em>Connection name</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*4w5E9qOTyg6BoRF4VN9EOA.png" /></figure><p>3. You can use this set up on your local minikube cluster too. But you need credentials to connect GCP SQL. Spring Cloud GCP SQL looks for an environment variable as default. The environment variable is GOOGLE_APPLICATION_CREDENTIALS . The credential exists in GKE there for it is not a problem. We have to create the same environment variable and set the value as the location of the credential file which we download at the “<em>Download GCP Project Default Account Private Key” section</em>. So we just add a new environment variable. I am going to show how you can do it in IntelliJ.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CMuvnOlJsmoMnJA3Y3Hmfw.png" /></figure><h4>Create a Kubernetes Cluster at Google Cloud with GKE</h4><ol><li>First, we should activate Google Kubernetes Engine(GKE) to create a Kubernetes cluster</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*04drDT6-hoBWaFEH3XA6pg.png" /></figure><p>2. We activated GKE, it is time to create a cluster. When you select Kubernetes Engine from the side menu,<strong> <em>step 1</em></strong><em> </em>will appear just click “<em>CREATE CLUSTER</em>”. You can use gcloud commands too, but it is not beginner-friendly. Now, you can create a custom cluster like <strong>step 2-a</strong> or select a pre-configured cluster like <strong>step 2-b</strong>. Pre-configured cluster assures the cheapest test options, the downside is you may face permission problems like connecting to the private container registry even though they are at the same project. I am going to create a custom cluster that is very similar to the pre-configured one but with more permissions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-G6aKzN5WYSphoyYZEoP4A.png" /></figure><p>3. We created our first cluster. GKE gives 3 nodes as default but you can change it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/0*yC9ioesKrKe3jYPh" /></figure><p>4. As you know we are using kubectl to interact with Kubernetes. So the question, <strong><em>How can we use kubectl to interreact multiple clusters? </em></strong>kubectl uses<strong> context configuration</strong> to decide which cluster it interacts with.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/597/1*TaoqBs6rbX5CKTAHQBc-sg.png" /></figure><p>so we should change the current context with GKE. It is very simple, but we have to use gcloud commands for this. First, let’s check our GKE clusters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0ezB5yMD8SY_e1ZA" /></figure><p>This is the cluster that we created. Its location is us-central1-c , note this information. We have to create credentials to connect our kubectl to GKE. It is one line of code.</p><pre>$gcloud container clusters get-credentials my-first-cluster-1 --zone=us-central1-c</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YwGAi1AMFuNUmnGV" /></figure><p>let’s check our context.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/0*24HKdG_5xzbkt-d0" /></figure><p>And that is all we can interact with GKE now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/859/0*haXg-jO1MDzKFQ43" /></figure><p><em>🧠Note: If you want to work with Minikube again you should change the context. </em><em>$kubectl config --help</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*klkN9J8P19NhagSRstfIEw.png" /></figure><pre>$kubectl config get-contexts<br>CURRENT  NAME                  CLUSTER   AUTHINFO   NAMESPACE<br>   *     gke_bionic-trilogy... gke_bi... gke_bi...           <br>         minikube              minikube  minikube   default</pre><p>to switch</p><pre>$kubectl config use-context minikube</pre><h4>Deploy to Google Kubernetes Engine(GKE)</h4><p>We bundled all our deployment files in the previous section except pv, pvc, MySqlService, and MySqlDeployment. In this section, we just need the bundled deployment file. Also, we have to create a new BE docker image because of the code changes,</p><ol><li>Create a new BE image and push it.</li></ol><p>2. Deploy GKE</p><p>initial state</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/904/1*dtyX-K_zR8l0JHrKzVEIhg.png" /></figure><pre>$kubectl apply -f kubernetes-deployment.yaml</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*LWzMTr1HJMN61xCULBKb3Q.png" /></figure><pre>$kubectl get all</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aYRaeJA9u4ix5gUjlJwtsA.png" /></figure><p>You can also check with GCP GKE UI</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tx1KBJpHBC2wcFqN4GStHg.png" /></figure><p>Everything seems fine. Let’s check 35.225.251.121:4200</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xYY5J2SZXpxzl5x4SttxxQ.png" /></figure><p>And we are <strong>ALIVE</strong>🎊🎊</p><p>If you want to stop the application</p><pre>$kubectl delete -f kubernetes-deployment.yaml</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/545/1*PIBowV9CwgQFtOqmWj7g1w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*OP9jdqnCvVhmIqkghFnFpw.png" /></figure><h3>References</h3><ul><li><a href="https://kadiremreozcan.medium.com/how-to-use-docker-and-docker-compose-for-mysql-phpmyadmin-spring-boot-angular-stack-crud-web-b7b868a7c69e">How to Use Docker and Docker-Compose for MySql, PhpMyAdmin, Spring Boot, Angular Stack CRUD Web…</a></li><li><a href="https://blog.getambassador.io/kubernetes-ingress-nodeport-load-balancers-and-ingress-controllers-6e29f1c44f2d">Kubernetes Ingress 101: NodePort, Load Balancers, and Ingress Controllers</a></li><li><a href="https://medium.com/google-cloud/kubernetes-nodeport-vs-loadbalancer-vs-ingress-when-should-i-use-what-922f010849e0">Kubernetes NodePort vs LoadBalancer vs Ingress? When should I use what?</a></li><li><a href="https://kubernetes.io/">Production-Grade Container Orchestration</a></li><li><a href="https://kubernetes.io/docs/tasks/tools/install-kubectl/">Install Tools</a></li><li><a href="https://minikube.sigs.k8s.io/docs/start/">minikube start</a></li><li><a href="https://spring.io/projects/spring-cloud-gcp">Spring Cloud GCP</a></li><li><a href="https://spring.io/guides/gs/spring-boot-kubernetes/">Getting Started | Spring Boot Kubernetes</a></li><li><a href="https://cloud.google.com/kubernetes-engine/docs/tutorials/hello-app">Deploying a containerized web application | Kubernetes Engine | Google Cloud</a></li><li><a href="https://github.blog/2016-02-01-working-with-submodules/">Working with submodules - The GitHub Blog</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69b8181ed36b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Use Docker and Docker-Compose for MySql, PhpMyAdmin, Spring Boot, Angular Stack CRUD Web…]]></title>
            <link>https://kadiremreozcan.medium.com/how-to-use-docker-and-docker-compose-for-mysql-phpmyadmin-spring-boot-angular-stack-crud-web-b7b868a7c69e?source=rss-b61c237af83b------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7b868a7c69e</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[spring-boot]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[docker-compose]]></category>
            <dc:creator><![CDATA[Kadir Emre Ozcan]]></dc:creator>
            <pubDate>Wed, 25 Nov 2020 10:22:03 GMT</pubDate>
            <atom:updated>2020-12-18T00:53:27.236Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to Use Docker and Docker-Compose for MySql, PhpMyAdmin, Spring Boot, Angular Stack CRUD Web Application Development</h3><h4>A <strong><em>Star Wars Quotes Web Application</em></strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1Ir-RKAMQm5BZuV77FbGgA.png" /></figure><h3>👋Introduction</h3><p>Most developers use Docker daily base or once in a while in the product development or at the deployment life cycle. It is very powerful and fun to play with the tool. But if you use quite a deal of docker containers, a management issue arises. Docker-composer provides a simple and fast solution for container management, on the other hand, it has its own shortcomings which lead us to systems like Kubernetes and Docker Swarm but they are more DevOps tools nevertheless knowing how they work and tampering with them don’t hurt. So I decided to show you how I use docker and docker-compose to create a CRUD web application. I used MySql for database, PhpMyAdmin for database visualization, Spring Boot(Java) as a back-end, and Angular 9 as a front-end. I am also planning to use Kubernetes for this project in the future.</p><p>The project is going to be about Quotes with Start Wars Theme and I am going to use <strong><em>Star Wars: The Clone Wars Episode Opening Quotes</em></strong> (I don’t know why but secretly I always want to do an app about that 😁) as initial data.</p><p>The final, front-end going to look like this;</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RgrRNmej-AjyDZFmcohkRA.png" /></figure><p><strong><em>You can find the final code in this </em></strong><a href="https://github.com/emreozcan3320/star-wars-quote-web-application"><strong><em>GitHub repo</em></strong></a><strong><em>;</em></strong></p><p><a href="https://github.com/emreozcan3320/star-wars-quote-web-application">emreozcan3320/star-wars-quote-web-application</a></p><h4>👉What This Post NOT About👈</h4><ol><li>Not step by step guide. I am just going to explain some important parts, some code snippets, some screenshots, and a GitHub repository for the entire project which you can dive into.</li><li>Not about Docker inner mechanism or Docker 101</li><li>Not a deep down explanation for Docker-compose just an example of its usage</li><li>Not a Spring Boot CRUD web application tutorial. There are enough of them out there.</li><li>Not a production-ready code but definitely helps.</li></ol><h4>👉What This Post About👈</h4><ol><li>To show how I work with Docker and Docker-compose</li><li>To show how you can use them to simplify your workflow</li><li>To give you a reference codebase.</li></ol><h3>📖TLTR;</h3><ol><li>Create a Spring Boot BE</li></ol><p>— By spring initializr</p><p>— By IntelliJ</p><p>2. Spring Boot “Hello World!”</p><p>3. Creating Application REST API Endpoints</p><p>4. Creating a MySql Container</p><p>5. Create a PhpMyAdmin Container and Connect the MySql Container</p><p>6. What is Docker Compose</p><p>7. Use Docker Compose to Create MySql and PhpMyAdmin Containers</p><p>8. Connect Spring Boot to Docker MySql Container for Local Development</p><p>9. Creating Angular FE</p><p>10. Reconfigure File Structure</p><p>11. ‍Dockerize Spring Boot Application</p><p>12. Dockerize Angular Application</p><p>13. Use Docker Compose to Manage Life Cycle of Containers</p><p>14. Configure FE to Communicate BE Over Docker Network</p><p>— What is Our BE URL at Docker Network</p><p>— NGINX Reverse Proxy</p><p>— Configure Environment</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*nS7WYWlbX_PlrewMRGC4FA.gif" /></figure><h3>Create a Spring Boot BE</h3><h4>By spring initializr</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wusITeUhC5DVy1ujakxF4w.png" /></figure><p>content of generated .zip file</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*thAoXP7SZi8lQUqrkPxXGA.png" /></figure><h4>By IntelliJ</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-io5F-ie_BppWn-IqR1Kpg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ctjIkvNNNJQT4kXh9rdQjg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*K4RiSl_0bG6ScHXPsjKU3w.png" /></figure><h3>Spring Boot “Hello World!”</h3><p>Separating project into sub-packages is one of the common approaches so this will be our final our file structure</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*oTpAeV8uHWhuRvfcbVnMog.png" /></figure><p><strong>but</strong> for now, the following one is enough,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*EXUQLQaa7UJGk6Oeb7oI_Q.png" /></figure><p>🧠Note: We changed the location of CrudApplication.java so we should say Spring where it should scan for components for proper creation of beans. We are going to add more annotation here in the future.</p><pre>@SpringBootApplication<br>//the following line has been added<br>@ComponentScan<em>(</em>&quot;com.example.crudapplication&quot;<em>)<br></em>public class CrudApplication <em>{<br><br>   </em>public static void main<em>(</em>String<em>[] </em>args<em>) {<br>      </em>SpringApplication.<em>run(</em>CrudApplication.class, args<em>)</em>;<br>   <em>}<br><br>}</em></pre><p>And I create the QuoteController</p><pre>@RestController<br>@RequestMapping<em>(</em>&quot;api/v1&quot;<em>)<br></em>public class QuoteController <em>{<br><br>   </em>@GetMapping<br>   public String helloWorld<em>(){<br>      </em>return &quot;Hello World!&quot;;<br>   <em>}<br>}</em></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/1*-4Vo8vTW7IzcQyKlttf5WQ.png" /></figure><h3>Creating Application REST API Endpoints</h3><p><strong>Tips for Lombok users: </strong>If you are using Lombok as I am, there is a nice touch that you can use with @Autowired. I used to use the constructor approach at Dependency Injection so my code looks like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/507/1*7zUyOnTVu7Uo3CPpCBJpsA.png" /></figure><p>but now, I use Lombok @AllArgsConstructor and my code looks like this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/1*pc2f5W0cs1MMm2WnGv45ZA.png" /></figure><p>it is helpful especially if there are lots of dependencies 😍</p><p><strong>REST APIs</strong></p><p>GET api/v1/quotes → to retrieve all quotes</p><p>GET api/v1/quotes/:id → to retrive single quoteById</p><p>POST api/v1/quotes → to save a quote</p><p>PUT api/v1/quotes/:id → to update a quote</p><p>DELETE api/v1/quotes/:id → to delete a quote</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*QP89EL1kuSRtZIGq8afCjA.png" /></figure><p>As you see it is a common rest endpoint pattern and there are things we can tweak more but I am not.</p><h3>Creating a MySql Container</h3><p>We are going to use the following MySql Docker image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LUEvwJb4yyUcjySbJQl7Og.png" /></figure><pre>docker run -d -p 3306:3306 --name=docker-mysql --env=&quot;MYSQL_ROOT_PASSWORD=password&quot; --env=&quot;MYSQL_DATABASE=crud-application&quot; mysql</pre><ul><li>d → detached mode means docker runs container at the background</li><li>-p 3306:3306 → container’s port 3306(MySql default port) redirected host machine’s port 3306</li><li>-name=docker-mysql → docker gives a random name for every container but instead, I prefer to name my own container name</li><li>-env= → environment variables that you can find at docker image documentation</li><li>mysql → the docker image, as default the last version, is pulled</li></ul><p>It is going to pull the image first because I don’t have it on my local. After it will run the container with the downloaded image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*Ofh1s9isIAgtLB-AuPq8Sg.png" /></figure><p>List of images docker image ls</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/827/1*lDoWVIrc5bOmukWDvrToTw.png" /></figure><p>List of containers docker container ls</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QFUuHn1m-KEz7EnFvNeJZw.png" /></figure><p>To access the running container docker exec -it &lt;container-name&gt; bash or docker exec -it &lt;cotainer-name&gt; sh one of them should work depending on the base Linux image of the downloaded image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*bi8ndNIIy6G1U-MReTPVdw.png" /></figure><p>After accessing the container use mysql -u root -p the command to connect MySQL and use SHOW DATABASES; the command to list databases. crud-application the database is there as we said in -env variable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*u719wGHMmGS_JX7ya9JO5w.png" /></figure><h3>Create a PhpMyAdmin Container and Connect the MySql Container</h3><p>We are going to use the following PhpMyAdmin Docker image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*myIHFhWukythN6IO_yjvJA.png" /></figure><pre>docker run -d --name docker-phpmyadmin --link docker-mysql:db -p 8081:80 phpmyadmin</pre><p>Same drill as before and as an addition to the previous command we have link which provides access to another container running in the host.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*h7YqosT_U7eaGCYTjmI8kw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dqyzMekQgbC7zhJO_exE1g.png" /></figure><p>We redirected docker container’s 80 port to host 8081 port which you can see above so to reach PhpMyAdmin UI go tohttp://localhost:8081 user: root and password: password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6bz7YzPJCwkoDJVD0R640g.png" /></figure><p>and you should connect the DB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*52SC7FQceKGoFNPlwNcFQQ.png" /></figure><h3>What is Docker Compose</h3><p>Creation of MySql and PhpMyAdmin are connected to each other and every time you need to change something, you have to run docker codes again and imagine what you have to do if the container number increase. Of course, you can write some script to automize this process but there is an easy way and called as docker-compose. We just need to create a YAML file and docker-compose is going to handle the rest for us🥰</p><blockquote>Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration. To learn more about all the features of Compose, see <a href="https://docs.docker.com/compose/#features">the list of features</a>.</blockquote><p>🧠Note: Docker-compose is written in python, if you curious, you can check its <a href="https://github.com/docker/compose">GitHub repository</a>.</p><h3>Use Docker Compose to Create MySql and PhpMyAdmin Containers</h3><p>What docker-compose is explained in the previous section so there is a small example. I create a YAML(.yaml or .yml) file as docker-compose.yaml. Its directory not important but generally it placed under the same project so I do the same and put it under the project directory.</p><pre>version: &quot;3.3&quot;<br>services:<br>  db:<br>    image: mysql<br>    restart: always<br>    container_name: mysql-db<br>    environment:<br>      MYSQL_ROOT_PASSWORD: &#39;password&#39;<br>      MYSQL_DATABASE: &#39;crud-application&#39;<br>    ports:<br>    - 3306:3306<br>  phpmyadmin:<br>    image: phpmyadmin<br>    restart: always<br>    container_name: php-my-admin<br>    ports:<br>    - 8081:80<br>  environment:        <br>    PMA_HOST: mysql-db</pre><p>two create container use following command</p><pre>sudo docker-compose -f docker-compose.yaml up</pre><p>you can use -d option to use as detached mode, which I do for the rest.</p><p>🧠Note: Creating network &quot;...&quot; with default driver this line important quote from documentation;</p><blockquote>By default Compose sets up a single <a href="https://docs.docker.com/engine/reference/commandline/network_create/">network</a> for your app. Each container for a service joins the default network and is both <em>reachable</em> by other containers on that network, and <em>discoverable</em> by them at a hostname identical to the container name.</blockquote><pre>$docker-compose -f docker-compose.yaml up -d</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*MPrV5lSDEBR9dPRMqynriw.png" /></figure><p>and our containers are ready and everything works just fine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kKkZVs9kDi710Viw91ir5w.png" /></figure><h3>Connect Spring Boot to Docker MySql Container for Local Development</h3><p>1- Add dependencies to pom.xml</p><pre><em>&lt;</em>dependency<em>&gt;<br>    &lt;</em>groupId<em>&gt;</em>org.springframework.boot<em>&lt;/</em>groupId<em>&gt;<br>    &lt;</em>artifactId<em>&gt;</em>spring-boot-starter-data-jpa<em>&lt;/</em>artifactId<em>&gt;<br>&lt;/</em>dependency<em>&gt;</em></pre><pre><em>&lt;</em>dependency<em>&gt;<br>    &lt;</em>groupId<em>&gt;</em>mysql<em>&lt;/</em>groupId<em>&gt;<br>    &lt;</em>artifactId<em>&gt;</em>mysql-connector-java<em>&lt;/</em>artifactId<em>&gt;<br>    &lt;</em>scope<em>&gt;</em>runtime<em>&lt;/</em>scope<em>&gt;<br>&lt;/</em>dependency<em>&gt;</em></pre><p>2- Update application.properties</p><p>There are 4 properties and 3 variable, I want to cover 2 cases. Case 1 spring-boot should connect containers while local development and case 2 spring boot container should connect database-container when docker-compose run them. So I entered hardcoded values for local development and not hardcoded once for docker-compose enviromet variables.</p><p>we should use MySql container URL asspring.datasource.url in our case localhost:3306</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1Xrv-VNI1jnH70ZTHFNJrg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UmgKJ-TkODh49pWN0R63kw.png" /></figure><p>3- Create an entity</p><pre>@Getter<br>@Setter<br>@Builder<br>@AllArgsConstructor<br>@NoArgsConstructor<br>@Entity<br>public class Quote <em>{<br>   </em>@Id<br>   @GeneratedValue<em>(</em>strategy = GenerationType.<em>AUTO)<br>   </em>private Integer id;<br>   private String title;<br>   private String quote;<br>   private String source;<br>   @Builder.Default private long date = Instant.<em>now()</em>.getEpochSecond<em>()</em>;<br><em>}</em></pre><p>4- Create repository</p><pre>public interface QuoteRepository extends CrudRepository<em>&lt;</em>Quote, Integer<em>&gt; {<br>}</em></pre><p>🧠Note: For spring boot to find our Repository and Entity we should add the following annotations</p><pre>@SpringBootApplication<br>@ComponentScan<em>(</em>&quot;com.example.crudapplication&quot;<em>)<br></em>@EnableJpaRepositories<em>(</em>&quot;com.example.crudapplication.repository&quot;<em>)<br></em>@EntityScan<em>(</em>&quot;com.example.crudapplication.model&quot;<em>)<br></em>public class CrudApplication <em>{<br><br>   </em>public static void main<em>(</em>String<em>[] </em>args<em>) {<br>      </em>SpringApplication.<em>run(</em>CrudApplication.class, args<em>)</em>;<br>   <em>}<br><br>}</em></pre><p>This is our current file structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*oTpAeV8uHWhuRvfcbVnMog.png" /></figure><p>Testing … ( we run via ide for IntelliJ in my case)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AvQDjoLw-gRfCra205GuBw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/869/1*tUg6f43hmCAXZsI77-GVJA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/643/1*40Q9g9wes9J6oX-1vjB7aw.png" /></figure><h3>Creating Angular FE</h3><p>For the front-end, I create a very simple Angular application that includes a service for HTTP calls, a model for Quote, and a modal(dialog) for quote creat-update operations.</p><pre>ng new quote<br>ng g s services/quote<br>ng g c components/modal<br>ng generate class models/quote --type=model</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*cwBIESxDyBJWj_iWkjzAeQ.png" /></figure><p>I put the following screenshots for fast demonstration, you can check<a href="https://github.com/emreozcan3320/star-wars-quote-web-application"> the GitHub repository for further code detail</a>.</p><p>quote.service.ts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*SRckBV-byx2DikzEMQmTTA.png" /></figure><p>modal.component.ts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/1*CeA8Z6ky8S2tqEqvvKnbOA.png" /></figure><p>app.component.ts</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/919/1*BFb6Tl2z89pn2-z6ACywAg.png" /></figure><pre>ng serve</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RgrRNmej-AjyDZFmcohkRA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*756P3Ud2hAm9SOkMFPl-fw.png" /></figure><h3>Reconfigure File Structure</h3><p>So to prevent confusion I change the FE Angular project’s name as FE and the BE Spring Boot- Java project’s name as BE also create a wrapper folder as Quote-CRUD-Application and moved the projects inside it.</p><p>🧠Note: Basically I turned them polyrepo to a monorepo.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/485/1*EQSMqqnsVy0ruJZeI3YhSQ.png" /></figure><h3>‍🏭Dockerize Spring Boot Application</h3><p>Dockerizing a Spring Boot web application pretty straightforward but there are 3 approaches you can find at this <a href="https://codefresh.io/docker-tutorial/create-docker-images-for-java/">link</a>. I use the multi-stage docker image build approach. The purposes of this approach are decreasing the final image size and leverage the docker cache mechanism to faster image build time. Please check docker documentation for further information about docker inner mechanism that topic not in the scope of the post.</p><p>1- Create a Docker file under the root directory of the project</p><pre>$touch Dockerfile</pre><p>2- copy&amp;paste following code</p><pre># Build Stage<br>FROM maven:3.6-openjdk-8-slim AS <em>build<br></em>COPY pom.xml /app/<br>COPY src /app/src<br>RUN mvn -DskipTests -f /app/pom.xml clean package<br><br># Run Stage<br>FROM openjdk:8-jre-alpine<br>COPY --from=<em>build </em>/app/target/crud-application*.jar /app/app.jar<br>ENTRYPOINT <em>[</em>&quot;java&quot;, &quot;-XX:+UnlockExperimentalVMOptions&quot;, &quot;-XX:+UseCGroupMemoryLimitForHeap&quot;,&quot;-Djava.security.egd=file:/dev/./urandom&quot;,&quot;-jar&quot;,&quot;/app/app.jar&quot;<em>]</em></pre><p>As you notice there is 2 stage;</p><p>Build stage → Use maven to create a jar</p><p>Run stage → Copy jar file from the previous build and define an ENTRYPOINT for docker container.</p><h3>‍💻Dockerize Angular Application</h3><p>I am going to use the same multi-stage Docker image build approach.</p><p>1- Create a Docker file under the root directory of the project</p><pre>$touch Dockerfile</pre><p>2- copy&amp;paste following code</p><pre># Build Stage<br>FROM node:12.7-alpine AS <em>build<br></em>WORKDIR /app<br>COPY package*.json /app/<br>RUN npm install<br>RUN npm install -g <a href="http://twitter.com/angular/cli">@angular/cli</a>@9.0.0-rc.2<br>COPY ./ /app/<br>RUN ng build --extract-css --output-path=dist --prod=true<br><br><br># Run Stage<br>FROM nginx:1.17.1-alpine<br>COPY default.conf /etc/nginx/conf.d/default.conf<br>COPY --from=<em>build </em>/app/dist /usr/share/nginx/html</pre><p>3- Create a default.conf file under the root directory of the project for nginx</p><pre>$touch default.conf</pre><p>4- copy&amp;paste following code</p><pre>server {<br>    listen 4200;<br>    server_name localhost;<br>    location / {<br>            root   /usr/share/nginx/html;<br>            index  index.html index.htm;<br>            error_page 404 =200 /index.html;<br>        }<br>}</pre><h3>💃Use Docker Compose to Manage Life Cycle of Containers</h3><p>We finally reach the end, more excited, and more advanced part of the post 🙂</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/252/1*3Ch822GEZykrxjQmhsxNeg.png" /></figure><p>This is our current folder structure. I added a dumb.sql file. If you want the application’s data to persist regardless of your container life cycle you can assign a volume from the host machine to the MySql container but I don&#39;t have such a concern so my choice is to insert dummy data every time the MySql container run and delete all data when container removed but data will insist when container just stopped.</p><p>1- Create a docker-compose file under the root directory of all the projects</p><pre>$touch docker-compose.yaml</pre><p>2- copy&amp;paste following code</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f6cd174b735a5d406f63b782116c8745/href">https://medium.com/media/f6cd174b735a5d406f63b782116c8745/href</a></iframe><p>services → docker container template created under it</p><p>mysql-db, php-my-admin, springboot-app, angular-app → service names</p><p>image → If you use an image from a Registry, image source as default DocherHub</p><p>build → If use a Dockerfile instead of an already created image, Dockerfile path</p><p>container_name → custom container name</p><p>environment → environment variables</p><p>If you want more information you can check docker-compose <a href="https://docs.docker.com/compose/">documentation</a>.</p><pre>$docker-compose -f docker-compose.yaml up -d</pre><p>🧠Note: if you change application code and need to get new build use --build argument</p><pre>$docker-compose -f docker-compose.yaml up -d --build</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1005/1*j_keXt6qAm9Mu-3nVpdD9A.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bzEouZ5J_cuqEh8Mc8Og9w.png" /></figure><p>👊 everything works as it should</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RgrRNmej-AjyDZFmcohkRA.png" /></figure><h3>Configure FE to Communicate BE Over Docker Network</h3><p>This is the final touch. I mentioned about creation of a network by docker-compose in one of the previous sections. You can learn more about docker networking via <a href="https://docs.docker.com/network/">documentation</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/882/1*gk2yZXkhT1vuzIpr9EsV3Q.png" /></figure><p>When you say docker-compose up it creates a default network. Use the following command to see docker networks</p><pre>$docker network ls</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/760/1*L341uoWH4jyo_t3ukwSjrQ.png" /></figure><p>Our FE communication with BE over <a href="http://localhost:4200/">http://localhost:4200/</a>. It is okay for the local development but I want my FE to communicate over the Docker network.</p><h4>What is Our BE URL at Docker Network</h4><p>This is the first question that comes to our mind. The answer is BE container name: in our casespringboot-app . So it is easy we just change <a href="http://localhost:4200/">http://localhost:4200/</a> to <a href="http://springboot-app:8080/">http://springboot-app:8080</a> and it is done. <strong>NO unfortunately not that easy</strong>😞</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*7XtcNGcCzZ9YlHr4GJfizA.png" /></figure><h4><a href="https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/">NGINX Reverse Proxy</a></h4><p>So to solve this problem, we should configure the NGNIX to redirect our HTTP call to our BE service. Update default.conf as follows;</p><blockquote>When NGINX proxies a request, it sends the request to a specified proxied server, fetches the response, and sends it back to the client. It is possible to proxy requests to an HTTP server (another NGINX server or any other server) or a non-HTTP server (which can run an application developed with a specific framework, such as PHP or Python) using a specified protocol. Supported protocols include <a href="https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html">FastCGI</a>, <a href="https://nginx.org/en/docs/http/ngx_http_uwsgi_module.html">uwsgi</a>, <a href="https://nginx.org/en/docs/http/ngx_http_scgi_module.html">SCGI</a>, and <a href="https://nginx.org/en/docs/http/ngx_http_memcached_module.html">memcached</a>.</blockquote><pre>server {<br>    listen 4200;<br>    server_name localhost;<br>    location / {<br>            root   /usr/share/nginx/html;<br>            index  index.html index.htm;<br>            error_page 404 =200 /index.html;<br>        }<br><br>    location /api/v1{<br>            proxy_pass http://springboot-app:8080/api/v1;<br>        }<br>}</pre><p>So when FE made a call to /api/v1 Nginx redirect it to http://springboot-app:8080/api/v1 .</p><h4>Configure Environment</h4><p>environment.ts</p><pre>export const <strong><em>environment </em></strong>= <em>{<br>  </em>production: false,<br>  apiUrl: &#39;http://localhost:8080/api/v1&#39;<br><em>}</em>;</pre><p>environment.prod.ts</p><pre>export const <strong><em>environment </em></strong>= <em>{<br>  </em>production: true,<br>  apiUrl: &#39;/api/v1&#39;<br><em>}</em>;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*vkn1x0nfwVbBRXYtt1Qb4w.png" /></figure><pre>$docker-compose -f docker-compose.yaml up --build</pre><p>--build →docker-compose get a new build.</p><p>And everything works🎉🎉</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RgrRNmej-AjyDZFmcohkRA.png" /></figure><h3>Recommended Readings</h3><ul><li><a href="https://www.ctl.io/developers/blog/post/dockerfile-entrypoint-vs-cmd">Dockerfile: ENTRYPOINT vs CMD</a></li><li><a href="https://docs.docker.com/network/">Networking overview</a></li></ul><h3>Resources</h3><ul><li><a href="https://docs.docker.com/"></a></li><li><a href="https://github.com/docker/compose">GitHub - docker/compose: Define and run multi-container applications with Docker</a></li><li><a href="https://codefresh.io/docker-tutorial/create-docker-images-for-java/">Three Ways to Create Docker Images for Java - Codefresh</a></li><li><a href="https://docs.docker.com/compose/"></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7b868a7c69e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Google Cloud Platform(GCP) — Dockerize a Node.js Web App and Deploy to Compute Engine Instance]]></title>
            <link>https://kadiremreozcan.medium.com/google-cloud-platform-gcp-dockerize-a-node-js-web-app-and-deploy-to-compute-engine-instance-501809832289?source=rss-b61c237af83b------2</link>
            <guid isPermaLink="false">https://medium.com/p/501809832289</guid>
            <category><![CDATA[gcp]]></category>
            <category><![CDATA[dockerize]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Kadir Emre Ozcan]]></dc:creator>
            <pubDate>Tue, 07 Apr 2020 09:40:20 GMT</pubDate>
            <atom:updated>2020-05-03T23:47:47.518Z</atom:updated>
            <content:encoded><![CDATA[<h3>Google Cloud Platform(GCP) — Dockerize a Node.js Web App and Deploy to Compute Engine Instance</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5XrmcupeUuAUvbGMqGxxDw.png" /></figure><h4>Plan of the post:</h4><blockquote>1. Create Node.js Web App</blockquote><blockquote>2. Create a Dockerfile</blockquote><blockquote>3. Install Google Cloud SDK</blockquote><blockquote>4. Create Google Cloud Project and Configure Local Environment</blockquote><blockquote>5. Build and Deploy Docker Image with Google Cloud Commands</blockquote><blockquote>6. Build and Deploy Docker Image with Docker Commands</blockquote><blockquote>7. Create Firewall Rules</blockquote><blockquote>8. Deploy Docker Image to Compute Engine and Run</blockquote><h4>Create Node.js Web App</h4><p>First, create a simple Node.js “Hello world!” example.</p><pre>$ mkdir node-web-app<br>$ cd node-web-app/<br>$ npm init</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*xsYhESap0ixLN0sZP2Q6sQ.png" /></figure><p>after we create<em> package.json</em></p><pre>$ npm install -save express</pre><p>and we add start command at <em>“package.json”, </em>I use vim you can use a simple text editor.</p><pre>$ vim package.json</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/423/1*S9TV1GEb7l2OC0y6sJ4UnA.png" /></figure><p>Then, We are going to create app.js file that will define a web app using the <a href="https://expressjs.com/">Express.js</a> framework.</p><pre>$ touch app.js<br>$ vim app.js</pre><p>Then, copy the following code inside app.js</p><pre>&#39;use strict&#39;;<br><br>const express = require(&#39;express&#39;);<br><br>// Constants<br>const PORT = 8080;<br>const HOST = &#39;0.0.0.0&#39;;<br><br>// App<br>const app = express();<br>app.get(&#39;/&#39;, (req, res) =&gt; {<br>  res.send(&#39;Hello World&#39;);<br>});<br><br>app.listen(PORT, HOST);<br>console.log(`Running on http://${HOST}:${PORT}`);</pre><p>Our app ready and works locally.</p><pre>$ npm start</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/753/1*mgxDdG9YFBLVStJolaFqzQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/478/1*H1nTqzldU6z4kusWNiYM0Q.png" /></figure><h4>Create a Dockerfile</h4><pre>$ touch Dockerfile</pre><p>After, we create Dockerfile to copy the following code inside it. If you want a detailed explanation of code check Node.js official “<a href="https://nodejs.org/en/docs/guides/nodejs-docker-webapp/"><em>Dockerizing a Node.js web app</em></a>” post.</p><pre>FROM node:10<br><br># Create app directory<br>WORKDIR /usr/src/app<br><br># Install app dependencies<br># A wildcard is used to ensure both package.json AND package-lock.json are copied<br># where available (npm@5+)<br>COPY package*.json ./<br><br>RUN npm install<br># If you are building your code for production<br># RUN npm ci --only=production<br><br># Bundle app source<br>COPY . .<br><br>EXPOSE 8080<br>CMD [ &quot;node&quot;, &quot;server.js&quot; ]</pre><p>Then we create a “<em>.dockerignore</em>” with the following content.</p><pre>node_modules<br>npm-debug.log</pre><h4>Install Google Cloud SDK</h4><p>To install Google Cloud SDK please follow this <a href="https://cloud.google.com/sdk/install">documentation</a>. After you install it you be able to use gcloud commands.</p><h4>Create Google Cloud Project and Configure Local Environment</h4><pre>$ gcloud projects create compute-engine-docker-nodejs</pre><p>It takes a while… After creation, you can see the project.</p><pre>$ gcloud projects list</pre><p>Output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ITWuMxQz1IzYwkPE43yiaA.png" /></figure><p>Get the project configuration with the following command if your project name is empty, follow the project id setting step, else skip it.</p><pre>$ gcloud config list</pre><p>Output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*LKfBcx_qXEC66H3mRtaktQ.png" /></figure><p>If the project name is empty:</p><pre>$ gcloud config set project &lt;<strong>project-id</strong>&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/680/1*vtNnDYck3iFPyErrmveoqw.png" /></figure><pre>$ gcloud config get-value project</pre><p>Output:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/371/1*KjgFo5Rx97CLU-T8yLGQEA.png" /></figure><p>After this point, we will see how we can <em>Build and Deploy Docker Image with Google Cloud Commands</em>and <em>Build and Deploy Docker Image with Docker Commands</em></p><p>I use both commands depend on my case but if you already use docker, you can continue to use Docker commands.</p><h4>Build and Deploy Docker Image with Google Cloud Commands</h4><p>we are going to build and deploy our docker file with the following command.</p><pre>$ gcloud builds submit --tag gcr.io/&lt;<strong>project-id&gt;</strong>/docker-image</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*C_JZz81jT5gtW5AT7grpGw.png" /></figure><p>GCP first upload our folder to GCP Storage Bucket as .tar:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mFVHyFXgW0InOk74ICCuiA.png" /></figure><p>After it gets a Build:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DULkvGKc8l8jrFqCreg0Rw.png" /></figure><p>And you can see your image at <em>Container Registry</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*S-WIyOR7XabwW35gsJJs6A.png" /></figure><p>If you click it, you will see details and deployment option but for now, we won’t deploy it, there is still some configuration that we must do</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mlrI7AsWn_yPRd0KxSnqZg.png" /></figure><h4>Build and Deploy Docker Image with Docker Commands</h4><p>Step 1) <em>Configured Docker to use </em><em>gcloud as a credential helper</em></p><pre>$gcloud auth configure-docker</pre><p>Step 2) <em>Build the docker image</em></p><blockquote><em>docker build -t [HOSTNAME]/[PROJECT-ID]/[IMAGE] .</em></blockquote><pre>$docker build -t gcr.io/compute-engine-docker-nodejs/nodejs-local-build .</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1011/1*fvEAsiJdjf3nXXEV2YCVxA.png" /></figure><p>Notice I use a dot (.) because I am in the same directory with my project. After the build, If you check docker images, you will see our image(<em>nodejs-local-build</em>) and it has a default tag<strong><em>:latest</em></strong></p><pre>$docker images -a | grep gcr.io</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_895iU0M5-qAxDfInd54fw.png" /></figure><p>Step 3) Push docker image to container registry</p><blockquote>docker push [HOSTNAME]/[PROJECT-ID]/[IMAGE]</blockquote><pre>$docker push  gcr.io/compute-engine-docker-nodejs/nodejs-local-build</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BMa7zn69bJMePcWIaiAZ8g.png" /></figure><p>and our image at container registry.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DKQMsYOVnUpG8F2pyndjig.png" /></figure><h4>Create Firewall Rules</h4><p>We have to make the same configuration if we want our application to communicate outside the world.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ivBZEIV0eaR9eH7jOZPI_w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*78FTOXOFFjShGdgvJ8aArg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xBt7DXDdAEIiA9jkxK41yw.png" /></figure><p>Now we can deploy our Image to Compute Engine</p><h4>Deploy Docker Image to Compute Engine and Run</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wcfy0kLq8g1_R5emG97_JQ.png" /></figure><p>We use our <em>firewall rule tag</em> to fill <em>Networking tags </em>under<em> Networking</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/957/1*ow4Uzt8fapvV7tD8mL1ZPA.png" /></figure><p>Our deployment is successful.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v3sDpt0qW_UrgYavJzaqQg.png" /></figure><p>And it is ALIVE :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/465/1*PE2Dn69F7wZ1ka8q8nqmlA.png" /></figure><p>Resources:</p><ul><li><a href="https://nodejs.org/en/docs/guides/nodejs-docker-webapp/">Dockerizing a Node.js web app | Node.js</a></li><li><a href="https://expressjs.com/">Express.js · Node.js web application framework</a></li><li><a href="https://cloud.google.com/sdk/install">Installing Google Cloud SDK | Cloud SDK Documentation</a></li><li><a href="https://cloud.google.com/container-registry/docs/quickstart">Quickstart for Container Registry | Container Registry Documentation</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=501809832289" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Google Cloud Platform(GCP) - Creating a Project and a Compute Engine Instance with a Startup…]]></title>
            <link>https://kadiremreozcan.medium.com/google-cloud-platform-gcp-creating-a-project-and-a-compute-engine-instance-with-a-startup-b23b284861ff?source=rss-b61c237af83b------2</link>
            <guid isPermaLink="false">https://medium.com/p/b23b284861ff</guid>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[compute-engine]]></category>
            <category><![CDATA[autoscaling]]></category>
            <category><![CDATA[gcp]]></category>
            <category><![CDATA[startup-script]]></category>
            <dc:creator><![CDATA[Kadir Emre Ozcan]]></dc:creator>
            <pubDate>Sun, 29 Mar 2020 16:02:32 GMT</pubDate>
            <atom:updated>2020-04-08T11:43:07.266Z</atom:updated>
            <content:encoded><![CDATA[<h3>Google Cloud Platform(GCP) Creating a Project and a Compute Engine Instance with a Startup Script Attached and Auto-scaling Process</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GlXFDTF0zFHuN4PdOg9VSQ.png" /></figure><h4>Plan of the post:</h4><blockquote>1. Creating GCP project</blockquote><blockquote>2. Enabling the Compute Engine Service API</blockquote><blockquote>3. Creating Instance</blockquote><blockquote>4. Starting Instance with Startup Script</blockquote><blockquote>— 4.a — Creating Bucket</blockquote><blockquote>— 4.b — Creating a Startup Script</blockquote><blockquote>— 4.c — Outcome of Startup Script</blockquote><blockquote>— 4.d — Last Touch: Providing the Startup Script via Cloud Storage</blockquote><blockquote>5. Auto Scaling Compute Engine Instance</blockquote><blockquote>— 5.a — Creating Instance Template and an Instance from Template</blockquote><blockquote>— 5.b — Creating Managed Instance Groups (MIG) and Setting Up Auto-Scaling</blockquote><h3>Creating a Project</h3><p>There are three ways of <a href="https://cloud.google.com/resource-manager/docs/creating-managing-projects">creating a project in GCP </a>the first one from GCP web UI , the second one from Terminal and the third one is via API. We will examine web UI and terminal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/259/1*DfC_Wx1F74PxgNIIUD8v4A.png" /></figure><p>Web UI: After entering GCP select project drop-down left upper side</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/995/1*MnhPFRXfK_zNbM9R6Js7ww.png" /></figure><p>and a new popup will appears which contains a list of all projects. Also, you can create a new project by clicking the ‘new project’ button right upper side.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/923/1*XsCdYiEX1bw5iCY0qvRm4Q.png" /></figure><p>We will name our project as the ‘<em>Compute Engine Test’</em>. It will take several seconds to create a new project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/586/0*fwbwu8EqXZlWB3Zq" /></figure><p>Now we have a new project.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/710/0*mFgIvdSu4i-fY2h8" /></figure><p>Terminal: Creating a project from Terminal is very straight forward. We open the embedded terminal at GCP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*Arvui3auumz78FvZyRUFCA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/0*12W0fgMtoH1HhHxb" /><figcaption><a href="https://cloud.google.com/resource-manager/docs/creating-managing-projects">Google Documentation</a></figcaption></figure><pre>$gcloud projects create compute-engine-terminal</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*tgpZbBtxENdq0piEBFn5eQ.png" /></figure><p>End result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*DIUIm_nu0Okym79ZpgtATw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JGiWFtS_rp6hG7hbDfXcuQ.png" /></figure><h3>Enable Compute Engine Service API</h3><p>Before, enabling the Compute Engine Service API, I will be deliberately fired Error for you to see differences.</p><blockquote>API [compute.googleapis.com] not enabled on project [169072720064].</blockquote><blockquote>Would you like to enable and retry (this will take a few minutes)?(y/N)? n</blockquote><blockquote><strong>ERROR:</strong> (gcloud.compute.instances.list) HTTPError 403: Access Not Configured.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*_FnU_Eefn8Va3ag_Nn6VCw.png" /></figure><p>If we look to the services list there is no Compute Engine listed in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/1*xIkD4K2Rtw8PJkoPk7CAZQ.png" /></figure><p>Also, you can check it from ‘<em>APIs &amp; Services’</em> tab too.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Yp273epNDwuXyIN-BxmyWA.png" /></figure><p>Also, I want to show you ‘<em>IAM &amp; Admin’</em> permissions too. There is just my email there. We will check it after activating the Compute Engine API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-ue6KQq6Y2kOfi2uNem1Gg.png" /></figure><p>But when we try to enable API, we face another error <strong><em>‘Billing must be enabled for activation of service’</em></strong>. Every project has to link a billing account. I already created a billing account and linked that account with another project. But you can check this <a href="https://cloud.google.com/billing/docs/how-to/manage-billing-account"><em>link</em></a> for billing account creation. Remember that when you design architecture, try to make your component as loosely coupled as possible this is why I created a separate project for the Billing account</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*ptR_KX6ulF0AsOXp3fOjLQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bdzws3L5zAHVeIml1K-mSw.png" /></figure><p>I clicked <strong><em>‘ENABLE BILLING’</em></strong> and set an Account. It takes several seconds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/656/1*a9gYzmxEKDUjQK6ACtrMuQ.png" /></figure><p>and now API enabled. If I look IAM &amp; Admin tab there will be 3 other members as an addition to the previous one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*khy_ZJjemhmKYj9TpzpUVQ.png" /></figure><p>also when we list, Compute Engine API will be there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*rZYD-wv2XxBIwpUIzm_MQw.png" /></figure><h3>Creating Instance</h3><p>As always there are three ways to create a compute engine instance from terminal, web UI and via API. I will continue with Web UI after this point of post but you can try doing the same things by terminal or API. For example, you can create one from the terminal even though it needs a little bit configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*UQc47GQQqFTxhnusw3kP0g.png" /><figcaption>Terminal Command if you want to use the terminal</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*2S4ZJk8w2C639_jklzRbEA.png" /></figure><p>We are choosing the Compute Engine tab from the left bar. We will see the create button after clicking it, there will be a list for configuration.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r56bDcveN76eoQd1pq3W2A.png" /></figure><p>Now I want to create an instance that qualifies for the<em> free tier.</em> So I will pick a region and zone that are in the<em> United States but not in Northern Virginia also F1 micro as machine type.</em> You will notice console providing us some valuable information as we play configuration like how much cost our selection to us.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OWuJtM2mhuiwA8NlSB2sgQ.png" /></figure><p>Also, there are lots of other configurations we can tweak but for now, that is enough and we leave the rest of it as is and hit the create button at the end of the page. But if you want to learn more you can visit Google <a href="https://cloud.google.com/compute/docs/instances/create-start-instance">documentation</a> and I also recommend you check some configurations which are the <em>Startup script, Metadata, Preemptibility and Automatic start</em>. Creating a new Compute Engine instance will take a couple of seconds.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2VsuHbtrij804ELnEJixKQ.png" /></figure><h3>Starting Instance with Startup Script</h3><p>We have a Compute Engine instance but it does nothing and this is not satisfying for me. So I want to do some fancy staff and to use as much as a feature I can. I am going to create a startup script that runs when the instance starts every time also create a bucket to put my log files.</p><h4>Creating Bucket</h4><p>We select Storage from the side menu,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/356/1*i9QDoWhMZq1uvTxHm8rZmg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S5FJv56H_pmp27IZuK1tRw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/1*GwPWldsEZid0nMN3v8sHWw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/452/1*lNQ0pJR8XtsqFo7NNbV8rQ.png" /></figure><p>I named my bucket as compute-engine-bucket and choose the region as location type and us-west2 for free tier and clicked create.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*z4CF7gg-f7LhSfild3rqpA.png" /></figure><p>So we created a bucket and it is time to connect it to Compute Engine. We can set a bucket whether at the creation of VM instance or after by editing already created VM. I am going to add the one that already created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*ia7RKlqCM2epPBiH8Vg4TA.png" /></figure><p>Notice how we write the bucket name gs://&lt;bucket-name&gt;/</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/397/1*gzKpWzSUar-56MCJmUluwQ.png" /></figure><h4>Troubleshooting</h4><p>After I deployed my script, I got “AccessDeniedException: 403 Insufficient Permission” error when instance tried to copy the log to the storage(bucket). It is self-explanatory but means that my VM instance doesn’t have the required permission.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8Jy9VoRTSrNWRQ99yvoJ9Q.png" /></figure><p>So how can we give permission VM to access Storage? <em>Answer: Service Account</em></p><blockquote>A <a href="https://cloud.google.com/compute/docs/access/service-accounts">service account</a> is a special account that can be used by services and applications running on your Compute Engine instance to interact with other Google Cloud APIs. Apps can use service account credentials to authorize themselves to a set of APIs and perform actions within the permissions granted to the service account and virtual machine instance. In addition, you can create firewall rules that allow or deny traffic to and from instances based on the service account that you associate with each instance.</blockquote><p>I copied VM instance service account which can be found VM instance detail and added it to the storage bucket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/297/1*MeM9J9vN45c5OgM3CYeDcg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/454/1*U-mfJHPHiMCJsU6YxnmkAw.png" /></figure><p>But what if we don’t want to add my service account every time we create an instance. When we are creating a bucket there is an option called “Choose how to access to object” and there is an explanation for each option. The step accepts <em>“</em><a href="https://cloud.google.com/storage/docs/access-control/lists?_ga=2.75556253.-962434924.1570446010"><em>Fine-grained</em></a><em>”</em> as default therefor we have to need to add every time we create an instance but if we change it as <em>“</em><a href="https://cloud.google.com/storage/docs/uniform-bucket-level-access?_ga=2.75556253.-962434924.1570446010"><em>uniform</em></a><em>” </em>we no longer need to add every service account at the permission list of the bucket.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/547/1*7OiMZtPqqhNWwBkuz3Mu-A.png" /></figure><h4>Creating a Startup Script</h4><p>You can find detailed usage of the startup script at this<a href="https://cloud.google.com/compute/docs/startupscript#startupscriptrunninginstances"> link</a>. Also, I am going to add all the sources that I use to create this startup script.</p><p>So what do we plan to do?</p><p>1- Making available system logs in Stackdriver Logs<br>2- Putting under stress our VM instance to monitor performance<br>3- Copying log files into the bucket</p><pre>#! /bin/bash</pre><pre>#<br># -x : the xtrace option can be applied to a script by invoking Bash with the -x switch. This tells Bash to show us what each statement looks like after evaluation, just before it is executed.<br># -v : oposite of -x, shows each line before it is evaluated instead of after. Options can be combined and by using both -xv<br># GCE startup script output shows up in &quot;/var/log/syslog&quot;.<br>#<br>set -x</pre><pre>#<br># noninteractive : This is the anti-frontend. It never interacts with you  at  all, and  makes  the  default  answers  be used for all questions.<br>#<br>export DEBIAN_FRONTEND=noninteractive</pre><pre>#<br># up to date system <br># -y : Automatic yes to prompts; assume &quot;yes&quot; as answer to all prompts and run non-interactively<br># -q : Quiet. Produces output suitable for logging, omitting progress indicators.<br>#<br>apt-get -yq update<br>apt-get -yq upgrade</pre><pre>#<br># Install Google&#39;s Stackdriver logging agent<br># <a href="https://cloud.google.com/logging/docs/agent/installation">https://cloud.google.com/logging/docs/agent/installation</a><br>#<br>curl -sSO <a href="https://dl.google.com/cloudagents/install-logging-agent.sh">https://dl.google.com/cloudagents/install-logging-agent.sh</a><br>bash install-logging-agent.sh</pre><pre>#<br># Install &quot;stress&quot; tool <br># Run the stress command to spawn 8 workers spinning on sqrt() with a timeout of 120 seconds<br>#<br>apt-get -yq install stress<br>stress -cpu 8 -timeout 120</pre><pre>#<br># Report that we&#39;re done.<br>#</pre><pre># Metadata should be set in the &quot;logs-bucket&quot; attribute using the &quot;gs://mybucketname/&quot; format.<br>log_bucket_metadata_name=logs-bucket<br>log_bucket_metadata_url=&quot;<a href="http://metadata.google.internal/computeMetadata/v1/instance/attributes/$%7Blog_bucket_metadata_name">http://metadata.google.internal/computeMetadata/v1/instance/attributes/${log_bucket_metadata_name</a>}&quot;<br>worker_log_bucket=$(curl -H &quot;Metadata-Flavor: Google&quot; &quot;${log_bucket_metadata_url}&quot;)</pre><pre># We write a file named after this machine.<br>worker_log_file=&quot;machine-$(hostname)-finished.txt&quot;<br>echo &quot;Eureka :)  Work completed at $(date)&quot; &gt;&quot;${worker_log_file}&quot;</pre><pre># And we copy that file to the bucket specified in the metadata.<br>echo &quot;Copying the log file to the bucket...&quot;<br>gsutil cp &quot;${worker_log_file}&quot; &quot;${worker_log_bucket}&quot;</pre><p>I am going to add the startup script directly and as a next step, we are going to carry it to storage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*li5Rsdp0F6Z5MdoF_S8CqQ.png" /></figure><p>We have one more thing when I created my VM I left Cloud API access scopes as <em>“Allow default access” </em>but the problem is default access has only read access and not write access to <em>“Storage”</em> so I am going to change it. We have to stop VM to change Cloud API access scopes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*4zOVA30FrUef9CLTOSoD5w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/434/1*QrAfl39j7Pm7uB20NOUO-Q.png" /></figure><p>Now, I am going to start my VM and watch what will happen :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/844/1*osNMSIX6qy-t7ixrxTW-FA.png" /></figure><h4>Outcome of Startup Script</h4><p>1- Making available system logs in Stackdriver Logs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*jhIyi_tSfTvqkchujDw-TA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kTcYrR7UldRPKuV1wVjYwQ.png" /></figure><p>2- Putting under stress our VM instance to monitor performance</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DSExcAn7n-CJwqQGQAyJ2g.png" /></figure><p>3- Copying log files into the bucket</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1017/1*k-011uvjLPG8nFfAjHV-2g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*SW6UEaVSZ3Idq42r-ExhXw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/779/1*tkebjf2EIzLr1xiQnR4C5w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/932/1*4iw6ca6KFFnFPtjxalXW8w.png" /></figure><h4>Last Touch: Providing the Startup Script via Cloud Storage</h4><p>We have to make a couple of changes to get and run the startup script. First, we have to update Cloud API access scope of VM instance and give Read/Write permission to the Storage</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/483/1*sYdkxnONzmekMA4RCtS6LQ.png" /></figure><p>Before upload, your startup script makes sure it is formatted “Unix(LF)” format or you can get an error. I used <em>“</em><a href="https://linux.die.net/man/1/dos2unix"><em>dos2unix</em></a><em> startup-script.sh” </em>command to make sure my formatting correct. You can download <em>“doas2unix”</em> via<em> “sudo apt install dos2unix”</em> command</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/802/1*MbQZOXpNwu_bc1ATb74QPA.png" /></figure><p>After we upload our startup script to Storage bucket, copy its URI, we can also use Link URL too but I am going to use URI. There are also differences between them but they are not the topic of this post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I9L0iP7NTG0jDTUxN_w_eg.png" /></figure><p>Now we update metadata of VM instance according to picture to get the startup script from storage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/661/1*qJSEjpV0UMeSWrLBmxMheA.png" /></figure><p>And everything works fine :) we can see it at the Stackdriverlogs</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZEj26dPP0Z-BzqxBgk7xwA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NfuGL3mqHGPsDs1ptDPK0w.png" /></figure><p>I also recommend “<a href="https://medium.com/google-cloud/manage-google-compute-engine-with-node-js-eef8e7a111b4">Manage Google Compute Engine with Node.js</a>” post that belongs to Franziska Hinkelmann who shows how can you create a Compute Engine instance with a startup script via Node.js</p><h3>Auto Scaling Compute Engine Instance</h3><blockquote>Managed instance groups offer autoscaling capabilities that let you automatically add or delete instances from a managed instance group based on increases or decreases in load. Autoscaling helps your apps gracefully handle increases in traffic and reduce costs when the need for resources is lower. You define the autoscaling policy and the autoscaler performs automatic scaling based on the measured load.</blockquote><blockquote>Autoscaling works by adding more instances to your instance group when there is more load (upscaling), and deleting instances when the need for instances is lowered (downscaling).</blockquote><h4>Creating Instance Template and an Instance from Template</h4><blockquote>An<a href="https://cloud.google.com/compute/docs/instance-templates"> instance template</a> is a <a href="https://cloud.google.com/compute/docs/reference/latest/instanceTemplates">resource</a> that you can use to create Virtual Machine (VM) instances and managed instance groups (MIGs).</blockquote><blockquote>Instance templates define the machine type, boot disk image or container image, labels, and other instance properties.</blockquote><blockquote>Use instance templates any time you want to quickly create VM instances based off of a preexisting configuration.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JtEhW38AqRm8aBSS4FXWYQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BaD_kyDBimh95Qe4yx72-g.png" /></figure><p>We created our instance template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/831/1*jUAYJiWQObD6Iq5dxsDuEQ.png" /></figure><p>If we click it, we can see the detail of it and also some options that we can do with that template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9WMHPo84iNKSyycLx28f9w.png" /></figure><p>Next, we are going to create an instance from this template.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/1*xONp29wCttvoWY4u2OjwtA.png" /></figure><p>I leave everything as it is but instance name which I choose one that I see more suitable and hit create button. GCP created an instance and it worked as we expected at least mine worked as I expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QtLDBeBk_MYOK8eMi33kQQ.png" /></figure><h4>Creating Managed Instance Groups (MIG) and Setting Up Auto-Scaling</h4><blockquote>A<a href="https://cloud.google.com/compute/docs/instance-groups/creating-groups-of-managed-instances"> managed instance group (MIG)</a> contains identical virtual machine (VM) instances that are based on an <a href="https://cloud.google.com/compute/docs/instance-templates">instance template</a>. MIGs maintain high availability of your applications by proactively keeping your VMs available, that is, in the RUNNING state.</blockquote><blockquote>MIGs support <a href="https://cloud.google.com/compute/docs/instance-groups/autohealing-instances-in-migs">autohealing</a>, <a href="https://cloud.google.com/compute/docs/instance-groups/adding-an-instance-group-to-a-load-balancer">load balancing</a>, <a href="https://cloud.google.com/compute/docs/autoscaler">autoscaling</a>, and <a href="https://cloud.google.com/compute/docs/instance-groups/rolling-out-updates-to-managed-instance-groups">auto-updating</a>.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*llzECwetDZ_ixi4Fp7ShrA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/986/1*LQ4A6vMBM-HlSsGKd6QxmA.png" /></figure><p>These are my configuration of MIG and you are free to play with them. Also, I recommend you to read documentation to get further information about them.</p><p>The important things about these configurations are as follows. First, MIG wants you to choose an instance template, not to include already created instances. This is important for auto-scaling and as you notice auto-scaling comes next. There are multiple choose for auto-scaling and we choose the <em>“ auto-scale option “</em> because we want from google to create and delete our instance the parameters that we gave. Therefore, Autoscaling metrics follow and I choose <em>“CPU utilization: 30%”</em> because as you remember we put our instances in CPU stress in the startup script to monitor. I choose 30seconds as a cool-down period you can read what it is in here also I want always at least one instance to be available, most importantly at least 1 instance has to be run if you are working with Compute Engine and I want them to scale max 4 instances. Auto healing is another important thing and again I recommend you to read the documentation for more information.</p><p>After clicking create button it directly creates a new instance and starts to scale when stress increase</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UaPowuvZUjxpBJssQYRl5Q.png" /></figure><p>If you click <em>“Instance groups”</em>, you will see our group is there.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wSqDKd0VMXlV5U0Wsyeyrg.png" /></figure><p>For now, One instance is working because when I was writing the post, I forgot to take a screenshot of the scaling process but I can show you to scale history.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xuj90D6TGuFvKU9OrEx6XQ.png" /></figure><p>and as you see when stress increases GCP scale our instance count until it reaches 4 then when stress is started to decrease our instance count decreases with it too. Also, we can verify they are worked as we expected by looking whether they copied a log file to Storage bucket as they should do.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Yu7bi-qQ7WdNekEJxiDUA.png" /></figure><h4>Resources:</h4><ul><li><a href="https://cloud.google.com/compute"></a></li><li><a href="https://cloud.google.com/resource-manager/docs/creating-managing-projects">Creating and managing projects | Resource Manager Documentation | Google Cloud</a></li><li><a href="https://www.udemy.com/course/google-certified-associate-cloud-engineer/">Google Certified GCP Associate Cloud Engineer Certification</a></li><li><a href="https://cloud.google.com/storage/docs/creating-buckets">Create buckets | Cloud Storage | Google Cloud</a></li><li><a href="https://cloud.google.com/storage/docs/access-control/lists?_ga=2.75556253.-962434924.1570446010">Access control lists (ACLs) | Cloud Storage | Google Cloud</a></li><li><a href="https://cloud.google.com/storage/docs/uniform-bucket-level-access?_ga=2.75556253.-962434924.1570446010">Uniform bucket-level access | Cloud Storage | Google Cloud</a></li><li><a href="https://cloud.google.com/compute/docs/instances/create-start-instance">Create and start a Compute Engine instance | Compute Engine Documentation | Google Cloud</a></li><li><a href="https://cloud.google.com/compute/docs/startupscript#startupscriptrunninginstances">Running startup scripts | Compute Engine Documentation | Google Cloud</a></li><li><a href="https://codelabs.developers.google.com/codelabs/cpo200-startup-scripts/index.html?index=..%2F..cpo200#0">Google Compute Engine Startup Scripts</a></li><li><a href="https://linuxconfig.org/how-to-debug-bash-scripts">How to Debug Bash Scripts</a></li><li><a href="https://cloud.google.com/logging/docs/agent/installation">Installing the Cloud Logging agent on a single VM | Google Cloud</a></li><li><a href="https://www.tecmint.com/linux-cpu-load-stress-test-with-stress-ng-tool/">How to Impose High CPU Load and Stress Test on Linux Using &#39;Stress-ng&#39; Tool</a></li><li><a href="https://medium.com/google-cloud/manage-google-compute-engine-with-node-js-eef8e7a111b4">Manage Google Compute Engine with Node.js</a></li><li><a href="https://cloud.google.com/compute/docs/access/service-accounts">Service accounts | Compute Engine Documentation | Google Cloud</a></li><li><a href="https://cloud.google.com/compute/docs/autoscaler">Autoscaling groups of instances | Compute Engine Documentation | Google Cloud</a></li><li><a href="https://cloud.google.com/compute/docs/instance-templates">Instance templates | Compute Engine Documentation | Google Cloud</a></li><li><a href="https://cloud.google.com/compute/docs/instance-groups/creating-groups-of-managed-instances">Creating managed instance groups | Compute Engine Documentation</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b23b284861ff" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>