Coder Social home page Coder Social logo

distributed_cowboys's Introduction

Distributed Cowboys

Environment

Minikube on Mac has been used for simulating a distributed environment. Docker containers represent cowboys. Shooting activity is abstracted by using a messaging platform. Each shooter subscribes and publishes to a topic on the messaging server. Orchestration: https://minikube.sigs.k8s.io/docs/ Messaging: https://nats.io/

Note: Please consult the document supplied to you to understand the requirements of the task at hand.

Implementation details

Shots fired among cowboys are represented as messages published on a topic each participating cowboy is listening to. If a message contains a cowboy's name and the message type is 'SHOT' then the subscribing cowboy must consume that message and update his health information by decrementing it by the damage amount. Immediately after that the shot cowboy should broadcast his updated health to rest of the shooters. Message type for such a message is 'HEALTH'. These messages are published on the same topic. Since the shoutout has to start at the same time in parallel, message type 'START' is sent only once at the beginning to commence the shooting. By the end of the shootout there may be a single winner.

Setup

For setting up minikube, please follow the instructions here https://minikube.sigs.k8s.io/docs/start/ . In my case I have used the docker driver for minikube, it's the default.

Start minikube:

minikube start

Enable registry addon for pushing docker images locally

minikube addons registry enable

Open a terminal and run the following command. This allows us to push docker images locally from host to the minikube's registry.

sudo docker run --rm -it --network=host alpine ash -c "apk add socat && socat TCP-LISTEN:5000,reuseaddr,fork TCP:$(minikube ip):5000

Open another terminal. Install nats on minikube.

kubectl apply -f https://raw.githubusercontent.com/nats-io/k8s/master/nats-server/single-server-nats.yml

After a successful installation of nats, check the service and the pods using:

kubectl get svc nats
kubectl get pods nats-0

Clone the repository :

 git clone https://github.com/smashra/distributed_cowboys.git

Setup the shooters:

cd distributed_cowboys
./bin/setup

Once the setup completes successfully, make sure all the pods have a status of Running:

kubectl get po 

Clone kubetail for tailing logs from all the pods at the same time:

git clone https://github.com/johanhaleby/kubetail.git

Open two terminals, in the first one: Note: Path to kubetail may vary depending on your install and where it's called from, please check.

../kubetail/kubetail bill,john,sam,philip,peter

in the second terminal:

kubectl exec -it bs -- go run bs.go

Assuming everything went fine, we should see output like below in the first terminal: Note: Actual output is coloured with different colours for different pod logs.

Using regex '.*bill.*|.*john.*|.*sam.*|.*philip.*|.*peter.*' to match pods
Will tail 5 logs...
bill
john
peter
philip
sam
[philip] Philip starts shooting ....
[john] John starts shooting ....
[peter] Peter starts shooting ....
[sam] Sam starts shooting ....
[bill] Bill starts shooting ....
[peter] Peter shoots John ...
[sam] Sam shoots Bill ...
[john] John got shot, health [9]
[bill] Bill shoots Peter ...
[bill] Bill got shot, health [6]
[sam] Sam got shot, health [9]
[john] John shoots Sam ...
[philip] Philip shoots Peter ...
[peter] Peter got shot, health [2]
[peter] Peter got shot, health [-1]
[peter] Peter is dead.
[sam] Sam shoots Philip ...
[philip] Philip got shot, health [14]
[john] John shoots Philip ...
[philip] Philip got shot, health [13]
[sam] Sam shoots Philip ...
[philip] Philip got shot, health [12]
[bill] Bill shoots John ...
[john] John got shot, health [8]

distributed_cowboys's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.