项目作者: colinbut

项目描述 :
Consul example
高级语言:
项目地址: git://github.com/colinbut/consul-example.git
创建时间: 2018-10-21T21:44:44Z
项目社区:https://github.com/colinbut/consul-example

开源协议:

下载


Consul Example

This repo demonstrates the uses of Hashicorp’s Consul.

  1. Service Registry
  2. Health Checks
  3. Key-Value (K/V) store

Table of Contents

Intro" class="reference-link">Intro

Starting the Consul agent:" class="reference-link">Starting the Consul agent:

  1. consul agent

Starting in “Dev” mode:

  1. consul agent -dev

Querying Agents (Members)" class="reference-link">Querying Agents (Members)

From CLI:

  1. consul members --detailed

via HTTP API:

  1. curl localhost:8500/v1/catalog/nodes

default port of 8500 applies

or via DNS interface:

e.g.

  1. dig @127.0.0.1 -p 8600 ip-192-168-20-127.eu-west-1.compute.internal

“Note that you have to make sure to point your DNS lookups to the Consul agent’s DNS server which runs on port 8600 by default.”

Configuration Directory" class="reference-link">Configuration Directory

A ‘config’ directory for Consul. The directory stores:

  1. Service definition files
  2. HealthChecks definitions
  3. Sidecar Proxy definitions

This config directory is commonly stored under /etc/conf.d

Create the folder before starting up Consul.

e.g.

  1. sudo mkdir /etc/conf.d

Services" class="reference-link">Services

You define ‘services’ in a “Service Definition” file which is of JSON format. See this repo for samples.

You store the definition files in the Configuration Directory.

If Consul is already started up after you placed the ‘new’ Definition files in the Configuration Directory then issue either a Consul reload or SIGNUP

e.g.

  1. consul reload

Queries
e.g.

  1. curl http://localhost:8500/v1/catalog/service/web
  2. curl 'http://localhost:8500/v1/health/service/web?passing'

or via DNS:

  1. dig @127.0.0.1 -p 8600 rails.web.service.consul SRV

HealthChecks" class="reference-link">HealthChecks

  1. curl http://localhost:8500/v1/health/state/critical

or via DNS:

  1. dig @127.0.0.1 -p 8600 web.service.consul

Consul Connect" class="reference-link">Consul Connect

[TBC]

  1. consul connect proxy -sidecar-for socat
  2. consul connect proxy -service web -upstream socat:9191
  3. consul connect proxy -sidecar-for web

Controlling access with Intentions" class="reference-link">Controlling access with Intentions

Interactions in Consul are a way to specify whether a service can or cannot communicate with another service.

Deny

  1. consul intention create -deny web socat

Allow it again

  1. consul intention delete web socat

Cluster" class="reference-link">Cluster

Assumming you’re running more than one node and therefore have multiple nodes forming a cluster.

Start the Server & Client agents on the nodes" class="reference-link">Start the Server & Client agents on the nodes

One agent acts as the ‘Server’ so you start up the agent as the Server.

  1. consul agent -server -bootstrap-expect=1 \
  2. -data-dir=/tmp/consul -node=agent-one -bind=172.20.20.10 \
  3. -enable-script-checks=true -config-dir=/etc/consul.d

Another ‘member’ comes along to this cluster as the ‘Client’

  1. consul agent -data-dir=/tmp/consul -node=agent-two -bind=172.20.20.11 -enable-script-checks=true -config-dir=/etc/consul.d

Joining a cluster" class="reference-link">Joining a cluster

To join a cluster, run as the agent of any node and connect to one of the nodes already on the cluster by specifying that nodes’ IP address.

e.g.

  1. consul join 172.20.20.11

Query the nodes" class="reference-link">Query the nodes

Agent from one node querying another Agent from a second node.

e.g.

  1. dig @127.0.0.1 -p 8600 agent-two.node.consul

Leave a cluster" class="reference-link">Leave a cluster

  1. consul leave

K/V Store" class="reference-link">K/V Store

The Key/Value store.

Put

  1. consul kv put redis/config/minconns 1
  2. consul kv put redis/config/maxconns 25
  3. consul kv put -flags=42 redis/config/users/admin abcd1234

Get one

  1. consul kv get redis/config/minconns
  2. consul kv get -detailed redis/config/minconns

Get All

  1. consul kv get -recurse

Delete

  1. consul kv delete redis/config/minconns
  2. consul kv delete -recurse redis

Update

  1. consul kv put foo bar
  2. consul kv put foo zip

Atomic Update

  1. consul kv put -cas -modify-index=123 foo bar

Ui Web Console" class="reference-link">Ui Web Console

via the -ui flag on the CLI

  1. consul agent -ui -config-dir=/etc/consul.d

and then you can visit the /ui path on the HTTP port of 8500

e.g.

  1. http://localhost:8500/ui/

The official Demo Consul UI is located here: https://demo.consul.io/ui