运维脚本

关于脚本

​ 这是我日常生活中写的一些脚本,自己也会经常用到,主要是对于一些应用的部署,也是非常简单的,这里的脚本我也会经常更新,写出更好的脚本

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
1.配置yum源
vim yum.sh
#!/bin/bash
rm -rf /etc/yum.repos.d/repo*
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache fast
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum makecache fast
yum -y install vim net-tools ifconfig netstat bash-completion-extras、bash-completion

2.安装mysql
vim.mysql.sh
#!/bin/bash
password=Xiaocai@123
yum -y remove mysqld
rpm -qa | grep mysql | xargs rpm -e --nodeps
find / -name "*mysql*" -exec rm -rf {} \; # 一般最好不要加这个
userdel -r mysql
groupdel mysql
rm -rf /etc/my*
rm -rf /var/lib/mysql
wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
yum -y install mysql80-community-release-el7-7.noarch.rpm
sed -i '/mysql-5.7-community/{n;s/0/1/};/mysql-8.0-community/{n;s/1/0/}' /etc/yum.repos.d/mysql-community.repo
sed -ri s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config && setenforce 0
systemctl stop firewalld && systemctl disable firewalld
yum -y groupinstall "Development Tools"i
yum -y install mysql-community-server
systemctl start mysqld && systemctl enable mysqld
passwd=`grep "A temporary password" /var/log/mysqld.log |sed -r 's/.*localhost: (.*)$/\1/'`
mysqladmin -u root -p"$passwd" password "$password"
echo "validate-password=OFF" >> /etc/my.cnf
systemctl restart mysqld
mysqladmin -u root -p"$password" password '123'
mysql -uroot -p123

3.安装docker
vim docker.sh
#!/bin/bash
yum remove docker-ce docker-ce-cli containerd.io -y
rm -rf /var/lib/docker
rm -rf /var/lib/containerd
rm -rf /var/lib/dockershim
rm -rf /run/docker
rm -rf /etc/docker
yum install -y yum-utils device-mapper-persistent-data lvm2
curl https://gitee.com/leedon21/k8s/raw/master/docker-ce.repo -o /etc/yum.repos.d/docker-ce.repo
yum install -y docker-ce-19.03.0-3.el7
systemctl start docker
docker version
systemctl daemon-reload
systemctl restart docker

4.安装docker-compose # 安装1.0的大版本脚本
vim docker-compose.sh
#!/bin/bash
rm -rf /usr/local/bin/docker-compose
rm -rf /usr/bin/docker-compose
curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
docker-compose --version

4.1安装docker-compose # 安装2.0大版本的脚步
vim docker-compose.sh
#!/bin/bash
rm -rf /usr/local/bin/docker-compose
rm -rf /usr/bin/docker-compose
mkdir -p ~/.docker/cli-plugins/
curl -SL https://github.com/docker/compose/releases/download/v2.6.0/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
chmod +x ~/.docker/cli-plugins/docker-compose
ln -s ~/.docker/cli-plugins/docker-compose /usr/bin/docker-compose
docker compose --version

5.安装gitlab
vim gitlab.sh
#!/bin/bash
wget https://mirrors.cloud.tencent.com/gitlab-ce/yum/el7/gitlab-ce-15.9.2-ce.0.el7.x86_64.rpm
yum localinstall -y gitlab-ce-15.9.2-ce.0.el7.x86_64.rpm
sed -i 's/external_url 'http:/\/\gitlab.example.com'/external_url 'http:/\/\10.0.9.234' /etc/gitlab/gitlab.rb
gitlab-ctl reconfigure
gitlab-ctl start
:wq
cat /etc/gitlab/initial_root_password # 默认用户名为root,查看默认密码

6.安装reids
vim redis.sh
#!/bin/bash
tar xf /root/test/redis-4.0.6.tar.gz -C /usr/local/
cd /usr/local/redis-4.0.6/
make
ln -s /usr/local/redis-4.0.6/src/redis-server /usr/bin
ln -s /usr/local/redis-4.0.6/src/redis-cli /usr/bin
sed -i 's/daemonize no/daemonize yes/' /usr/local/redis-4.0.6/redis.conf
sed -i 's/bind 127.0.0.1/bind 0.0.0.0/' /usr/local/redis-4.0.6/redis.conf
redis-server /usr/local/redis-4.0.6/redis.conf # 启动
redis-cli

