Documente Academic
Documente Profesional
Documente Cultură
History of Virtualization
What is Containerization?
What is Docker?
Docker Architecture
Docker Engine
Docker Images
Registries
Docker Containers
Installing Docker on Linux
Basic Docker commands
History of Virtualization
Earlier, the process for deploying a service was slow and painful. First, the developers were writing code;
then the operations team would deploy it on bare metal machines, where they had to look out for
library versions, patches, and language compilers for the code to work. If there were some bugs or
errors, the process would start all over again, the developers would fix it, and then again, the
operational team was there to deploy.
There was an improvement with the creation of Hypervisors. Hypervisors have multiple Virtual machines
or VMs on the same host, which may be running or turned off. VMs decreased the waiting time for
deploying code and bug fixing in a big manner, but the real game changer was Docker containers.
What is Virtualization?
Virtualization is the technique of importing a Guest operating system on top of a Host operating system.
This technique was a revelation at the beginning because it allowed developers to run multiple
operating systems in different virtual machines all running on the same host. This eliminated the need
for extra hardware resource.
In the diagram on the right, you can see there is a host operating system on which there are 3 guest
operating systems running which is nothing but the virtual machines.
As you know nothing is perfect, Virtualization also has some shortcomings. Running multiple Virtual
Machines in the same host operating system leads to performance degradation. This is because of the
guest OS running on top of the host OS, which will have its own kernel and set of libraries and
dependencies. This takes up a large chunk of system resources, i.e. hard disk, processor and especially
RAM.
Another problem with Virtual Machines which uses virtualization is that it takes almost a minute to
boot-up. This is very critical in case of real-time applications.
What is Containerization?
Containerization is the technique of bringing virtualization to the operating system level. While
Virtualization brings abstraction to the hardware, Containerization brings abstraction to the operating
system. Do note that Containerization is also a type of Virtualization. Containerization is however more
efficient because there is no guest OS here and utilizes a host’s operating system, share relevant
libraries & resources as and when needed unlike virtual machines. Application specific binaries and
libraries of containers run on the host kernel, which makes processing and execution very fast. Even
booting-up a container takes only a fraction of a second. Because all the containers share, host
operating system and holds only the application related binaries & libraries. They are lightweight and
faster than Virtual Machines.
All these containers are handled by the containerization layer which is not native to the host operating
system. Hence a software is needed, which can enable you to create & run containers on your host
operating system.
What is Docker?
Docker is a containerization platform that packages your application and all its dependencies together in
the form of Containers to ensure that your application works seamlessly in any environment.
As you can see in the diagram, each application will run on a separate container and will have its own
set of libraries and dependencies. This also ensures that there is process level isolation, meaning each
application is independent of other applications, giving developers surety that they can build
applications that will not interfere with one another.
As a developer, I can build a container which has different applications installed on it and give it to my
QA team who will only need to run the container to replicate the developer environment.
Benefits of Docker
Now, the QA team need not install all the dependent software and applications to test the code and this
helps them save lots of time and energy. This also ensures that the working environment is consistent
across all the individuals involved in the process, starting from development to deployment. The number
of systems can be scaled up easily and the code can be deployed on them effortlessly.
Virtualization vs Containerization
Virtualization and Containerization both let you run multiple operating systems inside a host machine.
Virtualization deals with creating many operating systems in a single host machine. Containerization on
the other hand will create multiple containers for every type of application as required.
Figure: What is Big Data Analytics – Virtualization versus Containerization
As we can see from the image, the major difference is that there are multiple Guest Operating Systems
in Virtualization which are absent in Containerization. The best part of Containerization is that it is very
lightweight as compared to the heavy virtualization.
Docker Architecture
Let's talk about Docker main components in the Docker Architecture
Docker Engine
Docker is the client-server type of application which means we have clients who relay to the server. So,
the Docker daemon called: dockerd is the Docker engine which represents the server. The docker
daemon and the clients can be run on the same or remote host, and they communicate through
command line client binary, as well as a full RESTful API to interact with the daemon: dockerd.
Docker Images
Docker images are the "source code" for our containers; we use them to build containers. They can have
software pre-installed which speeds up deployment. They are portable, and we can use existing images
or build our own.
Registries
Docker stores the images we build in registries. There are public and private registries. Docker company
has public registry called Docker hub, where you can also store images privately. Docker hub has millions
of images, which you can start using now.
Docker Containers
Containers are the organizational units of Docker. When we build an image and start running it; we are
running in a container. The container analogy is used because of the portability of the software we have
running in our container. We can move it, in other words, "ship" the software, modify, manage, create
or get rid of it, destroy it, just as cargo ships can do with real containers.
In simple terms, an image is a template, and a container is a copy of that template. You can have
multiple containers (copies) of the same image.
Below we have an image which perfectly represents the interaction between the different components
and how Docker container technology works.
There are certain packages you require in your system for installing Docker. Execute the below
command to install those packages.
Now, import Dockers official GPG key to verify packages signature before installing them with apt-get.
Run the below command on terminal:
Now, add the Docker repository on your Ubuntu system which contains Docker packages including its
dependencies, for that execute the below command:
$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
You may be prompted to confirm that you wish to add the repository and have the repository's GPG key
automatically added to your host.
The lsb_release command should populate the Ubuntu distribution version of your host.
We can now install the Docker package itself and install Docker community edition, for that execute the
below commands:
The above-given command installs Docker and other additional required packages. Before Docker 1.8.0,
the package name was lxc-docker, and between Docker 1.8 and 1.13, the package name was docker-
engine.
NOTE: Docker for Windows requires Windows 10 Pro or Enterprise version 14393, or Windows server
2016 RTM to run
The most basic command we must run after installing Docker is $ docker info as we said previously.
$ sudo docker info
As we can see we have information about docker containers how many are running, paused or stopped
and how many images we have downloaded. So, let's get our first image.
With this command we are telling docker to download the image alpine, to pull it from the public
registry, the latest version which is set by default.
*alpine is a minimal Docker image based on Alpine Linux with a complete package index and only 5 MB
in size.
If we want to run the image as a container, we will use the following command.
You can notice docker checks for the image locally, and if it's not there, the image is pulled from the
image library automatically, and once again we have an interactive shell running. We can also name the
containers as we run them.
$ sudo docker ps
And for all containers we add "- a"at the end of this same command, like this sudo docker ps -a
This command shows Container's ID, which image is using when was created, running status, exposed
ports and randomly generated name for the container for easier management.
When we run containers, we would also like to know how much resources they are using, for that
purpose we can use the command.
Summary
Earlier, the process for deploying a service was slow and painful but, VMs decreased the waiting time for
deploying code and bug fixing in a big manner
Docker is computer software used for Virtualization in order to have multiple Operating systems running
on the same host
Docker is the client-server type of application which means we have clients who relay to the server
Docker images are the "source code" for our containers; we use them to build
Docker has two types of registries 1.) public and 2) private registries
Containers are the organizational units of Docker. In simple terms, an image is a template, and a
container is a copy of that template. You can have multiple containers (copies) of the same image.
Command Description
Overview
A typical distributed system consists of many services collaborating together.
These services are prone to failure or delayed responses. If a service fails it may impact on other services
affecting performance and possibly making other parts of application inaccessible or in the worst case
bring down the whole application.
Of course, there are solutions available that help make applications resilient and fault tolerant – one
such framework is Hystrix.
The Hystrix framework library helps to control the interaction between services by providing fault
tolerance and latency tolerance. It improves overall resilience of the system by isolating the failing
services and stopping the cascading effect of failures.
Simple Example
The way Hystrix provides fault and latency tolerance is to isolate and wrap calls to remote services.
In this simple example we wrap a call in the run() method of the HystrixCommand:
CommandHelloWorld(String name) {
super(HystrixCommandGroupKey.Factory.asKey("ExampleGroup"));
this.name = name;
}
@Override
protected String run() {
return "Hello " + name + "!";
}
}
and we execute the call as follows:
@Test
public void givenInputBobAndDefaultSettings_whenCommandExecuted_thenReturnHelloBob(){
assertThat(new CommandHelloWorld("Bob").execute(), equalTo("Hello Bob!"));
}
Maven Setup
To use Hystrix in a Maven projects, we need to have hystrix-core and rxjava-core dependency from
Netflix in the project pom.xml:
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>com.netflix.rxjava</groupId>
<artifactId>rxjava-core</artifactId>
<version>0.20.7</version>
</dependency>
class RemoteServiceTestSimulator {
The call to the service is isolated and wrapped in the run() method of a HystrixCommand. Its this
wrapping that provides the resilience we touched upon above:
@Override
protected String run() throws Exception {
return remoteService.execute();
}
}
The call is executed by calling the execute() method on an instance of the RemoteServiceTestCommand
object.
@Test
public void givenSvcTimeoutOf100AndDefaultSettings_whenRemoteSvcExecuted_thenReturnSuccess()
throws InterruptedException {
@Test
public void
givenSvcTimeoutOf5000AndExecTimeoutOf10000_whenRemoteSvcExecuted_thenReturnSuccess()
throws InterruptedException {
Now let’s see what happens when the execution timeout is less than the service timeout call:
@Test(expected = HystrixRuntimeException.class)
public void
givenSvcTimeoutOf15000AndExecTimeoutOf5000_whenRemoteSvcExecuted_thenExpectHre()
throws InterruptedException {
We are expecting the service to respond within 5,000 ms, whereas we have set the service to respond
after 15,000 ms. If you notice when you execute the test, the test will exit after 5,000 ms instead of
waiting for 15,000 ms and will throw a HystrixRuntimeException.
This demonstrates how Hystrix does not wait longer than the configured timeout for a response. This
helps make the system protected by Hystrix more responsive.
In the below sections we will look into setting thread pool size which prevents threads being exhausted
and we will discuss its benefit.
We don’t want this to happen as we need these threads for other remote calls or processes running on
our server and we also want to avoid CPU utilization spiking up.
@Test
public void givenSvcTimeoutOf500AndExecTimeoutOf10000AndThreadPool_whenRemoteSvcExecuted
_thenReturnSuccess() throws InterruptedException {
We don’t want to keep firing off requests at it and waste resources. We would ideally want to stop
making requests for a certain amount of time in order to give the service time to recover before then
resuming requests. This is what is called the Short Circuit Breaker pattern.
@Test
public void givenCircuitBreakerSetup_whenRemoteSvcCmdExecuted_thenReturnSuccess()
throws InterruptedException {
config.andCommandPropertiesDefaults(properties);
config.andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter()
.withMaxQueueSize(1)
.withCoreSize(1)
.withQueueSizeRejectionThreshold(1));
Thread.sleep(5000);
try {
response = new RemoteServiceTestCommand(config,
new RemoteServiceTestSimulator(timeout)).execute();
} catch (HystrixRuntimeException ex) {
System.out.println("ex = " + ex);
}
return response;
}
In the above test we have set different circuit breaker properties. The most important ones are:
The CircuitBreakerSleepWindow which is set to 4,000 ms. This configures the circuit breaker
window and defines the time interval after which the request to the remote service will be
resumed
The CircuitBreakerRequestVolumeThreshold which is set to 1 and defines the minimum
number of requests needed before the failure rate will be considered.
With the above settings in place, our HystrixCommand will now trip open after two failed request. The
third request will not even hit the remote service even though we have set the service delay to be 500
ms, Hystrix will short circuit and our method will return null as the response.
We will subsequently add a Thread.sleep(5000) in order to cross the limit of the sleep window that we
have set. This will cause Hystrix to close the circuit and the subsequent requests will flow through
successfully.
Conclusion
In summary Hystrix is designed to
Provide protection and control over failures and latency from services typically accessed over
the network
Stop cascading of failures resulting from some of the services being down
Fail fast and rapidly recover
Degrade gracefully where possible
Real time monitoring and alerting of command center on failures
References
https://www.baeldung.com/introduction-to-hystrix
Zookeeper
What is Zookeeper?
Apache Zookeeper is an open source distributed coordination service that helps you manage a large set
of hosts. Management and coordination in a distributed environment are tricky. Zookeeper automates
this process and allows developers to focus on building software features rather worry about the
distributed nature of their application.
Zookeeper helps you to maintain configuration information, naming, group services for distributed
applications. It implements different protocols on the cluster so that the application should not
implement on their own. It provides a single coherent view of multiple machines.
ZooKeeper is a distributed co-ordination service to manage large set of hosts. Co-ordinating and
managing a service in a distributed environment is a complicated process. ZooKeeper solves this issue
with its simple architecture and API. ZooKeeper allows developers to focus on core application logic
without worrying about the distributed nature of the application.
The ZooKeeper framework was originally built at “Yahoo!” for accessing their applications in an easy and
robust manner. Later, Apache ZooKeeper became a standard for organized service used by Hadoop,
HBase, and other distributed frameworks. For example, Apache HBase uses ZooKeeper to track the
status of distributed data.
Server: The server sends an acknowledge when any client connects. In the case when there is no
response from the connected server, the client automatically redirects the message to another server.
Client: Client is one of the nodes in the distributed application cluster. It helps you to accesses
information from the server. Every client sends a message to the server at regular intervals that helps
the server to know that the client is alive.
Leader: One of the servers is designated a Leader. It gives all the information to the clients as well as an
acknowledgment that the server is alive. It would perform automatic recovery if any of the connected
nodes failed.
Client read requests are handled by the correspondingly connected Zookeeper server
The client writes requests are handled by the Zookeeper leader.
Ensemble/Cluster: Group of Zookeeper servers which is called ensemble or a Cluster. You can use
ZooKeeper infrastructure in the cluster mode to have the system at the optimal value when you are
running the Apache.
ZooKeeper WebUI: If you want to work with ZooKeeper resource management, then you need to use
WebUI. It allows working with ZooKeeper using the web user interface, instead of using the command
line. It offers fast and effective communication with the ZooKeeper application.
The Zookeeper Data Model (ZDM)
ZNode paths:
Canonical, slash-separated and
absolute
Not use any relative references
Names may have Unicode
characters
ZDM- Watches
Zookeeper, a watch event is a one-time trigger which is sent to the client that set watch. It occurred
when data from that watch changes. ZDM watch allows clients to get notifications when znode changes.
ZDM read operations like getData(), getChidleren(), exist have the option of setting a watch.
Watches are ordered, the order of watch events corresponds to the order of the updates. A client will
able to see a watch event for znode before seeing the new data which corresponds to that znode.
ACL Permissions:
CREATE
READ
WRITE
DELETE
ADMIN
E.x. (IP: 192.168.0.0/16, READ)
Before executing any request, it is important that the client must establish a session with service
All operations clients are sent to service are automatically associated with a session
The client may connect to any server in the cluster. But it will connect to only a single server
The session provides "order guarantees". The requests in the session are executed in FIFO order
The main states for a session are 1) Connecting, 2) Connected 3) Closed 4) Not Connected.
How to install ZooKeeper
Step 1) Go to this link and click "Continue to Subscribe"
Step 2) On next page, Click Accept Terms
Step 4) Refresh the page after 5 minutes and click "Continue to Configure"
Step 5) In next screen, click "Continue to Launch"
Redis
A database is a crucial aspect of applications that are often only considered as an afterthought.
However, for many developers deciding which database to use when building apps is a critical decision.
Among the many popular data structures such as MySQL, MongoDB and Oracle, Redis is slowly gaining
popularity within the NoSQL databases. Although, it already plays a supporting role for many companies
including Twitter and Github, Redis is now gaining traction as a primary database.
Redis is an open-source data structure server that allows developers to organize data using a key-value
storage method. This powerful database is perfect for high performance jobs such as caching. Redis is a
no-fuss and fast database for many different functions including as a cache or a message broker.
Redis Advantages
Following are certain advantages of Redis.
Exceptionally fast − Redis is very fast and can perform about 110000 SETs per second, about
81000 GETs per second.
Supports rich data types − Redis natively supports most of the datatypes that developers
already know such as list, set, sorted set, and hashes. This makes it easy to solve a variety of
problems as we know which problem can be handled better by which data type.
Operations are atomic − All Redis operations are atomic, which ensures that if two clients
concurrently access, Redis server will receive the updated value.
Multi-utility tool − Redis is a multi-utility tool and can be used in several use cases such as
caching, messaging-queues (Redis natively supports Publish/Subscribe), any short-lived data in
your application, such as web application sessions, web page hit counts, etc.
Start Redis
$redis-server
Check If Redis is Working
$redis-cli
This will open a redis prompt.
redis 127.0.0.1:6379>
In the above prompt, 127.0.0.1 is your machine's IP address and 6379 is the port on which Redis server
is running. Now type the following PING command.
Syntax
$ redis-cli -h host -p port -a password
Example
Following example shows how to connect to Redis remote server, running on host 127.0.0.1, port 6379
and has password mypass.
Syntax
Following is the basic syntax of Redis CONFIG command.
redis 127.0.0.1:6379> CONFIG GET CONFIG_SETTING_NAME
Example
redis 127.0.0.1:6379> CONFIG GET loglevel
1) "loglevel"
2) "notice"
Edit Configuration
To update configuration, you can edit redis.conf file directly or you can update configurations via
CONFIG set command.
Syntax
Following is the basic syntax of CONFIG SET command.
redis 127.0.0.1:6379> CONFIG SET CONFIG_SETTING_NAME NEW_CONFIG_VALUE
Example
redis 127.0.0.1:6379> CONFIG SET loglevel "notice"
OK
redis 127.0.0.1:6379> CONFIG GET loglevel
1) "loglevel"
2) "notice"
Strings
Redis string is a sequence of bytes. Strings in Redis are binary safe, meaning they have a known length
not determined by any special terminating characters. Thus, you can store anything up to 512
megabytes in one string.
Example
redis 127.0.0.1:6379> SET name "tutorialspoint"
OK
redis 127.0.0.1:6379> GET name
"tutorialspoint"
In the above example, SET and GET are Redis commands, name is the key used in Redis and
tutorialspoint is the string value that is stored in Redis.
Note − A string value can be at max 512 megabytes in length.
Hashes
A Redis hash is a collection of key value pairs. Redis Hashes are maps between string fields and string
values. Hence, they are used to represent objects.
Example
redis 127.0.0.1:6379> HMSET user:1 username tutorialspoint password
tutorialspoint points 200
OK
redis 127.0.0.1:6379> HGETALL user:1
1) "username"
2) "tutorialspoint"
3) "password"
4) "tutorialspoint"
5) "points"
6) "200"
In the above example, hash data type is used to store the user's object which contains basic information
of the user. Here HMSET, HGETALL are commands for Redis, while user − 1 is the key.
Every hash can store up to 232 - 1 field-value pairs (more than 4 billion).
Lists
Redis Lists are simply lists of strings, sorted by insertion order. You can add elements to a Redis List on
the head or on the tail.
Example
redis 127.0.0.1:6379> lpush tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> lpush tutoriallist mongodb
(integer) 2
redis 127.0.0.1:6379> lpush tutoriallist rabitmq
(integer) 3
redis 127.0.0.1:6379> lrange tutoriallist 0 10
1) "rabitmq"
2) "mongodb"
3) "redis"
The max length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per list).
Sets
Redis Sets are an unordered collection of strings. In Redis, you can add, remove, and test for the
existence of members in O(1) time complexity.
Example
redis 127.0.0.1:6379> sadd tutoriallist redis
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist mongodb
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 1
redis 127.0.0.1:6379> sadd tutoriallist rabitmq
(integer) 0
redis 127.0.0.1:6379> smembers tutoriallist
1) "rabitmq"
2) "mongodb"
3) "redis"
Note − In the above example, rabitmq is added twice, however due to unique property of the set, it is
added only once.
The max number of members in a set is 232 - 1 (4294967295, more than 4 billion of members per set).
Sorted Sets
Redis Sorted Sets are similar to Redis Sets, non-repeating collections of Strings. The difference is, every
member of a Sorted Set is associated with a score, that is used in order to take the sorted set ordered,
from the smallest to the greatest score. While members are unique, the scores may be repeated.
Example
redis 127.0.0.1:6379> zadd tutoriallist 0 redis
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 mongodb
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 1
redis 127.0.0.1:6379> zadd tutoriallist 0 rabitmq
(integer) 0
redis 127.0.0.1:6379> ZRANGEBYSCORE tutoriallist 0 1000
1) "redis"
2) "mongodb"
3) "rabitmq"
Redis - Keys
Redis keys commands are used for managing keys in Redis. Following is the syntax for using redis keys
commands.
Syntax
redis 127.0.0.1:6379> COMMAND KEY_NAME
Example
redis 127.0.0.1:6379> SET tutorialspoint redis
OK
redis 127.0.0.1:6379> DEL tutorialspoint
(integer) 1
In the above example, DEL is the command, while tutorialspoint is the key. If the key is deleted, then the
output of the command will be (integer) 1, otherwise it will be (integer) 0.
1 DEL key
This command deletes the key, if it exists.
2 DUMP key
This command returns a serialized version of the value stored at the specified key.
3 EXISTS key
This command checks whether the key exists or not.
8 KEYS pattern
Finds all keys matching the specified pattern.
9 MOVE key db
Moves a key to another database.
10 PERSIST key
Removes the expiration from the key.
11 PTTL key
Gets the remaining time in keys expiry in milliseconds.
12 TTL key
Gets the remaining time in keys expiry.
13 RANDOMKEY
Returns a random key from Redis.
16 TYPE key
Returns the data type of the value stored in the key.
Redis - Strings
Redis strings commands are used for managing string values in Redis. Following is the syntax for using
Redis string commands.
Syntax
redis 127.0.0.1:6379> COMMAND KEY_NAME
Example
redis 127.0.0.1:6379> SET tutorialspoint redis
OK
redis 127.0.0.1:6379> GET tutorialspoint
"redis"
In the above example, SET and GET are the commands, while tutorialspoint is the key.
2 GET key
Gets the value of a key.
11 STRLEN key
Gets the length of the value stored in a key
15 INCR key
Increments the integer value of a key by one
18 DECR key
Decrements the integer value of a key by one
Redis - Hashes
Redis Hashes are maps between the string fields and the string values. Hence, they are the perfect data
type to represent objects.
In Redis, every hash can store up to more than 4 billion field-value pairs.
Example
redis 127.0.0.1:6379> HMSET tutorialspoint name "redis tutorial"
description "redis basic commands for caching" likes 20 visitors 23000
OK
redis 127.0.0.1:6379> HGETALL tutorialspoint
1) "name"
2) "redis tutorial"
3) "description"
4) "redis basic commands for caching"
5) "likes"
6) "20"
7) "visitors"
8) "23000"
In the above example, we have set Redis tutorials detail (name, description, likes, visitors) in hash
named ‘tutorialspoint’.
4 HGETALL key
Gets all the fields and values stored in a hash at the specified key
7 HKEYS key
Gets all the fields in a hash
8 HLEN key
Gets the number of fields in a hash
13 HVALS key
Gets all the values in a hash
Redis - Lists
Redis Lists are simply lists of strings, sorted by insertion order. You can add elements in Redis lists in the
head or the tail of the list.
Maximum length of a list is 232 - 1 elements (4294967295, more than 4 billion of elements per list).
Example
redis 127.0.0.1:6379> LPUSH tutorials redis
(integer) 1
redis 127.0.0.1:6379> LPUSH tutorials mongodb
(integer) 2
redis 127.0.0.1:6379> LPUSH tutorials mysql
(integer) 3
redis 127.0.0.1:6379> LRANGE tutorials 0 10
1) "mysql"
2) "mongodb"
3) "redis"
In the above example, three values are inserted in Redis list named ‘tutorials’ by the command LPUSH.
6 LLEN key
Gets the length of a list
7 LPOP key
Removes and gets the first element in a list
14 RPOP key
Removes and gets the last element in a list
Example
redis 127.0.0.1:6379> SADD tutorials redis
(integer) 1
redis 127.0.0.1:6379> SADD tutorials mongodb
(integer) 1
redis 127.0.0.1:6379> SADD tutorials mysql
(integer) 1
redis 127.0.0.1:6379> SADD tutorials mysql
(integer) 0
redis 127.0.0.1:6379> SMEMBERS tutorials
1) "mysql"
2) "mongodb"
3) "redis"
In the above example, three values are inserted in Redis set named ‘tutorials’ by the command SADD.
2 SCARD key
Gets the number of members in a set
8 SMEMBERS key
Gets all the members in a set
10 SPOP key
Removes and returns a random member from a set
Example
redis 127.0.0.1:6379> ZADD tutorials 1 redis
(integer) 1
redis 127.0.0.1:6379> ZADD tutorials 2 mongodb
(integer) 1
redis 127.0.0.1:6379> ZADD tutorials 3 mysql
(integer) 1
redis 127.0.0.1:6379> ZADD tutorials 3 mysql
(integer) 0
redis 127.0.0.1:6379> ZADD tutorials 4 mysql
(integer) 0
redis 127.0.0.1:6379> ZRANGE tutorials 0 10 WITHSCORES
1) "redis"
2) "1"
3) "mongodb"
4) "2"
5) "mysql"
6) "4"
In the above example, three values are inserted with its score in Redis sorted set named ‘tutorials’ by
the command ZADD.
2 ZCARD key
Gets the number of members in a sorted set
Redis - HyperLogLog
Redis HyperLogLog is an algorithm that uses randomization in order to provide an approximation of the
number of unique elements in a set using just a constant, and small amount of memory.
HyperLogLog provides a very good approximation of the cardinality of a set even using a very small
amount of memory around 12 kbytes per key with a standard error of 0.81%. There is no limit to the
number of items you can count, unless you approach 264 items.
Example
Following example explains how Redis HyperLogLog works.
redis 127.0.0.1:6379> PFADD tutorials "redis"
1) (integer) 1
redis 127.0.0.1:6379> PFADD tutorials "mongodb"
1) (integer) 1
redis 127.0.0.1:6379> PFADD tutorials "mysql"
1) (integer) 1
redis 127.0.0.1:6379> PFCOUNT tutorials
(integer) 3
Redis HyperLogLog Commands
Following table lists some basic commands related to Redis HyperLogLog.
Example
Following example explains how publish subscriber concept works. In the following example, one client
subscribes a channel named ‘redisChat’.
Docker
1. What is Docker?
Docker is an open-source lightweight containerization technology. It has gained widespread popularity
in the cloud and application packaging world. It allows you to automate the deployment of applications
in lightweight and portable containers.
7. Explain Registries
Public Registry
Private Registry
Docker's public registry is called Docker hub, which allows you to store images privately. In Docker hub,
you can store millions of images.
8. What command should you run to see all running container in Docker?
$ docker ps
9. Write the command to stop the docker container
The common instruction in Dockerfile are: FROM, LABEL, RUN, and CMD.
Memory-swap is a modified flag that only has meaning if- memory is also set. Swap allows the container
to write express memory requirements to disk when the container has exhausted all the RAM which is
available to it.
Docker Swarm is native gathering for docker which helps you to a group of Docker hosts into a single
and virtual docker host. It offers the standard docker application program interface.
Docker states and Docker Events are used to monitoring docker in the production environment.
Running
Paused
Restarting
Exited
16. What is Docker hub?
Docker hub is a cloud-based registry that which helps you to link to code repositories. It allows you to
build, test, store your image in Docker cloud. You can also deploy the image to your host with the help
of Docker hub.
However, this scenario changed when companies and open source communities were able to offer a
method of handling privileged instructions. It allows multiple OS to run simultaneously on a single x86
based system.
Docker object labels is a method for applying metadata to docker objects including, images, containers,
volumes, network, swam nodes, and services.
20. Write a Docker file to create and copy a directory and built it using python modules?
FROM pyhton:2.7-slim
WORKDIR /app
COPY . /app
/var/lib/docker/volumes
22. List out some important advanced docker commands
Command Description
docker info Information Command
docker pull Download an image
docker stats Container information
Docker images List of images downloaded
23. How does communication happen between Docker client and Docker Daemon?
You can communicate between Docker client and Docker Daemon with the combination of Rest API,
socket.IO, and TCP.
24. Explain Implementation method of Continuous Integration(CI) and Continues Development (CD) in
Docker?
In docker file, we need to use COPY or ADD directive. This is useful to relocate code. However, we
should use a volume if we want to make changes.
The Docker containers can be scaled to any level starting from a few hundred to even thousands or
millions of containers. The only condition for this is that the containers need the memory and the OS at
all times, and there should not be a constraint when the Docker is getting scaled.
You can use any of the specific Docker images for creating a Docker container using the below
command.
31. What are the steps for the Docker container life cycle?
Build
Pull
Run
32. How can you run multiple containers using a single service?
By using docker-compose, you can run multiple containers using a single service. All docker-compose
files uses yaml language.
CNM stands for Container Networking Model. It is a standard or specification from Docker, Inc. that
forms the basis of container networking in a Docker environment. This docker's approach provides
container networking with support for multiple network drivers.
Yes, Docker provides support IPv6. IPv6 networking is supported only on Docker daemons runs on Linux
hosts. However, if you want to enable IPv6 support in the Docker daemon, you need to modify
/etc/docker/daemon.json and set the ipv6 key to true.
35. Can you lose data when the container exits?
No, any data that your application writes to disk get stored in container. The file system for the contain
persists even after the container halts.
36. What are a different kind of volume mount types available in Docker?
To configure the Docker daemon to default to a specific logging driver. You need to set the value of log-
driver to the name of the logging drive the daemon.jason.fie.
Docker Trusted Registry is the enterprise-grade image storage toll for Docker. You should install it after
your firewall so that you can securely manage the Docker images you use in your applications.
The Namespace in Docker is a technique which offers isolated workspaces called the Container.
Namespaces also offer a layer of isolation for the Docker containers.
Client
Docker-Host
Registry
41. What is client?
Docker provides Command Line Interface tools to the client to interact with Docker daemon.
It contains container, images, and Docker daemon. It offers a complete environment to execute and run
your application.
43. How do I run multiple copies of Compose file on the same host?
Compose uses the project name which allows you to create unique identifiers for all of a project's
containers and other resources. To run multiple copies of a project, set a custom project name using the
-a command-line option or using COMPOSE_PROJECT_NAME environment variable.
Random Topics
Typically, we use a RESTful design for our web APIs. The concept of REST is to separate the API structure
into logical resources. There are HTTP methods GET, DELETE, POST and PUT to operate with the
resources.
These are 10 best practices to design a clean RESTful API:
2. GET method and query parameters should not alter the state
Use PUT, POST and DELETE methods instead of the GET method to alter the state.
Do not use GET for state changes:
6. Use HATEOAS
Hypermedia as the Engine of Application State is a principle that hypertext links should be used to
create a better navigation through the API.
{
"id": 711,
"manufacturer": "bmw",
"model": "X5",
"seats": 5,
"drivers": [
{
"id": "23",
"name": "Stefan Jauker",
"links": [
{
"rel": "self",
"href": "/api/v1/drivers/23"
}
]
}
]
}
7. Provide filtering, sorting, field selection and paging for collections
Filtering
Use a unique query parameter for all fields or a query language for filtering.
Sorting
Allow ascending and descending sorting over multiple fields.
This returns a list of cars sorted by descending manufacturers and ascending models.
Field selection
Mobile clients display just a few attributes in a list. They don’t need all attributes of a resource. Give the
API consumer the ability to choose returned fields. This will also reduce the network traffic and speed up
the usage of the API.
Paging
Use limit and offset. It is flexible for the user and common in leading databases. The default should be
limit=20 and offset=0
To send the total entries back to the user use the custom HTTP header: X-Total-Count.
Links to the next or previous page should be provided in the HTTP header link as well. It is important to
follow this link header values instead of constructing your own URLs.
We are using the url for the API versioning starting with the letter, v
400 – Bad Request – The request was invalid or cannot be served. The exact error should be explained in
the error payload. E.g. The JSON is not valid.
401 – Unauthorized – The request requires a user authentication
403 – Forbidden – The server understood the request but is refusing it or the access is not allowed.
404 – Not found – There is no resource behind the URI.
422 – Un-Processable Entity – Should be used if the server cannot process the entity, e.g. if an image
cannot be formatted or mandatory fields are missing in the payload.
500 – Internal Server Error – API developers should avoid this error. If an error occurs in the global catch
block, the stracktrace should be logged and not returned as response.
{
"errors": [
{
"userMessage": "Sorry, the requested resource does not exist",
"internalMessage": "No car found in the database",
"code": 34,
"more info": "http://dev.mwaysolutions.com/blog/api/v1/errors/12345"
}
]
}
Use the custom HTTP Header X-HTTP-Method-Override to override the POST Method.