<p>Am new to Kubernetes, my question is related to Google Cloud platform.</p>
<p>Given a scenario we need to restart a kubernetes cluster and we have some services in Spring boot. As Spring boot services are like individual JVM's each and run like an independent process. Once the Kubernetes is restarted in order to restart the Spring boot services I need help in understanding what type of a script or mechanism to use to restart all the services in Kubernetes. Please let me know and thank you and appreciate all your inputs.</p>

gautham
<p>I am not sure if I fully understood your question but I think the best approach for you would be to <code>Pack</code> your <code>Spring Boot app</code> to a <a href="https://www.docker.com/resources/what-container" rel="nofollow noreferrer">Docker container</a> and then use it on <code>GKE</code>.</p>
<p>Good guide about <code>Packing</code> your <code>Spring Boot</code> application to <code>container</code> can be found in <a href="https://codelabs.developers.google.com/codelabs/cloud-springboot-kubernetes#4" rel="nofollow noreferrer">CodeLabs tutorial</a>.</p>
<p>When you will have your application in <code>container</code> you will be able to use it in <code>Deployment</code> or <code>Statefulsets</code> configuration file and deploy it in your cluster.</p>
<p>As mentioned in <a href="https://kubernetes.io/docs/concepts/workloads/controllers/deployment/" rel="nofollow noreferrer">Deployment Documentation</a>:</p>
<blockquote>
<p>A Deployment provides declarative updates for Pods and ReplicaSets.</p>
<p>You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.</p>
</blockquote>
<p>In short, <code>Deployment controller</code> ensure to keep your application in your desired state.</p>
<p>For example if you would like to restart your application you just could scale down <code>Deployment</code> to <code>0 replicas</code> and scale up to <code>5 replicas</code>.</p>
<p>Also as <code>GKE</code> is working on <code>Google Compute Engine</code> VMs you can also scale your cluster nodes number.</p>
<h2>Examples</h2>
<p><strong>Restarting Application</strong></p>
<p>For my test I've used <code>Nginx container</code> in <code>Deployment</code> but it should work similar with your <code>Spring boot app container</code>.</p>
<p>Let's say you have 2 node cluster with 5 replicas aplication.</p>
<pre><code>$ kubectl create deployment nginx --image=nginx --replicas=5
deployment.apps/nginx created
$ kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP          NODE                                       NOMINATED NODE   READINESS GATES
nginx-86c57db685-2x8tj   1/1     Running   0          2m45s   10.4.1.5    gke-cluster-1-default-pool-faec7b51-6kc3   &lt;none&gt;           &lt;none&gt;
nginx-86c57db685-6lpfg   1/1     Running   0          2m45s   10.4.1.6    gke-cluster-1-default-pool-faec7b51-6kc3   &lt;none&gt;           &lt;none&gt;
nginx-86c57db685-8lvqq   1/1     Running   0          2m45s   10.4.0.9    gke-cluster-1-default-pool-faec7b51-x07n   &lt;none&gt;           &lt;none&gt;
nginx-86c57db685-lq6l7   1/1     Running   0          2m45s   10.4.0.11   gke-cluster-1-default-pool-faec7b51-x07n   &lt;none&gt;           &lt;none&gt;
nginx-86c57db685-xn7fn   1/1     Running   0          2m45s   10.4.0.10   gke-cluster-1-default-pool-faec7b51-x07n   &lt;none&gt;           &lt;none&gt;
</code></pre>
<p>Now you would need to change some environment variables inside your application using <a href="https://kubernetes.io/docs/concepts/configuration/configmap/" rel="nofollow noreferrer">ConfigMap</a>. To apply this change you could just use <code>rollout</code>. It would <code>restart</code> your application and provide additional data from <code>ConfigMap</code>.</p>
<pre><code>$ kubectl rollout restart deployment nginx
deployment.apps/nginx restarted
$ kubectl get po -o wide
NAME                     READY   STATUS        RESTARTS   AGE     IP          NODE                                       NOMINATED NODE   READINESS GATES
nginx-6c98778485-2k98b   1/1     Running       0          6s      10.4.0.13   gke-cluster-1-default-pool-faec7b51-x07n   &lt;none&gt;           &lt;none&gt;
nginx-6c98778485-96qx7   1/1     Running       0          6s      10.4.1.7    gke-cluster-1-default-pool-faec7b51-6kc3   &lt;none&gt;           &lt;none&gt;
nginx-6c98778485-qb89l   1/1     Running       0          6s      10.4.0.12   gke-cluster-1-default-pool-faec7b51-x07n   &lt;none&gt;           &lt;none&gt;
nginx-6c98778485-qqs97   1/1     Running       0          4s      10.4.1.8    gke-cluster-1-default-pool-faec7b51-6kc3   &lt;none&gt;           &lt;none&gt;
nginx-6c98778485-skbwv   1/1     Running       0          4s      10.4.0.14   gke-cluster-1-default-pool-faec7b51-x07n   &lt;none&gt;           &lt;none&gt;
nginx-86c57db685-2x8tj   0/1     Terminating   0          4m38s   10.4.1.5    gke-cluster-1-default-pool-faec7b51-6kc3   &lt;none&gt;           &lt;none&gt;
nginx-86c57db685-6lpfg   0/1     Terminating   0          4m38s   &lt;none&gt;      gke-cluster-1-default-pool-faec7b51-6kc3   &lt;none&gt;           &lt;none&gt;
nginx-86c57db685-8lvqq   0/1     Terminating   0          4m38s   10.4.0.9    gke-cluster-1-default-pool-faec7b51-x07n   &lt;none&gt;           &lt;none&gt;
nginx-86c57db685-xn7fn   0/1     Terminating   0          4m38s   10.4.0.10   gke-cluster-1-default-pool-faec7b51-x07n   &lt;nont e&gt;           &lt;none&gt;
</code></pre>
<p><strong>Draining node to perform node operations</strong></p>
<p>Another example can be when you need to do something with your VMs. You can do it using by <a href="https://kubernetes.io/docs/tasks/administer-cluster/safely-drain-node/" rel="nofollow noreferrer">draining node</a>.</p>
<blockquote>
<p>You can use kubectl drain to safely evict all of your pods from a node before you perform maintenance on the node (e.g. kernel upgrade, hardware maintenance, etc.). Safe evictions allow the pod's containers to gracefully terminate and will respect the PodDisruptionBudgets you have specified.</p>
</blockquote>
<p>So it will reschedule all pods from this node to another nodes.</p>
<p><strong>Restarting Cluster</strong></p>
<p>Keep in Mind that <code>GKE</code> is managed by google and you cannot restart one machine as it's managed by <a href="https://cloud.google.com/compute/docs/instance-groups" rel="nofollow noreferrer">Managed instance group</a>.
You can ssh to each node, change some settings. When you scale them to 0 and scale up, you will get new machine with your requirements with new <code>ExternalIP</code>.</p>
<pre><code>$ kubectl get nodes -o wide
NAME                                       STATUS   ROLES    AGE    VERSION             INTERNAL-IP   EXTERNAL-IP     OS-IMAGE                             KERNEL-VERSION   CONTAINER-RUNTIME
gke-cluster-1-default-pool-faec7b51-6kc3   Ready    &lt;none&gt;   3d1h   v1.17.14-gke.1600   10.128.0.25   34.XX.176.56    Container-Optimized OS from Google   4.19.150+        docker://19.3.6
gke-cluster-1-default-pool-faec7b51-x07n   Ready    &lt;none&gt;   3d1h   v1.17.14-gke.1600   10.128.0.24   23.XXX.50.249   Container-Optimized OS from Google   4.19.150+        docker://19.3.6

