节点规划

master 192.168.19.222
node1 192.168.19.223
node2 192.168.19.224

软件版本

操作系统:CentOS Linux release7 4.4.174-1.el7.elrepo.x86_64
Docker版本:18.06.2-ce
kubernetes版本:1.13.3

环境准备

配置SSH免密登录

关闭所有节点防火墙

1
[root@k8s-master ~]# service firewalld stop && systemctl disable firewalld

关闭所有节点selinux

1
2
[root@k8s-master ~]# setenforce 0
[root@k8s-master ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

设置所有节点/etc/hosts文件

1
2
3
4
5
6
[root@k8s-master ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.19.222 k8s-master
192.168.19.223 node1
192.168.19.224 node2

haproxy安装脚本centos7.x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash
#description: configure and install haproxy software

SOFTDIR=/usr/local/src
H_SOFT="haproxy-1.7.10.tar.gz"
H_SOFT_DIR="haproxy-1.7.10"
H_PREFIX="/usr/local/haproxy"
H_CONFIG_DIR="/etc/haproxy"
H_WORK_DIR="/var/lib/haproxy"
USER="haproxy"
GROUP="haproxy"

if ! id $USER &>/dev/null;then
groupadd -g 3320 -r $GROUP
useradd -u 3320 -g $GROUP -M -s /sbin/nologin $USER
fi

#install haproxy
yum groupinstall -y "Development tools"
cd $SOFTDIR && [ ! -f ${H_SOFT} ] && wget https://www.haproxy.org/download/1.7/src/${H_SOFT}
[ ! -d ${H_SOFT_DIR} ] && tar xf ${H_SOFT}
cd ${SOFTDIR}/${H_SOFT_DIR} && make TARGET=linux2628 ARCH=x86_64 PREFIX=${H_PREFIX} && make install PREFIX=${H_PREFIX}
if [ $? -ne 0 ];then
echo "install haproxy fail"
exit 1
fi

#create haproxy configure file
[ ! -d ${H_WORK_DIR} ] && mkdir -p ${H_WORK_DIR} && chown -R ${USER}.${GROUP} ${H_WORK_DIR}
[ ! -d ${H_CONFIG_DIR} ] && mkdir -p ${H_CONFIG_DIR}
cat > /etc/haproxy/haproxy.cfg <<EOF
global
log 127.0.0.1 local2
chroot ${H_WORK_DIR}
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket ${H_WORK_DIR}/stats

defaults
mode tcp
log global
option tcplog
option dontlognull
option redispatch
retries 3
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout check 10s
maxconn 3000

listen stats
bind *:10800
mode http
option httplog
log 127.0.0.1 local3
stats refresh 30s
stats enable
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth Haproxyadmin:Aa123456
stats hide-version
stats admin if TRUE

frontend k8s-https-api
bind *:8443
mode tcp
option tcplog
tcp-request inspect-delay 5s
tcp-request content accept if { req.ssl_hello_type 1 }
default_backend k8s-https-api

backend k8s-https-api
mode tcp
option tcplog
option tcp-check
balance roundrobin
default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 2000 maxqueue 256 weight 100
server k8s-https-api-1 192.168.1.137:6443 check
server k8s-https-api-2 192.168.1.138:6443 check

frontend k8s-http-api
bind *:80
mode tcp
option tcplog
default_backend k8s-http-api

backend k8s-http-api
mode tcp
option tcplog
option tcp-check
balance roundrobin
default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 2000 maxqueue 256 weight 100
server k8s-http-api-1 192.168.1.137:8080 check
server k8s-http-api-2 192.168.1.138:8080 check
EOF

#copy haproxy start script
cd ${SOFTDIR}/${H_SOFT_DIR}
cp examples/haproxy.init /etc/init.d/haproxy
cd /etc/init.d/ && sed -i 's/\/usr\/sbin\/'\$BASENAME'/\/usr\/local\/haproxy\/sbin\/'\$BASENAME'/g' haproxy
chmod u+x /etc/init.d/haproxy
chkconfig --add haproxy
chkconfig haproxy on
ln -s /usr/local/haproxy/sbin/haproxy /usr/sbin/haproxy
if [ "ss -tunlp|grep 80|awk -F '[ :]+' '{print $6}'" = "80" ];then
echo "haproxy start fail,port 80"
exit 1
else
service haproxy start
fi
#configure log
sed -i 's/^#$ModLoad imudp/$ModLoad imudp/g' /etc/rsyslog.conf
sed -i 's/^#$UDPServerRun 514/$UDPServerRun 514/g' /etc/rsyslog.conf
echo "local2.* /var/log/haproxy.log" >> /etc/rsyslog.conf
echo "local3.* /var/log/haproxy_stats.log" >> /etc/rsyslog.conf
systemctl restart rsyslog

基本配置说明记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
global               #全局设置
log 127.0.0.1 local2 #日志输出配置,所有日志都记录在本机,通过local2输出
#log loghost local0 info
maxconn 4096 #最大连接数
chroot /usr/local/haproxy
uid 99 #所属运行的用户uid
gid 99 #所属运行的用户组
group haproxy #用户组
daemon #后台运行haproxy
nbproc 1 #启动1个haproxy实例
pidfile /usr/local/haproxy/haproxy.pid #将所有进程PID写入pid文件
#debug
#quiet

defaults #默认设置
#log global
log 127.0.0.1 local3 #日志文件的输出定向

#默认的模式:tcp|http|health
mode http #所处理的类别,默认采用http模式

option httplog #日志类别,采用http日志格式`
option dontlognull
option forwardfor #将客户端真实ip加到HTTP Header中供后端服务器读取
option retries 3 #三次连接失败则认证服务器不可用
option httpclose #每次请求完毕后主动关闭http通道,haproxy不支持keep-alive,只>能模拟这种模式的实现
retries 3 #3次连接失败就认为服务器不可用,主要通过后面的check检查
option redispatch #当serverid对应的服务器挂掉后,强制定向到其他健康服务器
option abortonclose #当服务器负载很高时,自动结束掉当前队列中处理比较久的链接
maxconn 2000 #默认最大连接数

timeout connect 5000 #连接超时时间
timeout client 50000 #客户端连接超时时间
timeout server 50000 #服务器端连接超时时间

stats enable
stats uri /haproxy-stats #haproxy监控页面的访问地址
stats auth test:test123 #设置监控页面的用户和密码
stats hide-version #隐藏统计页面的HAproxy版本信息

frontend http-in #前台
bind *:81
mode http
option httplog
log global
default_backend htmpool #静态服务器池

backend htmpool #后台
balance leastconn #负载均衡算法
option httpchk HEAD /index.html HTTP/1.0 #健康检查
server web1 192.168.2.10:80 cookie 1 weight 5 check inter 2000 rise 2 fall 3
server web2 192.168.2.11:80 cookie 2 weight 3 check inter 2000 rise 2 fall 3
# web1/web2:自定义服务器别名
# 192.168.2.10:80:服务器IP:Port
# cookie 1/2:表示serverid
# weight: 服务器权重,数字越大分配到的请求数越高
# check: 接受定时健康检查
# inter 2000: 检查频率
# rise 2: 两次检测正确认为服务器可用
# fall 3: 三次失败认为服务器不可用

listen w.gdu.me 0.0.0.0:80
option httpchk GET /index.html
server s1 192.168.2.10:80 weight 3 check
server s3 192.168.2.11:80 weight 3 check

# Haproxy统计页面
# --------------------------------------------------------------------------------------------
listen haproxy_stats
bind 0.0.0.0:1080 #侦听IP:Port
mode http
log 127.0.0.1 local 0 err #err|warning|info|debug]
stats refresh 30s
stats uri /haproxy-stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats auth test:test
stats hide-version
stats admin if TRUE #手工启用/禁用后端服务器


# 网站检测listen配置
# --------------------------------------------------------------------------------------------
listen site_status
bind 0.0.0.0:1081
mode http
log 127.0.0.1 local0 err

#网站健康检查URI,用来检测Haproxy管理的网站是否可能,正常返回200、异常返回500
monitor-uri /site_status

#定义网站down时的策略
#当backend中的有效服务器数<1时,返回true
acl site_dead nbsrv(denali_server) lt 1
acl site_dead nbsrv(tm_server) lt 1
acl site_dead nbsrv(mms_server) lt 1

#当满足策略的时候返回http-500,否则返回http-200
monitor fail if site_dead

#声名一个监测请求的来源网络
monitor-net 192.168.0.252/31


# https的配置方法
# --------------------------------------------------------------------------------------------
listen login_https_server
bind 0.0.0.0:443 #绑定HTTPS的443端口
mode tcp #https必须使用tcp模式
log global
balance roundrobin
option httpchk GET /member/login.jhtml HTTP/1.1\r\nHost:login.daily.taobao.net
#回送给server的端口也必须是443
server vm94f.sqa 192.168.212.94:443 check port 80 inter 6000 rise 3 fall 3
server v215120.sqa 192.168.215.120:443 check port 80 inter 6000 rise 3 fall 3


# frontend配置
# --------------------------------------------------------------------------------------------
frontend http_80_in
bind 0.0.0.0:80 #监听端口
mode http #http的7层模式
log global #使用全局的日志配置
option httplog #启用http的log
option httpclose #每次请求完毕后主动关闭http通道,HA-Proxy不支持keep-alive模式
option forwardfor ##如果后端服务器需要获得客户端的真实IP需要配置次参数,将可以从Http Header中获得客户端IP

#HAProxy的日志记录内容配置
capture request header Host len 40 # 请求中的主机名
capture request header Content-Length len 10 # 请求中的内容长度
capture request header Referer len 200 # 请求中的引用地址
capture response header Server len 40 # 响应中的server name
capture response header Content-Length len 10 # 响应中的内容长度(可配合option logasap使用)
capture response header Cache-Control len 8 # 响应中的cache控制
capture response header Location len 20 # 响应中的重定向地址


#ACL策略规则定义
#-------------------------------------------------
#如果请求的域名满足正则表达式返回true(-i:忽略大小写)
acl denali_policy hdr_reg(host) -i ^(www.gemini.taobao.net|my.gemini.taobao.net|auction1.gemini.taobao.net)$

#如果请求域名满足trade.gemini.taobao.net返回true
acl tm_policy hdr_dom(host) -i trade.gemini.taobao.net

#在请求url中包含sip_apiname=,则此控制策略返回true,否则为false
acl invalid_req url_sub -i sip_apiname=

#在请求url中存在timetask作为部分地址路径,则此控制策略返回true,否则返回false
acl timetask_req url_dir -i timetask

#当请求的header中Content-length等于0时返回true
acl missing_cl hdr_cnt(Content-length) eq 0


#ACL策略匹配相应
#-------------------------------------------------
#当请求中header中Content-length等于0阻止请求返回403
#block表示阻止请求,返回403错误
block if missing_cl

#如果不满足策略invalid_req,或者满足策略timetask_req,则阻止请求
block if !invalid_req || timetask_req

#当满足denali_policy的策略时使用denali_server的backend
use_backend denali_server if denali_policy

#当满足tm_policy的策略时使用tm_server的backend
use_backend tm_server if tm_policy

#reqisetbe关键字定义,根据定义的关键字选择backend
reqisetbe ^Host:\ img dynamic
reqisetbe ^[^\ ]*\ /(img|css)/ dynamic
reqisetbe ^[^\ ]*\ /admin/stats stats

#以上都不满足的时候使用默认mms_server的backend
default_backend mms_server

#HAProxy错误页面设置
errorfile 400 /home/admin/haproxy/errorfiles/400.http
errorfile 403 /home/admin/haproxy/errorfiles/403.http
errorfile 408 /home/admin/haproxy/errorfiles/408.http
errorfile 500 /home/admin/haproxy/errorfiles/500.http
errorfile 502 /home/admin/haproxy/errorfiles/502.http
errorfile 503 /home/admin/haproxy/errorfiles/503.http
errorfile 504 /home/admin/haproxy/errorfiles/504.http


# backend的设置
# --------------------------------------------------------------------------------------------
backend mms_server
mode http #http的7层模式
balance roundrobin #负载均衡的方式,roundrobin平均方式
cookie SERVERID #允许插入serverid到cookie中,serverid后面可以定义

#心跳检测的URL,HTTP/1.1¥r¥nHost:XXXX,指定了心跳检测HTTP的版本,XXX为检测时请求
#服务器的request中的域名是什么,这个在应用的检测URL对应的功能有对域名依赖的话需要设置
option httpchk GET /member/login.jhtml HTTP/1.1\r\nHost:member1.gemini.taobao.net

#服务器定义,cookie 1表示serverid为1,check inter 1500 是检测心跳频率
#rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重
server mms1 10.1.5.134:80 cookie 1 check inter 1500 rise 3 fall 3 weight 1
server mms2 10.1.6.118:80 cookie 2 check inter 1500 rise 3 fall 3 weight 2


backend denali_server
mode http
balance source #负载均衡的方式,source根据客户端IP进行哈希的方式
option allbackups #设置了backup的时候,默认第一个backup会优先,设置option allbackups后所有备份服务器权重一样

#心跳检测URL设置
option httpchk GET /mytaobao/home/my_taobao.jhtml HTTP/1.1\r\nHost:my.gemini.taobao.net

#可以根据机器的性能不同,指定连接数配置,如minconn 10 maxconn 20
server denlai1 10.1.5.114:80 minconn 4 maxconn 12 check inter 1500 rise 3 fall 3
server denlai2 10.1.6.104:80 minconn 10 maxconn 20 check inter 1500 rise 3 fall 3
#备份机器配置,正常情况下备机不会使用,当主机的全部服务器都down的时候备机会启用
server dnali-back1 10.1.7.114:80 check backup inter 1500 rise 3 fall 3
server dnali-back2 10.1.7.114:80 check backup inter 1500 rise 3 fall 3


backend tm_server
mode http
balance leastconn #负载均衡的方式,leastcon选择当前请求数最少的服务器
option httpchk GET /trade/itemlist/prepayCard.htm HTTP/1.1\r\nHost:trade.gemini.taobao.net
server tm1 10.1.5.115:80 check inter 1500 rise 3 fall 3
server tm2 10.1.6.105:80 check inter 1500 rise 3 fall 3


#reqisetbe自定义关键字匹配backend部分
backend dynamic
mode http
balance source
option httpchk GET /welcome.html
server denlai1 10.3.5.114:80 check inter 1500 rise 3 fall 3
server denlai2 10.4.6.104:80 check inter 1500 rise 3 fall 3

backend stats
mode http
balance source
option httpchk GET /welcome.html
server denlai1 10.5.5.114:80 check inter 1500 rise 3 fall 3
server denlai2 10.6.6.104:80 check inter 1500 rise 3 fall 3

javaagent https://github.com/prometheus/jmx_exporter

下载javaagent:

https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.3.1/jmx_prometheus_javaagent-0.3.1.jar

kubernetes监控jvm
每个应用都通过javaagent向外提供一个http服务暴露出自己的JMX信息。prometheus通过kubernetes 自动发现就能把这个应用加入监控对象列表,进行数据收集并跟踪服务的状态。为JVM添加参数: -javaagent:jmx_prometheus_javaagent-0.3.1.jar=9180:config.yaml即可运行此Exporter。其中9180为暴露的端口号,config.yaml为配置文件路径。要采集指标,访问http://host:9180/metrics即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 annotations:
prometheus.io/scrape: "true"
prometheus.io/port: "9180"
prometheus.io/path: /metrics
```
spec.template.metadata.annotations.prometheus.io/scrape
是否针对Discorvery
spec.template.metadata.annotations.prometheus.io/port
发现目标端口
spec.template.metadata.annotations.prometheus.io/path
发现目标路径

创建一个configmap
```
kubectl create cm prometheus-jmx-exporter-config --from-file=./config.yaml
kubectl apply -f tomcat-monitoring-demo.yaml
[root@centos-master jvm-exporter]# kubectl get pod | grep demo
tomcat-monitoring-demo-1780394632-c96hz 1/1 Running 0 1h
tomcat-monitoring-demo-1780394632-sk09b 1/1 Running 0 1h

把javaagent放nfs里面如果没有nfs可以考虑添加到基础镜像或者dockerfile时候添加
grafana 添加仪表盘id:7727

prometheus配置文件添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:

- role: pod
relabel_configs:

- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep

regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
action: replace

target_label: __metrics_path__
regex: (.+)
- source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace

regex: ([^:]+)(?::\d+)?;(\d+)
replacement: $1:$2
target_label: __address__
- action: labelmap
regex: __meta_kubernetes_pod_label_(.+)

- source_labels: [__meta_kubernetes_namespace]
action: replace

target_label: kubernetes_namespace
- source_labels: [__meta_kubernetes_pod_name]
action: replace

target_label: kubernetes_pod_name

使用docker测试

1
docker run -d   --name tomcat-jmx   -v ~/jvm-exporter:/jmx-exporter   -e CATALINA_OPTS="-Xms1G -Xmx1G -javaagent:/jmx-exporter/jmx_prometheus_javaagent-0.3.1.jar=6060:/jmx-exporter/config.yml"   -p 6060:6060   -p 9090:8080   tomcat:latest

资源限制namespace 设置 ResouceQuota 和 LimitRange两种

资源限额(Resource Quota)

资源限额可以为每一个命名空间提供一个总体的资源使用的限制,限制命名空间某种类型的资源对象的总数目上线,限制pod可以使用到的计算资源的总上限
创建应该dev的命名空间

安装Haproxy keepalived

两台HA haproxy同样的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
$ yum install -y haproxy
$at /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats

defaults
mode tcp
log global
option tcplog
option dontlognull
option redispatch
retries 3
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout check 10s
maxconn 3000
listen stats
bind *:9000
mode http
stats enable
stats hide-version
stats uri /stats
stats refresh 30s
stats realm Haproxy\ Statistics
stats auth Admin:Password

frontend in-apiserver-cluster
bind *:8443
mode tcp
option tcplog
tcp-request inspect-delay 5s
tcp-request content accept if { req.ssl_hello_type 1 }
default_backend https-apiserver-cluster

backend https-apiserver-cluster
mode tcp
option tcplog
option httpchk GET /healthz
balance roundrobin
default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 2000 maxqueue 256 weight 100
server k8s-https-api-1 192.168.1.137:6443 check check-ssl verify none
server k8s-https-api-2 192.168.1.138:6443 check check-ssl verify none

#frontend k8s-http-api
# bind *:80
# mode tcp
# option tcplog
# default_backend k8s-http-api

#backend k8s-http-api
# mode tcp
# option tcplog
# option tcp-check
# balance roundrobin
# default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 2000 maxqueue 256 weight 100
# server k8s-http-api-1 192.168.1.137:8080 check
# server k8s-http-api-2 192.168.1.138:8080 check

1
2
3
4
启动haproxy
$ sudo systemctl start haproxy
$ sudo systemctl enable haproxy
$ sudo systemctl status haproxy

keepalived安装

1
2
3
4
5
6
7
8
9
10
11
12
开启路由转发,这里我们定义虚拟IP为:192.168.1.100
$ vi /etc/sysctl.conf
# 添加以下内容
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1

# 验证并生效
$ sysctl -p
# 验证是否生效
$ cat /proc/sys/net/ipv4/ip_forward
1
yum install -y keepalived

将masterA设置为Master,masterB设置为Backup,修改配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
notification_email {
}
router_id kube_api
}

vrrp_script check_apiserver {
script "curl -o /dev/null -s -w %{http_code} -k https://192.168.1.4:6443"
interval 3
timeout 3
fall 2
rise 2
}

vrrp_instance haproxy-vip {
# 使用单播通信,默认是组播通信
unicast_src_ip 192.168.1.4
unicast_peer {
192.168.1.5
}
# 初始化状态
state MASTER
# 虚拟ip 绑定的网卡 (这里根据你自己的实际情况选择网卡)
interface eth0
# 此ID 要与Backup 配置一致
virtual_router_id 51
# 默认启动优先级,要比Backup 大点,但要控制量,保证自身状态检测生效
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 虚拟ip 地址
192.168.1.100
}
track_script {
check_apiserver
}
}

