├── README.md ├── example-nats-cluster.yaml ├── go_subscriber ├── Dockerfile └── client.go ├── images ├── nats-cluster-service.JPG ├── nats-cluster.JPG ├── nats-crd.JPG ├── nats-pub-deployment.JPG ├── nats-pub-logs.JPG ├── nats-pub-pod.JPG ├── nats-sub-deployment.JPG ├── nats-sub-logs.JPG └── nats-sub-pod.JPG ├── java_publisher ├── Dockerfile ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── wordpress │ └── simplydistributed │ └── nats │ └── producer │ └── NATSProducer.java ├── nats-pub-deployment.yaml └── nats-sub-deployment.yaml /README.md: -------------------------------------------------------------------------------- 1 | ## NATS on Kubernetes example 2 | 3 | This example uses [minikube](https://github.com/kubernetes/minikube) 4 | 5 | - start Kubernetes - `minikube start` 6 | - create NATS CRD - `kubectl apply -f https://raw.githubusercontent.com/nats-io/nats-operator/master/example/deployment.yaml` 7 | - check `kubectl get crd` 8 | 9 | ![](images/nats-crd.JPG) 10 | 11 | - deploy cluster - `kubectl apply -f https://raw.githubusercontent.com/nats-io/nats-operator/master/example/example-nats-cluster.yaml` 12 | - check `kubectl get natsclusters.nats.io` 13 | 14 | ![](images/nats-cluster.JPG) 15 | 16 | - create publisher app deployment - `kubectl create -f nats-pub-deployment.yaml` (will pull image from `https://hub.docker.com/r/abhirockzz/nats-pub/`) 17 | 18 | - check `kubectl get deployments -l app=nats-pub` 19 | 20 | ![](images/nats-pub-deployment.JPG) 21 | 22 | - locate the pod - `kubectl get pods -l app=nats-pub` .... 23 | 24 | ![](images/nats-pub-pod.JPG) 25 | 26 | - .. and check logs `kubectl logs ` 27 | 28 | ![](images/nats-pub-logs.JPG) 29 | 30 | - create subscriber app deployment - `kubectl create -f nats-sub-deployment.yaml` (will pull image from `https://hub.docker.com/r/abhirockzz/nats-sub/`) 31 | 32 | - check `kubectl get deployments -l app=nats-sub` 33 | 34 | ![](images/nats-sub-deployment.JPG) 35 | 36 | - locate the pod - `kubectl get pods -l app=nats-sub` .... 37 | 38 | ![](images/nats-sub-pod.JPG) 39 | 40 | - .. and check logs `kubectl logs ` 41 | 42 | ![](images/nats-sub-logs.JPG) 43 | -------------------------------------------------------------------------------- /example-nats-cluster.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: "nats.io/v1alpha2" 2 | kind: "NatsCluster" 3 | metadata: 4 | name: "example-nats-1" 5 | spec: 6 | size: 3 7 | version: "1.1.0" -------------------------------------------------------------------------------- /go_subscriber/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine 2 | COPY client . 3 | CMD ["./client"] -------------------------------------------------------------------------------- /go_subscriber/client.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "runtime" 6 | "time" 7 | "os" 8 | 9 | "github.com/nats-io/go-nats" 10 | ) 11 | 12 | func main() { 13 | subject := "foo" 14 | 15 | natsServer := os.Getenv("EXAMPLE_NATS_1_SERVICE_HOST") 16 | fmt.Println("NAT server --- "+ natsServer) 17 | 18 | natsURL := "nats://"+natsServer+":4222" 19 | fmt.Println("NAT server conn URL --- "+ natsURL) 20 | 21 | opts := nats.Options{ 22 | AllowReconnect: true, 23 | MaxReconnect: 5, 24 | ReconnectWait: 5 * time.Second, 25 | Timeout: 3 * time.Second, 26 | Url: natsURL, 27 | } 28 | 29 | conn, _ := opts.Connect() 30 | //defer conn.Close() 31 | fmt.Println("Subscriber connected to NATS server") 32 | 33 | fmt.Printf("Subscribing to subject %s\n", subject) 34 | conn.Subscribe(subject, func(msg *nats.Msg) { 35 | fmt.Printf("Got message '%s\n", string(msg.Data)+"'") 36 | }) 37 | 38 | runtime.Goexit() 39 | } 40 | -------------------------------------------------------------------------------- /images/nats-cluster-service.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhirockzz/nats-on-kubernetes-example/3c02b77c0e320f32fbdf88b178146d4b706a01af/images/nats-cluster-service.JPG -------------------------------------------------------------------------------- /images/nats-cluster.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhirockzz/nats-on-kubernetes-example/3c02b77c0e320f32fbdf88b178146d4b706a01af/images/nats-cluster.JPG -------------------------------------------------------------------------------- /images/nats-crd.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhirockzz/nats-on-kubernetes-example/3c02b77c0e320f32fbdf88b178146d4b706a01af/images/nats-crd.JPG -------------------------------------------------------------------------------- /images/nats-pub-deployment.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhirockzz/nats-on-kubernetes-example/3c02b77c0e320f32fbdf88b178146d4b706a01af/images/nats-pub-deployment.JPG -------------------------------------------------------------------------------- /images/nats-pub-logs.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhirockzz/nats-on-kubernetes-example/3c02b77c0e320f32fbdf88b178146d4b706a01af/images/nats-pub-logs.JPG -------------------------------------------------------------------------------- /images/nats-pub-pod.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhirockzz/nats-on-kubernetes-example/3c02b77c0e320f32fbdf88b178146d4b706a01af/images/nats-pub-pod.JPG -------------------------------------------------------------------------------- /images/nats-sub-deployment.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhirockzz/nats-on-kubernetes-example/3c02b77c0e320f32fbdf88b178146d4b706a01af/images/nats-sub-deployment.JPG -------------------------------------------------------------------------------- /images/nats-sub-logs.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhirockzz/nats-on-kubernetes-example/3c02b77c0e320f32fbdf88b178146d4b706a01af/images/nats-sub-logs.JPG -------------------------------------------------------------------------------- /images/nats-sub-pod.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/abhirockzz/nats-on-kubernetes-example/3c02b77c0e320f32fbdf88b178146d4b706a01af/images/nats-sub-pod.JPG -------------------------------------------------------------------------------- /java_publisher/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM anapsix/alpine-java 2 | RUN mkdir /app 3 | WORKDIR /app 4 | COPY target/natsio-java-publisher-jar-with-dependencies.jar /app 5 | CMD ["java","-jar", "natsio-java-publisher-jar-with-dependencies.jar"] -------------------------------------------------------------------------------- /java_publisher/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | com.wordpress.simplydistributed 5 | java_publisher 6 | 1.0 7 | jar 8 | 9 | UTF-8 10 | 1.8 11 | 1.8 12 | 13 | 14 | 15 | io.nats 16 | jnats 17 | 1.0 18 | 19 | 20 | 21 | 22 | natsio-java-publisher 23 | 24 | 25 | org.apache.maven.plugins 26 | maven-assembly-plugin 27 | 3.1.0 28 | 29 | 30 | jar-with-dependencies 31 | 32 | 33 | 34 | com.wordpress.simplydistributed.nats.producer.NATSProducer 35 | 36 | 37 | 38 | 39 | 40 | 41 | make-assembly 42 | package 43 | 44 | single 45 | 46 | 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /java_publisher/src/main/java/com/wordpress/simplydistributed/nats/producer/NATSProducer.java: -------------------------------------------------------------------------------- 1 | package com.wordpress.simplydistributed.nats.producer; 2 | 3 | import io.nats.client.Connection; 4 | import io.nats.client.Nats; 5 | import io.nats.client.Options; 6 | import java.util.Date; 7 | import java.util.concurrent.TimeUnit; 8 | 9 | public class NATSProducer { 10 | 11 | final static String SUBJECT = "foo"; 12 | 13 | public static void main(String[] args) throws Exception { 14 | Options.Builder builder = new Options.Builder(); 15 | Options options = builder.timeout(3, TimeUnit.SECONDS) 16 | .reconnectWait(5, TimeUnit.SECONDS) 17 | .maxReconnect(5) 18 | .build(); 19 | 20 | String natsServer = System.getenv("EXAMPLE_NATS_1_SERVICE_HOST"); 21 | //if(natsServer == null){natsServer = "example-nats-1";} 22 | //String natsServer = "example-nats-1"; 23 | System.out.println("NATS server running on --- "+ natsServer); 24 | 25 | String natsURL = "nats://"+natsServer+":4222"; 26 | System.out.println("NATS conn URL --->> "+ natsURL); 27 | 28 | //Connection nc = Nats.connect("nats://192.168.99.100:4222", options); 29 | Connection nc = Nats.connect(natsURL, options); 30 | 31 | System.out.println("Connected"); 32 | while(true){ 33 | String msg = "Hello World "+ new Date(); 34 | nc.publish(SUBJECT, msg.getBytes()); 35 | System.out.println("Sent message - '" + msg + "'"); 36 | Thread.sleep(5000); 37 | } 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /nats-pub-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 2 | kind: Deployment 3 | metadata: 4 | name: nats-pub 5 | spec: 6 | selector: 7 | matchLabels: 8 | app: nats-pub 9 | replicas: 1 10 | template: 11 | metadata: 12 | labels: 13 | app: nats-pub 14 | spec: 15 | containers: 16 | - name: nats-pub 17 | image: abhirockzz/nats-pub 18 | -------------------------------------------------------------------------------- /nats-sub-deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 2 | kind: Deployment 3 | metadata: 4 | name: nats-sub 5 | spec: 6 | selector: 7 | matchLabels: 8 | app: nats-sub 9 | replicas: 1 10 | template: 11 | metadata: 12 | labels: 13 | app: nats-sub 14 | spec: 15 | containers: 16 | - name: nats-sub 17 | image: abhirockzz/nats-sub 18 | --------------------------------------------------------------------------------