7.编译安装nginx
vim bianyi-nginx.sh
#!/bin/bash
yum -y install gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel
id nginx
if [ $? -ne 0 ]
then
useradd nginx
fi
echo 1 | passwd --stdin nginx
cd /root/test
wget http://nginx.org/download/nginx-1.16.0.tar.gz
tar xf nginx-1.16.0.tar.gz -C /usr/local/
cd /usr/local/nginx-1.16.0/
./configure --prefix=/usr/local/nginx --group=nginx --user=nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/tmp/nginx/client_body --http-proxy-temp-path=/tmp/nginx/proxy --http-fastcgi-temp-path=/tmp/nginx/fastcgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-pcre --with-http_realip_module --with-stream
make && make install
/usr/local/nginx/sbin/nginx -t
mkdir -p /tmp/nginx
mkdir /usr/local/nginx/logs
mkdir -p /tmp/nginx/client_body
/usr/local/nginx/sbin/nginx
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx
:wq
# 命令
nginx -V # 查看nginx模块
nginx -c /path/nginx.conf # 以特定目录下的配置文件启动nginx:
nginx -s reload # 修改配置后重新加载生效
nginx -s stop # 快速停止nginx
nginx -t # 测试当前配置文件是否正确
nginx -t -c /path/to/nginx.conf # 测试特定的nginx配置文件是否正确
chmod +x /etc/init.d/nginx #添加权限
systemctl daemon-reload #重新加载系统启动文件
systemctl start nginx #启动并设置开机自启


8.脚本部署es
#!/bin/bash
tar -xvf /root/elasticsearch-7.17.1-linux-x86_64.tar.gz
echo -e '* soft nofile 65536\n* hard nofile 131072\n* soft nproc 2048\n* hard nproc 4096' >> /etc/security/limits.conf
echo 'vm.max_map_count=655360' >> /etc/sysctl.conf
sysctl -p
sed -i 's/-Xms1g/-Xms3g/' elasticsearch-7.17.1/config/jvm.options
sed -i 's/-Xmx1g/-Xmx3g/' elasticsearch-7.17.1/config/jvm.options
echo -e 'cluster.name: es-cluster\nnode.name: ES01\nnode.master: true\nnode.data: true\nnode.max_local_storage_nodes: 1\nnetwork.host: 0.0.0.0\nhttp.port: 9200\ntransport.tcp.port: 9300\ndiscovery.seed_hosts: ["11.25.255.631:9300"]\ncluster.initial_master_nodes: ["ES01"]\npath.data: /home/elastic/elasticsearch-7.17.1/data\npath.logs: /home/elastic/elasticsearch-7.17.1/logs\nxpack.security.enabled: true\nxpack.license.self_generated.type: basic\nxpack.security.transport.ssl.enabled: true\nxpack.security.transport.ssl.verification_mode: certificate\nxpack.security.transport.ssl.keystore.path: certs/elastic-certificates.p12\nxpack.security.transport.ssl.truststore.path: certs/elastic-certificates.p12\ningest.geoip.downloader.enabled: false' > elasticsearch-7.17.1/config/elasticsearch.yml
mkdir -p /home/elastic/elasticsearch-7.17.1/data
mkdir -p /home/elastic/elasticsearch-7.17.1/logs
cd elasticsearch-7.17.1
echo -e '\n\n' | ./bin/elasticsearch-certutil ca
echo -e '\n\n\n' | ./bin/elasticsearch-certutil cert --ca elastic-stack-ca.p12
mkdir config/certs
mv elastic-certificates.p12 elastic-stack-ca.p12 /config/certs
cd ~/elasticsearch-7.17.1/bin
./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.17.1/elasticsearch-analysis-ik-7.17.1.zip
cd ~/elasticsearch-7.17.1/bin
./elasticsearch-plugin install https://artifacts.elastic.co/downloads/elasticsearch-plugins/ingest-attachment/ingest-attachment-7.17.1.zip
cd ~/elasticsearch-7.17.1/bin
./elasticsearch-plugin install https://github.com/hailin0/elasticsearch-analysis-dynamic-synonym/archive/refs/heads/master.zip

useradd elastic
echo elastic | passwd --stdin elastic
mv elasticsearch-7.17.1 /home/elastic/
cd /home/elastic
chown -R elastic:elastic elasticsearch-7.17.1/
su - elastic
cd elasticsearch-7.17.1/bin
./elasticsearch -d
echo -e \"y\nelastic\nelastic\nelastic\nelastic\nelastic\nelastic\nelastic\nelastic\nelastic\nelastic\nelastic\nelastic\nelastic\n\" | ./elasticsearch-setup-passwords interactive

运维脚本
https://xiaocai.icu/2023/08/07/博客-脚本/
作者
小菜
发布于
2023年8月7日
许可协议