龙空技术网

搭建环境tomcat+nginx+keepalived+zabbix

Cyylog 1695

前言:

而今你们对“mysql授权网段”大体比较注意,朋友们都想要剖析一些“mysql授权网段”的相关文章。那么小编同时在网络上收集了一些对于“mysql授权网段””的相关知识,希望朋友们能喜欢,同学们一起来了解一下吧!

准备机子和环境

10.3.145.63 Tomcat(apache-tomcat-8.0.53)​10.3.145.64 nginx(nginx version: nginx/1.12.2)  ​10.3.145.39 nginx.bak(nginx version: nginx/1.12.2)​10.3.145.65 zabbinx-server(4.0) + ansible(ansible 2.8.0)​## 所有的机子都执行以下命令初始化环境​systemctl stop firewalldsystemctl disable firewalldsed -ri s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/configsetenforce 0​curl -o /etc/yum.repos.d/CentOS-Base.repo  -o /etc/yum.repos.d/epel.repo  clean allyum makecache fast​yum install -y wget ntpdate net-tools vim bash-completion ShellCheckntpdate -b ntp1.aliyun.com​

为10.3.145.63 部署JDK&Tomcat

## 先将已经下载的jdk压缩包解压到 /usr/local/下,并改名为java[root@10v3v145v63 src]# tar -xf jdk-8u20-linux-x64.tar.gz -C /usr/local/[root@10v3v145v63 src]# mv /usr/local/jdk1.8.0_20 /usr/local/java​## 配置jdk的环境配置[root@10v3v145v63 src]# vim /etc/profile.d/java.shJAVA_HOME=/usr/local/javaPATH=$JAVA_HOME/bin:$PATHexport JAVA_HOME PATH​## 检查jdk是否安装成功[root@10v3v145v63 src]# source /etc/profile.d/java.sh[root@10v3v145v63 src]# java -versionjava version "1.8.0_20"Java(TM) SE Runtime Environment (build 1.8.0_20-b26)Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)## 如果java -version能查看到以上信息,则java配置成功​## 解压Tomcat压缩包到 /usr/local 下,并改名为tomcat1[root@10v3v145v63 src]# tar -xf apache-tomcat-8.0.53.tar.gz -C /usr/local/[root@10v3v145v63 src]# mv /usr/local/apache-tomcat-8.0.53 /usr/local/tomcat1​## 部署多台tomcat[root@10v3v145v63 src]# cd ../[root@10v3v145v63 local]# cp -R tomcat1 tomcat2[root@10v3v145v63 local]# cp -R tomcat1 tomcat3​## 修改tomcat的配置[root@10v3v145v63 local]# sed -i s/8080/8081/g /usr/local/tomcat2/conf/server.xml[root@10v3v145v63 local]# sed -i s/8080/8082/g /usr/local/tomcat3/conf/server.xml[root@10v3v145v63 local]# sed -i s/8005/8012/g /usr/local/tomcat2/conf/server.xml[root@10v3v145v63 local]# sed -i s/8005/8013/g /usr/local/tomcat3/conf/server.xml​## 启动tomcat[root@10v3v145v63 local]# ./tomcat1/bin/startup.sh[root@10v3v145v63 local]# ./tomcat2/bin/startup.sh[root@10v3v145v63 local]# ./tomcat3/bin/startup.sh​## 查看端口是否启动了tomcat[root@10v3v145v63 local]# ss -tunlp | grep 80tcp    LISTEN     0      100      :::8009                 :::*                   users:(("java",pid=38902,fd=52))tcp    LISTEN     0      1      ::ffff:127.0.0.1:8012                 :::*                   users:(("java",pid=39103,fd=63))tcp    LISTEN     0      1      ::ffff:127.0.0.1:8013                 :::*                   users:(("java",pid=39210,fd=63))tcp    LISTEN     0      100      :::8080                 :::*                   users:(("java",pid=38902,fd=47))tcp    LISTEN     0      100      :::8081                 :::*                   users:(("java",pid=39103,fd=47))tcp    LISTEN     0      100      :::8082                 :::*                   users:(("java",pid=39210,fd=47))tcp    LISTEN     0      1      ::ffff:127.0.0.1:8005                 :::*                   users:(("java",pid=38902,fd=74))​## 三个端口 8080,8081,8082 已经启动了,说明tomcat服务启动成功​修改tomcat的访问页面,进行区分
echo "111" > tomcat1/webapps/ROOT/index.jspecho "222" > tomcat2/webapps/ROOT/index.jspecho "333" > tomcat3/webapps/ROOT/index.jsp​用浏览器访问10.3.145.63:8080  10.3.145.64:8081 10.3.145.65:8082能分别看到"111","222","333"---部署成功​

为10.3.145.65安装anxible服务先为其他几台机子执行 copy-key.sh 脚本(附录1),传递公钥

​[root@10v3v145v65 ~]# bash copy-key.sh 

在10.3.145.65上部署ansible服务,并进行配置

## yum安装ansible[root@10v3v145v65 ~]# yum -y install ansible​## 配置ansible的hosts[root@10v3v145v65 ~]# vim /etc/ansible/hosts [nginx]10.3.145.3910.3.145.64​​## 创建data目录[root@10v3v145v65 ~]# mkdir /data#在/data下写安装nginx的剧本---创建以下目录结构的目录[root@10v3v145v65 roles]# tree ..├── nginx│   ├── files│   ├── handlers│   │   └── main.yaml│   ├── tasks│   │   └── main.yaml│   └── templates│       └── nginx.conf.j2└── site.yaml​[root@10v3v145v65 ~]# mkdir /data/roles/nginx/{files,handlers,tasks,templates}  -p​---编写剧本[root@10v3v145v65 ~]# cd /data/roles[root@10v3v145v65 roles]# vim site.yaml- hosts: nginx  roles:  - nginx  [root@10v3v145v65 roles]# cd nginx/  [root@10v3v145v65 nginx]# vim tasks/main.yaml---- name: install nginx packge  yum: name=nginx state=present​- name: copy nginx.conf template  template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf  notify: restart nginx​- name: make sure nginx service running  service: name=nginx state=started enabled=yes  [root@10v3v145v65 nginx]# vim handlers/main.yaml---- name: restart nginx  service: name=nginx state=restarted  ​## 编写配置文件[root@10v3v145v65 nginx]# vim templates/nginx.conf.j2​# For more information on configuration, see:#   * Official English Documentation:    * Official Russian Documentation: ​user nginx nginx;worker_processes auto;error_log /var/log/nginx/error.log;pid /run/nginx.pid;​# Load dynamic modules. See /usr/share/nginx/README.dynamic.include /usr/share/nginx/modules/*.conf;​events {    worker_connections 1024;} http {    upstream web1 {    server 10.3.145.63:8080;    }​    upstream web2 {    server 10.3.145.63:8081;    }​    upstream web3 {    server 10.3.145.63:8082;    }    server {        listen       80 default_server;        listen       [::]:80 default_server;        server_name  cyylog;        root         /usr/share/nginx/html;​        include /etc/nginx/default.d/*.conf;​        location / {                proxy_set_header Host http_host;                proxy_set_header X-Real-IP remote_addr;                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        }                location /web1  {        root html;        index index.jsp index.htm;        proxy_pass ;        }​        location /web2  {        root html;        index index.jsp index.htm;        proxy_pass ;        }        location /web3  {        root html;        index index.jsp index.htm;        proxy_pass ;        }        error_page 500 502 503 504 /50x.html;            location = /50x.html {        }    }}​## 实施 [root@10v3v145v65 nginx]# ansible-playbook ../site.yaml --syntax-check  //检查语法[root@10v3v145v65 nginx]# ansible-playbook ../site.yaml     //实施剧本​###修改本地解析[root@10v3v145v65 nginx]# vim /etc/hosts10.3.145.39     cyylog10.3.145.64     cyylog​###本地访问[root@10v3v145v65 nginx]# curl cyyog/web1[root@10v3v145v65 nginx]# curl cyylog/web2[root@10v3v145v65 nginx]# curl cyylog/web3​能分别看到"111","222","333"则为部署成功​​
在10.3.145.39(备)和10.3.145.64(主)上部署keepalived两台部署和配置大部分一样,不同之处有说明
## 安装keepalived[root@10v3v145v64 ~]# yum -y install keepalived​## 修改配置文件[root@10v3v145v64 ~]# vim /etc/keepalived/keepalived.conf​! Configuration File for keepalived​global_defs {        router_id 10v3v145v64 #另一台机器为10v3v145v39   }​vrrp_script check_nginx {   script "/etc/keepalived/check_nginx_status.sh"   interval 5}​vrrp_instance VI_1 {    state BACKUP    nopreempt    interface ens33    virtual_router_id 51    priority 100        #辅助改为80    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        10.3.145.60     #虚拟IP地址    }    track_script {        check_nginx    }}​[root@10v3v145v64 ~]# vim /etc/keepalived/check_nginx_status.sh     //脚本文件#!/bin/bash                                                                                                     /usr/bin/curl -I  &>/dev/nullif [ $? -ne 0 ];then        systemctl restart keepalivedfi​##  注:必须先启动nginx,再启动keepalived[root@10v3v145v64 ~]# systemctl start keepalived[root@10v3v145v64 ~]# ip a能看到10.3.145.60/32则启动成功###测试服务,在64上 stop nginx 的服务,查看到10.3.145.60/32的IP不在64上,而是在39上,则说明服务没有问题。​
增加hosts解析
10.3.145.60     cyylog