virtual_server 192.168.1.100 80 {
delay_loop 5
lvs_sched wlc
lvs_method NAT
persistence_timeout 1800
protocol TCP

real_server 192.168.1.4 8080 {
weight 1
TCP_CHECK {
connect_port 8080
connect_timeout 3
}
}
}

virtual_server 192.168.1.100 8443 {
delay_loop 5
lvs_sched wlc
lvs_method NAT
persistence_timeout 1800
protocol TCP

real_server 192.168.1.4 6443 {
weight 1
TCP_CHECK {
connect_port 6443
connect_timeout 3
}
}
}

或者使用健康检查脚本

1
2
3
4
5
6
7
8
9
haproxy检查脚本:/etc/keepalived/haproxy_check.sh
#!/bin/bash
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ] ; then
docker restart k8s-haproxy
sleep 2
if [ `ps -C haproxy --no-header |wc -l` -eq 0 ] ; then
service keepalived stop
fi
fi

一、filebeat

k8s日志收集方案使用官方推荐的EFK方案F(fluentd),部分宿主机日志使用filebeat
filebeat是一个日志文件托运工具,在你的服务器上安装客户端后,filebeat会监控日志目录或者指定的日志文件,追踪读取这些文件(追踪文件的变化,不停的读),并且转发这些信息到elasticsearch或者logstarsh、kafka、redis中存放。Filebeat 所占系统的 CPU 和内存几乎可以忽略不计,filebeat使用Go语言开发运行不依赖环境。
filebeat.png

