多种方式安装与使用Consul

2023年 10月 4日 57.8k 0

前言

使用了英中文相结合的方式, 可能会导致阅读不太习惯

Install

Divided into binary installation and image installation, you can choose any way to install according to your preferences

Binary

  • click hashicorp to download your os consul package
  • unzip consul and added to system environment variables
  • if you use linux os you can use this command to unzip consul

    Apt package manager: apt install unzip -y

    Yum package manager: yum install unzip -y

    Dnf package manager: dnf install unzip -y

    unzip consul.zip
    

    Image package

    this used hashicorp/consul image

    docker-compose.yml

    services:
      # consul 注册中心、配置中心
      consul:
        image: hashicorp/consul:1.16.1
        container_name: consul-stand-alone
        ports:
          - 8500:8500
        command:
          - agent
          - -server
          - -ui
          - -node=server-1
          - -bootstrap-expect=1
          - -client=0.0.0.0
    

    Use

    Stand-alone

    Binary

    dev mode

    consul agent -dev -ui
    

    Docker

    docker-compose up -d
    

    Cluster

    An example: a cluster of 3 servers with UI enable

    click Github
    check consul-server-node1.hcl, consul-server-node2.hcl, consul-server-node3.hcl for more details
    你需要根据你的实际情况来修改consul-server-node{1,2,3}.hcl的文件内容, 文件的字段说明如下:

    根据你的需要, 参阅官方文档配置文件, 根据你的实际需求来添加修改

    • node_name: 节点名称
    • bind_addr: 绑定地址, 一般是本机IP
    • bootstrap_expect: 集群中server节点的数量, 一般是3,5,7等奇数数量
    • retry_join: 集群中server节点的地址, 除了本机的地址, 其他server节点的地址都需要填写
    • datacenter: 数据中心名称, 默认是dc1
    • data_dir: 数据存储目录
    • log_file: 日志文件路径
    • log_level: 日志级别, 一般是INFO
    • ui_config: Web UI选项
      • enabled: 是否启用Web UI, 默认是true
      • content_path : Web UI的路径, 默认是/ui/
    • addresses: http和dns的监听地址
    • ports: http和dns的监听端口, 默认是8500

    used:

    • nohup: no hang up
    • -config-file: config file path
    • & : run in background

    server node1:

    nohup consul agent -config-file=/home/data/consul/consul.d/consul-server-node1.hcl &
    

    server node2:

    nohup consul agent -config-file=/home/data/consul/consul.d/consul-server-node2.hcl &
    

    server node3:

    nohup consul agent -config-file=/home/data/consul/consul.d/consul-server-node3.hcl &
    

    OR

    你可能更喜欢命令行的方式:

    server node1:

    consul agent -server -bootstrap-expect 3 -data-dir /tmp/consul -node=node_158 -bind=192.168.0.158 -rejoin -config-dir=/home/data/consul/consul.d/ -client 0.0.0.0
    

    server node2:

    consul agent -server -bootstrap-expect 3 -data-dir /tmp/consul -node=node_152 -bind=192.168.0.152 -rejoin -config-dir=/home/data/consul/consul.d/ -client 0.0.0.0
    

    server node3:

    consul agent -server -bootstrap-expect 3 -data-dir /tmp/consul -node=node_155 -bind=192.168.0.155 -rejoin -config-dir=/home/data/consul/consul.d/ -client 0.0.0.0
    

    Docker

    TODO, 与上面的cluster的docker-compose.yml类似, 只是需要修改command的内容

    Client

    Golang

  • install package

    go mod tidy
    
  • run test

    go run consul_test.go
    
  • run main

    go run main.go
    
  • check consul
    visited link http://localhost:8080/health to check consul health if you can see the network status code is 200 then consul is ok

  • 后言

    后续可能会添加Kubernetes的安装方式, 与更多在golang应用上的使用

    Github Repo: github.com/lisa-sum/co…

    参考

    1.developer.hashicorp.com/consul/docs…

    2.developer.hashicorp.com/consul/docs…

  • www.cnblogs.com/sunsky303/p…
  • 相关文章

    服务器端口转发,带你了解服务器端口转发
    服务器开放端口,服务器开放端口的步骤
    产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
    如何使用 WinGet 下载 Microsoft Store 应用
    百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
    百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

    发布评论