zabbix自动发现

在一台机器(例10.3.145.65) 部署zabbix-server,实现监控。 其他机器均须部署 zabbix-agent (10.3.145.39、10.3.145.64、10.3.145.63)

安装zabbix-server

## 安装zabbix源(官方)(四台机子都进行配置)[root@10v3v145v65 roles]#rpm -ivh ​## 而后更新我们的yum仓库:[root@10v3v145v65 roles]# yum repolist ​## 安装[root@10v3v145v65 ~]# yum -y install zabbix-agent zabbix-get zabbix-sender zabbix-server-mysql zabbix-web zabbix-web-mysql​​
## 1.创建 mariadb.repo[root@10v3v145v65 roles]# vim /etc/yum.repos.d/mariadb.repo[mariadb]name = MariaDB baseurl =  gpgkey= gpgcheck=12.下载安装[root@10v3v145v65 roles]# yum -y install MariaDB-server MariaDB-client3.首先,我们修改一下配置文件——/etc/my.cnf.d/server.cnf [root@10v3v145v65 roles]#vim /etc/my.cnf.d/server.cnf [mysqld]    skip_name_resolve = ON          #跳过主机名解析    innodb_file_per_table = ON      #    innodb_buffer_pool_size = 256M  #缓存池大小    max_connections = 2000          #最大连接数    log-bin = master-log            #开启二进制日志4.重启我们的数据库服务[root@10v3v145v65 roles]# systemctl restart mariadb[root@10v3v145v65 roles]# mysql_secure_installation		#初始化mariadb5.登录数据库#mysqlMariaDB [(none)]> create database zabbix character set 'utf8';  # 创建zabbix数据库MariaDB [(none)]> grant all on zabbix.* to 'zbxuser'@'10.3.145.%' identified by 'keer';										# 注意授权网段MariaDB [(none)]> flush privileges;           # 刷新授权6.导入表首先,我们来查看一下,`zabbix-server-mysql`这个包提供了什么/usr/share/doc/zabbix-server-mysql-4.0.9/create.sql.gz      #生成表的各种脚本 #gzip -d create.sql.gz #head  create.sql   #查看内容 vim /usr/share/doc/zabbix-server-mysql-4.0.9/create.sql  USER zabbix    #如果缺少,第一行添加 7.导入数据 cd /usr/share/doc/zabbix-server-mysql-4.0.9/[root@10v3v145v65 zabbix-server-mysql-4.0.9]# mysql -uzbxuser -h10.3.145.65 -p zabbix < create.sql 8.修改配置文件 cd /etc/zabbix/ cp zabbix_server.conf{,.bak}  #为了可以反悔,cp一份 vim zabbix_server.conf #修改后配置如下ListenPort=10051            #默认监听端口SourceIP=10.3.145.65       #发采样数据请求的IP(本机)DBHost=10.3.145.65         #数据库对外的主机DBName=zabbix               #数据库名称DBUser=zbxuser              #数据库用户DBPassword=keer             #数据库密码DBPort=3306                 #数据库启动端口9.开启服务#systemctl start zabbix-server.service#ss -nutl |grep 10051tcp    LISTEN     0      128       *:10051                 *:*                  tcp    LISTEN     0      128      :::10051                :::*    Alias /zabbix /usr/share/zabbix     #我们访问的时候要在主机后加上/zabbix来访问我们这个服务#vim /etc/php.ini      #配置时区    [Date]    ; Defines the default timezone used by the date functions    ;     date.timezone = Asia/Shanghai   10.接下来,我们就可以启动我们的`httpd`服务了:[root@10v3v145v65 zabbix]# systemctl restart httpd.service ## 访问 10.3.145.65/zabbix## 进行初始化配置

