目录
实现目的:
Linux上一键安装Mysql、Nginx、Redis软件
一键安装Mysql脚本
一键安装Redis脚本
一键安装Nginx脚本
docker上安装部署Mysql、Nginx、Redis容器
Kubernetes上安装部署Mysql、Nginx、Redis的Pod和通过Service发布
创建Pod生成容器
使用Service发布
实现目的:
目的是方便用户快速安装Mysql、Redis、Nginx这三个软件,其中有部分配置可以自行舍取,脚本经供参考,实际工作可按需求调整。
Linux上一键安装Mysql、Nginx、Redis软件
一键安装Mysql脚本
[root@localhost ~]# cat onekey_install_mysql_binary.sh
#!/bin/bash#步骤:
#解决软件依赖关系
yum install libaio cmake ncurses-devel gcc gcc-c++ vim lsof bzip2 openssl-devel -y#需要自己下载Mysql二进制镜像#解压二进制安装包
tar -xvf mysql-8.0.33-linux-glibc2.28-x86_64.tar.gz#移动mysql解压文件到/usr/local下,改名为mysql
mv mysql-8.0.33-linux-glibc2.28-x86_64 /usr/local/mysql#新建组和用户 mysql
groupadd mysql#mysql这个用户的shell 是/bin/false 无法被调用 表示系统用户 属于mysql这个组
useradd -r -g mysql -s /bin/false mysql#进入/usr/local/mysql
cd /usr/local/mysql#关闭firewalld防火墙服务,并且设置开机不启动
service firewalld stop
systemctl disable firewalld #临时关闭selinux,永久关闭selinux
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config#mysql初始化操作
#创建/data/mysql文档用于存放数据使用
mkdir /data/mysql -p
#修改文件权限和组别,这样mysql用户可以对这个文件夹进行读写操作了
chown -R mysql:mysql /data/mysql/
chmod -R 750 /data/mysql/#进入/usr/local/mysql/bin目录下进行初始化操作
cd /usr/local/mysql/bin
#设置启动用户为mysql base目录为/usr/local/mysql/ data目录为/data/mysql &>passwd.txt目的是将生成的临时密码存入passwd.txt文件中
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql --lower-case-table-names=1 &>passwd.txt #让mysql支持ssl方式登录的设置
./mysql_ssl_rsa_setup --datadir=/data/mysql/#获得临时密码
tem_passwd=$(cat passwd.txt| grep "temporary" | awk '{print $NF}')
#$NF表示最后一个字段#修改环境变量,添加我们编译安装的mysql的可执行命令的路径
#临时修改PATH变量的值
export PATH=/usr/local/mysql/bin/:$PATH
#永久修改
echo "PATH=/usr/local/mysql/bin:$PATH" >>/root/.bashrc#刷新环境变量
source ~/.bashrc#复制support-files里的mysql.server文件到/etc/init.d目录下叫mysqld
cp ../support-files/mysql.server /etc/init.d/mysqld#修改/etc/init.d/mysqld内的第70行的内容(datadir目录的值)
sed -i '70c datadir=/data/mysql' /etc/init.d/mysqld#生成/etc/my.cnf配置文件
cat >/etc/my.cnf <<EOF
[mysqld_safe]
[client]
socket=/data/mysql/mysql.sock
[mysqld]
socket=/data/mysql/mysql.sock
port = 3306
open_files_limit = 8192
innodb_buffer_pool_size = 512M
character-set-server=utf8
[mysql]
auto-rehash
prompt=\\u@\\d \\R:\\m mysql>
EOF#启动mysqld服务
service mysqld start#将mysqld添加到linux系统里的服务管理名单里
chkconfig --add mysqld
#设置mysqld服务开机启动
/sbin/chkconfig mysqld on#登录重新设置初始密码为123456
#初次修改密码需要使用 --connect-expired-password 选项
#-e 后面接的命令是表示我们需要在mysql里执行的命令
#set password='123456'; 表示修改root用户的密码为:123456
mysql -uroot -p$tem_passwd --connect-expired-password -e "set password='123456';"#建议修改密码是否修改成功
mysql -uroot -p123456 -e 'show databases;' && echo "database is installed success" [root@localhost ~]#
注意:当部署好Mysql的时候,需要外部访问的时候,我们需要在Mysql内创建一个用户使他有权限被外部访问,因为我们的Mysql默认是不允许root用户外部登陆的
参考:Mysql服务器的外部连接_mysql外部连接_Claylpf的博客-CSDN博客
一键安装Redis脚本
[root@localhost ~]# cat onekey_install_redis.sh
#!/bin/bash#安装Redis依赖
yum install gcc tcl -ymkdir /usr/local/redis/#下载好的redis安装包
#wget https://github.com/redis/redis/archive/7.2.1.tar.gz#上传到/usr/local/src目录下
#mv redis-7.2.1.tar.gz /usr/loacl/redis/redis-7.2.1.tar.gzcd /usr/local/redis/#解压redis安装包
tar -zxvf redis-7.0.11.tar.gz#进入redis包,并运行编译
cd redis-7.0.11
make && make install#关闭firewalld防火墙服务,并且设置开机不启动
service firewalld stop
systemctl disable firewalld#临时关闭selinux,永久关闭selinux
setenforce 0
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config#将redis配置文件备份一份
cp redis.conf redis.conf.bck# 修改redis备份文件
sed -i 's/^bind .*/bind 0.0.0.0/' redis.conf
sed -i 's/^\(daemonize\) no$/\1 yes/' redis.conf
sed -i '/^# requirepass/s/^# //; /^requirepass/s/.*/requirepass 123456/' redis.conf
sed -i 's/^logfile ""$/logfile "redis.log"/' redis.conf#重新启动redis
redis-server redis.conf#实现redis开机自启cat >/etc/systemd/system/redis.service <<EOF
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/bin/redis-server /usr/local/redis/redis-7.0.11/redis.conf
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF#重载系统服务
systemctl daemon-reload#重启redis
systemctl restart redis#实现开机自启
systemctl enable redis#查看redis状态
systemctl status redis#查看redis版本
redis-server --version[root@localhost ~]#
一键安装Nginx脚本
[root@localhost ~]# cat onekey_install_nginx.sh
#!/bin/bash#新建一个文件夹用来存放下载的nginx源码包
mkdir -p /nginx
cd /nginx#新建工具人用户、设置无法登录模式
useradd -s /sbin/nologin clay#下载nginx
#wget http://nginx.org/download/nginx-1.23.2.tar.gz
#curl -O http://nginx.org/download/nginx-1.24.0.tar.gzmv ~/nginx-1.24.0.tar.gz nginx-1.24.0.tar.gz #解压nginx源码包
tar xf nginx-1.24.0.tar.gz#解决软件依赖关系、需要安装的软件包
yum install epel-release -y
yum install gcc gcc-c++ openssl openssl-devel pcre pcre-devel automake make psmisc net-tools lsof vim geoip geoip-devel wget zlib zlib-devel -y #到达nginx配置文件目录下
cd nginx-1.24.0#编译前的配置
./configure --prefix=/usr/local/nginx --user=clay --with-http_ssl_module --with-http_v2_module --with-stream --with-http_stub_status_module --with-threads #编译、开启一个进程同时编译
make -j 1#编译安装
make install#启动nginx
/usr/local/nginx/sbin/nginx#永久修改PATH变量
PATH=$PATH:/usr/local/nginx/sbin
echo "PATH=$PATH:/usr/local/nginx/sbin" >>/root/.bashrc
#设置nginx的开机启动--手动添加
#在/etc/rc.local中添加启动命令
#/usr/local/scnginx66/sbin/nginx
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
#给文件可执行权限
chmod +x /etc/rc.d/rc.local#刷新/root/.bashrc配置文件
source ~/.bashrc#selinux和firewalld防火墙都需要关闭
service firewalld stop
systemctl disable firewalld#临时关闭selinux
setenforce 0#永久关闭selinux (需要开机重启)
#vim /etc/selinx/config
sed -i '/^SELINUX=/ s/enforcing/disabled/' /etc/selinux/config#查看nginx是否运行成功
ps aux|grep nginx[root@localhost ~]#
安装完成后查看效果:
docker上安装部署Mysql、Nginx、Redis容器
首先拉取其中的镜像
下图为拉取的镜像类型(nginx:1.24.0 Mysql:8.0.33 redis:7.0.11)
使用docker run命令部署容器
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql-8.0.33 mysql:8.0.33docker run -d -p 80:80 --name nginx-1.24 nginx:1.24docker run -d -p 6379:6379 --name redis-7.0.11 redis:7.0.11
部署成果:
Kubernetes上安装部署Mysql、Nginx、Redis的Pod和通过Service发布
创建Pod生成容器
首先编辑nginx-deployment.yaml 文件
(base) root@sd-cluster-04:~# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploymentnamespace: testns
spec:replicas: 1selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.24ports:- containerPort: 80
(base) root@sd-cluster-04:~#
编辑mysql-deployment.yaml文件
(base) root@sd-cluster-04:~# cat mysql-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: mysql-deploymentnamespace: testns
spec:replicas: 1selector:matchLabels:app: mysqltemplate:metadata:labels:app: mysqlspec:containers:- name: mysqlimage: mysql:8.0.33env:- name: MYSQL_ROOT_PASSWORDvalue: "123456"ports:- containerPort: 3306
(base) root@sd-cluster-04:~#
编辑 redis-deployment.yaml
(base) root@sd-cluster-04:~# cat redis-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: redis-deploymentnamespace: testns
spec:replicas: 1selector:matchLabels:app: redistemplate:metadata:labels:app: redisspec:containers:- name: redisimage: redis:7.0.11ports:- containerPort: 6379
(base) root@sd-cluster-04:~#
使用 kubectl apply -f 命令运行.yaml文件
(base) root@sd-cluster-04:~# kubectl apply -f mysql-deployment.yaml
查看效果
(base) root@sd-cluster-04:~# kubectl get pods -n testns -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-deployment-66c4d975f5-zm4sz 1/1 Running 0 77m 10.244.0.212 sd-cluster-04 <none> <none>
nginx-deployment-76bd958557-slspm 1/1 Running 0 89m 10.244.1.250 sd-cluster-05 <none> <none>
redis-deployment-f7d7dd455-xk7h8 1/1 Running 0 21m 10.244.0.215 sd-cluster-04 <none> <none>
(base) root@sd-cluster-04:~#
使用Service发布
创建nginx-service.yaml文件
(base) root@sd-cluster-04:~# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:name: nginx-servicenamespace: testnslabels:app: nginx
spec:type: NodePortselector:app: nginx ports:- protocol: TCPport: 80targetPort: 80
(base) root@sd-cluster-04:~#
编辑mysql-service.yaml文件
(base) root@sd-cluster-04:~# cat mysql-service.yaml
apiVersion: v1
kind: Service
metadata:name: mysql-servicenamespace: testns labels:app: mysql
spec:type: NodePortports:- port: 3306targetPort: 3306 protocol: TCPselector:app: mysql
(base) root@sd-cluster-04:~#
编辑redis-service.yaml 文件
(base) root@sd-cluster-04:~# cat redis-service.yaml
apiVersion: v1
kind: Service
metadata:name: redis-servicenamespace: testnslabels:app: redis
spec:type: NodePortselector:app: redisports:- protocol: TCPport: 6379targetPort: 6379
(base) root@sd-cluster-04:~#
使用kubectl apply -f 命令运行.yaml文件
(base) root@sd-cluster-04:~# kubectl apply -f nginx-service.yaml
查看效果
(base) root@sd-cluster-04:~# kubectl get service -n testns -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
mysql-service NodePort 10.96.132.123 <none> 3306:30859/TCP 85m app=mysql
nginx-service NodePort 10.96.16.39 <none> 80:30325/TCP 87m app=nginx
redis-service NodePort 10.96.10.164 <none> 6379:30026/TCP 89m app=redis
(base) root@sd-cluster-04:~#