{"id":328724,"date":"2022-01-28T15:00:41","date_gmt":"2022-01-28T15:00:41","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=328724"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=328724","title":{"rendered":"<span>\u041f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u043e\u0432 \u043a primary \u043d\u043e\u0434\u0435 patroni \u0438\u043b\u0438 \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c taints, labels \u0432 k8s<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0438\u0437\u043a\u0430\u044f \u0441\u0435\u0442\u0435\u0432\u0430\u044f \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043e\u043c \u0438 \u0431\u0430\u0437\u043e\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0431\u043e\u043b\u044c\u044e \u0434\u043b\u044f fintech, \u0435\u0441\u043b\u0438 \u0432\u044b \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0435\u0442\u0435 kubernetes \u0438 patroni \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430 bare metal. \u0420\u0430\u0437\u0440\u044b\u0432 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0441\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0441\u043c\u0435\u043d\u0430 primary node patroni \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043a\u0435\u0439\u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u0432 \u0436\u0438\u0437\u043d\u0438 ops, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u043a\u043e\u0433\u0434\u0430 \u0433\u043e\u0440\u044f\u0442 \u043a\u0440\u0443\u043f\u043d\u044b\u0435 \u0434\u0430\u0442\u0430\u0446\u0435\u043d\u0442\u0440\u044b <s>\u0438\u043b\u0438 \u0443\u0431\u043e\u0440\u0449\u0438\u0446\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0437\u0430\u0434\u0435\u043b\u0430 \u0448\u0432\u0430\u0431\u0440\u043e\u0439 \u043a\u0430\u0431\u0435\u043b\u044c \u043f\u0438\u0442\u0430\u043d\u0438\u044f<\/s>. \u0418 \u0442\u0443\u0442 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/scheduling-eviction\/taint-and-toleration\/\" rel=\"noopener noreferrer nofollow\">taints<\/a>, <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/overview\/working-with-objects\/labels\/\" rel=\"noopener noreferrer nofollow\">labels<\/a> \u0438\u0437 kubernetes.<\/p>\n<p>\u0418\u0442\u0430\u043a \u0447\u0442\u043e \u043c\u044b \u0438\u043c\u0435\u0435\u043c:<\/p>\n<ol>\n<li>\n<p>\u0433\u0435\u043e\u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0442\u0435\u0440 kubernetes<\/p>\n<\/li>\n<li>\n<p>\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u043e\u0432 \u0441 \u043d\u0430\u0448\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <em>\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e<\/em> \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0438 network latency \u0431\u043b\u0438\u0437\u043a\u043e\u0435 \u043a 20ms<\/p>\n<\/li>\n<li>\n<p>\u0433\u0435\u043e\u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0442\u0435\u0440 patroni \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u0447\u0430\u0441\u0438\u043a\u0438, \u043d\u043e \u043a\u0442\u043e \u0437\u043d\u0430\u0435\u0442 \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437.<\/p>\n<\/li>\n<\/ol>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0432\u0437\u0430\u0445\u043b\u0435\u0431 \u043e\u0431\u0447\u0438\u0442\u0430\u0432\u0448\u0438\u0441\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, \u043c\u0430\u043d\u0443\u0430\u043b\u043e\u0432 \u0438 stackoverflow, \u044f \u043f\u0440\u0438\u0448\u0435\u043b \u043a \u0432\u044b\u0432\u043e\u0434\u0443 \u0447\u0442\u043e taint \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u043e\u0434\u043e\u0432, \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0432\u044b\u0441\u0435\u043b\u0435\u043d\u0438\u044f \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0435\u0441\u043b\u0438 \u043d\u0430\u043b\u0435\u0442\u0443 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c taint \u0438 label. \u0418 \u0431\u044b\u043b\u043e \u0440\u0435\u0448\u0435\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 <s>\u043a\u043e\u0441\u0442\u044b\u043b\u044c<\/s> \u0441\u0435\u0440\u0432\u0438\u0441.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u044f \u0438\u043c\u0435\u044e \u043a\u0430\u043a\u043e\u0439 \u043b\u0438\u0431\u043e \u043e\u043f\u044b\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043d\u0430 python \u044f \u0432\u044b\u0431\u0440\u0430\u043b \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u0433\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u0431\u0435\u0437 \u043c\u0443\u043a. \u041b\u043e\u0433\u0438\u043a\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/daemonset\/\" rel=\"noopener noreferrer nofollow\">daemonset<\/a>, \u0433\u0434\u0435 <a href=\"https:\/\/kubernetes.io\/docs\/tasks\/configure-pod-container\/configure-liveness-readiness-startup-probes\/\" rel=\"noopener noreferrer nofollow\">livenes probe<\/a> \u044d\u0442\u043e \u0440\u0443\u0447\u043a\u0430 \u043c\u0435\u0440\u044f\u044e\u0449\u0430\u044f \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0443 \u0434\u043e \u043c\u0430\u0441\u0442\u0435\u0440 \u043d\u043e\u0434\u044b \u043f\u0430\u0442\u0440\u043e\u043d\u0438 (\u0441 \u043e\u043a\u043d\u043e\u043c 10 icmp \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432), \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 redis, \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0448\u0430\u0435\u043c \u0442\u0435\u0433\u0438 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0431\u043b\u0435\u0441\u0442\u044f\u0449\u0438\u0439 rollout restart<\/p>\n<h3>Discover primary node<\/h3>\n<p>\u041d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0433\u043e \u043b\u0438\u0431\u043e \u043d\u0435\u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0442\u0430\u043a \u043a\u0430\u043a patroni \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0435\u0440\u0432\u0438\u0441 \u043d\u0430 8008 \u043f\u043e\u0440\u0442\u0443, \u043e\u0442\u0434\u0430\u044e\u0449\u0438\u0439 \u0441\u0442\u0430\u0442\u0443\u0441 <em>200<\/em> \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043c\u0430\u0441\u0442\u0435\u0440 \u0438 <em>503<\/em> \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0440\u0435\u043f\u043b\u0438\u043a\u0430.<\/p>\n<pre><code class=\"python\">import requests from flask import Blueprint, Response from json import dumps, loads  from app.constants import *  load_dotenv(find_dotenv()) main = Blueprint('main', name) redis_conn = REDIS_CONN  @main.route('\/primary') def discover_primary():     res = RESPONSE     for node in PATRONI_HOSTS:         r = requests.get(f'{getenv(\"PATRONI_SCHEMA\")}{node}:{getenv(\"PATRONI_PORT\")}')         if r.status_code == 200:             res['data']['node'] = node             res['message'] = 'successful'             redis_conn.set('primary', node)             return Response(dumps(res), status=200, mimetype='application\/json')     res['error'] = 1     res['message'] = 'primary not found'     return Response(dumps(res), status=500, mimetype='application\/json') <\/code><\/pre>\n<h3>Liveness probe<\/h3>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0438 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u043c\u0435\u0440 \u043f\u0438\u043d\u0433\u043e\u0432 \u0434\u043e \u043c\u0430\u0441\u0442\u0435\u0440 \u043d\u043e\u0434\u044b \u043f\u0430\u0442\u0440\u043e\u043d\u0438<\/p>\n<pre><code class=\"python\">#... from ping3 import ping  @main.route('\/liveness-probe') def liveness_probe():     res = RESPONSE     res['data']['node'] = NODE     res['data']['round-trip'] = 0     result_pings = []      for _ in range(COUNT_PING):         single_ping = ping(loads(discover_master().data)['data']['node'], unit='ms')         if single_ping:             result_pings.append(single_ping)     try:         res['data']['round-trip'] = str(sum(result_pings) \/ len(result_pings))         res['message'] = 'successful'     except ZeroDivisionError:         res['message'] = 'something was going on (ping)'     redis_conn.set(NODE, res['data']['round-trip'])     return Response(dumps(res), status=200, mimetype='application\/json') <\/code><\/pre>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"python\">@main.route('\/') def init():     primary = redis_conn.get('primary_host')     for node in KUBE_NODES:         if redis_conn.get(node):             pass         else:             redis_conn.set(node, 0)     if primary is None:         redis_conn.set('primary_host', 'localhost')     return \"It's a Switchover for patroni and kubernetes\" <\/code><\/pre>\n<p><strong>Cronjob \u0438\u043b\u0438 \u043a\u0430\u043a \u043c\u043e\u0434\u043d\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440<\/strong>:<\/p>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 cronjob \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e \u0441\u0443\u0442\u0438 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0441\u0442\u0435\u0439\u0442\u043e\u043c \u043d\u0430\u0448\u0438\u0445 taints, label \u0438 \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0442\u044d\u0433\u0438, \u043b\u0435\u0439\u0431\u043b\u044b \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044f [far,intermediate,closely]<\/p>\n<pre><code class=\"python\">import subprocess  from app.constants import * from app.main.views import init   redis_conn = REDIS_CONN check = dict() min_ping = MIN_PING max_ping = MAX_PING  try:     current_db_master = redis_conn.get('primary').decode()     pre_current_db_master = redis_conn.get('primary_host').decode() except BaseException as error:     init()     current_db_master = redis_conn.get('primary').decode()     pre_current_db_master = redis_conn.get('primary_host').decode()   def subprocess_wrapper(command):     return subprocess.run(         command, text=True, check=False,         capture_output=True     )   for node in KUBE_NODES:     value = redis_conn.get(node)     if value:         check[node] = int(float(value.decode()))  for _, value in check.items():     max_ping = value if max_ping &lt; value else max_ping     min_ping = value if min_ping > value else min_ping  for node in KUBE_NODES:     if check.get(node) in range(0, min_ping + WINDOW_PING):         subprocess_wrapper(             ['kubectl', 'taint', 'nodes', node, 'switchover:NoExecute', '--overwrite']         )         subprocess_wrapper(             ['kubectl', 'label', 'nodes', node, 'switchover=closely', '--overwrite']         )     elif check.get(node) in range(max_ping - WINDOW_PING, max_ping + WINDOW_PING):         subprocess_wrapper(             ['kubectl', 'taint', 'nodes', node, 'switchover:NoExecute-']         )         subprocess_wrapper(             ['kubectl', 'label', 'nodes', node, 'switchover=far', '--overwrite']         )     else:         subprocess_wrapper(             ['kubectl', 'taint', 'nodes', node, 'switchover:NoExecute-']         )         subprocess_wrapper(             ['kubectl', 'label', 'nodes', node, 'switchover=intermediate', '--overwrite']         )  if pre_current_db_master != current_db_master:     for app in APPS:         subprocess_wrapper(             ['kubectl', 'rollout', 'restart', app]         )     redis_conn.set('primary_host', current_db_master) <\/code><\/pre>\n<p>\u042f \u043d\u0435 \u043f\u0440\u0435\u0442\u0435\u043d\u0434\u0443\u044e \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0447\u0435\u0433\u043e \u043b\u0438\u0431\u043e \u043d\u043e\u0432\u043e\u0433\u043e, \u043c\u0435\u043d\u044f \u043c\u0443\u0447\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441, \u043a\u0430\u043a \u043e\u043f\u0441\u044b \u0432 \u043d\u0430\u0448\u0435 \u0432\u0440\u0435\u043c\u044f \u0440\u0435\u0448\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443. \u041c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u0443\u0442 \u043d\u0430\u0439\u0434\u0443\u0442\u0441\u044f \u043b\u044e\u0434\u0438 \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0431\u043e\u043b\u044c\u044e \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u043b\u0443\u0447\u0448\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c. \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043a\u0430\u043a \u043b\u044e\u0434\u0438 \u0440\u0435\u0448\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u0435\u0439\u0441.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/648389\/\"> https:\/\/habr.com\/ru\/post\/648389\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0438\u0437\u043a\u0430\u044f \u0441\u0435\u0442\u0435\u0432\u0430\u044f \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043e\u043c \u0438 \u0431\u0430\u0437\u043e\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0431\u043e\u043b\u044c\u044e \u0434\u043b\u044f fintech, \u0435\u0441\u043b\u0438 \u0432\u044b \u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u0435\u0442\u0435 kubernetes \u0438 patroni \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430 bare metal. \u0420\u0430\u0437\u0440\u044b\u0432 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u0441\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0441\u043c\u0435\u043d\u0430 primary node patroni \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043a\u0435\u0439\u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u0432 \u0436\u0438\u0437\u043d\u0438 ops, \u043a \u043f\u0440\u0438\u043c\u0435\u0440\u0443 \u043a\u043e\u0433\u0434\u0430 \u0433\u043e\u0440\u044f\u0442 \u043a\u0440\u0443\u043f\u043d\u044b\u0435 \u0434\u0430\u0442\u0430\u0446\u0435\u043d\u0442\u0440\u044b <s>\u0438\u043b\u0438 \u0443\u0431\u043e\u0440\u0449\u0438\u0446\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0437\u0430\u0434\u0435\u043b\u0430 \u0448\u0432\u0430\u0431\u0440\u043e\u0439 \u043a\u0430\u0431\u0435\u043b\u044c \u043f\u0438\u0442\u0430\u043d\u0438\u044f<\/s>. \u0418 \u0442\u0443\u0442 \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/scheduling-eviction\/taint-and-toleration\/\" rel=\"noopener noreferrer nofollow\">taints<\/a>, <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/overview\/working-with-objects\/labels\/\" rel=\"noopener noreferrer nofollow\">labels<\/a> \u0438\u0437 kubernetes.<\/p>\n<p>\u0418\u0442\u0430\u043a \u0447\u0442\u043e \u043c\u044b \u0438\u043c\u0435\u0435\u043c:<\/p>\n<ol>\n<li>\n<p>\u0433\u0435\u043e\u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0442\u0435\u0440 kubernetes<\/p>\n<\/li>\n<li>\n<p>\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u043e\u0432 \u0441 \u043d\u0430\u0448\u0438\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <em>\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e<\/em> \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0438 network latency \u0431\u043b\u0438\u0437\u043a\u043e\u0435 \u043a 20ms<\/p>\n<\/li>\n<li>\n<p>\u0433\u0435\u043e\u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0442\u0435\u0440 patroni \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u043a \u0447\u0430\u0441\u0438\u043a\u0438, \u043d\u043e \u043a\u0442\u043e \u0437\u043d\u0430\u0435\u0442 \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u043e\u0439\u0442\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437.<\/p>\n<\/li>\n<\/ol>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0432\u0437\u0430\u0445\u043b\u0435\u0431 \u043e\u0431\u0447\u0438\u0442\u0430\u0432\u0448\u0438\u0441\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, \u043c\u0430\u043d\u0443\u0430\u043b\u043e\u0432 \u0438 stackoverflow, \u044f \u043f\u0440\u0438\u0448\u0435\u043b \u043a \u0432\u044b\u0432\u043e\u0434\u0443 \u0447\u0442\u043e taint \u043e\u0442\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u043e\u0434\u043e\u0432, \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0432\u044b\u0441\u0435\u043b\u0435\u043d\u0438\u044f \u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0435\u0441\u043b\u0438 \u043d\u0430\u043b\u0435\u0442\u0443 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c taint \u0438 label. \u0418 \u0431\u044b\u043b\u043e \u0440\u0435\u0448\u0435\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 <s>\u043a\u043e\u0441\u0442\u044b\u043b\u044c<\/s> \u0441\u0435\u0440\u0432\u0438\u0441.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u044f \u0438\u043c\u0435\u044e \u043a\u0430\u043a\u043e\u0439 \u043b\u0438\u0431\u043e \u043e\u043f\u044b\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043d\u0430 python \u044f \u0432\u044b\u0431\u0440\u0430\u043b \u0438\u043c\u0435\u043d\u043d\u043e \u0435\u0433\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434 \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u0431\u0435\u0437 \u043c\u0443\u043a. \u041b\u043e\u0433\u0438\u043a\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0434\u0435\u043b\u0430\u0435\u043c <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/workloads\/controllers\/daemonset\/\" rel=\"noopener noreferrer nofollow\">daemonset<\/a>, \u0433\u0434\u0435 <a href=\"https:\/\/kubernetes.io\/docs\/tasks\/configure-pod-container\/configure-liveness-readiness-startup-probes\/\" rel=\"noopener noreferrer nofollow\">livenes probe<\/a> \u044d\u0442\u043e \u0440\u0443\u0447\u043a\u0430 \u043c\u0435\u0440\u044f\u044e\u0449\u0430\u044f \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0443 \u0434\u043e \u043c\u0430\u0441\u0442\u0435\u0440 \u043d\u043e\u0434\u044b \u043f\u0430\u0442\u0440\u043e\u043d\u0438 (\u0441 \u043e\u043a\u043d\u043e\u043c 10 icmp \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432), \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 redis, \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0448\u0430\u0435\u043c \u0442\u0435\u0433\u0438 \u0438 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0431\u043b\u0435\u0441\u0442\u044f\u0449\u0438\u0439 rollout restart<\/p>\n<h3>Discover primary node<\/h3>\n<p>\u041d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u043a\u0430\u043a\u043e\u0433\u043e \u043b\u0438\u0431\u043e \u043d\u0435\u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0442\u0430\u043a \u043a\u0430\u043a patroni \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0435\u0440\u0432\u0438\u0441 \u043d\u0430 8008 \u043f\u043e\u0440\u0442\u0443, \u043e\u0442\u0434\u0430\u044e\u0449\u0438\u0439 \u0441\u0442\u0430\u0442\u0443\u0441 <em>200<\/em> \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043c\u0430\u0441\u0442\u0435\u0440 \u0438 <em>503<\/em> \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u0440\u0435\u043f\u043b\u0438\u043a\u0430.<\/p>\n<pre><code class=\"python\">import requests from flask import Blueprint, Response from json import dumps, loads  from app.constants import *  load_dotenv(find_dotenv()) main = Blueprint('main', name) redis_conn = REDIS_CONN  @main.route('\/primary') def discover_primary():     res = RESPONSE     for node in PATRONI_HOSTS:         r = requests.get(f'{getenv(\"PATRONI_SCHEMA\")}{node}:{getenv(\"PATRONI_PORT\")}')         if r.status_code == 200:             res['data']['node'] = node             res['message'] = 'successful'             redis_conn.set('primary', node)             return Response(dumps(res), status=200, mimetype='application\/json')     res['error'] = 1     res['message'] = 'primary not found'     return Response(dumps(res), status=500, mimetype='application\/json') <\/code><\/pre>\n<h3>Liveness probe<\/h3>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0438 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u043c\u0435\u0440 \u043f\u0438\u043d\u0433\u043e\u0432 \u0434\u043e \u043c\u0430\u0441\u0442\u0435\u0440 \u043d\u043e\u0434\u044b \u043f\u0430\u0442\u0440\u043e\u043d\u0438<\/p>\n<pre><code class=\"python\">#... from ping3 import ping  @main.route('\/liveness-probe') def liveness_probe():     res = RESPONSE     res['data']['node'] = NODE     res['data']['round-trip'] = 0     result_pings = []      for _ in range(COUNT_PING):         single_ping = ping(loads(discover_master().data)['data']['node'], unit='ms')         if single_ping:             result_pings.append(single_ping)     try:         res['data']['round-trip'] = str(sum(result_pings) \/ len(result_pings))         res['message'] = 'successful'     except ZeroDivisionError:         res['message'] = 'something was going on (ping)'     redis_conn.set(NODE, res['data']['round-trip'])     return Response(dumps(res), status=200, mimetype='application\/json') <\/code><\/pre>\n<p>\u041d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u043f\u0440\u043e \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e:<\/p>\n<pre><code class=\"python\">@main.route('\/') def init():     primary = redis_conn.get('primary_host')     for node in KUBE_NODES:         if redis_conn.get(node):             pass         else:             redis_conn.set(node, 0)     if primary is None:         redis_conn.set('primary_host', 'localhost')     return \"It's a Switchover for patroni and kubernetes\" <\/code><\/pre>\n<p><strong>Cronjob \u0438\u043b\u0438 \u043a\u0430\u043a \u043c\u043e\u0434\u043d\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440<\/strong>:<\/p>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430 cronjob \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e \u0441\u0443\u0442\u0438 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0441\u0442\u0435\u0439\u0442\u043e\u043c \u043d\u0430\u0448\u0438\u0445 taints, label \u0438 \u0432\u044b\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0442\u044d\u0433\u0438, \u043b\u0435\u0439\u0431\u043b\u044b \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044f [far,intermediate,closely]<\/p>\n<pre><code class=\"python\">import subprocess  from app.constants import * from app.main.views import init   redis_conn = REDIS_CONN check = dict() min_ping = MIN_PING max_ping = MAX_PING  try:     current_db_master = redis_conn.get('primary').decode()     pre_current_db_master = redis_conn.get('primary_host').decode() except BaseException as error:     init()     current_db_master = redis_conn.get('primary').decode()     pre_current_db_master = redis_conn.get('primary_host').decode()   def subprocess_wrapper(command):     return subprocess.run(         command, text=True, check=False,         capture_output=True     )   for node in KUBE_NODES:     value = redis_conn.get(node)     if value:         check[node] = int(float(value.decode()))  for _, value in check.items():     max_ping = value if max_ping &lt; value else max_ping     min_ping = value if min_ping > value else min_ping  for node in KUBE_NODES:     if check.get(node) in range(0, min_ping + WINDOW_PING):         subprocess_wrapper(             ['kubectl', 'taint', 'nodes', node, 'switchover:NoExecute', '--overwrite']         )         subprocess_wrapper(             ['kubectl', 'label', 'nodes', node, 'switchover=closely', '--overwrite']         )     elif check.get(node) in range(max_ping - WINDOW_PING, max_ping + WINDOW_PING):         subprocess_wrapper(             ['kubectl', 'taint', 'nodes', node, 'switchover:NoExecute-']         )         subprocess_wrapper(             ['kubectl', 'label', 'nodes', node, 'switchover=far', '--overwrite']         )     else:         subprocess_wrapper(             ['kubectl', 'taint', 'nodes', node, 'switchover:NoExecute-']         )         subprocess_wrapper(             ['kubectl', 'label', 'nodes', node, 'switchover=intermediate', '--overwrite']         )  if pre_current_db_master != current_db_master:     for app in APPS:         subprocess_wrapper(             ['kubectl', 'rollout', 'restart', app]         )     redis_conn.set('primary_host', current_db_master) <\/code><\/pre>\n<p>\u042f \u043d\u0435 \u043f\u0440\u0435\u0442\u0435\u043d\u0434\u0443\u044e \u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u0438\u044f \u0447\u0435\u0433\u043e \u043b\u0438\u0431\u043e \u043d\u043e\u0432\u043e\u0433\u043e, \u043c\u0435\u043d\u044f \u043c\u0443\u0447\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441, \u043a\u0430\u043a \u043e\u043f\u0441\u044b \u0432 \u043d\u0430\u0448\u0435 \u0432\u0440\u0435\u043c\u044f \u0440\u0435\u0448\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443. \u041c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0442\u0443\u0442 \u043d\u0430\u0439\u0434\u0443\u0442\u0441\u044f \u043b\u044e\u0434\u0438 \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0431\u043e\u043b\u044c\u044e \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \u043b\u0443\u0447\u0448\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c. \u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043a\u0430\u043a \u043b\u044e\u0434\u0438 \u0440\u0435\u0448\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u0435\u0439\u0441.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/648389\/\"> https:\/\/habr.com\/ru\/post\/648389\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-328724","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/328724","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=328724"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/328724\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=328724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=328724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=328724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}