How to connect consul in dev and prod modes

black and gray laptop computer turned on doing computer codes
Reading Time: 5 minutes

Hello folks. In this blog, I’m gonna show you How to connect consul in dev and prod modes and its configuration and start a service/agent properly so that the other agents can join them to make a data center.

you may refer to this blog only for the installation but the problem was we can’t bind the port every time that’s what I’m gonna show you in this blog.

Alright on both ami we’ve installed consul to verify.

consul -v

consul connect in dev mode:

to start the Consul server/agent, we will see by the below command

consul agent --dev
sudo apt install net-tools
netstat -ntlp

the agent has started but all the connections on ports 8600, 8300, etc everything is listening

on localhost so these services won’t be able to reachable from outside of this machine.

so we need to fix this.

consul agent -dev -client=0.0.0.0 

it should fix a few things and I do

netstat -ntlp

you see some of the services are now open they aren’t only listening on only localhost ports

and they are now open for external access.

so we’ve started the agent and who can be the client here 0.0.0.0 means clients can be

anyone who wants to access the consul here.

but still, the internal components the server itself, and the ports which are requested by the

other agents to join all those are still running on localhost so it’s not enough.

consul agent -dev -client=0.0.0.0 -bind 172.31.14.119

it’ll start the agent again and you can see instead of localhost because we’ve bound this host’ip

so it’s running on this IP address.

Therefore, it’ll help because this agent and new agents can talk to this machine at 8301 port.

and then get connected to them.

but there are a lot of commands for agents and join the agents and run other commands also, it’ll difficult to remember and bind the address every time.

we can just add them to the configuration file and use that config file to work with the consul. so how do we do that? we can create the consul directory and save these configuration files.

Consul configuration file:

mkdir consul && cd consul
vim default.hcl

Hashicorp uses a custom language or custom domain-specific language which is called as hcl named as

“Hashicorp configuration language”.

client_addr= "0.0.0.0"
bind_addr = "172.31.14.119"
cd ..
consul agent -dev --config-dir consul/

it will take all the parameters from all the hcl or JSON files present in the consul directory.

consul accepts both hcl and JSON format to have the configuration.

tada, it’s working as before.

<agent-public-ip>:8500

as we can see GUI also working fine.

NOTE;

we’ve created a key pair like below.

but we have started agent in dev mode, any changes you do, their changes are stored in memory

, and if you want to reset your server or reset your service so all these values or changes will be lost.

so you shouldn’t use the dev mode of running agents in production.

as we can see I’ve stopped the agent and then restart again the key-value pair is gone.

After that, let’s add another agent here.

Join another consul agent:

consul agent --join 172.31.14.119 -bind 172.31.10.242 --data-dir /tmp/consul

we’re joining the server agent by using its IP address and binding with its local machine itself.

and all the incoming connections will be received to this agent itself.

if we didn’t bind this so you’ll see all the connections bound to the localhost.

However, we bind the address but it will also complain about the data directory so It can store all

the generated data.

Now consul agent is started and running.

just like this, you can add multiple nodes in the cluster.

but again if I restart the cluster again it all will be gone. so I stop the server in dev mode.

consul connect in prod mode:

Similarly, we’ll see how to run the server in production mode.

consul agent -server --config-dir consul/ --data-dir /tmp/consul_data --bootstrap-expect=1

we can either define the data-dir in the config file and we need to add this data-dir in prod mode because it persistently stores the data, and we can get it even after restarting the server again.

bootstrap-expect it’s a boolean parameter (true, false, 0, 1) that means it’ll be the first machine in the cluster.

the server is started and running in production mode.

but you can see GUI isn’t available. so we need to pass the parameters for GUI also.

generally, in prod mode, you don’t want people to interact with the cluster but if you want to, you need to only add -ui=1 parameter.

consul agent -server --config-dir consul/ --data-dir /tmp/consul_data --bootstrap-expect=1 -ui=1

now the agent is started and running fine so we can see the GUI here.

so as you can see after stopping the server, the key-value pair isn’t gone because it’s running in production mode, and data is stored on the disk whenever the server restarts or starts. it automatically loads all the data.

references:

https://www.consul.io/docs/agent/options

Conclusion:

In this blog, we’ve seen how to run agents in both dev and prod mode and how they work, and also you can configure the parameters by using hcl also. If you find this blog helpful do share it with your friends.

Written by 

A curious DevOps Intern , love to learn and working on technical skills/tools, know the basics of Linux, Docker, Ansible , Kubernetes and more..

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading