SHELL脚本之if

if语句的几种常用方法

  • if then fi
if command
then
    commands
fi
  • if then else fi
if command
then
    commands
else
    commands
fi
  • if then elif then fi
if command1
then
    commands
elif command2
then
    more commands
fi

PS:
1.if-then语句允许你使用布尔逻辑来组合测试,有两种布尔运算符可用:
[ condition ] && [ condition2 ]
[ condition ] || [ condition2 ]
2.双圆括号命令允许你将高级数学表达式放入比较中:
(( expression ))
3.双方括号里的expression使用了test命令中采用的标准字符串进行比较。但他提供了test命令未提供的另一种特性—-模式匹配
[[ expression ]]

go环境搭建

本方法在centos系列系统中实用,其余系统暂没有测试过!

  • 下载go的tar文件并解压
cd /opt && wget https://go.dev/dl/go1.17.5.linux-amd64.tar.gz
tar zxvf go1.17.5.linux-amd64.tar.gz
[ -f go1.17.5.linux-amd64.tar.gz ] && rm go1.17.5.linux-amd64.tar.gz -rf
  • 设置环境变量
echo "" >> /etc/profile
echo "##go 环境变量设置" >> /etc/profile
echo "export GOPATH=/opt/go" >> /etc/profile
echo "export PATH=\$PATH:\$GOPATH/bin" >> /etc/profile
echo 'export GOPROXY="https://goproxy.cn"' >> /etc/profile
echo "export GO111MODULE=on" >> /etc/profile
source /etc/profile

ngrok作为内网穿透工具来使用

  • 创建自有证书,并编译服务端和客户端的bin文件。 编译的时候需要go环境,请参考 go环境搭建
NGROK_DOMAIN="your_domain_name"
[ ! -d /opt/ssl ] && mkdir -p /opt/ssl
cd /opt && git clone https://github.com/inconshreveable/ngrok.git
cd /opt/ssl
openssl genrsa -out base.key 2048
openssl req -new -x509 -nodes -key base.key -days 3650 -subj "/CN=${NGROK_DOMAIN}" -out base.pem
openssl genrsa -out server.key 2048
openssl req -new -key server.key -subj "/CN=${NGROK_DOMAIN}" -out server.csr
openssl x509 -req -in server.csr -CA base.pem -CAkey base.key -CAcreateserial -days 3650 -out server.crt
cp base.pem /opt/ngrok/assets/client/tls/ngrokroot.crt
cd /opt/ngrok
make release-server release-client
  • ngrokd 会开一个 4443 端口用来跟客户端通讯(可通过 -tunnelAddr=”:xxx” 指定),这儿使用的是8899
/opt/ngrok/bin/ngrokd -tlsKey=/opt/ssl/server.key -tlsCrt=/opt/ssl/server.crt -domain="your_domain_name" -httpAddr=":80" -httpsAddr=":443" -tunnelAddr=":8899"
  • 客户端的一个示例配置文件(ngrok.cfg)
server_addr: "your_domain_name:8899"
trust_host_root_certs: false
tunnels:
  http:
    subdomain: "httptest"
    proto:
       http: "172.16.50.35:80"
  https:
    subdomain: "httpstest"
    proto:
      https: "172.16.50.35:443"
  ssh:
    remote_port: 12345
    proto:
      tcp: "172.16.50.35:22"
  • 客户端启动,客户端bin程序从刚刚的服务端编译好的下载下来使用
/opt/ngrok/ngrok -config=/opt/ngrok/ngrok.cfg start-all

LVS的两种模式实践

使用LVS NAT模式搭建一个nginx负载
  • 环境准备
Real server 1: 192.168.188.10(需要搭建NGINX服务)
Real server 2: 192.168.188.20(需要搭建NGINX服务)
Director Server: 192.168.188.5/172.16.50.10(模拟外网ip)
  • Director Server开启转发功能,关闭selinux和防火墙功能
  • 在 Director Server 添加LVS规则
ipvsadm -A -t 172.16.50.10:80 -s wrr   ## 创建一个服务
ipvsadm -a -t 172.16.50.10:80 -r 192.168.188.10:80 -m   ## 添加规则
ipvsadm -a -t 172.16.50.10:80 -r 192.168.188.20:80 -m
ipvsadm -e -t 172.16.50.10:80 -r 192.168.188.20:80 -w 3 -m  ## 修改规则,-w指定权重
ipvsadm -L -n  ## 查看lvs规则
  • 在Real server中将网关设置为: 192.168.188.5 ; 防火墙也需要放开
