Separating consul from haproxy health checks. Consul doesn't care what the status of the service is so long as it's reachable. Don't take out bad nodes, let the software do that.

This commit is contained in:
Samuel Clay 2021-12-06 15:12:12 -05:00
parent 966f0998fc
commit ec33dfab48
7 changed files with 44 additions and 16 deletions

View file

@ -2,7 +2,8 @@
"datacenter": "nyc1", "datacenter": "nyc1",
"data_dir": "/opt/consul", "data_dir": "/opt/consul",
"log_level": "INFO", "log_level": "INFO",
"enable_syslog": false, "log_file": "/var/log/consul/consul.log",
"enable_syslog": true,
"retry_join": [{{ consul_manager_ip.stdout|trim }}], "retry_join": [{{ consul_manager_ip.stdout|trim }}],
"advertise_addr": "{% raw %}{{ GetAllInterfaces | include \"name\" \"^eth\" | include \"flags\" \"forwardable|up\" | attr \"address\" }}{% endraw %}", "advertise_addr": "{% raw %}{{ GetAllInterfaces | include \"name\" \"^eth\" | include \"flags\" \"forwardable|up\" | attr \"address\" }}{% endraw %}",
"bind_addr": "0.0.0.0", "bind_addr": "0.0.0.0",

View file

@ -9,14 +9,12 @@
become: yes become: yes
apt_repository: apt_repository:
repo: "deb [arch=amd64] https://apt.releases.hashicorp.com {{ ansible_distribution_release }} main" repo: "deb [arch=amd64] https://apt.releases.hashicorp.com {{ ansible_distribution_release }} main"
when: ansible_distribution_version != '14.04'
- name: Installing Consul - name: Installing Consul
become: yes become: yes
apt: apt:
pkg: consul pkg: consul
state: latest state: latest
when: ansible_distribution_version != '14.04'
- name: Register Manager IP - name: Register Manager IP
run_once: yes run_once: yes
@ -34,6 +32,13 @@
path: /etc/consul.d path: /etc/consul.d
state: directory state: directory
- name: Ensure /var/log/consul exists
become: yes
file:
path: /var/log/consul
state: directory
mode: 0777
- name: Remove default /etc/consul.d/consul.hcl - name: Remove default /etc/consul.d/consul.hcl
become: yes become: yes
copy: copy:

View file

@ -19,7 +19,7 @@
docker_container: docker_container:
pull: true pull: true
name: grafana name: grafana
image: grafana/grafana:7.5.7 image: grafana/grafana:8.2.6
restart_policy: unless-stopped restart_policy: unless-stopped
hostname: "{{ inventory_hostname }}" hostname: "{{ inventory_hostname }}"
user: root user: root

View file

@ -8,7 +8,7 @@
"port": 27017, "port": 27017,
"checks": [{ "checks": [{
"id": "mongo-analytics-ping", "id": "mongo-analytics-ping",
"http": "http://{{ ansible_ssh_host }}:5579/db_check/mongo_analytics", "http": "http://{{ ansible_ssh_host }}:5579/db_check/mongo_analytics?consul=1,
"interval": "15s" "interval": "15s"
}] }]
} }

View file

@ -7,7 +7,7 @@
"port": 5432, "port": 5432,
"checks": [{ "checks": [{
"id": "postgres-ping", "id": "postgres-ping",
"http": "{% if inventory_hostname.startswith('db-postgres') %}http://{{ ansible_ssh_host }}:5579/db_check/postgres{% else %}http://{{ ansible_ssh_host }}:5000/db_check/postgres{% endif %}", "http": "http://{{ ansible_ssh_host }}:5579/db_check/postgres?consul=1",
"interval": "15s", "interval": "15s",
"failures_before_critical": 4 "failures_before_critical": 4
}] }]

View file

