The docker container uses domain name instead of ip to deploy the spring cloud project

Posted by qhiiyr on Sat, 18 Dec 2021 06:13:55 +0100

Project requirements:
At present, all the projects developed use ip addresses to start services and deploy. According to the feedback of the implementation personnel, this method increases the difficulty of their deployment on the customer's site. For colleagues with less attributes, it is designed that N multiple services need to be started and viewed when deploying a project, which is not easy to troubleshoot service problems, so we modify it to deploy through dns. Colleagues are compatible with the customer's on-site deployment scenarios. For example, we only need to deploy services in the company, directly install the server to the customer's site, link the network and start the service, so as to reduce the difficulties of construction personnel.

Taking the actual deployment of spring cloud project as an example, this paper records that the original direct link of each service component through ip is modified to domain name. This method will change the inconvenience caused by the original project ip deployment and improve the project deployment and maintenance cost.

The docker container uses dns instead of ip to deploy the spring cloud project

Official website: https://docs.docker.com
Docker command: https://docs.docker.com/engine/reference/commandline/commit/

docker network mode

After docker is installed, there are four modes by default: bridge, host, Container and none
Bridge: after the container is started in this mode, IP will be automatically assigned to each container, and the container will be connected to a docker0 virtual bridge to communicate with the host through the docker0 bridge and Iptables nat table configuration.
Host: the startup container in this mode will not virtualize its own network card and configure its own IP, but use the IP and port of the host.
Container: in this mode, the container will not create its own network card and configure its own IP, but share the IP and port range with a specified container.
None: this mode turns off the network function of the container and is applicable to custom network protocol development, etc.

Detailed reference: https://www.cnblogs.com/zuxing/articles/8780661.html

Here, we use the host and bridge modes to accept two different ways of using redis in actual projects.

Deploy through host mode

As explained above, the host mode uses the same network as the host, that is, it is equivalent to direct deployment on the host, but there are some differences. The processing network is shared and others are container specific. The following is an example of deploying redis.

network configuration

1. Configure network ip address

vim /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.0.87
PREFIX=24
GATEWAY=192.168.0.1
DNS1=8.8.8.8
IPV6_PRIVACY=no
UUID=c96bc909-188e-ec64-3a96-6a90982b08ad


2. Modify host name

vim /etc/sysconfig/network
[root@daison ~]# cat /etc/sysconfig/network
# Created by anaconda
NETWORKING_IPV6=no
PEERNTP=no
HOSTNAME=daison
[root@daison ~]# 


Hostnamectl set hostname daison # takes effect immediately

3. Modify ip domain name mapping
vim /etc/hosts

Deployment Services

1. Original deployment:

sudo docker run -dit --hostname redis --name redis -p 6379:6379 \
-v /home/data/redis/redis.conf:/etc/redis/redis.conf  \
-v /home/data/redis/data:/data \
--restart=always --privileged=true \
redis:6.0.9 redis-server /etc/redis/redis.conf --appendonly yes

Parameter Description:
– hostname: Specifies the domain name
-p 6379:6379: map the 6379 port in the container to the host 6379 port
-v /home/data/redis/redis.conf:/etc/redis/redis.conf: redis. Config the host computer Conf is placed in this location within the container
-v /home/data/redis/data:/data: display the redis persistent data in the host computer for data backup
redis-server /etc/redis/redis.conf: This is the key configuration, so that redis is not started without configuration, but according to this redis Conf configuration startup
– append only yes: data persistence after redis is started

This mode uses the bridge bridge mode, which can not directly access the host network, but access the host network through docker0 bridge. For example, to use the domain name, the host cannot be implemented, so it is modified to use the host mode.

2. host network mode deployment

sudo docker run -dit --net=host --hostname redis --name redis \
-v /home/data/redis/redis.conf:/etc/redis/redis.conf  \
-v /home/data/redis/data:/data \
--restart=always --privileged=true \
redis:6.0.9 redis-server /etc/redis/redis.conf --appendonly yes

The host mode uses the unified network of the host without declaring the port mapping, that is, the port of the host is used. In this mode, the domain name can be used to directly access the container.