使用LVS DR模式搭建一个nginx负载
  • 环境准备
VIP: 172.16.50.65
Real server 1: 172.16.50.66(需要搭建NGINX服务)
Real server 2: 172.16.50.67(需要搭建NGINX服务)
Director Server: 172.16.50.10
  • Director Server关闭selinux和防火墙功能
  • 在 Director Server 配置VIP和添加路由
ifconfig ens32:0 172.16.50.65/24 up
route add -host 172.16.50.65 dev ens32:0
  • 在 Director Server 配置lvs规则
ipvsadm -A -t 172.16.50.65:80 -s wrr
ipvsadm -a -t 172.16.50.65:80 -r 172.16.50.66:80 -g -w 1
ipvsadm -a -t 172.16.50.65:80 -r 172.16.50.67:80 -g -w 2
  • 在Real server中配置VIP和添加路由
ifconfig lo:0 172.16.50.65 netmask 255.255.255.255 broadcast 172.16.50.65
route add -host 172.16.50.65  dev lo:0
  • 在Real server修改内核参数,控制Real server的响应模式
net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.lo.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
net.ipv4.conf.lo.arp_announce=2

docker的使用

  • --link表示连接到redis这个容器; -p 是映射端口 ;-e 是设置环境变量
docker run -d --link redis --name flask-redis -p 5000:5000 -e REDIS_HOST=redis xiang/flash-redis
  • 不同宿主机里面容器的通信,通过Overlay网络和etcd实现
  • 数据持久化
docker run -d -v mysql:/var/lib/mysql --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true mysql

多机器通信环境搭建

  1. 环境是node1为:172.16.50.32; node2为: 172.16.50.33;系统都为CENTOS7系统
  2. 安装etcd集群,并用下面命令运行,系统需要放开2379和2380两个端口:
nohup /opt/etcd/etcd --name docker-node1 --initial-advertise-peer-urls http://172.16.50.32:2380 \
--listen-peer-urls http://172.16.50.32:2380 \
--listen-client-urls http://172.16.50.32:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://172.16.50.32:2379 \
--initial-cluster-token etcd-cluster \
--initial-cluster docker-node1=http://172.16.50.32:2380,docker-node2=http://172.16.50.33:2380 \
--initial-cluster-state new&

nohup /opt/etcd/etcd --name docker-node2 --initial-advertise-peer-urls http://172.16.50.33:2380 \
--listen-peer-urls http://172.16.50.33:2380 \
--listen-client-urls http://172.16.50.33:2379,http://127.0.0.1:2379 \
--advertise-client-urls http://172.16.50.33:2379 \
--initial-cluster-token etcd-cluster \
--initial-cluster docker-node1=http://172.16.50.32:2380,docker-node2=http://172.16.50.33:2380 \
--initial-cluster-state new&

/opt/etcd/etcdctl cluster-health ##查看etcd集群状态
  1. 停止运行docker服务,并用下面的命令在相应的节点上运行,需要放行2375和7496两个端口
/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=etcd://172.16.50.32:2379 --cluster-advertise=172.16.50.32:2375&

/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --cluster-store=etcd://172.16.50.33:2379 --cluster-advertise=172.16.50.33:2375&
  1. 创建一个overlay的网络, 只在一个节点上面建立就可以了
docker network create -d overlay demo
  1. 测试在node1和node2上面分别创建一个docker
docker run -d --name test1 --net demo busybox sh -c "while true;do sleep 3600;done"  # node1上面运行
docker run -d --name test2 --net demo busybox sh -c "while true;do sleep 3600;done"  # node2上面运行

使用compose搭建一个wordpress站点

version: '3'
services:
  wordpress:
    image: wordpress
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: mysql
      WORDPRESS_DB_PASSWORD: wordpress_password
    networks:
      - my-bridge
  mysql:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: wordpress_password
      MYSQL_DATABASE: wordpress
    volumes:
      - mysql-data:/var/lib/mysql
    networks:
      - my-bridge
volumes:
  mysql-data:
networks:
  my-bridge:
    driver: bridge