I want to load the config changes into the pod and start using it without pod restart in Micronaut. I have created a sample application to try this out. Used Minikube to the test the changes.
Created 2 simple classes along with the Application.java
DummyConfiguration.java
@Refreshable
@ConfigurationProperties("dummy-configuration")
public class DummyConfiguration {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
ConfigResource.java
@Controller("/config")
public class ConfigResource{
private final DummyConfiguration dummyConfiguration;
public ConfigResource(DummyConfiguration dummyConfiguration) {
this.dummyConfiguration = dummyConfiguration;
}
@Get("/color")
public String getColor() {
return "The color is: " + dummyConfiguration.getColor();
}
}
Application.java
import io.micronaut.runtime.Micronaut;
public class Application {
public static void main(String[] args) {
Micronaut.run(Application.class, args);
}
}
Dockerfile
FROM openjdk:18-alpine
EXPOSE 8080
COPY target/k8s-0.1.jar /etc/configdemo.jar
COPY target/classes/application.yml /etc/configuration/
WORKDIR /etc
ENTRYPOINT ["java", "-Dmicronaut.config.files=file:/etc/configuration/application.yml", "-jar", "configdemo.jar"]
configmap.yml
apiVersion: v1
kind: ConfigMap
metadata:
name: k8s-config
data:
bootstrap.yaml: |
kubernetes:
client:
config-maps:
watch: true
config:
enabled: true
namespace: default
config.yaml: |
micronaut:
application:
name: k8s
config-client:
enabled: true
dummy-configuration:
color: white
And mounted the configmap.yml to the deployment.yml
Deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s
spec:
replicas: 1
selector:
matchLabels:
app: k8s
template:
metadata:
labels:
app: k8s
spec:
containers:
- name: k8s
image: k8s:1
imagePullPolicy: Never
env:
- name: MICRONAUT_CONFIG_FILES
value: "/etc/configuration/bootstrap.yml,/etc/configuration/application.yml"
ports:
- containerPort: 8080
volumeMounts:
- name: config-volume
mountPath: /etc/configuration
volumes:
- name: config-volume
configMap:
name: k8s-config
According to the micronaut ideally the code also should start populating the new values which isn’t happening(https://micronaut-projects.github.io/micronaut-kubernetes/snapshot/guide/). I have changed the color in the configmap and then applied it which isn’t reflecting when I am calling the api.
I am new to Micronaut. Not sure what am I missing out. Thanks in Advance.
2
Answers
Changes to a
ConfigMap
are not reflected in running pods (they need to be restarted).I have used a container called configbump as a sidecar to solve this problem in the past. Basically, instead of mounting the
ConfigMap
as a volume,configbump
will watch theConfigMap
through the kubernetes api and be notified of changes, and then reload those changes. The main container can see those changes when the main container and the sidecar share aemptyDir
volume.Here is a sidecar deployment example.
In addition to Jason Snouffer’s answer, there is also k8s-sidecar doing continuous updates for files from ConfigMaps. The project has a bit more reputation than configbump.