译文
原文链接: https://nsq.io/deployment/docker.html
This post details how to deploy and run the nsq binaries inside Docker containers.
本文详细介绍了如何在Docker容器中部署和运行nsq二进制文件。
There is a single, minimal nsq image that contains all of the NSQ binaries.
有一个单一的、最小的nsq映像,它包含所有nsq二进制文件。
Each binary can be run by specifying the binary as the command when running Docker. The basic format is:
每个二进制文件都可以通过在运行Docker时将二进制文件指定为命令来运行。基本格式为:
docker run nsqio/nsq /<command>
Note the /
before the command. For example:
注意命令前面的' / '。例如:
docker run nsqio/nsq /nsq_to_file
Links 链接
Run nsqlookupd 运行 nsqlookupd
docker pull nsqio/nsq
docker run --name lookupd -p 4160:4160 -p 4161:4161 nsqio/nsq /nsqlookupd
Run nsqd 运行 nsqd
First, get the docker host’s ip:
首先,获取docker主机的ip:
ifconfig | grep addr
Second, run the nsqd
container:
其次,运行
nsqd
容器:
docker pull nsqio/nsq
docker run --name nsqd -p 4150:4150 -p 4151:4151 \
nsqio/nsq /nsqd \
--broadcast-address=<host> \
--lookupd-tcp-address=<host>:<port>
Set the --lookupd-tcp-address
flag to the host’s IP and the TCP port of previously run nsqlookupd
, i.e. dockerIP:4160
:
将
--lookupd-tcp-address
标记设置为主机的ip和之前运行起来的nsqlookupd
的TCP端口,即dockerIP:4160
:
For example, given a host IP of 172.17.42.1
:
例如,给定主机IP为
172.17.42.1
:
docker run --name nsqd -p 4150:4150 -p 4151:4151 \
nsqio/nsq /nsqd \
--broadcast-address=172.17.42.1 \
--lookupd-tcp-address=172.17.42.1:4160
Note that this uses port 4160
, the port exposed when we started the nsqlookupd
container (which also happens to be the default port for nsqlookupd
).
注意,这里使用的端口是“4160”,这是我们启动“nsqlookupd”容器时公开的端口(恰好也是“nsqlookupd”的默认端口)。
If you want to use a non-default port, change the -p
parameter:
如果您想使用非默认端口,请更改' -p '参数:
docker run --name nsqlookupd -p 5160:4160 -p 5161:4161 nsqio/nsq /nsqlookupd
That will make nsqlookupd available on the docker host’s IP on the ports 5160 and 5161.
这将使nsqlookupd在端口5160和5161上的docker主机的IP上可用。
Using TLS 使用TLS
To use TLS with containerized NSQ binaries, you need to include the certificate file, the private key, and the root CA file.
要使用包含NSQ二进制文件的TLS,需要包含证书文件、私钥和根CA文件。
The Docker image has a volume mount available at /etc/ssl/certs/
available for this purpose.
Docker映像的卷挂载位于
/etc/ssl/certs/
,可用于此目的。
Mount a host directory containing the files to the volume, then specify the files in the command line, as usual:
将包含文件的主机目录装入到卷中,然后像往常一样在命令行中指定文件:
docker run -p 4150:4150 -p 4151:4151 -p 4152:4152 -v /home/docker/certs:/etc/ssl/certs \
nsqio/nsq /nsqd \
--tls-root-ca-file=/etc/ssl/certs/certs.crt \
--tls-cert=/etc/ssl/certs/cert.pem \
--tls-key=/etc/ssl/certs/key.pem \
--tls-required=true \
--tls-client-auth-policy=require-verify
This will load the certificates from /home/docker/certs
into the Docker container to use when running.
这将把证书从
/home/docker/certs
加载到docker容器中,以便在运行时使用。
Persisting NSQ Data
To store nsqd
data on the host disk, use the /data
volume as your data directory, which lets you mount to a data-only Docker container or mount to a host-directory:
要在主机磁盘上存储“nsqd”数据,请使用“/data”卷作为数据目录,它允许您挂载到只包含数据的Docker容器或挂载到主机目录:
docker run nsqio/nsq /nsqd \
--data-path=/data
Using docker-compose 使用docker-compose
To start nsqd
, nsqlookupd
, and nsqadmin
together using docker-compose
then create a docker-compose.yml
.
要使用docker- composition启动
nsqd
、nsqlookupd
和nsqadmin
,然后创建一个docker- composition .yml
。 建议按照docker-compose的规则来编写!!!!!!
version: '3'
services:
nsqlookupd:
image: nsqio/nsq
command: /nsqlookupd
ports:
- "4160"
- "4161"
nsqd:
image: nsqio/nsq
command: /nsqd --lookupd-tcp-address=nsqlookupd:4160
depends_on:
- nsqlookupd
ports:
- "4150"
- "4151"
nsqadmin:
image: nsqio/nsq
command: /nsqadmin --lookupd-http-address=nsqlookupd:4161
depends_on:
- nsqlookupd
ports:
- "4171"
To start run the following command from the same directory as the docker-compose.yml
created previously.
先创建
docker-compose.yml
文件,在本文件夹下使用如下命令 -d 参数是 后台守护运行的意思 。
docker-compose up -d
A private network will be created and three containers will be started using the private network.
将创建一个专用网络,并使用该专用网络启动三个容器。
On the local host each container will have a random port mapped to ports exposed in the docker-compose.yml
.
在本地主机上,每个容器都有一个随机端口映射到
docker- composition .yml
中公开的端口。
To view the running containers status and mapped ports.
以查看正在运行的容器状态和映射的端口。
docker-compose ps
To see the logs from the running containers.
要查看运行容器中的日志。
docker-compose logs
Assuming that the nsqlookupd
had host port 31001 mapped to the container port 4161 a simple ping could be performed using curl
.
假设
nsqlookupd
有主机端口31001映射到容器端口4161,则可以使用curl
执行简单的ping。
curl http://127.0.0.1:31001/ping