By: Saikrishna Madupu – Sr Devops Engineer
Reloader is a Kubernetes tool that automatically reloads configuration files in a running container when a change is detected. This can be useful for updating configurations without having to manually restart your application. This blog will walk through the process of setting up Reloader in Kubernetes. It will provide an example of MySQL deployment to enable reloader using annotations and MySQL deployment YAML to watch for changes in secrets.
Installation:
• kubectl apply -f
https://raw.githubusercontent.com/stakater/Reloader/master/deployments/kubernetes/reloader.yaml
• Helm
helm repo add stakater https://stakater.github.io/stakater-charts
helm repo update
helm install stakater/reloader
Notes:
By default, Reloader is deployed in the default namespace and monitors all namespaces for changes to secrets and configmaps.
Example:
Create a secret for the MySQL DB:
secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: demo-secret
annotations:
reloader.stakater.com/auto: "true"
data:
password: dGVzdGluZzEyMzQK
kubectl apply -f secret.yaml
kubectl describe secret demo-secret
Name: demo-secret
Namespace: keyva
Labels: <none>
Annotations: reloader.stakater.com/auto: true
Type: Opaque
Data
====
password: 13 bytes
username: 8 bytes
Create PVC and PC for MySQL:
persistent-volume.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 2Gi
kubectl apply -f persistent-volume.yaml
kubectl describe pvc
Name: mysql-pv-claim
Namespace: keyva
StorageClass: manual
Status: Bound
Volume: mysql-pv-volume
Labels: <none>
Annotations: pv.kubernetes.io/bind-completed: yes
pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pvc-protection]
Capacity: 2Gi
Access Modes: RWO
VolumeMode: Filesystem
Used By: mysql-7ddf8fdbb8-bbbms
mysql-7ddf8fdbb8-thdv9
mysql-7ddf8fdbb8-z2rjj
Events: <none>
kubectl describe pv
Name: mysql-pv-volume
Labels: type=local
Annotations: pv.kubernetes.io/bound-by-controller: yes
Finalizers: [kubernetes.io/pv-protection]
StorageClass: manual
Status: Bound
Claim: keyva/mysql-pv-claim
Reclaim Policy: Retain
Access Modes: RWO
VolumeMode: Filesystem
Capacity: 2Gi
Node Affinity: <none>
Message:
Source:
Type: HostPath (bare host directory volume)
Path: /mnt/data
HostPathType:
Events: <none>
Create deployment YML for MySQL container using above Secrets, PVC and PV:
kubectl apply -f my-sql.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
clusterIP: None
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
annotations:
reloader.stakater.com/auto: "true"
spec:
selector:
matchLabels:
app: mysql
replicas: 3
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
# Use secret in real usage
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: demo-secret
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
Once deployed, you can verify the pods status:
Kubectl get pods
kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-5b8d8bd99b-277mf 1/1 Running 0 10s
mysql-5b8d8bd99b-6x5wz 1/1 Running 0 10s
mysql-5b8d8bd99b-srlnb 1/1 Running 0 10s
Describing the pod to view details:
kubectl describe deployment mysql
Name: mysql
Namespace: keyva
CreationTimestamp: Sun, 19 Feb 2023 20:28:02 -0600
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
reloader.stakater.com/auto: true
Selector: app=mysql
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: Recreate
MinReadySeconds: 0
Pod Template:
Labels: app=mysql
Containers:
mysql:
Image: mysql:5.6
Port: 3306/TCP
Host Port: 0/TCP
Environment:
MYSQL_ROOT_PASSWORD: <set to the key 'password' in secret 'demo-secret'> Optional: false
Mounts:
/var/lib/mysql from mysql-persistent-storage (rw)
Volumes:
mysql-persistent-storage:
Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
ClaimName: mysql-pv-claim
ReadOnly: false
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: mysql-5b8d8bd99b (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 11m deployment-controller Scaled up replica set mysql-5b8d8bd99b to 3
- Next, validate the Reloader by changing the secret values and redeploy the secret.yaml file. You should see the MySQL pods restart to pick reference the new secret value.
Reloader can monitor changes to ConfigMap and Secret, as well as perform rolling upgrades on Pods and their associated DeploymentConfigs, Deployments, Daemonsets Statefulsets, and Rollouts.
Validation of reloader:
kubectl logs reloader-reloader-7f6b8d49f7-9lxrx
time="2023-02-22T01:59:48Z" level=info msg="Changes detected in 'demo-secret' of type 'SECRET' in namespace 'keyva', Updated 'mysql' of type 'Deployment' in namespace 'keyva'"
Verify the age of Pods:
kubectl get pods
NAME READY STATUS RESTARTS AGE
mysql-7bd6b6d789-fqfgd 1/1 Running 0 3s
mysql-7bd6b6d789-tbxxp 1/1 Running 0 3s
mysql-7bd6b6d789-trd7v 1/1 Running 0 3s
Ref: K8-Reloader
About the Author
Saikrishna Madupu, Sr. DevOps Engineer Sai is an IT professional with experience in DevOps Automation, Configuration Management tools, Container workloads and orchestration of those workloads via Kubernetes. A self-starter, and passionate problem-solver, with a flair for innovative design, with an ability to work towards automation whenever possible. He is an experienced Linux, Cloud data center operations and infrastructure engineer. He worked as a Devops cloud consultant in the past, helping clients in migrate on-prem applications to cloud, and holds certifications for AWS, Terraform, Gitlab, and other technologies. |