-
Notifications
You must be signed in to change notification settings - Fork 0
/
saltstack一键部署haproxy-keepalived-nginx负载均衡高可用环境.html
1830 lines (1529 loc) · 160 KB
/
saltstack一键部署haproxy-keepalived-nginx负载均衡高可用环境.html
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta name="google-site-verification" content="5_ThYU83P_c_1Cw6mhGsUSQKKq0LsSCInvlWnaoNAPU" />
<meta charset="utf-8">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge" >
<link rel="dns-prefetch" href="http://idcat.cn">
<title>saltstack一键部署haproxy+keepalived+nginx负载均衡高可用环境 | deelaaay learning</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description" content="本文档仅作为自己学习记录使用。部分内容参考https://www.kancloud.cn/louis1986/saltstack/521459 本例环境架构: centos7.2-1511 测试环境,均关闭防火墙以及selinux 主机名 角色 ip master master 192.168.4.10 minion1 haproxy keepalived 192.168.4.">
<meta name="keywords" content="saltstack haproxy keepalived nginx">
<meta property="og:type" content="article">
<meta property="og:title" content="saltstack一键部署haproxy+keepalived+nginx负载均衡高可用环境">
<meta property="og:url" content="http://idcat.cn/saltstack一键部署haproxy-keepalived-nginx负载均衡高可用环境.html">
<meta property="og:site_name" content="deelaaay learning">
<meta property="og:description" content="本文档仅作为自己学习记录使用。部分内容参考https://www.kancloud.cn/louis1986/saltstack/521459 本例环境架构: centos7.2-1511 测试环境,均关闭防火墙以及selinux 主机名 角色 ip master master 192.168.4.10 minion1 haproxy keepalived 192.168.4.">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://i.imgur.com/z6tC1Ms.jpg">
<meta property="og:image" content="https://i.imgur.com/1g3n3nT.jpg">
<meta property="og:image" content="https://i.imgur.com/DITW0Ny.jpg">
<meta property="og:image" content="https://i.imgur.com/fyh10cx.jpg">
<meta property="og:image" content="https://i.imgur.com/3BlE632.jpg">
<meta property="og:image" content="https://i.imgur.com/VsJiaZl.jpg">
<meta property="og:image" content="https://i.imgur.com/b8RTcHH.jpg">
<meta property="og:updated_time" content="2018-06-09T12:32:40.421Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="saltstack一键部署haproxy+keepalived+nginx负载均衡高可用环境">
<meta name="twitter:description" content="本文档仅作为自己学习记录使用。部分内容参考https://www.kancloud.cn/louis1986/saltstack/521459 本例环境架构: centos7.2-1511 测试环境,均关闭防火墙以及selinux 主机名 角色 ip master master 192.168.4.10 minion1 haproxy keepalived 192.168.4.">
<meta name="twitter:image" content="https://i.imgur.com/z6tC1Ms.jpg">
<link rel="alternative" href="/atom.xml" title="deelaaay learning" type="application/atom+xml">
<link rel="icon" href="/favicon.png">
<link rel="stylesheet" type="text/css" href="/./main.0cf68a.css">
<style type="text/css">
#container.show {
background: linear-gradient(200deg,#a0cfe4,#e8c37e);
}
</style>
<script type="text/javascript">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?193f9dfeb50288f265def224cd2bcccb";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
</head>
<body>
<div id="container" q-class="show:isCtnShow">
<canvas id="anm-canvas" class="anm-canvas"></canvas>
<div class="left-col" q-class="show:isShow">
<div class="overlay" style="background: #4d4d4d"></div>
<div class="intrude-less">
<header id="header" class="inner">
<a href="/" class="profilepic">
<img src="/images/tuxiang.jpg" class="js-avatar">
</a>
<hgroup>
<h1 class="header-author"><a href="/">deelaaay</a></h1>
</hgroup>
<nav class="header-menu">
<ul>
<li><a href="/">主页</a></li>
<li><a href="/archives">所有文章</a></li>
<li><a href="/tags">标签</a></li>
</ul>
</nav>
<nav class="header-smart-menu">
<a q-on="click: openSlider(e, 'innerArchive')" href="javascript:void(0)">所有文章</a>
<a q-on="click: openSlider(e, 'friends')" href="javascript:void(0)">友链</a>
<a q-on="click: openSlider(e, 'aboutme')" href="javascript:void(0)">关于我</a>
</nav>
<nav class="header-nav">
<div class="social">
<a class="github" target="_blank" href="/deelaaay/github.io" title="github"><i class="icon-github"></i></a>
</div>
</nav>
</header>
</div>
</div>
<div class="mid-col" q-class="show:isShow,hide:isShow|isFalse">
<nav id="mobile-nav">
<div class="overlay js-overlay" style="background: #4d4d4d"></div>
<div class="btnctn js-mobile-btnctn">
<div class="slider-trigger list" q-on="click: openSlider(e)"><i class="icon icon-sort"></i></div>
</div>
<div class="intrude-less">
<header id="header" class="inner">
<div class="profilepic">
<img src="/images/tuxiang.jpg" class="js-avatar">
</div>
<hgroup>
<h1 class="header-author js-header-author">deelaaay</h1>
</hgroup>
<nav class="header-nav">
<div class="social">
<a class="github" target="_blank" href="/deelaaay/github.io" title="github"><i class="icon-github"></i></a>
</div>
</nav>
<nav class="header-menu js-header-menu">
<ul style="width: 70%">
<li style="width: 33.333333333333336%"><a href="/">主页</a></li>
<li style="width: 33.333333333333336%"><a href="/archives">所有文章</a></li>
<li style="width: 33.333333333333336%"><a href="/tags">标签</a></li>
</ul>
</nav>
</header>
</div>
<div class="mobile-mask" style="display:none" q-show="isShow"></div>
</nav>
<div id="wrapper" class="body-wrap">
<div class="menu-l">
<div class="canvas-wrap">
<canvas data-colors="#eaeaea" data-sectionHeight="100" data-contentId="js-content" id="myCanvas1" class="anm-canvas"></canvas>
</div>
<div id="js-content" class="content-ll">
<article id="post-saltstack一键部署haproxy-keepalived-nginx负载均衡高可用环境" class="article article-type-post " itemscope itemprop="blogPost">
<div class="article-inner">
<header class="article-header">
<h1 class="article-title" itemprop="name">
saltstack一键部署haproxy+keepalived+nginx负载均衡高可用环境
</h1>
<a href="/saltstack一键部署haproxy-keepalived-nginx负载均衡高可用环境.html" class="archive-article-date">
<time datetime="2018-06-09T12:28:21.000Z" itemprop="datePublished"><i class="icon-calendar icon"></i>2018-06-09</time>
</a>
</header>
<div class="article-entry" itemprop="articleBody">
<p>本文档仅作为自己学习记录使用。<br>部分内容参考<a href="https://www.kancloud.cn/louis1986/saltstack/521459" target="_blank" rel="noopener">https://www.kancloud.cn/louis1986/saltstack/521459</a></p>
<p>本例环境架构: centos7.2-1511 测试环境,均关闭防火墙以及selinux</p>
<table>
<thead>
<tr>
<th>主机名</th>
<th style="text-align:center">角色</th>
<th style="text-align:right">ip</th>
</tr>
</thead>
<tbody>
<tr>
<td>master</td>
<td style="text-align:center">master</td>
<td style="text-align:right">192.168.4.10</td>
</tr>
<tr>
<td>minion1</td>
<td style="text-align:center">haproxy keepalived</td>
<td style="text-align:right">192.168.4.11</td>
</tr>
<tr>
<td>minion2</td>
<td style="text-align:center">haproxy keepalived</td>
<td style="text-align:right">192.168.4.12</td>
</tr>
<tr>
<td>minion3</td>
<td style="text-align:center">nginx</td>
<td style="text-align:right">192.168.4.13</td>
</tr>
<tr>
<td>minion3</td>
<td style="text-align:center">nginx</td>
<td style="text-align:right">192.168.4.14</td>
</tr>
<tr>
<td></td>
<td style="text-align:center">VIP</td>
<td style="text-align:right">192.168.4.16</td>
</tr>
</tbody>
</table>
<p>该配置环境主要是配置haproxy + keepalived负载均衡的高可用,其中haproxy通过轮训的方式连接到后端实际的2台nginx服务器。<br><a id="more"></a><br><strong>salt安装</strong></p>
<p>master节点: yum install epel-release -y yum install salt-master</p>
<p>minion节点: yum install epel-release -y yum install salt-minion</p>
<p><strong>salt基础配置</strong></p>
<p>本例不作详细介绍。</p>
<p>master节点上 vi /etc/salt/master 修改如下:其他均默认。</p>
<pre><code>file_roots:
base:
- /srv/salt/base
prod:
- /srv/salt/prod
interface: 192.168.4.10
</code></pre><p>minion节点上 vi /etc/salt/minion 修改如下: 其他均默认。</p>
<pre><code>master: master
</code></pre><p>所有master minion节点启动服务后(systemctl start salt-minion/salt-master)<br>在master执行 salt-key -A 接受所有minion节点的key。相关情况不做详细介绍。</p>
<p>所有节点/etc/hosts 均一致</p>
<pre><code>[root@master ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.4.10 master
192.168.4.11 minion1
192.168.4.12 minion2
192.168.4.13 minion3
192.168.4.14 minion4
</code></pre><p>下面配置state等不作详细介绍,直接复制粘贴。</p>
<p><strong>所有配置均在master上,首先查看tree目录</strong></p>
<pre><code>[root@master ~]# cd /srv/salt/
[root@master salt]# tree
.
├── base
│ ├── init
│ │ ├── audit.sls
│ │ ├── cron.sls
│ │ ├── dns.sls
│ │ ├── env_init.sls
│ │ ├── epel.sls
│ │ ├── files
│ │ │ ├── resolv.conf
│ │ │ └── sysctl.conf
│ │ ├── history.sls
│ │ ├── sysctl.sls
│ │ └── yum.sls
│ └── top.sls
└── prod
├── cluster
│ ├── files
│ │ ├── haproxy-outside.cfg
│ │ └── haproxy-outside-keepalived.cfg
│ ├── haproxy-outside-keepalived.sls
│ └── haproxy-outside.sls
├── haproxy
│ ├── files
│ │ └── haproxy-1.8.9.tar.gz
│ └── install_haproxy.sls
├── keepalived
│ ├── files
│ │ ├── keepalived
│ │ ├── keepalived-1.4.2.tar.gz
│ │ ├── keepalived.conf
│ │ └── keepalived.sysconfig
│ └── install_keepalived.sls
├── nginx
│ ├── files
│ │ ├── nginx-1.12.2.tar.gz
│ │ ├── nginx.conf
│ │ ├── nginx.init
│ │ ├── pcre-8.41.tar.gz
│ │ └── zlib-1.2.11.tar.gz
│ ├── nginx-install.sls
│ ├── nginx-service.sls
│ ├── nginx-user.sls
│ ├── pcre-install.sls
│ └── zlib-install.sls
└── pkg
└── pkg-init.sls
13 directories, 33 files
</code></pre><p><strong>首先介绍base/init目录下的文件</strong></p>
<p>[root@master init]# tree</p>
<pre><code>.
├── audit.sls
├── cron.sls
├── dns.sls
├── env_init.sls
├── epel.sls
├── files
│ ├── resolv.conf
│ └── sysctl.conf
├── history.sls
├── sysctl.sls
└── yum.sls
1 directory, 10 files
</code></pre><p>该目录文件为所有节点配置初始化的一些配置,比方说统一dns,统一安装epel源 统一sysctl参数等等。其中env_init.sls 是统一调配入口,这样只需要运行env_init就可以自动运行其他所有配置文件。可自行增加编辑。</p>
<pre><code>[root@master init]# cat env_init.sls
include:
- init.audit
- init.cron
- init.dns
- init.epel
- init.history
- init.sysctl
- init.yum
[root@master init]# cat audit.sls
/etc/bashrc:
file.append:
- text:
- export PROMPT_COMMAND='{ msg=$(history 1 | { read x y; echo $y; });logger "[euid=$(whoami)]":$(who am i):[`pwd']"$msg; }'
[root@master init]# cat cron.sls
ntpdate-install:
pkg.installed:
- name: ntpdate
set-crontab:
cron.present:
- name: /usr/sbin/ntpdate time1.aliyun.com >> /dev/null 2>&1
- user: root
- minute: "*2"
- require:
- pkg: ntpdate-install
[root@master init]# cat dns.sls
/etc/resolv.conf:
file.managed:
- source: salt://init/files/resolv.conf
- user: root
- group: root
- mode: 644
[root@master init]# cat epel.sls
yum_epel:
pkg.installed:
- name: epel-release
- unless: rpm -qa |grep epel-release
[root@master init]# cat history.sls
/etc/profile:
file.append:
- text:
- export HISTTIMEFORMAT="%F %T `whoami`"
[root@master init]# cat sysctl.sls
/etc/sysctl.conf:
file.managed:
- source: salt://init/files/sysctl.conf
- user: root
- group: root
- mode: 644
[root@master init]# cat yum.sls
yum_base:
pkg.installed:
- names:
- gcc
- gcc-c++
- make
- autoconf
- net-tools
- lrzsz
- sysstat
- vim-enhanced
- openssh-clients
- lsof
- tree
- wget
- cmake
</code></pre><p>该目录下file目录</p>
<pre><code>[root@master init]# tree files/
files/
├── resolv.conf
└── sysctl.conf
0 directories, 2 files
[root@master init]# cd files/
[root@master files]# ll
total 8
-rw-r--r-- 1 root root 53 Jun 6 11:37 resolv.conf
-rw-r--r-- 1 root root 449 Jun 6 11:57 sysctl.conf
[root@master files]# cat resolv.conf
# Generated by NetworkManager #根据实际情况填写
nameserver 192.168.0.1
[root@master files]# cat sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
#本例为空,测试环境不想调试内核参数,若实际应用中,请自行输入需要调整的内核参数
</code></pre><p><strong>介绍prod目录</strong></p>
<p>该目录为实际的安装包以及配置等目录。首先查看tree</p>
<p>每个目录均为一个需要安装的软件包以及其配置文件。cluster目录是后期在生成环境下结合不同环境配置haproxy和keepalived的配置文档,最后介绍。其他的目录比如nginx haproxy等都是安装配置。</p>
<pre><code>[root@master prod]# tree
.
├── cluster
│ ├── files
│ │ ├── haproxy-outside.cfg
│ │ └── haproxy-outside-keepalived.cfg
│ ├── haproxy-outside-keepalived.sls
│ └── haproxy-outside.sls
├── haproxy
│ ├── files
│ │ └── haproxy-1.8.9.tar.gz
│ └── install_haproxy.sls
├── keepalived
│ ├── files
│ │ ├── keepalived
│ │ ├── keepalived-1.4.2.tar.gz
│ │ ├── keepalived.conf
│ │ └── keepalived.sysconfig
│ └── install_keepalived.sls
├── nginx
│ ├── files
│ │ ├── nginx-1.12.2.tar.gz
│ │ ├── nginx.conf
│ │ ├── nginx.init
│ │ ├── pcre-8.41.tar.gz
│ │ └── zlib-1.2.11.tar.gz
│ ├── nginx-install.sls
│ ├── nginx-service.sls
│ ├── nginx-user.sls
│ ├── pcre-install.sls
│ └── zlib-install.sls
└── pkg
└── pkg-init.sls
9 directories, 22 files
</code></pre><p><strong>首先看pkg目录</strong></p>
<p>这个目录是所有节点部署nginx haproxy keepalived等软件需要的依赖包</p>
<pre><code>[root@master prod]# cd pkg/
[root@master pkg]# ll
total 4
-rw-r--r-- 1 root root 167 Jun 6 14:07 pkg-init.sls
[root@master pkg]# cat pkg-init.sls
pkg-init:
pkg.installed:
- names:
- gcc
- gcc-c++
- glibc
- make
- autoconf
- openssl
- openssl-devel
- automake
</code></pre><p><strong>其次haproxy目录</strong></p>
<pre><code>[root@master haproxy]# pwd
/srv/salt/prod/haproxy
[root@master haproxy]# tree
.
├── files
│ └── haproxy-1.8.9.tar.gz
└── install_haproxy.sls
1 directory, 2 files
</code></pre><p>file目录下为haproxy安装源码包</p>
<pre><code>[root@master haproxy]# cd files/
[root@master files]# ll
total 2012
-rw-r--r-- 1 root root 2057051 Jun 6 14:15 haproxy-1.8.9.tar.gz
安装配置文件
[root@master haproxy]# cat install_haproxy.sls
include:
- pkg.pkg-init
haproxy-install:
file.managed:
- name: /usr/local/src/haproxy-1.8.9.tar.gz
- source: salt://haproxy/files/haproxy-1.8.9.tar.gz
- user: root
- group: root
- mode: 755
cmd.run:
- name: cd /usr/local/src && tar xf haproxy-1.8.9.tar.gz && cd haproxy-1.8.9 && make TARGET=linux2628 PREFIX=/usr/local/haproxy && make install PREFIX=/usr/local/haproxy && sed -i 's?BIN=/usr/sbin/$BASENAME?BIN=/usr/local/haproxy/sbin/$BASENAME?' /usr/local/src/haproxy-1.8.9/examples/haproxy.init && sed -i '/NETWORKING/c [[ $NETWORKING = "no" ]] && exit 0' /usr/local/src/haproxy-1.8.9/examples/haproxy.init && cp /usr/local/src/haproxy-1.8.9/examples/haproxy.init /etc/init.d/haproxy && chmod +x /etc/init.d/haproxy
- unless: test -d /usr/local/haproxy
- require:
- pkg: pkg-init
- file: haproxy-install
haproxy_chkconfig:
cmd.run:
- name: chkconfig --add haproxy && chkconfig --level 2345 haproxy on
- unless: chkconfig --list |grep haproxy
- require:
- file: haproxy-install
haproxy-config-dir:
file.directory:
- name: /etc/haproxy
- user: root
- group: root
- mode: 755
net.ipv4.ip_nonlocal_bind:
cmd.run:
- name: echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf && sysctl -p
- unless: cat /etc/sysctl.conf | grep net.ipv4.ip_nonlocal_bind
- require:
- file: haproxy-install
</code></pre><p><strong>keepalived目录</strong></p>
<pre><code>[root@master prod]# cd keepalived/
[root@master keepalived]# ll
total 4
drwxr-xr-x 2 root root 102 Jun 7 11:03 files
-rw-r--r-- 1 root root 1452 Jun 7 11:18 install_keepalived.sls
</code></pre><p>首先查看files目录</p>
<pre><code>[root@master files]# ll
total 736
-rwxr-xr-x 1 root root 1335 Jun 7 11:01 keepalived
-rw-r--r-- 1 root root 738096 Feb 26 00:48 keepalived-1.4.2.tar.gz
-rw-r--r-- 1 root root 3550 Jun 7 11:02 keepalived.conf
-rw-r--r-- 1 root root 667 Jun 7 11:02 keepalived.sysconfig
</code></pre><p>keepalived文件为keepalived的service启动服务文件,在/etc/init.d/目录下,keepalived.conf 为其基础配置文件,keepalived.sysconfig为启动文件需要的配置文件。</p>
<pre><code>[root@master files]# cat keepalived
#!/bin/sh
#
# Startup script for the Keepalived daemon
#
# processname: keepalived
# pidfile: /var/run/keepalived.pid
# config: /etc/keepalived/keepalived.conf
# chkconfig: - 21 79
# description: Start and stop Keepalived
# Source function library
. /etc/rc.d/init.d/functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived
RETVAL=0
prog="keepalived"
start() {
echo -n $"Starting $prog: "
daemon /usr/local/keepalived/sbin/keepalived ${KEEPALIVED_OPTIONS}
##上面参数是修改之后的,默认的为/sbin/keepalived ${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
}
stop() {
echo -n $"Stopping $prog: "
killproc keepalived
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
}
reload() {
echo -n $"Reloading $prog: "
killproc keepalived -1
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
condrestart)
if [ -f /var/lock/subsys/$prog ]; then
stop
start
fi
;;
status)
status keepalived
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|reload|restart|condrestart|status}"
RETVAL=1
esac
exit $RETVAL
[root@master files]# cat keepalived.conf
##该文件为默认文件,放这里是为了启动过程中有个初始默认文件,后期结合实际生产环境会被修改的,在cluster目录中介绍。
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.16
192.168.200.17
192.168.200.18
}
}
virtual_server 192.168.200.100 443 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.201.100 443 {
weight 1
SSL_GET {
url {
path /
digest ff20ad2481f97b1754ef3e12ecd3a9cc
}
url {
path /mrtg/
digest 9b3a0c85a887a256d6939da88aabd8cd
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.2 1358 {
delay_loop 6
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
sorry_server 192.168.200.200 1358
real_server 192.168.200.2 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 192.168.200.3 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334c
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
virtual_server 10.10.10.3 1358 {
delay_loop 3
lb_algo rr
lb_kind NAT
persistence_timeout 50
protocol TCP
real_server 192.168.200.4 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
real_server 192.168.200.5 1358 {
weight 1
HTTP_GET {
url {
path /testurl/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl2/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
url {
path /testurl3/test.jsp
digest 640205b7b0fc66c1ea91c463fac6334d
}
connect_timeout 3
retry 3
delay_before_retry 3
}
}
}
[root@master files]# cat keepalived.sysconfig
#默认文件,在解压之后的安装包里面
# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp -P Only run with VRRP subsystem.
# --check -C Only run with Health-checker subsystem.
# --dont-release-vrrp -V Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs -I Dont remove IPVS topology on daemon stop.
# --dump-conf -d Dump the configuration data.
# --log-detail -D Detailed log messages.
# --log-facility -S 0-7 Set local syslog facility (default=LOG_DAEMON)
#
KEEPALIVED_OPTIONS="-D"
</code></pre><p>查看keepalived的salt配置文档</p>
<pre><code>[root@master keepalived]# ll
total 4
drwxr-xr-x 2 root root 102 Jun 7 11:03 files
-rw-r--r-- 1 root root 1452 Jun 7 11:18 install_keepalived.sls
[root@master keepalived]# cat install_keepalived.sls
include:
- pkg.pkg-init
dependency_package_install:
pkg.installed:
- names:
- libnl3-devel
- libnfnetlink-devel
keepalived-install:
file.managed:
- name: /usr/local/src/keepalived-1.4.2.tar.gz
- source: salt://keepalived/files/keepalived-1.4.2.tar.gz
- user: root
- group: root
- mode: 755
cmd.run:
- name: cd /usr/local/src && tar -xf keepalived-1.4.2.tar.gz && cd keepalived-1.4.2 && ./configure --prefix=/usr/local/keepalived && make && make install
- unless: test -d /usr/local/keepalived
- require:
- pkg: pkg-init
- pkg: dependency_package_install
- file: keepalived-install
keepalived-init:
file.managed:
- name: /etc/init.d/keepalived
- source: salt://keepalived/files/keepalived
- user: root
- group: root
- mode: 755
cmd.run:
- name: chkconfig --add keepalived && chkconfig --level 2345 keepalived on
- unless: chkconfig --list | grep keepalived
- require:
- file: keepalived-init
/etc/sysconfig/keepalived:
file.managed:
- source: salt://keepalived/files/keepalived.sysconfig
- user: root
- group: root
- mode: 644
/etc/keepalived:
file.directory:
- user: root
- group: root
- mode: 755
/etc/keepalived/keepalived.conf:
file.managed:
- source: salt://keepalived/files/keepalived.conf
- user: root
- group: root
- mode: 644
- require:
- file: /etc/keepalived
</code></pre><p><strong>nginx目录</strong></p>
<pre><code>[root@master nginx]# tree
.
├── files
│ ├── nginx-1.12.2.tar.gz
│ ├── nginx.conf
│ ├── nginx.init
│ ├── pcre-8.41.tar.gz
│ └── zlib-1.2.11.tar.gz
├── nginx-install.sls
├── nginx-service.sls
├── nginx-user.sls
├── pcre-install.sls
└── zlib-install.sls
1 directory, 10 files
</code></pre><p>file目录中为nginx的源码包以及需要的依赖包pcre和zlib的源码包。nginx.conf为nginx的配置文件,nginx.init为启动脚本 既/etc/init.d目录下的service控制服务脚本。</p>
<p> nginx-install.sls 为nginx的安装脚本 nginx-service.sls启动nginx服务脚本 nginx-user.sls 为创建nginx用户脚本 pcre-install.sls zlib-install.sls 分别为安装pcr和zlib的脚本。</p>
<p>files目录下:</p>
<pre><code>[root@master files]# cat nginx.conf
user nginx;
worker_processes auto;
error_log logs/error.log error;
worker_rlimit_nofile 30000;
pid /var/run/nginx.pid;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
underscores_in_headers on;
keepalive_timeout 10;
send_timeout 60;
gzip on;
include /usr/local/nginx/conf/vhost/*.conf;
server {
listen 80;
root /usr/local/nginx/html;
index index.html;
server_name 127.0.0.1;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
}
}
[root@master files]# cat nginx.init
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
##指定nginx的配置文件目录
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?