Created
July 8, 2024 11:51
-
-
Save jjzazuet/abd7abddc733cf73ee2e4647d9b1fd13 to your computer and use it in GitHub Desktop.
nats-k8s
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.grhk.infra; | |
import io.k8s.api.core.v1.Namespace; | |
import io.k8s.api.core.v1.volumeresourcerequirements.Requests; | |
import io.vacco.beleth.rt.BlKubeRt; | |
import java.util.List; | |
import static io.k8s.api.apps.v1.StatefulSet.statefulSet; | |
import static io.k8s.api.apps.v1.StatefulSetSpec.statefulSetSpec; | |
import static io.k8s.api.core.v1.ConfigMap.configMap; | |
import static io.k8s.api.core.v1.ConfigMapVolumeSource.configMapVolumeSource; | |
import static io.k8s.api.core.v1.Container.container; | |
import static io.k8s.api.core.v1.ContainerPort.containerPort; | |
import static io.k8s.api.core.v1.PersistentVolumeClaim.persistentVolumeClaim; | |
import static io.k8s.api.core.v1.PersistentVolumeClaimSpec.persistentVolumeClaimSpec; | |
import static io.k8s.api.core.v1.PodSpec.podSpec; | |
import static io.k8s.api.core.v1.PodTemplateSpec.podTemplateSpec; | |
import static io.k8s.api.core.v1.ResourceRequirements.resourceRequirements; | |
import static io.k8s.api.core.v1.Service.service; | |
import static io.k8s.api.core.v1.ServicePort.servicePort; | |
import static io.k8s.api.core.v1.ServiceSpec.serviceSpec; | |
import static io.k8s.api.core.v1.Volume.volume; | |
import static io.k8s.api.core.v1.VolumeMount.volumeMount; | |
import static io.k8s.api.core.v1.VolumeResourceRequirements.volumeResourceRequirements; | |
import static io.k8s.api.core.v1.configmap.Data.data; | |
import static io.k8s.api.core.v1.resourcerequirements.Limits.limits; | |
import static io.k8s.api.core.v1.resourcerequirements.Requests.requests; | |
import static io.k8s.api.core.v1.servicespec.Selector.selector; | |
import static io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector.labelSelector; | |
import static io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta.objectMeta; | |
import static io.k8s.apimachinery.pkg.apis.meta.v1.labelselector.MatchLabels.matchLabels; | |
import static io.k8s.apimachinery.pkg.apis.meta.v1.objectmeta.Labels.labels; | |
public class Nats { | |
public static void apply(BlKubeRt k, Namespace ns, String memoryMi, String cpuM) { | |
var configMap = configMap() | |
.apiVersion("v1") | |
.kind("ConfigMap") | |
.metadata( | |
objectMeta() | |
.name("nats-config") | |
.namespace(ns.metadata.name) | |
) | |
.data( | |
data().kv("nats.conf", String.join("\n", | |
"server_name: $HOSTNAME", | |
"listen: 0.0.0.0:4222", | |
"http: 0.0.0.0:8222", | |
"jetstream: { store_dir: \"/data\" }", | |
"cluster: {", | |
" name: \"c1\"", | |
" listen: 0.0.0.0:6222", | |
" routes: [\"nats://nats-0.nats:6222\", \"nats://nats-1.nats:6222\", \"nats://nats-2.nats:6222\"]", | |
" cluster_advertise: \"$HOSTNAME:6222\"", | |
"}" | |
)) | |
); | |
var statefulSet = statefulSet() | |
.apiVersion("apps/v1") | |
.kind("StatefulSet") | |
.metadata( | |
objectMeta() | |
.name("nats") | |
.namespace(ns.metadata.name) | |
.labels(labels().kv("app", "nats")) | |
) | |
.spec( | |
statefulSetSpec() | |
.serviceName("nats") | |
.replicas(3L) | |
.selector(labelSelector().matchLabels(matchLabels().kv("app", "nats"))) | |
.template( | |
podTemplateSpec() | |
.metadata(objectMeta().labels(labels().kv("app", "nats"))) | |
.spec( | |
podSpec() | |
.containers(List.of( | |
container() | |
.name("nats") | |
.image("docker.io/nats:2.10") | |
.command(List.of("/nats-server")) | |
.args(List.of("-c", "/etc/nats-config/nats.conf")) | |
.ports(List.of( | |
containerPort().name("nats-client").containerPort(4222L), | |
containerPort().name("nats-cluster").containerPort(6222L), | |
containerPort().name("nats-http").containerPort(8222L) | |
)) | |
.volumeMounts(List.of( | |
volumeMount().name("data").mountPath("/data"), | |
volumeMount().name("nats-config").mountPath("/etc/nats-config") | |
)) | |
.resources( | |
resourceRequirements() | |
.requests(requests().kv("memory", memoryMi).kv("cpu", cpuM)) | |
.limits(limits().kv("memory", memoryMi).kv("cpu", cpuM)) | |
) | |
)) | |
.volumes(List.of( | |
volume() | |
.configMap(configMapVolumeSource().name("nats-config")) | |
.name("nats-config") | |
)) | |
) | |
) | |
.volumeClaimTemplates(List.of( | |
persistentVolumeClaim() | |
.metadata(objectMeta().name("data").labels(labels().kv("app", "nats"))) | |
.spec( | |
persistentVolumeClaimSpec() | |
.accessModes(List.of("ReadWriteOnce")) | |
.resources(volumeResourceRequirements().requests(new Requests().kv("storage", "8Gi"))) | |
.storageClassName("local-nats") | |
) | |
)) | |
); | |
var service = service() | |
.apiVersion("v1") | |
.kind("Service") | |
.metadata( | |
objectMeta() | |
.name("nats") | |
.namespace(ns.metadata.name) | |
.labels(labels().kv("app", "nats")) | |
) | |
.spec( | |
serviceSpec() | |
.type("NodePort") | |
.externalTrafficPolicy("Local") | |
.selector(selector().kv("app", "nats")) | |
.ports(List.of( | |
servicePort().name("nats-client").port(4222L).targetPort("nats-client").nodePort(30422L), | |
servicePort().name("nats-http").port(8222L).targetPort("nats-http").nodePort(30081L) | |
)) | |
); | |
k | |
.add(configMap) | |
.add(statefulSet) | |
.add(service) | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment