Docker builds Redis cluster from scratch

Docker installation

Docker official installation instructions address

I met Docker

  • Lightweight, simple modeling approach.
  • Born for cloud computing.
  • Multiple platforms are portable, easy to build, and easy to collaborate with.
  • Infrastructure is code: With Docker mirroring, we can make the creation process automatic and repeatable, and do version management.
  • Immutable infrastructure: With the stateless service upgrade, deployment is much easier and simpler, we don’t need to change the configuration, just destroy and rebuild.
  • Docker is even more fragrant when paired with SOA or microservices architectures.

The structure of the Docker

Explain a few key words from the graph above:

  • ClientThis is what you see in the Docker control terminal.
  • DOCKER_HOST: This is the overall runtime context in which docker runs
  • Docker daemon: This is the daemon of Docker, which communicates with Client, obtains Client commands, and manages Image Container and other components in a unified manner.
  • ImagesThis is the template or basis for all containers to run on. Containers are created using images.
  • Containers: Run instances created by an Image. Programs run in containers. An Image can be extended to N containers
  • Docker Registry: The repository of Docker Image. In general, the official DockerHub.com is used. However, you can also set up your own Docker Registry to privatize Image management.

Docker Container

After Docker is successfully installed, please use Docker Info in the command line to confirm whether Docker is correctly installed in the local environment.

Start your first Container

docker run -t -i ubuntu /bin/bash

If the startup is successful, you will see your command line become

root@c8fabbcb9f8a:/#

This string of characters after the @ sign might not be the same as mine, but they’re all randomly generated container ids

You can use the ps aux,hostname, IP a commands in the Container terminal to appreciate the convenience of containers.

You can also install vim in Container by using apt-get update && apt-get install vim.

Remember, if the Container is not contained in the Image, it will no longer exist when the Container is removed.

Next, let’s explain what a command means one by one:

  • docker runThe command that tells Docker to start a container.
  • -t: Creates a virtual TTY terminal.
  • -i: indicates that the Container we created will be capturedSTDIN.
  • ubuntu: This is the Image we used to start the Container. If you need to specify the version, you can pass itUbuntu: 18.04This way.
  • /bin/bash: This command is executed after the Container is started.

Finally, type exit and press Enter to stop the terminal. At this point, you’ll be back to your own operating system terminal, so how can we see where the container that we just created went?

Enter docker ps -a

You’ll see something like this

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c8fabbcb9f8a ubuntu "/bin/bash" 7 seconds ago Exited (0) 4 seconds  ago elated_banzaiCopy the code

We can find a lot of useful information through these:

  • CONTAINER ID: container ID we saw in the TTY earlier.
  • IMAGE: Container Indicates the Container used.
  • COMMAND: Command used after the Container is started.
  • CREATED: Indicates the time when the Container is created.
  • STATUS: Indicates the current status of Container.
  • PORTSThe Container exposes the internal ports, but this example does not. We will see the specific operations and concepts in later examples.
  • NAMES: The name of the Container. We can pass the Container when it runs--nameSpecify a name. If not, Docker will automatically generate a name.

Look at the Container

Docker ps,docker container ls these two commands have the same result, that is, view the Containers that are currently running. To view all containers, add -a to the end of the command

Restart the Stopped Container

Docker start elated_banzai or docker start c8FABBCB9F8a

You can restart the container by following the name or ID of the container with the docker start command.

However, we will find that after this startup, we do not enter the terminal, but check the latest status, it is in UP state. Why is that

Because we only restarted the Container this time, we did not connect to the Container via tty.

What if we want to connect it back, which we’ll talk about

Connect to running Containers using a TTY

docker exec -i -t elated_banzai /bin/bash

Of course, the Container name used above can also be used as the Container ID. In the Docker command, the name and ID are interchangeable wherever the Container is specified.

This command is relatively simple, and is basically similar to docker run, so I will not expand the explanation here.

View the Container output

First, the following command creates a Container called log_container

docker run -d --name log_container ubuntu /bin/sh -c "while true; do echo hello world; sleep 3; done"

You may notice that there is a -d configuration in this command, which specifies how to run the Container in daemon mode.

We ended up writing a shell script with an infinite loop that prints hello World every three seconds.

After you press Enter, the container only displays the ID of the container. You can run the following command with the ID or name to view the output.

docker logs log_container

Add -f to keep looking at its output, which is similar to the tail command in Linux.

We can also add -t to see the timestamp of the output.

View processes inside containers

docker top name/id

View statistics on all Containers or specified Containers

docker stats,docker stats ContainerName1 ContainerName2...

Stop the Container

docker stop name/id

Remove the Container

docker rm name/id

If -f is not added to this command, we can only delete stopped containers.

View Container information in depth

docker inspect name/id

Keyname: keyname: keyname: keyname: keyname: keyname: keyname: keyname: keyname: keyname: keyname

Image

To view Images

docker images,docker image ls

Delete the Image

docker images

Dockerfile

Dockerfile is an instruction that uses basic Docker DSL syntax to build a Docker Image file. Dockerfile is used to build images, which are more repeatable, transparent and idempotent.

Basic theory basically introduced almost, so we directly on dry goods.

Build a Redis cluster with Docker

Create a Replica Base Image

First, we create the master image of Redis

$ mkdir redis-replica
$ cd redis-replica
$ touch Dockerfile
Copy the code

Dockerfile:

# The parent image used
FROM redis
# Maintainer information, this is filled in my own email
MAINTAINER crowhyc@163.com
Environment variables can be used in containers and in subsequent commands
This environment variable is used to define the version and date of the image
ENV REFESHED_AT 2020-04-01
# to add volumes to image-based containers
A volume is a specific directory that can reside in one or more Containers. This directory can share data or persist data
The following VOLUME creates the two directories in the Container
VOLUME ["/var/lib/redis"."/var/log/redis/"]
The Container created based on this Image exposes the 6379 interface
Docker ports name/ ID can be used to query the mapping between the exposed interface and the local interface
You can also use -p 6379:6379 to bind the Container to the specified port when the Container Docker runs
EXPOSE 6379
The # command is used to specify the command to RUN when the Container is started, similar to the RUN command you will see later
The RUN command specifies that the Image should be RUN when it is built
The #docker run command overrides the CMD command, leaving the command blank so that the docker run command can be customized
The ENTRYPOINT command described below cannot be overridden
CMD []
Copy the code

After saving the file, we run the following command

Docker build-t Crowhyc/Redis-Replica :1.0.

Let’s explain this command

Docker builds are used to create images.

-t and Crowhyc/Redis-Replica :1.0 are used to indicate that we will use this format to specify the Image name and version later.

Finally, there is the unremarkable. This is used to specify the address of the Dockerfile.. If yes, the Dockerfile is in the current directory

We will not run the Image. It is the master Image of the following redis-primary and redis-slave

Create a Redis Primary Image

$ mkdir redis-primary
$ cd redis-primary
$ touch Dockerfile
Copy the code
FROM crowhyc/redis-primary:1.0
MAINTAINER crowhyc "[email protected]"
ENV REFRESHED_AT 2020-04-01
The # command is executed after the Container is started. Unlike CMD, it cannot be overridden by the Container command
ENTRYPOINT ["redis-server"."--logfile /var/log/redis/redis-server.log"]
Copy the code

Next, we create the master image of Redis

Docker build-t crowhyc/redis-primary:1.0

Create a Redis slave Image

$ mkdir redis-slave
$ cd redis-slave
$ touch Dockerfile
Copy the code
FROM crowhyc/redis-replica:1.0
MAINTAINER crowhyc "[email protected]"
ENV REFRESHED_AT 2020-04-01
One thing to note here is that the third parameter we use is redis-primary and we need the same name when we start the Primary Container
ENTRYPOINT ["redis-server"."--logfile /var/log/redis/redis-server.log"."--slaveof redis-primary 6379"]
Copy the code

Start redis Cluster and view the status

Create a Network

docker network create redis-cluster

First, we create a new network using the command above.

Docker network is a network used for communication between Docker Containers. Users can create a network to form their own cluster network.

docker network ls

Similarly, we can use the command above to view all the networks

Start the redis – primary

Next, we use the following Docker run to start our Primary Redis Server.

Docker run -d -h redis-primary --net redis-cluster --name reids-primary crowhyc/redis-primary:1.0

-h is a new flag that is used to set the hostname of the Container. This overrides the default behavior of setting the Container hostname to ContainerId.

Using this flag ensures that the redis-primary is used as the hostname of the Container and is correctly resolved by the local DNS service.

Net is used to set the Network used by the Container

View the redis-primary log

We enter the docker logs command to view the logs

docker logs -f redis-primary

If you wait a little, you will find that there is nothing at all… So why is that?

/var/log/redis/redis-server.log (redis/redis/redis-server. log)

Next, we look at log output in situations like this in a more subtle way.

docker run -it --rm --volumes-from redis-primary ubuntu cat /var/log/redis/redis-server.log

Redis-primary: Ready to accept connections redis-primary: Ready to accept connections

Then let’s start two redis-slaves for him

Start multiple Redis-slaves

Docker run -d -h redis-slave01 –net redis-cluster crowhyc/redis-slave:1.0 docker run -d -h redis-slave02 –net Redis – cluster crowhyc/redis – slave: 1.0

Docker runit –rm –volumes-from Redis-slave01 Ubuntu cat Docker runit –rm –volumes-from Redis-slave01 Ubuntu cat /var/log/redis/redis-server.log docker run -it –rm –volumes-from redis-slave02 ubuntu cat /var/log/redis/redis-server.log

If, we can see something like MASTER <-> REPLICA sync: Finished with Success (slave and primary

Brief redis cluster testing

Docker runit –rm –net redis-cluster redis /bin/bash

redis-cli -h redis-primary

Then, we SET a value

redis-primary:6379> set "test" 1234

Next, we enter Redis-Slave01

docker exec -it redis-slave01 /bin/bash

redis-cli -h localhost

get "test"

If we see “1234” after executing the get command, our Redis cluster has been set up.