@ -9,15 +9,15 @@
"checks": [{ "checks": [{
"id": "{{inventory_hostname}}-ping", "id": "{{inventory_hostname}}-ping",
{% if inventory_hostname == 'db-redis-story' %} {% if inventory_hostname == 'db-redis-story' %}
"http": "http://{{ ansible_ssh_host }}:5579/db_check/redis_story", "http": "http://{{ ansible_ssh_host }}:5579/db_check/redis_story?consul=1",
{% elif inventory_hostname == 'db-redis-user' %} {% elif inventory_hostname == 'db-redis-user' %}
"http": "http://{{ ansible_ssh_host }}:5579/db_check/redis_user", "http": "http://{{ ansible_ssh_host }}:5579/db_check/redis_user?consul=1",
{% elif inventory_hostname == 'db-redis-pubsub' %} {% elif inventory_hostname == 'db-redis-pubsub' %}
"http": "http://{{ ansible_ssh_host }}:5579/db_check/redis_pubsub", "http": "http://{{ ansible_ssh_host }}:5579/db_check/redis_pubsub?consul=1",
{% elif inventory_hostname == 'db-redis-sessions' %} {% elif inventory_hostname == 'db-redis-sessions' %}
"http": "http://{{ ansible_ssh_host }}:5579/db_check/redis_sessions", "http": "http://{{ ansible_ssh_host }}:5579/db_check/redis_sessions?consul=1",
{% else %} {% else %}
"http": "http://{{ ansible_ssh_host }}:5000/db_check/redis", "http": "http://{{ ansible_ssh_host }}:5000/db_check/redis?consul=1",
{% endif %} {% endif %}
"interval": "15s", "interval": "15s",
"failures_before_critical": 4 "failures_before_critical": 4

View file

@ -25,11 +25,14 @@ SECONDARY_STATE = 2
@app.route("/db_check/postgres") @app.route("/db_check/postgres")
def db_check_postgres(): def db_check_postgres():
if request.args.get('consul') == '1':
return str(1)
connect_params = "dbname='%s' user='%s' password='%s' host='%s' port='%s'" % ( connect_params = "dbname='%s' user='%s' password='%s' host='%s' port='%s'" % (
settings.DATABASES['default']['NAME'], settings.DATABASES['default']['NAME'],
settings.DATABASES['default']['USER'], settings.DATABASES['default']['USER'],
settings.DATABASES['default']['PASSWORD'], settings.DATABASES['default']['PASSWORD'],
'db-postgres.service.nyc1.consul', f'{settings.SERVER_NAME}.node.nyc1.consul',
settings.DATABASES['default']['PORT'], settings.DATABASES['default']['PORT'],
) )
try: try:
@ -48,6 +51,9 @@ def db_check_postgres():
@app.route("/db_check/mysql") @app.route("/db_check/mysql")
def db_check_mysql(): def db_check_mysql():
if request.args.get('consul') == '1':
return str(1)
connect_params = "dbname='%s' user='%s' password='%s' host='%s' port='%s'" % ( connect_params = "dbname='%s' user='%s' password='%s' host='%s' port='%s'" % (
settings.DATABASES['default']['NAME'], settings.DATABASES['default']['NAME'],
settings.DATABASES['default']['USER'], settings.DATABASES['default']['USER'],
@ -76,12 +82,13 @@ def db_check_mysql():
@app.route("/db_check/mongo") @app.route("/db_check/mongo")
def db_check_mongo(): def db_check_mongo():
try:
# The `mongo` hostname below is a reference to the newsblurnet docker network, where 172.18.0.0/16 is defined
client = pymongo.MongoClient(f"mongodb://{settings.MONGO_DB['username']}:{settings.MONGO_DB['password']}@{settings.SERVER_NAME}/?authSource=admin")
db = client.newsblur
if request.args.get('consul') == '1': if request.args.get('consul') == '1':
return str(1) return str(1)
try:
# The `mongo` hostname below is a reference to the newsblurnet docker network, where 172.18.0.0/16 is defined
client = pymongo.MongoClient(f"mongodb://{settings.MONGO_DB['username']}:{settings.MONGO_DB['password']}@{settings.SERVER_NAME}.node.nyc1.consul/?authSource=admin")
db = client.newsblur
except: except:
abort(503) abort(503)
@ -122,6 +129,9 @@ def db_check_mongo():
@app.route("/db_check/mongo_analytics") @app.route("/db_check/mongo_analytics")
def db_check_mongo_analytics(): def db_check_mongo_analytics():
if request.args.get('consul') == '1':
return str(1)
try: try:
client = pymongo.MongoClient(f"mongodb://{settings.MONGO_ANALYTICS_DB['username']}:{settings.MONGO_ANALYTICS_DB['password']}@{settings.SERVER_NAME}/?authSource=admin") client = pymongo.MongoClient(f"mongodb://{settings.MONGO_ANALYTICS_DB['username']}:{settings.MONGO_ANALYTICS_DB['password']}@{settings.SERVER_NAME}/?authSource=admin")
db = client.nbanalytics db = client.nbanalytics
@ -144,6 +154,9 @@ def db_check_mongo_analytics():
@app.route("/db_check/redis_user") @app.route("/db_check/redis_user")
def db_check_redis_user(): def db_check_redis_user():
if request.args.get('consul') == '1':
return str(1)
try: try:
r = redis.Redis('db-redis-user.service.nyc1.consul', db=0) r = redis.Redis('db-redis-user.service.nyc1.consul', db=0)
except: except:
@ -161,6 +174,9 @@ def db_check_redis_user():
@app.route("/db_check/redis_story") @app.route("/db_check/redis_story")
def db_check_redis_story(): def db_check_redis_story():
if request.args.get('consul') == '1':
return str(1)
try: try:
r = redis.Redis('db-redis-story.service.nyc1.consul', db=1) r = redis.Redis('db-redis-story.service.nyc1.consul', db=1)
except: except:
@ -178,6 +194,9 @@ def db_check_redis_story():
@app.route("/db_check/redis_sessions") @app.route("/db_check/redis_sessions")
def db_check_redis_sessions(): def db_check_redis_sessions():
if request.args.get('consul') == '1':
return str(1)
try: try:
r = redis.Redis('db-redis-sessions.service.nyc1.consul', db=5) r = redis.Redis('db-redis-sessions.service.nyc1.consul', db=5)
except: except:
@ -195,6 +214,9 @@ def db_check_redis_sessions():
@app.route("/db_check/redis_pubsub") @app.route("/db_check/redis_pubsub")
def db_check_redis_pubsub(): def db_check_redis_pubsub():
if request.args.get('consul') == '1':
return str(1)
try: try:
r = redis.Redis('db-redis-pubsub.service.nyc1.consul', db=1) r = redis.Redis('db-redis-pubsub.service.nyc1.consul', db=1)
except: except: