Web集群部署实战
Web集群架构图
系统规划
实验要求:购买一个 SFS 服务,以及 3 台云主机,部署 apache + php 的网站
购买云主机3台
部署网页数据
# 挂载 SFS 服务到跳板机
[root@ecs-proxy ~]# dnf install -y nfs-utils
[root@ecs-proxy ~]# mount -t nfs -o vers=3,nolock 192.168.1.197:/ /mnt/
[root@ecs-proxy ~]# df -h /mnt/
Filesystem Size Used Avail Use% Mounted on
192.168.1.197:/ 500G 0 500G 0% /mnt
# 部署网页到 NFS 服务
[root@ecs-proxy ~]# cd 4
[root@ecs-proxy 4]# tar -zxf public/website.tar.gz -C /mnt/
部署web服务
[root@ecs-proxy ~]# mkdir website
[root@ecs-proxy ~]# cd website
[root@ecs-proxy website]# vim ansible.cfg
[defaults]
inventory = hostlist
host_key_checking = False
[root@ecs-proxy website]# vim hostlist
[web]
192.168.1.[11:13]
[root@ecs-proxy website]# ansible all -m ping
可能报错
Ansible conld noe initialize the preferred locale: unsupported local setting
这个报错是由于ansible 无法初始化首选语言环境所导致的。
解决方法:1.在~/.bashrc 文件末尾添加2行
export LC_ALL =en_US.UTF-8
c
从重新执行 source ~/.bashrc 解决问题
这两个命令的作用是设置系统的默认语言环境为英语(美国)和UTF-8编码。
export LC_ALL=en_US.UTF-8 命令会将系统的语言环境设置为英语(美国),并使用UTF-8编码格式。这意味着在系统中输入和输出的所有文本都将使用UTF-8编码格式,以确保它们可以正确地显示和处理非英语字符。
export LANG=en_US.UTF-8 命令与 export LC_ALL=en_US.UTF-8 命令类似,但它只设置了系统的默认语言环境,而不是整个系统的语言环境。这意味着只有当前终端窗口中的语言环境会被设置为英语(美国)和UTF-8编码格式,而其他终端窗口仍然保持原来的语言环境设置不变。
方法2:
cat > /etc/profile.d/lang.sh<<EOF
export LANG="en_US.UTF-8"
EOF
[root@ecs-proxy website]# cat > web_install.yaml<< EOF
---
- name: web 集群安装
hosts: web
tasks:
- name: 安装 apache 服务
dnf:
name: httpd,php,nfs-utils
state: latest
update_cache: yes
- name: 配置 httpd 服务
service:
name: httpd
state: started
enabled: yes
- name: 编辑 fstab 文件,添加 NFS 配置
lineinfile:
path: /etc/fstab
regexp: '^192.168.1.197:/.*'
line: '192.168.1.197:/ /var/www/html nfs defaults,_netdev,vers=3,nolock 1 1'
create: yes
- name: 挂载 NFS
shell:
cmd: mount /var/www/html
EOF
[root@ecs-proxy website]# ansible-playbook web_install.yaml
验证结果
[root@ecs-proxy ~]# curl http://192.168.1.11/info.php
<pre>
Array
(
[REMOTE_ADDR] => 192.168.1.252
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /info.php
)
php_host: web-0001
1229
配置负载均衡
购买负载均衡
负载均衡(ELB)
配置华为云负责均衡对外发布服务
[root@ecs-proxy ~]# curl http://192.168.1.250/info.php
<pre>
Array
(
[REMOTE_ADDR] => 100.125.99.180
[REQUEST_METHOD] => GET
[HTTP_USER_AGENT] => curl/7.29.0
[REQUEST_URI] => /info.php
)
php_host: web-0001
1229
弹性公网ip绑定到负载均衡服务器上,可以在公网访问80网站
http://124.70.1.159/info.php
Elasticsearch 安装
项目架构图
添加软件包
# 添加 ELK 软件包到自定义 Yum 仓库
[root@ecs-proxy s4]# rsync -av elk/ /var/localrepo/elk/
[root@ecs-proxy s4]# createrepo --update /var/localrepo
购买云主机
集群安装
部署 es-0001
[root@es-0001 ~]# vim /etc/hosts
192.168.1.21 es-0001
192.168.1.22 es-0002
192.168.1.23 es-0003
192.168.1.24 es-0004
192.168.1.25 es-0005
[root@es-0001 ~]# dnf install -y elasticsearch
[root@es-0001 ~]# vim /etc/elasticsearch/elasticsearch.yml
17: cluster.name: my-es
23: node.name: es-0001
56: network.host: 0.0.0.0
70: discovery.seed_hosts: ["es-0001", "es-0002", "es-0003"]
74: cluster.initial_master_nodes: ["es-0001", "es-0002", "es-0003"]
[root@es-0001 ~]# systemctl enable --now elasticsearch
# 服务启动成功
[root@es-0001 ~]# curl http://127.0.0.1:9200
{
"name" : "es-0001",
"cluster_name" : "my-es",
"cluster_uuid" : "_na_",
"version" : {
"number" : "7.17.8",
"build_flavor" : "default",
"build_type" : "rpm",
"build_hash" : "120eabe1c8a0cb2ae87cffc109a5b65d213e9df1",
"build_date" : "2022-12-02T17:33:09.727072865Z",
"build_snapshot" : false,
"lucene_version" : "8.11.1",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}
部署 es-0002
# 验证集群状态失败
[root@es-0002 ~]# curl http://es-0001:9200/_cat/nodes?pretty
{
"error" : {
"root_cause" : [
{
"type" : "master_not_discovered_exception",
"reason" : null
}
],
"type" : "master_not_discovered_exception",
"reason" : null
},
"status" : 503
}
# 部署服务
[root@es-0002 ~]# vim /etc/hosts
192.168.1.21 es-0001
192.168.1.22 es-0002
192.168.1.23 es-0003
192.168.1.24 es-0004
192.168.1.25 es-0005
[root@es-0002 ~]# dnf install -y elasticsearch
[root@es-0002 ~]# vim /etc/elasticsearch/elasticsearch.yml
17: cluster.name: my-es
23: node.name: es-0002
56: network.host: 0.0.0.0
70: discovery.seed_hosts: ["es-0001", "es-0002", "es-0003"]
74: cluster.initial_master_nodes: ["es-0001", "es-0002", "es-0003"]
[root@es-0002 ~]# systemctl enable --now elasticsearch
# 验证集群状态
[root@es-0002 ~]# curl http://es-0001:9200/_cat/nodes?pretty
192.168.1.21 16 89 2 0.15 0.06 0.04 cdfhilmrstw * es-0001
192.168.1.22 6 88 61 1.00 0.23 0.08 cdfhilmrstw - es-0002
集群扩容
部署插件页面
# 拷贝插件 public/head.tar.gz 到 es-0001 主机
[root@ecs-proxy s4]# rsync -av public/head.tar.gz 192.168.1.21:./
#-------------------------------------------------
# 在 es-0001 上安装 web 服务,并部署插件
[root@es-0001 ~]# dnf install -y nginx
[root@es-0001 ~]# systemctl enable --now nginx
[root@es-0001 ~]# tar zxf head.tar.gz -C /usr/share/nginx/html/
API原语管理
集群状态查询
# 查询支持的关键字
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/
# 查具体的信息
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master
# 显示详细信息 ?v
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master?v
# 显示帮助信息 ?help
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master?help
# 显示易读格式 ?pretty
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/_cat/master?pretty
创建索引
# 设置默认分片副本数量
[root@es-0001 ~]# curl -XPUT -H 'Content-Type: application/json' \
http://127.0.0.1:9200/_template/index_defaults -d '{
"template": "*",
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1
}
}'
{"acknowledged":true}
# 创建 tedu 索引
[root@es-0001 ~]# curl -XPUT http://127.0.0.1:9200/tedu?pretty
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "tedu"
}
增加数据
[root@es-0001 ~]# curl -XPUT -H "Content-Type: application/json" \
http://127.0.0.1:9200/tedu/teacher/1?pretty -d '{
"职业": "诗人","名字": "李白","称号": "诗仙","年代": "唐"
}'
查询数据
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/tedu/teacher/_search?pretty
[root@es-0001 ~]# curl -XGET http://127.0.0.1:9200/tedu/teacher/1?pretty
修改数据
[root@es-0001 ~]# curl -XPOST -H "Content-Type: application/json" \
http://127.0.0.1:9200/tedu/teacher/1/_update -d '{
"doc": {"年代":"公元701"}
}'
删除数据
# 删除一条
[root@es-0001 ~]# curl -XDELETE http://127.0.0.1:9200/tedu/teacher/1
# 删除索引
[root@es-0001 ~]# curl -XDELETE http://127.0.0.1:9200/tedu