EFK 环境应该部署好,这里主要记录filrbeat-kafka-logstash-es-kibana

下载安装filebeat

1
2
curl -L -O https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-6.5.4-x86_64.rpm
rpm -ivh filebeat-1.3.1-x86_64.rpm

prometheus+Consul自动服务发现减少修改修改prometheus.yml重启prometheus等操作

Consul 介绍

Consul有多个组件,但总体而言,它是基础架构中的一款服务发现和配置的工具。 它提供了几个关键功能:
服务发现:Consul client 可以提供服务,例如api或mysql,也可以使用Consul client来发现指定服务的提供者。 使用DNS或HTTP,应用程序可以轻松找到他们所依赖的服务。
健康检查:Consul client 可以提供任何数量的健康检查,或者与给定的服务(“Web服务器是否返回200 OK”),或与本地节点(“内存利用率是否低于90%”)相关联。 可以使用此信息来监控集群运行状况,服务发现组件使用此信息将流量从有问题的主机中移除出去。
KV Store:应用程序可以使用Consul的分层键/值存储,包括动态配置,功能标记,协调,leader选举等等。 简单的HTTP API使其易于使用。
多数据中心:Consul支持多个数据中心。 这意味着Consul的用户不必担心构建额外的抽象层以扩展到多个区域。
官方架构图
Consul