SaltStack 으로 Nginx 배치 하기
13451 단어 automationSALTconfig_manage
1. 컴 파일 설치 nginx
2. 설정 파일, 서비스, 사용자, 로그 절단, 가상 호스트 의 자동 배치 실현
3. 서로 다른 클 라 이언 트 자원 설정 에 따라 grains 를 이용 하여 가 변 설정 을 실현 합 니 다.
4. pillar 를 이용 하여 클 라 이언 트 기능 차이 설정 실현
환경:
master: 192.168.111.129(Hostname: Server2)
client: 192.168.111.128(Hostname: Server1)
(여 기 는 ID 표지 입 니 다. 서로 다른 설정 을 위해 필요 합 니 다)
창고 루트 디 렉 터 리 설정:
[root@Server2 ~]# vim /etc/salt/master
file_roots:
base:
- /srv/salt
입구 파일 생 성:
[root@Server2 ~]# cat /srv/salt/top.sls
base:
'*':
- nginx.init
먼저 디 렉 터 리 붙 이기:
[root@Server2 nginx]# tree
.
├── conf.sls
├── files
│ ├── nginx
│ ├── nginx-1.4.5.tar.gz
│ ├── nginx.conf
│ ├── nginx_log_cut.sh
│ └── vhost.conf
├── gcc.sls
├── init.sls
├── install.sls
└── vhost.sls
다음 파일 분석:
init.sls
[root@Server2 nginx]# cat init.sls
include:
- nginx.gcc
- nginx.install
- nginx.conf
- nginx.vhost
인용 할 때 nginx 디 렉 터 리 만 지정 하면 됩 니 다. 이 빵 은 nginx 디 렉 터 리 아래 에 있 는 sls 파일 4 개 를 포함 하고 있 습 니 다.
gcc. sls nginx 컴 파일 에 필요 한 패 키 지 를 미리 설치 합 니 다.
[root@Server2 nginx]# cat gcc.sls
gcc:
pkg:
- name: gcc
- installed
gcc-c++:
pkg:
- name: gcc-c++
- installed
make:
pkg:
- name: make
- installed
cmake:
pkg:
- name: cmake
- installed
install.sls nginx 설치
[root@Server2 nginx]# cat install.sls
#nginx.tar.gz
nginx_source:
file.managed:
- name: /tmp/nginx-1.4.5.tar.gz
- unless: test -e /tmp/nginx-1.4.5.tar.gz
- source: salt://nginx/files/nginx-1.4.5.tar.gz
#extract
extract_nginx:
cmd.run:
- cwd: /tmp
- names:
- tar zxvf nginx-1.4.5.tar.gz
- unless: test -d /tmp/nginx-1.4.5
- require:
- file: nginx_source
#user
nginx_user:
user.present:
- name: nginx
- uid: 1501
- createhome: False
- gid_from_name: True
- shell: /sbin/nologin
#nginx_pkgs
nginx_pkg:
pkg.installed:
- pkgs:
- gcc
- openssl-devel
- pcre-devel
- zlib-devel
#nginx_compile
nginx_compile:
cmd.run:
- cwd: /tmp/nginx-1.4.5
- names:
- ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/client/ --http-proxy-temp-path=/usr/local/nginx/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_random_index_module --with-pcre --with-http_stub_status_module
- make
- make install
- require:
- cmd: extract_nginx
- pkg: nginx_pkg
- unless: test -d /usr/local/nginx
#cache_dir
cache_dir:
cmd.run:
- names:
- mkdir -p /usr/local/nginx/{client,proxy,fcgi} && chown -R nginx.nginx /usr/local/nginx/
- unless: test -d /usr/local/nginx/client/
- require:
- cmd: nginx_compile
nginx 컴 파일 설치, 파일 관리, 패키지 관리, 사용자 관리 및 cmd 활용 과 관련 되 어 있 습 니 다. 그 중에서 주의해 야 할 것 은 cmd 를 사용 하면 클 라 이언 트 를 동기 화 할 때마다 실 행 됩 니 다. 이 현상 을 방지 하기 위해 unless 를 사용 하면 해결 할 수 있 습 니 다.
설치 후 설정 파일 관리 conf. sls 를 보 겠 습 니 다.
[root@Server2 nginx]# cat conf.sls
include:
- nginx.install
{% set nginx_user = 'nginx' + ' ' + 'nginx' %}
nginx_conf:
file.managed:
- name: /usr/local/nginx/conf/nginx.conf
- source: salt://nginx/files/nginx.conf
- template: jinja
- defaults:
nginx_user: {{ nginx_user }}
num_cpus: {{grains['num_cpus']}}
nginx_service:
file.managed:
- name: /etc/init.d/nginx
- user: root
- mode: 755
- source: salt://nginx/files/nginx
cmd.run:
- names:
- /sbin/chkconfig --add nginx
- /sbin/chkconfig nginx on
- unless: /sbin/chkconfig --list nginx
service.running:
- name: nginx
- enable: True
- reload: True
- watch:
- file: /usr/local/nginx/conf/*.conf
nginx_log_cut:
file.managed:
- name: /usr/local/nginx/sbin/nginx_log_cut.sh
- source: salt://nginx/files/nginx_log_cut.sh
cron.present:
- name: sh /usr/local/nginx/sbin/nginx_log_cut.sh
- user: root
- minute: 10
- hour: 0
- require:
- file: nginx_log_cut
nginx. conf, nginx log cut. sh, nginx 세 파일 을 사 용 했 습 니 다. 이 세 파일 은 모두 nginx / files 디 렉 터 리 에 저 장 됩 니 다. 살 펴 보 겠 습 니 다.
nginx 시작 스 크 립 트
[root@Server2 files]# cat nginx
#!/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: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/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_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
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=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
nginx 주 프로필
[root@Server2 files]# cat nginx.conf
#
user {{ nginx_user }};
worker_processes {{grains['num_cpus']}};
error_log logs/nginx_error.log notice;
pid /usr/local/nginx/sbin/nginx.pid;
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 128m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
server_tokens off;
client_body_buffer_size 512k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$host"' ;
include vhost*.conf;
}
로그 절단 스 크 립 트
[root@Server2 files]# cat nginx_log_cut.sh
#!/bin/bash
logs_path=/usr/local/nginx/logs
yesterday=`date -d "yesterday" +%F`
mkdir -p $logs_path/$yesterday
cd $logs_path
for nginx_logs in `ls *log` ;
do
mv $nginx_logs ${yesterday}/${yesterday}-${nginx_logs}
kill -USR1 `cat /usr/local/nginx/sbin/nginx.pid`
done
가상 호스트 의 설정 은 pillar 에 사 용 됩 니 다. pillar 설정 에 따라 client 에서 서로 다른 설정 파일 을 사용 합 니 다. 먼저 pillar 설정 을 보십시오.
pillar 디 렉 터 리
[root@Server2 pillar]# pwd
/srv/pillar
[root@Server2 pillar]# ls
top.sls vhost.sls
pillar 설정
[root@Server2 pillar]# cat top.sls
base:
'*':
- vhost
[root@Server2 pillar]# cat vhost.sls
vhost:
{% if 'Server' in grains['id'] %}
- name: www
target: /usr/local/nginx/conf/vhost_www.conf
{% else %}
- name: bbs
target: /usr/local/nginx/conf/vhost_bbs.conf
{% endif %}
다음은 가상 호스트 의 프로필 관리 vhost. sls 입 니 다.
[root@Server2 nginx]# pwd
/srv/salt/nginx
[root@Server2 nginx]# cat vhost.sls
include:
- nginx.install
{% for vhostname in pillar['vhost'] %}
{{vhostname['name']}}:
file.managed:
- name: {{vhostname['target']}}
- source: salt://nginx/files/vhost.conf
- target: {{vhostname['target']}}
- template: jinja
- defaults:
server_name: {{grains['fqdn_ip4'][0]}}
log_name: {{vhostname['name']}}
- watch_in:
service: nginx
{% endfor %}
여기에 vhost. conf 파일 을 사 용 했 습 니 다. 살 펴 보 겠 습 니 다.
[root@Server2 files]# pwd
/srv/salt/nginx/files
[root@Server2 files]# cat vhost.conf
server
{
listen 80;
server_name {{ server_name }};
index index.html index.htm ;
root html;
#location ~ .*\.(php|php5)?$
# {
# try_files $uri =404;
# fastcgi_pass unix:/tmp/php-cgi.sock;
# fastcgi_index index.php;
# include fcgi.conf;
# }
location /status {
stub_status on;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1d;
}
access_log logs/{{ log_name }}-access.log main;
}
자, 이상 은 모든 설정 입 니 다. 다음은 실행 결 과 를 살 펴 보 겠 습 니 다.
salt 'Server1' state.highstate
Summary
-------------
Succeeded: 17
Failed: 0
-------------
Total: 17
실행 에 성 공 했 습 니 다. 설정 파일 을 보십시오.
[root@Server1 conf]# ls -lt *.conf
-rw-r--r--. 1 nginx nginx 963 Apr 4 20:06 vhost_www.conf
-rw-r--r--. 1 nginx nginx 1339 Apr 4 20:06 nginx.conf
............
[root@Server1 conf]# cat vhost_www.conf
server
{
listen 80;
server_name 192.168.111.128;
index index.html index.htm ;
root html;
#location ~ .*\.(php|php5)?$
# {
# try_files $uri =404;
# fastcgi_pass unix:/tmp/php-cgi.sock;
# fastcgi_index index.php;
# include fcgi.conf;
# }
location /status {
stub_status on;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1d;
}
access_log logs/www-access.log main;
}
위 에 있 는 pillar 설정 과 일치 합 니 다. grains [id] 에는 'Server' 가 포함 되 어 있 습 니 다. 설정 파일 은 vhost www. conf 입 니 다.
192.168.11.128 의 집행 결 과 를 살 펴 보 겠 습 니 다.
[root@Server1 conf]# ls -lt *.conf
-rw-r--r--. 1 nginx nginx 963 Apr 4 21:15 vhost_www.conf
............
본 고 는 '모든 것 이 바람 에 날 리 게' 블 로그 에서 나 온 것 으로 전 재 를 사절 합 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Azure Monitor, Azure Automation (Runbooks)을 사용하여 App Service (PaaS)의 모니터링 알림 대상 (Webhook)을 Chatwork로 만들기PaaS는 Zabbix Agent와 같은 감시 에이전트를 설치할 수 없기 때문에 감시 힘들다. 그렇지만 IaaS로 DB라든가 만들면 감시는 에이전트로 할 수 있지만, 메인터넌스가 힘들다. 가능하면 PaaS 장애를 C...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.