二级域名以及 Docker 容器互相访问

二级域名

起因

由于在一台服务器上部署了多个不同的应用/服务,之前是使用域名+端口号的方式来区分应用。但是这种方式在应用或服务多了以后,就不好使了,对于个人来说,需要记上应用或服务对应的端口号以及服务的域名后缀。
于是就有了这篇文章记录。

在 Nginx 配置子域名

  • 在 Nginx 中配置需要的子域名(我习惯将每一个子域名的配置分开来,一个子域名对应一个字配置文件)
    • 在 Nginx 默认配置文件 http 节点下配置:include /path/to/config/*.conf
  • Portainer 配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# portainer.conf
server {
listen 80;
listen [::]:80;
server_name docker.noexception.cn;

location / {
proxy_redirect off;
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
proxy_pass http://portainer:9000/;
}
}
  • 定时任务配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

# tasks.conf
server {
listen 80;
listen [::]:80;
server_name tasks.noexception.cn;

location ^~ /xxl-job-admin {
proxy_redirect off;
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
proxy_pass http://xxl-job-admin:7397/xxl-job-admin/;
}

location / {
proxy_redirect off;
proxy_set_header host $host;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
proxy_pass http://xxl-job-admin:7397/xxl-job-admin/;
}
}

配置域名解析

5QXZP.png

  • 在域名管理界面中,添加一条解析记录:
    • 记录类型为A
    • 主机记录为: 子域名(如:docker),
    • 记录值为服务器的IP地址,
    • 提交保存。

总结

  • 整个配置过程并不复杂,概况来说就是三步:
    1. 部署应用;
    2. 添加 Nginx 配置(如 portainer.conf);
    3. 在域名解析页面中,添加子域名的解析记录。

补充:

1. 关于在 Docker 中,配置好子域名后,发现无法访问的情况。

  • 原因: 是因为 docker 的网络策略默认情况下,容器与容器之间是无法相互访问的,在启动容器的时候,得给她们指定好网络组。
  • 又因为容器重启,IP 会随之变化,所以我们需要利用 docker 网络,通过容器 name 在容器中互相访问。

Docker 网络

1. 创建自定义网络

  • 指令: docker network create domain
  • 默认使用的是 bridge 驱动
  • 不在同一个 docker network 中的容器,不能互相访问,他们不在同一个网段上

2. 创建容器时,指定 docker network

  • 启动第一个容器,指令: docker run -it --name portainer --network domain -p 9000:9000 portainer/portainer

  • --network 参数指定容器使用的网络

  • 启动第二个容器,指令: docker run -p 6379:6379 --network domain --name redis -v /docker/data/redis/redis.conf:/etc/redis/redis.conf -v /docker/data/redis/data:/data -d redis redis-server /etc/redis/redis.conf --appendonly yes

  • 启动一个 redis 服务容器,通过在 redis 容器访问 portainer 容器,使用 curl 工具来协助我们验证

验证:

  1. 使用docker exec进入 redis 容器中
1
2
3
# docker exec -it redis bash
[root@noexception ~]# docker exec -it redis bash
root@a01af1db7118:/data#
  1. 使用 curl 工具进行验证:
1
2
# curl http://portainer:9000
# ping http://portainer:9000
  1. 使用 docker network inspect domain 来查看 docker network 信息,检查两个容器是否链接到了同一个网络下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
[
{
"Name": "domain",
"Id": "17a05973b0e8805b372e143d3a7ce591a076e98e3d7cb357444ed2ee1338e009",
"Created": "2022-04-08T15:13:19.017550444+08:00",
"Scope": "local",
"Driver": "bridge",
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.18.0.0/16",
"Gateway": "172.18.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"11ccb85d3c61b0a5826d311965929b1dad07aec2013778bc7d4fbf46dbaace7c": {
"Name": "nginx",
"EndpointID": "d0d611461ff9a3849e53c05c27fdf26ca39bea739d24de613b013b86cb8a43ff",
"MacAddress": "02:42:ac:12:00:03",
"IPv4Address": "172.18.0.3/16",
"IPv6Address": ""
},
"7f2e31026a6249577632239f6cd69a6781718a6e4b436db5747fece6599b05ae": {
"Name": "zookeeper",
"EndpointID": "38cbc10011c3e68ccbcf7f7c7b69948df0fd6a72834d10258752139e149efa0d",
"MacAddress": "02:42:ac:12:00:05",
"IPv4Address": "172.18.0.5/16",
"IPv6Address": ""
},
"8e37d220283e84af7382a4481baea5f8dca61b30166922bd8bc03adfd2dbb322": {
"Name": "portainer",
"EndpointID": "0f51d369c1fe824b0d3941d8a3dce868a88581a17733d4a685515401ea6b08a6",
"MacAddress": "02:42:ac:12:00:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
"a01af1db711844c4547d2b64e53021f5bb6967fb7e83120592d5c913872f4cd9": {
"Name": "redis",
"EndpointID": "1754b57297a8a7ea0933fcab19727b045c1948551ff1bdab449c2b4c525cda11",
"MacAddress": "02:42:ac:12:00:04",
"IPv4Address": "172.18.0.4/16",
"IPv6Address": ""
},
"dc550ff519c39f9f9800e3ba5dc25ae4c435550d1259425caba426601a4dc358": {
"Name": "xxl-job-admin",
"EndpointID": "6373e88cc3ce52fefbcbcb99fdccdd74df16c64c805a071e82cf07985675b8e7",
"MacAddress": "02:42:ac:12:00:06",
"IPv4Address": "172.18.0.6/16",
"IPv6Address": ""
},
"f7762c952a1c21cbc230fe8c65c705bacafd57c9655744b2cc43af658dd945d7": {
"Name": "kafka",
"EndpointID": "52ca42e07513833630f4906b34590641bbb743d9586eba9da08e9796a56fd147",
"MacAddress": "02:42:ac:12:00:07",
"IPv4Address": "172.18.0.7/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]