前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >通过Y20流水线安装Consul集群

通过Y20流水线安装Consul集群

原创
作者头像
airxiechao
发布于 2022-04-14 10:43:03
发布于 2022-04-14 10:43:03
30500
代码可运行
举报
运行总次数:0
代码可运行

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
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
# 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
代码运行次数:0
运行
AI代码解释
复制
[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
代码运行次数:0
运行
AI代码解释
复制
sudo systemctl daemon-reload
sudo systemctl enable consul
sudo systemctl restart consul

查看 Consul 状态

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
> 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
代码运行次数:0
运行
AI代码解释
复制
> 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的集群。

下面是演示视频:

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
安装Consul集群
TIPS •本文基于Consul 1.5.3,理论适用于Consul 1.6及更低版本。•安装单机版Consul详见:《安装单机版Consul》
用户1516716
2019/12/23
1.7K0
学习搭建 Consul 服务发现与服务网格-有丰富的示例和图片
那么,我们对 Consul 的理解,就是服务网格、服务发现,官网文档说的这两个特征,到底是啥意思?跨什么云?
痴者工良
2021/04/26
9840
consul微服务治理中心踩坑
本工程完整演示了以consul为微服务治理中心的标准微服务架构各个基本模块功能,通过该项目能够完整了解微服务注册、发现、健康监测、负载均衡、全链路监控、配置中心、权限控制等。
IT运维技术圈
2022/06/26
9110
consul微服务治理中心踩坑
linux上安装consul 原
    官网上下载consul,下载地址是https://www.consul.io/downloads.html,根据不同操作系统选中不同的版本。
克虏伯
2019/04/15
1.6K0
linux上安装consul
                                                                            原
Consul 集群2
此时已经分别在104和103上启动了两个代理a1和a2,a1准备用来作server ,a2用来作client,但它们彼此还互不认识,都是自己的单节点集群中的唯一节点,可以通过 consul members 来进行查看
franket
2021/12/01
3840
微服务 - 搭建Consul集群服务,Consul配置中心
传统配置文件的弊端 静态化配置,例如env文件 配置文件无法区分环境 配置文件过于分散 历史版本无法查看 配置中心如何解决的呢?配置中心的思路是把项目中的配置参数全部放在一个集中的地方来管理,并提供一
stark张宇
2023/04/22
7820
Spring Cloud Consul入门 1. Consul介绍2. 安装3. 启动Consul4. 使用SpringCloud Consul组件
Consul是一套开源的分布式服务发现和配置管理系统,支持多数据中心分布式高可用。Consul是HashiCorp( Vagrant的创建者)开发的一个服务发现与配置项目,用Go语言开发,基于 Mozilla Public License 2.0 的协议开源。
mantou
2019/02/13
1.2K0
Spring Cloud Consul入门
		1. Consul介绍2. 安装3. 启动Consul4. 使用SpringCloud Consul组件
Consul部署
默认Policy:global-management,这个是拥有最高权限的SecretID,等于超级管理员
陳斯托洛夫斯記
2022/10/27
6960
Consul部署
微服务Consul系列之服务部署、搭建、使用
以上只是列举的笔者曾经遇到的几点问题,当然问题还不止于这些,下面介绍的Consul可以有效解决这些问题,当然还有一些其它的优点,让我们一起期待下文的Consul的讲解。
五月君
2019/07/12
1.6K0
微服务Consul系列之服务部署、搭建、使用
Kubernetes中Consul重启自动加入集群实践
近期频繁的容器母机调整导致我们的业务需要多次重启,不得不寻找一种自动重建Consul集群的方式。在网上搜索和学习一番后发现,基本没有针对Kubernetes容器环境的自动重建方案。
sherlock99
2018/08/02
1.8K0
Consul v1.18.0集群搭建
Consul 是一种用于服务发现、配置和分布式一致性的开源工具和平台。它由 HashiCorp 公司开发和维护,旨在简化构建和维护分布式系统的任务。
全栈工程师熊明才
2024/03/29
4383
Consul v1.18.0集群搭建
基于consul的Redis高可用方案
这几天在研究如何做Redis的高可用容灾方案,查询了资料和咨询DBA同行,了解到Redis可以基于consul和sentinel实现读写分离以及HA高可用方案。本文讲述基于consul的Redis高可用方案实践。
用户1278550
2018/08/09
3K0
Consul 启动命令,Web UI
-bootstrap:启动模式,此模式下,节点可以选举自己为leader,一个数据中心只能有一个此模式启动的节点。机群启动后,新启动的节点不建议使用这种模式。
WindWant
2020/09/11
4.6K0
微服务Consul系列之集群搭建
在上一篇中讲解了Consul的安装、部署、基本的使用,使得大家有一个基本的了解,本节开始重点Consul集群搭建,官方推荐3~5台Server,因为在异常处理中,如果出现Leader挂了,只要有超过一半的Server还处于活跃状态,consul就会重新选举新的Leader,保证集群可以正常工作。
五月君
2019/07/12
1.2K0
微服务Consul系列之集群搭建
Consul初探-从安装到运行
伟大领袖毛主席说过:实践是检验真理的唯一标准!经过上一篇的学习,我基本掌握了 Consul 的基本原理,接下来就是动手实践了;Consul 的部署方式分为两种,分别是二进制包和docker方式,这次就以二进制包的方式进行实验吧。
梁规晓
2019/07/09
7560
Consul初探-从安装到运行
Consul安装和初步使用
如果上述安装方式出现了consul not be found的错误,说明环境变量PATH没有配置正确,请返回检查consul安装路径是否包含在PATH中。
AsiaYe
2019/11/06
1.5K0
Consul安装和初步使用
Consul 入门教程
微服务的框架体系中,服务发现是不能不提的一个模块。我相信了解或者熟悉微服务的童鞋应该都知道它的重要性。这里我只是简单的提一下,毕竟这不是我们的重点。我们看下面的一幅图片:
菲宇
2019/06/12
48.1K1
Consul 入门教程
Consul 集群部署
默认Policy:global-management,这个是拥有最高权限的SecretID,等于超级管理员
陳斯托洛夫斯記
2024/08/07
760
Consul 集群部署
Prometheus 通过 consul 分布式集群实现自动服务发现
本次演示环境,我是在虚拟机上安装 Linux 系统来执行操作,以下是安装的软件及版本:
哎_小羊
2020/04/08
2.2K0
Prometheus 通过 consul 分布式集群实现自动服务发现
Consul 基础3
每一个数据中心必须有至少一个服务节点,3到5个服务节点最好,非常不建议只运行一个服务节点,因为在节点失效的情况下数据有极大的丢失风险
franket
2021/12/01
2670
相关推荐
安装Consul集群
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验