consul集群安装

本文详细介绍了如何在Linux环境下安装和配置Consul1.9.4,包括下载与解压软件包,设置自动补全,创建用户和数据目录,单机与集群安装,配置systemd服务,以及生成和分发密钥和TLS证书,确保服务的安全性。此外,还提供了配置文件示例和启动服务的步骤。
摘要由CSDN通过智能技术生成

hosts

10.0.0.11 node1
10.0.0.12 node2
10.0.0.13 node3

安装

下载

curl --silent --remote-name https://releases.hashicorp.com/consul/1.9.4/consul_1.9.4_linux_amd64.zip
unzip consul_1.9.4_linux_amd64.zip
cp consul /usr/bin/
consul --version

配置自动补全

consul -autocomplete-install
complete -C /usr/bin/consul consul

创建启动用户及数据目录

useradd --system --home /etc/consul.d --shell /bin/false consul
mkdir /opt/consul
chown -R consul:consul /opt/consul

单机安装

consul agent -server -data-dir=/opt/consul -bootstrap -ui -advertise=10.0.0.11 -client=10.0.0.11

访问UI:http://10.0.0.11:8500/ui

常用参数

-bind:为该节点绑定一个地址
-enable-script-checks=true:设置检查服务为可用
-join:加入到已有的集群中
-server 表示当前使用的server模式
-node:集群中的每个node必须有一个唯一的名称。默认情况下,Consul使用机器的hostname

-config-file: 要加载的配置文件
-config-dir:指定配置文件,定义服务的,默认所有以.json结尾的文件都会读
-datacenter: 数据中心没名称,不设置的话默认为dc
-client: 客户端模式
-ui: 使用consul自带的ui界面
-data-dir consul存储数据的目录
-bootstrap:用来控制一个server是否在bootstrap模式,在一个datacenter中只能有一个server处于bootstrap模式,当一个server处于bootstrap模式时,可以自己选举为raft leader。

-bootstrap-expect:在一个datacenter中期望提供的server节点数目,当该值提供的时候,consul一直等到达到指定sever数目的时候才会引导整个集群,该标记不能和bootstrap共用

配置systemd

vim /usr/lib/systemd/system/consul.service

[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target

[Service]
Type=notify
User=consul
Group=consul
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

集群安装

生成密钥(可选)

consul keygen

输出的密钥用于在服务端的配置文件中在 encrypt 项中配置

生成TLS证书以进行RPC加密(可选)

创建CA

consul tls ca create
==> Saved consul-agent-ca.pem
==> Saved consul-agent-ca-key.pem

创建证书

# 创建服务端证书
consul tls cert create -server -dc <dc_name>

创建客户端证书

consul tls cert create -client -dc <dc_name>

分发证书

scp consul-agent-ca.pem <dc-name>-<server/client>-consul-<cert-number>.pem <dc-name>-<server/client>-consul-<cert-number>-key.pem <USER>@<PUBLIC_IP>:/etc/consul.d/

创建配置文件

一般创建 consul.json 用于所有agent,创建 server.json 用于服务端

mkdir  /etc/consul.d

vim /etc/consul.d/consul.json

node1

{
  "datacenter": "dc",
  "data_dir": "/opt/consul",
  "server": true,
  "bootstrap_expect": 3,
  "log_file": "/opt/consul/consul.log",
  "log_level": "INFO",
  "node_name": "node1",
  "advertise_addr": "10.0.0.11",
  "bind_addr": "10.0.0.11",
  "client_addr": "10.0.0.11",
  "retry_join": ["10.0.0.11", "10.0.0.12", "10.0.0.13"],
  "ui_config": {
     "enabled": true
  }
}

node2

{
  "datacenter": "dc",
  "data_dir": "/opt/consul",
  "server": true,
  "bootstrap_expect": 3,
  "log_file": "/opt/consul/consul.log",
  "log_level": "INFO",
  "node_name": "node2",
  "advertise_addr": "10.0.0.12",
  "bind_addr": "10.0.0.12",
  "client_addr": "10.0.0.12",
  "retry_join": ["10.0.0.11", "10.0.0.12", "10.0.0.13"],
  "ui_config": {
     "enabled": false
  }
}

node3

{
  "datacenter": "dc",
  "data_dir": "/opt/consul",
  "server": true,
  "bootstrap_expect": 3,
  "log_file": "/opt/consul/consul.log",
  "log_level": "INFO",
  "node_name": "node3",
  "advertise_addr": "10.0.0.13",
  "bind_addr": "10.0.0.13",
  "client_addr": "10.0.0.13",
  "retry_join": ["10.0.0.11", "10.0.0.12", "10.0.0.13"],
  "ui_config": {
     "enabled": false
  }
}
chown -R consul:consul /etc/consul.d
chmod 640 /etc/consul.d/consul.json

启动文件

vim /usr/lib/systemd/system/consul.service

[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target

[Service]
Type=notify
User=consul
Group=consul
ExecStart=/usr/bin/consul agent -config-dir=/etc/consul.d/
ExecReload=/bin/kill -HUP $MAINPID
KillSignal=SIGINT
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

检查配置

consul validate /etc/consul.d/consul.json

三个节点分别启动

systemctl start consul.service

在任意节点查看集群

consul members -http-addr=http://10.0.0.11:8500
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wuxingge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值