Sunday, April 23, 2017

Docker Volumes - How to keep data persistent

Here we are going to see how we can tell a docker image to use a host directory to store that data it may generate.

The reason for this is that images once they are stopped the data it creates is lost, and we really do not want this to happen; specially if we are dealing with a data repository such as cassandra.

Lets consider we a have a cassandra image running like was explained in my previous post Set up Cassandra Cluster with Docker

First of all we are going to stop and remove the image (if was already created).

Lets create a directory in the host system wherever you prefer.

mkdir ~/cassandradata

And now lets start Cassandra image making it to keep its data in the directory just created

docker run -d --name cassandra1 --net=host -v ~/cassandradata:/var/lib/cassandra

We have telled our image to store its data in our directory.

Lets go inside Cassandra image and create a simpole table to see how data is keept.


docker exec -ti cassandra1 cqlsh localhost

Now we create a keyspace and a table.

CREATE KEYSPACE mykeyspace WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };

CREATE TABLE mytable (id int PRIMARY KEY, name text, surname text);

INSERT INTO mytable (id,name,surname) values (1,'rafa','reyes');

And lets exit from Cassandra. If we check our directory we can see now something inside it.







We stop our Cassandra image

docker cassandra1 stop

And start it again

docker start cassandra1

If go to our keyspace again and select the data we can see it is still there










So like this we can keep our data safe even if our image stops. Also useful in case you need to move your image to another host system.

Thursday, April 6, 2017

Docker installation and set up in Ubuntu server

Here we are going to see easily how Docker can be installed on a Ubuntu based server.

First of all we will run two well known commands for Linux users:

apt-get update

apt-get upgrade

Once this is done we can continue and install Docker

apt-get install -y docker.io


once installed we can start Docker


systemctl start docker

And then probably you want it to start on system boot

systemctl enable docker

After these you can check docker status by

docker ps



Now you have Docker ready to start. In other entries will be seen the usage and deployment of different kind of images.

References:

https://www.howtoforge.com/tutorial/docker-installation-and-usage-on-ubuntu-16.04/

Set up Cassandra Cluster with Docker


Here I'm going to set up a Cassandra cluster over several virtual machines tunning Ubuntu operating system, furthermore they will be running under Docker containers.


Not going to discuss about the advantages or when is suitable to use Cassandra as there are many resources over the Web where this can be found. Just to point one of the most interesting ones from my point of view, which is the possibility to add new nodes easily to the cluster in case you are running out of space.

First step is to have at least 2 virtual machines running Ubuntu and docker as explained in my entry another entry Docker install


Lets say you have these IP addresses 192.168.1.74 (Node A) and 192.186.1.75 (Node B)

On Node A type

docker pull cassandra

This will get the latest image version of Cassandra to your Docker.

Then to run it just

docker run -d --name=cassandra1 -e CASSANDRA_BROADCAST_ADDRESS=192.168.1.74 -p 9042:9042 -p 7000-7001:7000-7001 -p 7199:7199 -p 9160:9160 cassandra

Main things there are the broadcast address (so other nodes can see this one) and expose the ports to be able to communicate. Rest ot options are the usual ones to run an image in Docker.

Now we have our first node running.




Lets move to second virtual machine

Once we pull Cassandra we run there

docker run -d --name=cassandra2 -p 9042:9042 -p 7000-7001:7000-7001 -p 7199:7199 -p 9160:9160 -e CASSANDRA_BROADCAST_ADDRESS="192.168.1.75" -e CASSANDRA_SEEDS="192.168.1.74" cassandra

We will have our second instace of Cassandra running




To check that our cluster in correct we can run in any of the nodes this


docker exec cassandra1 nodetool status




And here we have our cluster ready!

In case you want to add a new node to the cluster is a simple as we have dont with the second one.


Obstacles:

One important thing if you are running instances in separated virtual machines is that to make sure you state the bradcast and the seeds IP addresses, in some documentation you can see people just use
the option --net=host so instance just take the host network configuration

Also make sure you expose the ports



References:

https://hub.docker.com/_/cassandra/

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-data-cassandra

https://yurisubach.com/2016/03/24/cassandra-docker-test-cluster/

http://blog.ditullio.fr/2016/06/10/docker-docker-basics-cassandra/