centos搭建consul高可用集羣

一、概念

    consul是spring cloud的服務註冊組件

二、單點搭建

    下載consol安裝包,下載地址爲https://releases.hashicorp.com/consul/

    將.zip(consul_1.7.2_linux_amd64.zip)文件放置到需要安裝的目錄下

    使用unzip命令解壓.zip文件

    將解壓後的文件(consul)拷貝至指定目錄下

    使用./consul 命令查看是否安裝成功

 

    若出現上方圖片中的內容,則說明consul安裝成功

    使用./consul agent -dev -ui -node=consul-dev -client=0.0.0.0

    參數說明:

        -dev 表示以開發模式啓動,如在生產環境啓動,則使用-server

        -ui 表示webui的路徑,用web來管理consul

        -node=consul-dev 表示該服務器節點名

        -net=host docker參數, 使得docker容器越過了net namespace的隔離,免去手動指定端口映射的步驟

        -advertise 將本機私有IP傳遞到consul

        -bootstrap-expect 指定consul將等待幾個節點連通,成爲一個完整的集羣

        -retry-join 指定要加入的consul節點地址,失敗會重試, 可多次指定不同的地址

        -client consul綁定在哪個client地址上,這個地址提供HTTP、DNS、RPC等服務,默認是127.0.0.1

        -bind 該地址用來在集羣內部的通訊,集羣內的所有節點到地址都必須是可達的,默認是0.0.0.0

        allow_stale 設置爲true, 表明可以從consul集羣的任一server節點獲取dns信息, false則表明每次請求都會經過consul server leader

 

若出現上方圖片中的內容,則說明consul啓動成功

三、集羣搭建

1、集羣搭建

    IP地址規劃

        192.168.47.3          consul_server_1

        192.168.47.4          consul_server_2

        192.168.47.5          consul_server_3

        192.168.47.6          consul_client_1

    在四臺服務器上分別在/opt/spring-cloud-consul目錄創建名爲consul_config.json的文件,內容如下

192.168.47.3

{

    "bootstrap_expect": 1,

    "datacenter": "cangzhou_consul",

    "data_dir": "/opt/consul-data",

    "node_name": "consul_server_1",

    "server": true,

    "client_addr": "0.0.0.0",

    "ui": true,

    "bind_addr": "192.168.47.3",

    "primary_datacenter": "cangzhou_consul",

    "acl": {

        "enabled": true,

        "default_policy": "allow",

        "enable_token_persistence": true,

        "tokens": {

            "master": "8dc1eb67-1f5f-4e10-ad9d-5e58b047647c"

        }

    }

}

192.168.47.4

{

    "datacenter": "cangzhou_consul",

    "data_dir": "/opt/consul-data",

    "node_name": "consul_server_2",

    "server": true,

    "client_addr": "0.0.0.0",

    "ui": true,

    "bind_addr": "192.168.47.4",

    "start_join":["192.168.47.3","192.168.47.4","192.168.47.5"],

    "retry_join":["192.168.47.3","192.168.47.4","192.168.47.5"],

    "primary_datacenter": "cangzhou_consul",

    "acl": {

        "enabled": true,

        "default_policy": "allow",

        "enable_token_persistence": true,

        "tokens": {

            "master": "8dc1eb67-1f5f-4e10-ad9d-5e58b047647c"

        }

    }

}

192.168.47.5

{

    "datacenter": "cangzhou_consul",

    "data_dir": "/opt/consul-data",

    "node_name": "consul_server_3",

    "server": true,

    "client_addr": "0.0.0.0",

    "ui": true,

    "bind_addr": "192.168.47.5",

    "start_join":["192.168.47.3","192.168.47.4","192.168.47.5"],

    "retry_join":["192.168.47.3","192.168.47.4","192.168.47.5"],

    "primary_datacenter": "cangzhou_consul",

    "acl": {

        "enabled": true,

        "default_policy": "allow",

        "enable_token_persistence": true,

        "tokens": {

            "master": "8dc1eb67-1f5f-4e10-ad9d-5e58b047647c"

        }

    }

}

192.168.47.6

{

    "datacenter": "cangzhou_consul",

    "data_dir": "/opt/consul-data",

    "node_name": "consul_client_1",

    "server": false,

    "client_addr": "0.0.0.0",

    "ui": true,

    "bind_addr": "192.168.47.6",

    "start_join":["192.168.47.3","192.168.47.4","192.168.47.5"],

    "retry_join":["192.168.47.3","192.168.47.4","192.168.47.5"],

    "primary_datacenter": "cangzhou_consul"

}

啓動consul

    在192.168.47.3、192.168.47.4、192.168.47.5,分別運行指令:consul agent -config-file /opt/spring-cloud-consul/consul_config.json

待這三臺都上線後,再在192.168.47.6上運行指令:consul agent -config-file /opt/spring-cloud-consul/consul_config.json

 

可以看到已有4個nodes

2、配置acl

    使用192.168.47.3、192.168.47.4、192.168.47.5的/opt/spring-cloud-consul/consul.config.json文件中的acl.token.master中的內容,登錄acl

 

    創建policy(在ui上創建)

 

    用上一步創建的policy創建一個token(在ui上創建)

 

    查看token值(在ui上查看)

 

拿到token值:2a06e1da-1ead-d783-33c5-845e011a96d4

修改服務端配置文件中的acl配置項,將"tokens"節點新增"agent"值 

"acl": {

    "enabled": true,

    "default_policy": "allow",

    "enable_token_persistence": true,

    "tokens": {

        "master": "8dc1eb67-1f5f-4e10-ad9d-5e58b047647c",

        "agent":"2a06e1da-1ead-d783-33c5-845e011a96d4"

    }

}

修改客戶端配置文件,新增acl配置

"acl":{

    "tokens":{

        "agent":"2a06e1da-1ead-d783-33c5-845e011a96d4"

    }

}

重新啓動consul,加載新的配置文件

3、添加服務註冊token

沒配置ACL之前默認策略爲allow,可以任意進行服務註冊,配置acl後,可以添加一個用於服務註冊的token,某個服務要註冊到consul,必須帶上這個token

先添加一個policy(在ui上添加)

 

用上一步創建的policy創建一個token(在ui上創建)

 

查看token值(在ui上查看)

 

4、服務註冊

在微服務的筆記中講到

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章