運維工作中經常用到的一些知識總結(四)
http://blog.51cto.com/bobo365/2125159
54、rabbitMQ
http://blog.csdn.net/lishaojun0115/article/details/53152255
用戶管理
用戶管理包括增加用戶,刪除用戶,查看用戶列表,修改用戶密碼。
(1) 新增一個用戶
rabbitmqctl add_user Username Password
(2) 刪除一個用戶
rabbitmqctl delete_user Username
(3) 修改用戶的密碼
rabbitmqctl change_password Username Newpassword
(4) 查看當前用戶列表
rabbitmqctl list_users
設置用戶角色的命令為:
rabbitmqctl set_user_tags User Tag
用戶角色可分為五類,超級管理員, 監控者, 策略制定者, 普通管理者以及其他。
(1) 超級管理員(administrator)
可登陸管理控制臺(啟用management plugin的情況下),可查看所有的信息,並且可以對用戶,策略(policy)進行操作。
(2) 監控者(monitoring)
可登陸管理控制臺(啟用management plugin的情況下),同時可以查看rabbitmq節點的相關信息(進程數,內存使用情況,磁盤使用情況等)
(3) 策略制定者(policymaker)
可登陸管理控制臺(啟用management plugin的情況下), 同時可以對policy進行管理。但無法查看節點的相關信息(上圖紅框標識的部分)。
(4) 普通管理者(management)
僅可登陸管理控制臺(啟用management plugin的情況下),無法看到節點信息,也無法對策略進行管理。
(5) 其他
無法登陸管理控制臺,通常就是普通的生產者和消費者。
設置用戶權限
rabbitmqctl set_permissions -p VHostPath User ConfP WriteP ReadP
例如:
(1)設置guest用戶對路徑 / 的configuration、write、read權限
rabbitmqctl set_permissions -p / guest “” “.” “.*”
(2) 查看(指定hostpath)所有用戶的權限信息
rabbitmqctl list_permissions [-p VHostPath]
(3) 查看指定用戶的權限信息
rabbitmqctl list_user_permissions User
(4) 清除用戶的權限信息
rabbitmqctl clear_permissions [-p VHostPath] User
55、yourls部署:
LNMP環境
(1)創建數據庫:
CREATE DATABASE yourls;
GRANT ALL PRIVILEGES ON yourls.* TO ‘yourls‘@‘%‘ IDENTIFIED BY ‘bobo365‘;
FLUSH PRIVILEGES;
\q
(2)主配置文件
Copy user/config-sample.php to user/config.php
define( ‘YOURLS_DB_USER‘, ‘yourls‘ );
define( ‘YOURLS_DB_PASS‘, ‘bobo365‘ );
define( ‘YOURLS_DB_NAME‘, ‘yourls‘ );
define( ‘YOURLS_DB_HOST‘, ‘10.150.27.51‘ );
define( ‘YOURLS_DB_PREFIX‘, ‘yourls_‘ );
define( ‘YOURLS_SITE‘, ‘http://bobo365.com‘ );
define( ‘YOURLS_HOURS_OFFSET‘, 0 );
define( ‘YOURLS_LANG‘, ‘‘ );
define( ‘YOURLS_UNIQUE_URLS‘, true );
define( ‘YOURLS_PRIVATE‘, true );
define( ‘YOURLS_CObobo365IEKEY‘, ‘qQ4KhL_pu|s@Zm7n#%:b^{A[vhm‘ );
define( ‘YOURLS_DEBUG‘, false );
define( ‘YOURLS_URL_CONVERT‘, 36 );
(3)Nginx配置
[root@bobo365 conf.d]# more your.conf
server {
listen 80;
server_name bobo365.com www.bobo365.com;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 500m;
rewrite ^/([0-9A-Za-z]+)/?$ /yourls-go.php?id=$1 last;
rewrite ^/([0-9A-Za-z]+)\+/?$ /yourls-infos.php?id=$1 last;
rewrite ^/([0-9A-Za-z]+)\+all/?$ /yourls-infos.php?id=$1&all=1 last;
location / {
root /data/php;
index index.html;
}
location ~ \.php$ {
root /data/php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
(4)安裝
http://bobo365.com/admin/install.php
56、Tomcat多實例
/usr/local/apache-tomcat-8.0.26
[root@promotion ~]# cd /usr/local/tomcat_instance/
drwxr-xr-x 8 root root 4096 Feb 8 10:06 tomcat1
drwxr-xr-x 8 root root 4096 Feb 8 10:56 tomcat2
drwxr-xr-x 8 root root 4096 Feb 8 10:56 tomcat3
drwxr-xr-x 8 root root 4096 Feb 8 10:15 tomcat4
drwxr-xr-x 8 root root 4096 Feb 8 10:56 tomcat5
[root@promotion tomcat_instance]# cd tomcat1/
drwxr-xr-x 2 root root 4096 Mar 8 16:41 bin
drwxr-xr-x 3 root root 4096 Feb 11 17:19 conf
drwxr-xr-x 2 root root 4096 Mar 8 11:28 logs
drwxr-xr-x 2 root root 4096 Feb 8 10:06 temp
drwxr-xr-x 3 root root 4096 Mar 8 13:57 webapps
drwxr-xr-x 3 root root 4096 Feb 8 10:06 work
端口:
shutdown 8015 8025 8035 8045 8055
HTTP 8888 8889 8890 8891 8892
AJP 8019 8029 8039 8049 8059
webapps目錄:
/usr/local/tomcat_instance/tomcat1/webapps
/usr/local/tomcat_instance/tomcat2/webapps
/usr/local/tomcat_instance/tomcat3/webapps
/usr/local/tomcat_instance/tomcat4/webapps
/usr/local/tomcat_instance/tomcat5/webapps
[root@promotion tomcat1]# tree
.
├── bin
│?? ├── shutdown.sh
│?? ├── startup.sh
│?? └── tomcat.pid
├── conf
│?? ├── Catalina
│?? │?? └── localhost
│?? ├── catalina.policy
│?? ├── catalina.properties
│?? ├── context.xml
│?? ├── logging.properties
│?? ├── server.xml
│?? ├── server.xml_ori
│?? ├── tomcat-users.xml
│?? ├── tomcat-users.xsd
│?? └── web.xml
more conf/server.xml
<Server port="8015" shutdown="SHUTDOWN">
<Connector port="8888" redirectPort="8443"
minProcessors="10" maxProcessors="1000"
maxThreads="5001" minSpareThreads="1000" maxSpareThreads="2000"
acceptCount="31500" connectionTimeout="60000" disableUploadTimeout="true" debug="0" URIEncoding="UTF-8"/>
<Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />
<Host name="localhost" appBase="/usr/local/tomcat_instance/tomcat1/webapps"
unpackWARs="true" autoDeploy="true">
startup.sh
[root@promotion bin]# vim ../../tomcat1/bin/startup.sh
#!/bin/bash
export JRE_HOME=/usr/local/jdk1.8.0_60/jre
export CATALINA_HOME=/usr/local/apache-tomcat-8.0.26
export CATALINA_BASE="/usr/local/tomcat_instance/tomcat1"
export CATALINA_TMPDIR="$CATALINA_BASE/temp"
export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
export JAVA_OPTS="-Xmx4096m -Xms4096m -Xmn2g -Xss256k -Dtomcat.name=tomcat1"
#創建logs目錄
if [ ! -d "$CATALINA_BASE/logs" ]; then
mkdir $CATALINA_BASE/logs
fi
#創建temp目錄
if [ ! -d "$CATALINA_BASE/temp" ]; then
mkdir $CATALINA_BASE/temp
fi
#調用tomcat啟動腳本
bash $CATALINA_HOME/bin/startup.sh "$@"
57、sftp上傳文件
[root@ext script]# more test.sh
#!/bin/bash
USER=bobo365
PASSWORD=bobo365
SRCDIR=/home/bobo365
TODAY=`date +"%Y%m%d"`
DESDIR=./upload/${TODAY}
IP=192.168.2.133
PORT=20002
cd ${SRCDIR}
FILES=`find ${SRCDIR} -name ‘*.tar.gz‘`
for FILE in ${FILES}
do
echo ${FILE}
lftp -u ${USER},${PASSWORD} sftp://${IP}:${PORT} <<EOF
#mkdir ${DESDIR}
cd ${DESDIR}/
lcd ${SRCDIR}
put ${FILE}
echo "put ${FILE} finished."
by
EOF
done
lftp -u lzdat,lzdat@201711 sftp://192.168.2.23:52000 <<EOF
cd upload/20180313/
put /upload/in_sheet_20180313.csv
echo "put in_sheet_20180313.csv finished."
by
EOF
58、Nginx轉發配置
在Nginx中的默認Proxy是只能對後面Real Server做端口轉發的,而不能做域名轉發。如果想使用Nginx對後端是同一IP、同一端口 轉發不同的域名則需要配置Nginx Proxy。
這個是因為默認情況下:
proxy_set_header Host $proxy_host;
這樣就等於前端輸入域名後到nginx這裏直接轉換成IP進行轉發了。
於是我們需要修改proxy_set_header的參數。
proxy_set_header Host $http_host;
例:
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_set_header Host $host;
proxy_set_header Referer $http_referer;
proxy_set_header Cobobo365ie $http_cobobo365ie;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Accept-Encoding ‘‘;
proxy_hide_header Vary;
proxy_redirect off;
59、壓力測試
yum -y install epel-release
yum install python-pip
[root@bobo365 ~]# more load_test.py
from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
@task(1)
def baidu(self):
self.client.get("/")
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 3000
max_wait = 6000
locust -f load_test.py --host=https://www.baidu.com
60、JVM故障排查
jps獲取java進程的pid
jstack -F PID >> java.txt 導出cpu占用高進程的線程棧
top -H -p PID查看對應進程的哪個線程占用cpu過高
echo "obase=16; PID" | bc將線程的PID轉化為16進制
在第二步導出的java.txt中查找轉化為16進制的線程的pid。找到對應的線程棧。
分析負載高的線程棧都是什麽業務操作。優化程序並處理問題。
61、MySQL優化
http://blog.51cto.com/leesir/2084306
硬件層優化
目標一: 全面關閉節能模式,讓MySQL跑在高性能模式下
關閉CPU節能,OPI Link Speed Select “Max Performance”
關閉內存節能模式,Memory Speed Select “Max Performance”
Power C-States Select “Disable”
C1 Enhanced Mode Select "Disable"
目標二:關閉NUMA,讓CPU能始終高效的使用內存
關閉NUMA,Socket Interleave,"Non-NUMA"
目標三:提升IOPS性能
SSD/PCIe-SSD
機械盤搭配陣列卡,cache策略,BBU電池,RAID-10,15KRPM
系統層的優化
目標一:提升IOPS性能
配置合理的I/O調度器
機械盤配deadline,執行命令echo deadline >/sys/block/sda/queue/scheduler
固態盤配noop,執行命令echo noop >/sys/block/sda/queue/scheduler
文件系統盡量使用XFS
mount參數增加noatime,nodiratime,nobarrier
vi /etc/fstab
/dev/sda1 /data xfs defaults,noatime,nodiratime,nobarrier 0 0
/dev/sda2 / xfs defaults,noatime,nodiratime,nobarrier 0 0
/dev/sda3 swap swap defaults,noatime,nodiratime,nobarrier 0 0
mount -o remount /data
mount
目標二:減少SWAP使用傾向甚至禁掉,穩定磁盤I/O和網絡減少等待時間
1.vm.swappiness設為5甚至0,假如不關心發生OOM
echo ‘vm.swappiness = 5‘ >>/etc/sysctl.conf
/sbin/sysctl -p
2.vm.dirty_background_ratio設為5,vm.dirty_ratio設為10,讓臟頁持續刷入磁盤,避免磁盤I/O瞬間寫產生TIME_WAIT
echo ‘vm.dirty_background_ration = 5‘ >>/etc/sysctl.conf
echo ‘vm.dirty_ratio = 10‘ >>/etc/sysctl.conf
/sbin/sysctl -p
3.net.ipv4.tcp_tw_recycle和net.ipv4.tcp_tw_reuse設為雙1,減少網絡等待時間,提高效率
echo ‘net.ipv4.tcp_tw_recycle = 1‘ >>/etc/sysctl.conf
echo ‘net.ipv4.tcp_tw_reuse = 1‘ >>/etc/sysctl.conf
/sbin/sysctl -p
MySQL層的優化
略...
62、vim是自動開啟智能縮進導致#號
開啟
set paste
關閉
set nopaste
或
set paste!
63、PHP
yum install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm -y
yum --enablerepo remi --enablerepo remi-php56 install php ……
yum --enablerepo remi --enablerepo remi-php70 install php ……
yum --enablerepo remi --enablerepo remi-php71 install php ……
升級:
yum -y install epel-release
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
yum -y remove php* php-common?
yum -y install php70w? php70w-opcache
yum -y install php70w-mysql php70w-xml php70w-soap php70w-xmlrpc php70w-mbstring php70w-json php70w-gd php70w-mcrypt
yum list | grep php70w*
yum -y install php70w-pecl-redis.x86_64 php70w-pecl-memcached.x86_64
systemctl status php-fpm
64、NGINX
yum install http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm -y
yum install nginx
65、MySQL:
toolkit:
- 分析慢查詢:
./pt-query-digest /data/percona-server/logs/mysql-slow.log > query.log
- 碎片整理:
/pt-online-schema-change --user=root --password=bobo365 --host=localhost --alter="ENGINE=Innodb" D=yourls,t=yourls_url --execute
表統計信息:
- 統計每個庫的大小:
select table_schema,sum(data_length)/1024/1024/1024 as data_length,sum(index_length)/1024/1024/1024 as index_length,sum(data_length+index_length)/1024/1024/1024 as sum_data_index from information_schema.tables where table_schema!=‘information_schema‘ and table_schema!=‘mysql‘ group by table_schema;
+-------------------------+----------------+----------------+----------------+
| table_schema | data_length | index_length | sum_data_index |
+-------------------------+----------------+----------------+----------------+
| activemq | 0.000000037253 | 0.000009536743 | 0.000009573996 |
| alarms | 0.000045776367 | 0.000061035156 | 0.000106811523 |
| ams | 0.000030517578 | 0.000015258789 | 0.000045776367 |
| dashboard | 0.000045776367 | 0.000061035156 | 0.000106811523 |
| falcon_portal | 0.000183105469 | 0.000183105469 | 0.000366210938 |
| graph | 0.000045776367 | 0.000045776367 | 0.000091552734 |
| performance_schema | 0.000000000000 | 0.000000000000 | 0.000000000000 |
| replication_wwww.ymq.io | 0.000015258789 | 0.000000000000 | 0.000015258789 |
| sys | 0.000015258789 | 0.000000000000 | 0.000015258789 |
| uic | 0.000061035156 | 0.000091552734 | 0.000152587891 |
| yourls | 0.000045776367 | 0.000061035156 | 0.000106811523 |
+-------------------------+----------------+----------------+----------------+
11 rows in set (0.09 sec)
- 統計庫中每個表的大小:
select table_name,data_length,index_length,sum(data_length+index_length) as total_size from information_schema.tables where table_schema=‘yourls‘ group by table_name;
+----------------+-------------+--------------+------------+
| table_name | data_length | index_length | total_size |
+----------------+-------------+--------------+------------+
| yourls_log | 16384 | 16384 | 32768 |
| yourls_options | 16384 | 16384 | 32768 |
| yourls_url | 16384 | 32768 | 49152 |
+----------------+-------------+--------------+------------+
3 rows in set (0.00 sec)
- 統計所有數據庫的大小
select sum(data_length+index_length)/1024/124/1024 from information_schema.tables;
+---------------------------------------------+
| sum(data_length+index_length)/1024/124/1024 |
+---------------------------------------------+
| 18.510122414558 |
+---------------------------------------------+
1 row in set (0.03 sec)
- 統計信息的收集方法:
mysql> select * from information_schema.tables where table_name=‘yourls_url‘\G
*************************** 1. row ***************************
TABLE_CATALOG: def
TABLE_SCHEMA: yourls
TABLE_NAME: yourls_url
TABLE_TYPE: BASE TABLE
ENGINE: InnoDB
VERSION: 10
ROW_FORMAT: Dynamic
TABLE_ROWS: 6
AVG_ROW_LENGTH: 2730
DATA_LENGTH: 16384
MAX_DATA_LENGTH: 0
INDEX_LENGTH: 32768
DATA_FREE: 0
AUTO_INCREMENT: NULL
CREATE_TIME: 2018-05-25 14:36:30
UPDATE_TIME: 2018-05-25 14:36:30
CHECK_TIME: NULL
TABLE_COLLATION: utf8_unicode_ci
CHECKSUM: NULL
CREATE_OPTIONS:
TABLE_COMMENT:
1 row in set (0.00 sec)
66、SLA幾個9的計算方式
99% 365*24*(1-99%)=87.6小時
99.9% 365*24*(1-99.9%)=8.76小時
99.99% 365*24*(1-99.99%)*60=52.56分鐘
99.999% 365*24*(1-99.999%)*60=5.256分鐘```
運維工作中經常用到的一些知識總結(四)