通过Y20流水线安装Consul集群

2022-04-14 18:43:03 浏览数 (1)

Consul是在微服务架构中做注册中心的一个程序,非常小巧。它可以和openresty配合做网关路由,和springcloud配合做服务发现。除了单机运行,它还能部署为高可用集群。下面,我先讲一下如何手动安装Consul节点并加入集群,再演示如何通过流水线安装Consul集群。

机器准备

3台腾讯云主机

确保防火墙允许内网中以下端口可访问

10.0.16.12、10.0.16.16、10.0.16.7

8300、8301、8302、8500、8600

手动安装

【步骤1】下载 Consul,解压到 /usr/bin

代码语言:javascript复制
wget https://releases.hashicorp.com/consul/1.11.4/consul_1.11.4_linux_amd64.zip

unzip consul_1.11.4_linux_amd64.zip
sudo mv consul /usr/bin/

【步骤2】创建配置文件和数据目录

创建 /etc/consul.d/consul.env 文件,内容为空

创建 /etc/consul.d/consul.hcl,内容从官方模板复制:

代码语言:javascript复制
# Full configuration options can be found at https://www.consul.io/docs/agent/options.html

# datacenter
# This flag controls the datacenter in which the agent is running. If not provided,
# it defaults to "dc1". Consul has first-class support for multiple datacenters, but 
# it relies on proper configuration. Nodes in the same datacenter should be on a 
# single LAN.
#datacenter = "my-dc-1"

# data_dir
# This flag provides a data directory for the agent to store state. This is required
# for all agents. The directory should be durable across reboots. This is especially
# critical for agents that are running in server mode as they must be able to persist
# cluster state. Additionally, the directory must support the use of filesystem
# locking, meaning some types of mounted folders (e.g. VirtualBox shared folders) may
# not be suitable.
data_dir = "/data/consul"

# client_addr
# The address to which Consul will bind client interfaces, including the HTTP and DNS
# servers. By default, this is "127.0.0.1", allowing only loopback connections. In
# Consul 1.0 and later this can be set to a space-separated list of addresses to bind
# to, or a go-sockaddr template that can potentially resolve to multiple addresses.
#client_addr = "0.0.0.0"

# ui
# Enables the built-in web UI server and the required HTTP routes. This eliminates
# the need to maintain the Consul web UI files separately from the binary.
# Version 1.10 deprecated ui=true in favor of ui_config.enabled=true
#ui_config{
#  enabled = true
#}

# server
# This flag is used to control if an agent is in server or client mode. When provided,
# an agent will act as a Consul server. Each Consul cluster must have at least one
# server and ideally no more than 5 per datacenter. All servers participate in the Raft
# consensus algorithm to ensure that transactions occur in a consistent, linearizable
# manner. Transactions modify cluster state, which is maintained on all server nodes to
# ensure availability in the case of node failure. Server nodes also participate in a
# WAN gossip pool with server nodes in other datacenters. Servers act as gateways to
# other datacenters and forward traffic as appropriate.
#server = true

# Bind addr
# You may use IPv4 or IPv6 but if you have multiple interfaces you must be explicit.
#bind_addr = "[::]" # Listen on all IPv6
#bind_addr = "0.0.0.0" # Listen on all IPv4
#
# Advertise addr - if you want to point clients to a different address than bind or LB.
#advertise_addr = "127.0.0.1"

# Enterprise License
# As of 1.10, Enterprise requires a license_path and does not have a short trial.
#license_path = "/etc/consul.d/consul.hclic"

# bootstrap_expect
# This flag provides the number of expected servers in the datacenter. Either this value
# should not be provided or the value must agree with other servers in the cluster. When
# provided, Consul waits until the specified number of servers are available and then
# bootstraps the cluster. This allows an initial leader to be elected automatically.
# This cannot be used in conjunction with the legacy -bootstrap flag. This flag requires
# -server mode.
#bootstrap_expect=3

# encrypt
# Specifies the secret key to use for encryption of Consul network traffic. This key must
# be 32-bytes that are Base64-encoded. The easiest way to create an encryption key is to
# use consul keygen. All nodes within a cluster must share the same encryption key to
# communicate. The provided key is automatically persisted to the data directory and loaded
# automatically whenever the agent is restarted. This means that to encrypt Consul's gossip
# protocol, this option only needs to be provided once on each agent's initial startup
# sequence. If it is provided after Consul has been initialized with an encryption key,
# then the provided key is ignored and a warning will be displayed.
#encrypt = "..."