ansible安装zabbix-agent

## 添加ansibled的hosts解析[root@10v3v145v65 ~]#  vim /etc/ansible/hosts[zabbix-agent]10.3.145.3910.3.145.6410.3.145.63## 写剧本---创建以下目录[root@10v3v145v65 roles]# tree ..├── site.yaml└── zabbix-agent    ├── files    ├── handlers    │   └── main.yaml    ├── tasks    │   └── main.yaml    └── templates        └── zabbix_agentd.conf.j2[root@10v3v145v65 ~]#  mkdir /data/roles/zabbix-agent/{files,handlers,tasks,templates} -p[root@10v3v145v65 ~]# cd /data/roles/zabbix-agent[root@10v3v145v65 roles]# vim ../site.yaml - hosts: zabbix-agent  roles:  - zabbix-agent[root@10v3v145v65 zabbix-agent]# vim handlers/main.yaml---- name: restart zabbix-agent  service: name=zabbix-agent state=restarted[root@10v3v145v65 zabbix-agent]# vim tasks/main.yaml---- name: install zabbix-agent packge  yum: name=zabbix-agent state=present- name: copy zabbix-agent.conf template  template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf  notify: restart zabbix-agent- name: make sure zabbix-agent service running  service: name=zabbix-agent state=started enabled=yes[root@10v3v145v65 zabbix-agent]# cp /etc/zabbix/zabbix_agentd.conf ./templates/zabbix_agentd.conf.j2## 编写配置文件[root@10v3v145v65 zabbix-agent]# vim templates/zabbix_agentd.conf.j2Server=192.168.195.139///服务的IP			ServerActive=192.169.195.139///服务的IPHostname=192v168v195v139///服务的hostname## 修改这三项即可 ##[root@10v3v145v65 zabbix-agent]# ansible-playbook ./site.yaml --syntax-check //检查语法[root@10v3v145v65 zabbix-agent]# ansible-playbook ./site.yaml	//实施剧本
配置自动发现

一、创建发现规则

Configuration ---- discovery ---- Create discovery rule

图片-1

图片-2

二、主机自动加入主机组并关联模板

2.1、为discovery(发现)创建action(动作)

Configuration ---- Actions ---- Event source(Discovery) ---- Create action

图片-3

图片-4

图片-5

图片-6

图片-7

架构图

架构图

附录1

[root@10v3v145v65 ~]# vim copy-key.sh#!/usr/bin/env bash## Author: cyy# GitHub: cyylog# usage: transfer keyrpm -qa | grep expectif [ ! $? -eq 0 ];then  yum -y install expectfiCreateKey(){  /usr/bin/expect <<-EOF  spawn ssh-keygen  expect ":" { send "\r" }  expect ":" { send "\r" }  expect ":" { send "\r" }  expect eofEOF}TransferKey(){/usr/bin/expect <<-EOF  spawn ssh-copy-id $User@$Ip  expect "yes/no" { send "yes\r" }  expect "password:" { send "$Passwd\r" }  expect eofEOF}Information(){  read -p"please input your username: " User  read -p"please input your IPaddress: " Ip  read -p"please input your passeord: " Passwd}if [ -f $HOME/.ssh/id_rsa ];then  Information  TransferKeyelse  CreateKey  Information  TransferKeyfi

标签: #mysql授权网段