A logo showing the text blog.marcnuri.com
Español
Home»Java»Rollout Restart Kubernetes Deployment from Java using YAKC

Recent Posts

  • Fabric8 Kubernetes Client 7.2 is now available!
  • Connecting to an MCP Server from JavaScript using AI SDK
  • Connecting to an MCP Server from JavaScript using LangChain.js
  • The Future of Developer Tools: Adapting to Machine-Based Developers
  • Connecting to a Model Context Protocol (MCP) Server from Java using LangChain4j

Categories

  • Artificial Intelligence
  • Front-end
  • Go
  • Industry and business
  • Java
  • JavaScript
  • Legacy
  • Operations
  • Personal
  • Pet projects
  • Tools

Archives

  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • August 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • February 2020
  • January 2020
  • December 2019
  • October 2019
  • September 2019
  • July 2019
  • March 2019
  • November 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • December 2017
  • July 2017
  • January 2017
  • December 2015
  • November 2015
  • December 2014
  • March 2014
  • February 2011
  • November 2008
  • June 2008
  • May 2008
  • April 2008
  • January 2008
  • November 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007

Rollout Restart Kubernetes Deployment from Java using YAKC

2020-10-12 in Java tagged Java / Kubernetes / REST / YAKC by Marc Nuri | Last updated: 2021-05-23

Introduction

In this post, I’ll show you how to perform the equivalent to kubectl rollout restart deployment/$deployment (rolling restart) from Java using YAKC- Yet Another Kubernetes Client.

Rollout Restart

Starting at version 1.15, Kubernetes offers a new way to restart Deployments, DaemonSets, and StatefulSets using kubectl.

You can simply invoke a kubectl rollout restart deployment/$deployment and Kubernetes will restart your application with zero downtime.

Before v1.15 you could do something similar by adding an annotation to the Deployment spec template. If we dig into the source code for Kubectl, we’ll find out that what the provided command does is exactly that. Kubectl adds a new annotation to the Pod templates so that the underlying Pods get reconciled via new ReplicaSets without any downtime.

1case *appsv1.Deployment:
2  if obj.Spec.Paused {
3    return nil, errors.New("can't restart paused deployment (run rollout resume first)")
4  }
5  if obj.Spec.Template.ObjectMeta.Annotations == nil {
6    obj.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
7  }
8  obj.Spec.Template.ObjectMeta.Annotations["kubectl.kubernetes.io/restartedAt"] = time.Now().Format(time.RFC3339)
9  return runtime.Encode(scheme.Codecs.LegacyCodec(appsv1.SchemeGroupVersion), obj)

Rollout Restart from Java

Now that we know that performing a rolling restart is as simple as patching a Deployment/DaemonSet/StatefulSet to add a new annotation to the PodTemplate, let’s see how we can achieve this with YAKC.

Since it’s a simple matter of patching the PodTemplate annotations, you can achieve this by using the following code:

1final Deployment deployment = new Deployment();
2deployment.setSpec(new DeploymentSpec());
3deployment.getSpec().setTemplate(PodTemplateSpec.builder()
4  .metadata(ObjectMeta.builder()
5    .putInAnnotations("yakc.marcnuri.com/restartedAt", Instant.now().toString())
6    .build())
7  .build());
8kubernetesClient.create(AppsV1Api.class)
9  .patchNamespacedDeployment("deployment-namespace", "your-namespace", deployment)
10  .get();

In the first lines, I create an empty Deployment with an empty DeploymentSpec. I could try to do this with the builders provided by YAKC. The problem is that since we are going to perform a PATCH some of the required fields will be missing and the builder validation would throw an Exception otherwise.

For the annotation part, I’m using the ObjectMeta#builder (no required field exceptions this time). I’m simply adding an annotation for yakc.marcnuri.com/restartedAt with the current timestamp (same as Kubectl).

Finally, I’m sending the Deployment PATCH using YAKC Kubernetes Client for the AppsV1API.

I showed you how to do this for Deployments, but you can do the exact same thing with DaemonSets:

1final DaemonSet daemonSet = new DaemonSet();
2daemonSet.setSpec(new DaemonSetSpec());
3daemonSet.getSpec().setTemplate(PodTemplateSpec.builder()
4  .metadata(ObjectMeta.builder()
5    .putInAnnotations("yakc.marcnuri.com/restartedAt", Instant.now().toString())
6    .build())
7  .build());
8kubernetesClient.create(AppsV1Api.class)
9  .patchNamespacedDaemonSet("daemonset-namespace", "your-namespace", daemonSet)
10  .get();

and StatefulSets:

1final StatefulSet statefulSet = new StatefulSet();
2statefulSet.setSpec(new StatefulSetSpec());
3statefulSet.getSpec().setTemplate(PodTemplateSpec.builder()
4  .metadata(ObjectMeta.builder()
5    .putInAnnotations("yakc.marcnuri.com/restartedAt", Instant.now().toString())
6    .build())
7  .build());
8kubernetesClient.create(AppsV1Api.class)
9  .patchNamespacedStatefulSet("statefulset-name", "your-namespace", statefulSet)
10  .get();

Following you can check the operation result in YAKC – Kubernetes Dashboard interface (you can read more about this project here):

YAKC Kubernetes Dashboard Deployment Rollout Restart Demo
YAKC Kubernetes Dashboard Deployment Rollout Restart Demo

Conclusion

In this post, I’ve shown you how to perform rollout restarts for Deployments, DaemonSets, and StatefulSets from Java using YAKC.

I’ve extracted the code for this post from my YAKC – Kubernetes Dashboard quickstart. You can learn more by visiting YAKC’s GitHub project site. You can also see related code here.

Twitter iconFacebook iconLinkedIn iconPinterest iconEmail icon

Post navigation
Prometheus and Grafana setup in MinikubeReact + Quarkus integration using Maven
© 2007 - 2025 Marc Nuri