$ gcloud container clusters resize cluster-1 --node-pool default-pool \
&gt;     --num-nodes 0 \
&gt;     --zone us-central1-c
Pool [default-pool] for [cluster-1] will be resized to 0.

$ kubectl get nodes -o wide
No resources found

$ gcloud container clusters resize cluster-1 --node-pool default-pool     --num-nodes 2     --zone us-central1-c
Pool [default-pool] for [cluster-1] will be resized to 2.

Do you want to continue (Y/n)?  y

$ $ kubectl get nodes -o wide
NAME                                       STATUS   ROLES    AGE   VERSION             INTERNAL-IP   EXTERNAL-IP     OS-IMAGE                             KERNEL-VERSION   CONTAINER-RUNTIME
gke-cluster-1-default-pool-faec7b51-n5hm   Ready    &lt;none&gt;   68s   v1.17.14-gke.1600   10.128.0.26   23.XXX.50.249   Container-Optimized OS from Google   4.19.150+        docker://19.3.6
gke-cluster-1-default-pool-faec7b51-xx01   Ready    &lt;none&gt;   74s   v1.17.14-gke.1600   10.128.0.27   35.XXX.135.41   Container-Optimized OS from Google   4.19.150+        docker://19.3.6

</code></pre>
<h2>Conclusion</h2>
<p>When you are using <code>GKE</code> you are using pre-definied nodes, managed by google and those nodes are automatically upgrading (some security features, etc). Due to that, changing nodes capacity it's easy.</p>
<p>When you <code>pack</code> your application to container and used it in <code>Deployment</code>, your application will be handled by <code>Deployment Controller</code> which will try to keep desired state all the time.</p>
<p>As mention in <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">Service Documentation</a>.</p>
<blockquote>
<p>In Kubernetes, a Service is an abstraction which defines a logical set of Pods and a policy by which to access them</p>
</blockquote>
<p>Service will be still visible in your cluster even if you will scale you cluster to 0 node as this is abstraction. You don't have to restart it. However if you would change some static service configuration (like port) you would need to recreate service with new configuration.</p>
<h2>Useful links</h2>
<ul>
<li><a href="https://cloud.google.com/kubernetes-engine/docs/tutorials/migrating-node-pool" rel="nofollow noreferrer">Migrating workloads to different machine types</a></li>
<li><a href="https://cloud.google.com/kubernetes-engine/docs/how-to/node-auto-repair" rel="nofollow noreferrer">Auto-repairing nodes</a></li>
</ul>