# retry_join
# Similar to -join but allows retrying a join until it is successful. Once it joins 
# successfully to a member in a list of members it will never attempt to join again.
# Agents will then solely maintain their membership via gossip. This is useful for
# cases where you know the address will eventually be available. This option can be
# specified multiple times to specify multiple agents to join. The value can contain
# IPv4, IPv6, or DNS addresses. In Consul 1.1.0 and later this can be set to a go-sockaddr
# template. If Consul is running on the non-default Serf LAN port, this must be specified
# as well. IPv6 must use the "bracketed" syntax. If multiple values are given, they are
# tried and retried in the order listed until the first succeeds. Here are some examples:
#retry_join = ["consul.domain.internal"]
#retry_join = ["10.0.4.67"]
#retry_join = ["[::1]:8301"]
#retry_join = ["consul.domain.internal", "10.0.4.67"]
# Cloud Auto-join examples:
# More details - https://www.consul.io/docs/agent/cloud-auto-join
#retry_join = ["provider=aws tag_key=... tag_value=..."]
#retry_join = ["provider=azure tag_name=... tag_value=... tenant_id=... client_id=... subscription_id=... secret_access_key=..."]
#retry_join = ["provider=gce project_name=... tag_value=..."]

创建数据目录 /data/consul

【步骤3】创建启动服务

创建启动服务文件 /usr/lib/systemd/system/consul.service。这里配置为以 server 模式启动

代码语言:javascript复制
[Unit]
Description="HashiCorp Consul - A service mesh solution"
Documentation=https://www.consul.io/
Requires=network-online.target
After=network-online.target
ConditionFileNotEmpty=/etc/consul.d/consul.hcl

[Service]
EnvironmentFile=/etc/consul.d/consul.env
#User=consul
#Group=consul
ExecStart=/usr/bin/consul agent -server -ui -data-dir=/data/consul -config-dir=/etc/consul.d/ -bootstrap-expect=1
ExecReload=/bin/kill --signal HUP $MAINPID
KillMode=process
KillSignal=SIGTERM
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

启动服务

代码语言:javascript复制
sudo systemctl daemon-reload
sudo systemctl enable consul
sudo systemctl restart consul

查看 Consul 状态

代码语言:javascript复制
> consul members
Node             Address          Status  Type    Build   Protocol  DC   Partition  Segment
VM-16-12-ubuntu  10.0.16.12:8301  alive   server  1.11.4  2         dc1  default    <all>

这样 Consul Server 节点1就安装好了。重复在其他机器上安装好节点2、节点3

【步骤4】加入集群

在节点2、节点3上执行加入节点1的命令,组成集群

代码语言:javascript复制
> consul join 10.0.16.12
Successfully joined cluster by contacting 1 nodes.
> 
> consul members
Node             Address          Status  Type    Build   Protocol  DC   Partition  Segment
VM-16-12-ubuntu  10.0.16.12:8301  alive   server  1.11.4  2         dc1  default    <all>
VM-16-16-ubuntu  10.0.16.16:8301  alive   server  1.11.4  2         dc1  default    <all>
VM-16-7-ubuntu   10.0.16.7:8301   alive   server  1.11.4  2         dc1  default    <all>

这样,3节点的 Consul 集群就安装好了!

流水线安装

我已经将上面的步骤做成了流水线,使用的是Y20持续部署,以后有新机器就可以直接拿来用。流水线在安装Consul并加入集群 - Y20持续部署

流水线的步骤就是手动安装的步骤,配置文件也已经上传,但是,流水线有3个输入变量要说明一下:

  • AGENT:安装 Consul 的节点
  • AGENT_MODE:安装模式(服务端或客户端)
  • JOIN_AGENT:要加入的集群节点。如果为空,则不会加入集群

流水线需要在每个安装节点上运行,也就是运行3次。安装节点1时,不填 JOIN_AGENT,不需要加入其他集群。安装节点2、节点3时,JOIN_AGENT选择节点1,流水线会查询节点1的IP,并加入节点1的集群。

下面是演示视频:

视频内容

0 人点赞