Parameter Description:
– net=host: indicates that the network is shared with the host, that is, a network is shared with the host. This mode does not need to declare ports
– hostname: Specifies the domain name
-p 6379:6379: map the 6379 port in the container to the host 6379 port
-v /home/data/redis/redis.conf:/etc/redis/redis.conf: redis. Config the host computer Conf is placed in this location within the container
-v /home/data/redis/data:/data: display the redis persistent data in the host computer for data backup
redis-server /etc/redis/redis.conf: This is the key configuration, so that redis is not started without configuration, but according to this redis Conf configuration startup
– append only yes: data persistence after redis is started

Login server authentication:

[root@daison rabbitmq]# 
[root@daison rabbitmq]# sudo docker exec -it redis bash
root@daison:/data# 
#redis container linked by domain name
root@daison:/data# redis-cli -h daison -p 6379 -a Dszn@2020
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
daison:6379> 
daison:6379> keys *
(empty array)
daison:6379> quit
root@daison:/data# 
#Link redis container through ip address
root@daison:/data# redis-cli -h 192.168.0.87 -p 6379 -a Dszn@2020
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.0.87:6379> 
192.168.0.87:6379> keys *
(empty array)
192.168.0.87:6379> set name wangwu
OK
192.168.0.87:6379> get name
"wangwu"
192.168.0.87:6379> quit
root@daison:/data# 


3. Modify local host configuration
C:\Windows\System32\drivers\etc\hosts

3. Modify project configuration
When used in a project, domain name links are used directly. Examples are as follows:

spring:
  #redis config
  redis:
    open: false  # Enable redis cache? true enable false close
    #redis uses database
    database: 1
    #Link via domain name
    host: daison
    #port
    port: 6379
    #Password, blank by default
    password: 123456
    # Connection timeout length (MS)
    timeout: 120s
    jedis:
      pool:
        # Maximum number of connections in the connection pool (negative value indicates no limit)
        max-active: 1000
        # Maximum blocking wait time of connection pool (negative value indicates no limit)
        max-wait: -1ms
        # Maximum free connections in the connection pool
        max-idle: 10
        # Minimum free connections in connection pool
        min-idle: 5

Deployment through bridge mode

Take redis deployment as an example:

sudo docker run -dit --add-host=daison:192.168.0.87 --hostname redis --name redis -p 6379:6379
-v /home/data/redis/redis.conf:/etc/redis/redis.conf
-v /home/data/redis/data:/data
–restart=always --privileged=true
redis:6.0.9 redis-server /etc/redis/redis.conf --appendonly yes

Parameter Description:
–add-host=daison:192.168.0.87: add the host domain name and ip mapping to the / etc/hosts file in the container, so that the container can directly access the external network through the domain name or ip.
– hostname: Specifies the domain name
-p 6379:6379: map the 6379 port in the container to the host 6379 port
-v /home/data/redis/redis.conf:/etc/redis/redis.conf: redis. Config the host computer Conf is placed in this location within the container
-v /home/data/redis/data:/data: display the redis persistent data in the host computer for data backup
redis-server /etc/redis/redis.conf: This is the key configuration, so that redis is not started without configuration, but according to this redis Conf configuration startup
– append only yes: data persistence after redis is started

Start service test:

[root@daison ~]# 
[root@daison ~]# docker exec -it redis bash
root@28c88ea243dc:/data# cat /etc/hosts 
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
ff00::0	ip6-mcastprefix
ff02::1	ip6-allnodes
ff02::2	ip6-allrouters
192.168.0.87	daison
172.17.0.2	28c88ea243dc
root@28c88ea243dc:/data# 
root@28c88ea243dc:/data# redis-cli -h daison -p 6379 -a Dszn@2020
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
daison:6379> keys *
1) "name"
daison:6379> quit
root@28c88ea243dc:/data# 
root@28c88ea243dc:/data# redis-cli -h 192.168.0.87 -p 6379 -a Dszn@2020
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.0.87:6379> 
192.168.0.87:6379> keys *
1) "name"
192.168.0.87:6379> quit;
(error) ERR unknown command `quit;`, with args beginning with: 
192.168.0.87:6379> quit
root@28c88ea243dc:/data# 

Topics: Docker Spring Cloud