ETCD + CONFD + NGINX的配置

上一篇文章我们介绍了 ETCD 的容器化,搞这件事情的主要目的其实是要动态更新 Nginx 的配置 这一章我们就来配置 confd 和 Nginx,来达到动态更新 Nginx 配置的目的 一、安装配置confd 下载并安装: wget https://github.com/kelseyhightower/confd/releases/download/v0.16.0/confd-0.16.0-linux-amd64 mv confd-0.16.0-linux-amd64 /usr/sbin/confd chmod +x /usr/sbin/confd 生成配置文件: 我们在 etcd 中存放的格式如下 etcdctl set /nginx/app01/subdomain app1 etcdctl set /nginx/app01/upstream/app01_1 "192.168.0.1:5601" /nginx/app01/subdomain "app01" /nginx/app01/upstream/app01_1 "192.168.0.1:5601" /nginx/app01/upstream/app01_2 "192.168.0.2:5601" 那么,我们先生成 confd 的配置文件: mkdir -p /etc/confd/{conf.d,templates} cat <<EOF>>/etc/confd/conf.d/nginx.toml [template] src = "nginx.conf.tmpl" dest = "/etc/nginx/conf.d/nginx-auto.conf" keys = [ "/nginx/app01/subdomain", "/nginx/app01/upstream", ] check_cmd = "/usr/sbin/nginx -t" reload_cmd = "/usr/sbin/nginx -s reload" EOF cat <<EOT>>/etc/confd/templates/nginx.conf.tmpl upstream {{getv "/nginx/app01/subdomain"}} { {{range getvs "/nginx/app01/upstream/*"}} server {{.}}; {{end}} } server { server_name {{getv "/nginx/app01/subdomain"}}.example.com; location / { proxy_pass http://{{getv "/nginx/app01/subdomain"}}; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } EOT confd 会根据 etcd 的值,结合 nginx.conf.tmpl ,生成 nginx-auto.conf,然后 nginx -t 验证通过后,执行 nginx -s rolad。 ...

2021年10月21日 · 1 分钟 · 145 字 · 八戒

etcd单节点应用

由于使用到了阿里的 K8S 托管集群 ACK,于是想占便宜。想用到托管 master node 的 etcd 来保存数据。 结果是,未遂!!无法使用。 阿里有单独的配置管理服务,复杂化了,不想用。 那么解决方案就是,启动只有一个节点副本的 etcd pod,然后数据持久化到 OSS 的 S3 桶中。 一、实现etcd的单节点docker化 首先我们只想在测试环境中跑一个单节点的 etcd,还没有用到 k8s,做法如下: #!/bin/bash NODE1=172.18.31.33 REGISTRY=quay.io/coreos/etcd # available from v3.2.5 #REGISTRY=gcr.io/etcd-development/etcd docker run \ -p 2379:2379 \ -p 2380:2380 \ --volume=/data/etcd:/etcd-data \ --name etcd ${REGISTRY}:latest \ /usr/local/bin/etcd \ --data-dir=/etcd-data --name node1 \ --initial-advertise-peer-urls http://${NODE1}:2380 --listen-peer-urls http://0.0.0.0:2380 \ --advertise-client-urls http://${NODE1}:2379 --listen-client-urls http://0.0.0.0:2379 \ --initial-cluster node1=http://${NODE1}:2380 如上就可以了,容器跑起来以后进入容器测试一下: docker exec -it 425f26903466 /bin/sh etcdctl -C http://127.0.0.1:2379 member list c3511611548b7c7c: name=node1 peerURLs=http://172.18.31.33:2380 clientURLs=http://172.18.31.33:2379 isLeader=true etcdctl ls --recursive / 这样一个单节点的 etcd 就弄好了,对外暴露的是 2379 和 2380 端口 ...

2021年10月21日 · 2 分钟 · 289 字 · 八戒

Libguestfs的救援手段

在生产环境中我们大量使用了 kvm 的虚拟技术,虚拟机的镜像系统使用的是 Cloud-init 的技术 不可避免的,虚机会遭到各种损坏,维护的手段就十分必要了 假设我们有一个虚机文件 vis-16-41-18.qcow2 坏了 一、安装支持包 yum install libguestfs libguestfs-tools 二、查看日志 virt-log -a vis-16-41-18.qcow2 没有什么特殊的报错信息 三、分析文件系统组成 virt-filesystems和virt-df都可以,用virt-df看的更多一些 virt-filesystems -l -a vis-16-41-18.qcow2 Name Type VFS Label Size Parent /dev/sda1 filesystem ext4 - 209715200 - /dev/sda2 filesystem ext4 - 214536355840 - virt-df -a vis-16-41-18.qcow2 Filesystem 1K-blocks Used Available Use% vis-16-41-18.qcow2:/dev/sda1 194241 31706 152295 17% vis-16-41-18.qcow2:/dev/sda2 206088704 5639856 189973444 3% 四、挂载文件系统开始修复(方法1) 从上面可以看到 vis-16-41-18.qcow2 里面有两个分区,/dev/sda1 和/dev/sda2 第一个应该是/boot,第二个是/ 把 / mount 出来 mkdir 18 guestmount -a vis-16-41-18.qcow2 -m /dev/sda2 --rw ./18 或者全自动mount ...

2021年10月21日 · 2 分钟 · 411 字 · 八戒

如何用CPU挖狗币Dogecoin

这是个娱乐话题,Dogecoin 狗币在马斯克的吹捧鼓动下,冲上云霄 其实真的用CPU挖币,应该是挖 xmb 门罗币才是对的选择,挖狗币只是娱乐一下 废话不多说,直接放上教程,我的机器是 CentoOS 首先需要有个狗币钱包地址,这个我就不教大家了 一、下载xmrig挖矿软件 下载地址:https://github.com/xmrig/xmrig/releases 我们选择最近的下载就好 二、做好加密通道 我们需要做好一条加密tcp通道 用 ghostunnel, localhost:9999 —> vps:9999 —> rx.unmineable.com:3333 三、用screen后台开挖 screen #./xmrig -o localhost:9999 -a rx -k -u DOGE:狗币地址.矿工名#heyt-3711 ./xmrig -o localhost:9999 -a rx -k -u DOGE:DLR3DZGucJiSdahARW1vV5B1h3WYiw454a.work01 ctrl+a+d 四、查看挖了多少 查看地址:https://unmineable.com/coins/DOGE/address/

2021年10月21日 · 1 分钟 · 41 字 · 八戒

Chrony的几个详细配置细节

chrony 已经成了事实标准,替代了ntp。 但是,有几个细节,需要非常注意。 给出我们的配置,/etc/chrony.conf # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server 172.10.1.1 iburst prefer minpoll 6 maxpoll 10 server 172.10.1.2 iburst # Record the rate at which the system clock gains/losses time. driftfile /var/lib/chrony/drift # Allow the system clock to be stepped in the first three updates # if its offset is larger than 1 second. makestep 1.0 3 # Enable kernel synchronization of the real-time clock (RTC). rtcsync # Enable hardware timestamping on all interfaces that support it. #hwtimestamp * # Increase the minimum number of selectable sources required to adjust # the system clock. #minsources 2 # Allow NTP client access from local network. #allow 192.168.0.0/16 # Serve time even if not synchronized to a time source. #local stratum 10 # Specify file containing keys for NTP authentication. #keyfile /etc/chrony.keys # Specify directory for log files. logdir /var/log/chrony logchange 0.5 # Select which information is logged. #log measurements statistics tracking rtc 里面有好几个细节,下面逐一解释一下: ...

2021年10月20日 · 2 分钟 · 219 字 · 八戒

ghostunnel使用TLS加密TCP流量

很现实的问题,局域网内有态势感知和网络流量分析,这很讨厌! 那么,如何把某段流量隐藏起来,让态势感知无法分析呢? 前提条件,你需要有国外的一台 VPS 作为外援,把 TCP 流量通过 TLS 加密送到国外的服务器,然后再转发到正确的目标服务器上,这样就不会被人追踪了。 这里推荐 Ghostunnel ,这是个 Go 的程序,只有一个执行文件。配合 certik 证书生成,就完美了。 项目地址:https://github.com/ghostunnel/ghostunnel 首先我们使用 certik 生成三个证书,ca.pem server.pem 和 client1.pem 然后ghostunnel以及三个证书文件都放在/usr/local/bin下 一、在VPS上运行ghostunnel模式server /usr/local/bin/ghostunnel server --listen 0.0.0.0:9999 --target tr.dero.herominers.com:1117 --keystore server.pem --cacert ca.pem --allow-cn client1 --unsafe-target 上面监听了端口0.0.0.0:9999(监听0.0.0.0必须加参数–unsafe-target),远程转发到dero的矿池端口1117,只允许验证过的cn client1连接。 二、在本地机器上运行ghostunnel模式client /usr/local/bin/ghostunnel client --listen localhost:9999 --target 193.42.114.129:9999 --keystore client.pem --cacert ca.pem 本地监听localhost:9999,所以不用加–unsafe-target参数,然后连接到远程 vps 服务器,ip地址是193.42.114.129,端口是9999 这样就完成了。可以放心的启动程序,连接到本地端口localhost:9999,TCP流量就会被隐藏起来,不会被分析到.

2021年10月19日 · 1 分钟 · 55 字 · 八戒

Certik 证书签发软件

我们会有很多时候需要用到TLS证书,一个非常方便、小众的工具就是 certik 。 这个软件纯由 Go 组成,就一个可执行文件,使用了 etcd 的 boltdb 格式存放所有的证书。 软件地址:https://github.com/opencoff/certik 使用: 一、初始化证书库 ./certik -v tls.db init CA 二、签发一张server证书 #./certik -v tls.db server -i IP.ADDR.ES server.domain.name ./certik -v tls.db server -i 193.42.114.129 server 上面我们 server 的 ip 是193.42.114.129,域名简洁起见就叫 server 了 。 三、签发一张client1证书 ./certik -v tls.db client client1 四、查看证书库 ./certik -v tls.db list 我们就可以看到三个证书了,一个 CA ,一个 server ,一个 client1 五、导出各个证书 #导出CA ./certik -v tls.db export --root-ca #导出server ./certik -v tls.db export server #导出client1 ./certik -v tls.db export client1 以上就可以得到各个东西了。 ...

2021年10月19日 · 1 分钟 · 83 字 · 八戒

使用IPSET封掉某个国家整个的访问

现在国内都禁止挖币,什么币安、火币、cnspark之类的都不允许国内 IP 访问了。 如何实现的呢? 首先需要得到国家IP段,下载地址:http://www.ipdeny.com/ipblocks/。这里以我们国家 cn 为例 步骤如下: 一、安装ipset #Debian/Ubuntu系统 apt-get -y install ipset #CentOS系统 yum -y install ipset 二、清空iptable规则 #防止设置不生效,清空之前的防火墙规则 iptables -P INPUT ACCEPT iptables -F 三、创建ipset规则集 #创建一个名为cnip的规则 ipset -N cnip hash:net #下载国家IP段,这里以中国为例 wget -P . http://www.ipdeny.com/ipblocks/data/countries/cn.zone #将IP段添加到cnip规则集中 for i in $(cat /root/cn.zone ); do ipset -A cnip $i; done 四、创建iptable黑名单 #扔黑名单 iptables -A INPUT -p tcp --dport 80 -m set --match-set cnip src -j DROP iptables -A INPUT -p tcp --dport 443 -m set --match-set cnip src -j DROP #然后放行其他的 iptables -P INPUT ACCEPT

2021年10月18日 · 1 分钟 · 86 字 · 八戒

OpenVPN 限制流量带宽

公司安装了 openvpn ,带来方便,但是也有很多不便的地方,机房的总带宽就那么多。 很多人共用 vpn 的时候,就会抢占带宽。 那么,我们需要限制一下,限制 openvpn 所能使用的带宽,避免抢占 WEB 的带宽 做法如下: 由于我们不是要单独限制某一个 openvpn 用户,而是限制整体,所以简单用 TC 就可以了 #!/bin/sh tc qdisc del dev tun0 root tc qdisc add dev tun0 root handle 1: htb default 1 tc class add dev tun0 parent 1: classid 1:1 htb rate 30Mbit ceil 30Mbit 解释一下: 我们 openvpn 启的是 tun0 ,所以限制的对象就是 dev tun0 首先第一行清除 tun0 的根队列 然后第二行建立 tun0 的 root 根队列为 1:0 htb ,缺省是1:1的子队列 最后一行,第三行建立 1:1 的子队列,带宽限制是 30Mbit ,注意这里是大B,就是网络术语中的带宽,换算成小b的话,需要除以8 效果很明显,直接被限制住(41兆而不是30M是因为这台机器是虚机,实体机上还有别的流量): ...

2021年10月18日 · 1 分钟 · 74 字 · 八戒

本站博客的由来以及搭建使用教程

本站这个博客的由来: 源自于 wiredcraft 的面试,这个公司让老八很是向往,可以远程工作,第一次面试是 devops ,老外见面聊了后,由代理中国人面。因为面的是K8S的东西,正好刚给画包包公司做了整体迁往阿里ACK的工程,以为没问题,实际是直接问倒了我。就好比master node上面都跑了什么进程,唉,一言难尽啊,又问到ansible的变量,回答估计也不满意,结果就挂了 知耻而后勇,后面去恶补了一下ansible和k8s的东西,实际也是实际操作居多,然后第二次面的是 sysadmin,全程老外面。还出了几道题,应该是没问题。但是工资要高了,也被刷了。 这个博客就是其中一道题,那既然搭建出来了,干脆就好好用吧。 本身自己对静态的 Blog 系统也比较感兴趣,自己主站 www.rendoumi.com 的 Blog 是由 journey 搭建的,是一个精巧的 go 程序,最妙的是它兼容 Ghost 博客系统,也能使用 Ghost 的 theme ,这样就完美的把自己以前 Ghost 的博客迁移了过去,但是,里面文章有很多过时了,但也不想清理,干脆借着这个机会,从新开始。搭一个自己喜欢的系统继续写新博客 流行的 Markdown 写作平台有Hexo和Hugo,选Hugo是因为实在是不喜欢node,弄一堆npm,迁移麻烦死。 这个博客程序基于Hugo,托管在 github,众所周知,github 是托管静态文件的地方,Markdown的最大毛病是图片。图片放在图床也不是好办法,所以图片和静态文件要在一起,下面就说一下搭建过程,我的主机是Ubuntu: 一、下载Hugo 下载地址: https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_Linux-64bit.tar.gz wget https://github.com/gohugoio/hugo/releases/download/v0.88.1/hugo_0.88.1_Linux-64bit.tar.gz tar zxvf hugo_0.88.1_Linux-64bit.tar.gz 二、初始一个博客写作目录 ./hugo new site MyBlog ​ 三、下载theme cd MyBlog git clone https://github.com/halogenica/beautifulhugo.git themes/beautifulhugo echo theme = \"beautifulhugo\" >> config.toml ​ 四、写一篇新文章 cd MyBlog ../hugo new posts/my-first-post.md echo "#### This is another Blog" >> content/posts/my-first-post.md 五、运行server,build草稿 cd MyBlog ../hugo server --buildDrafts ​ ...

2021年10月15日 · 2 分钟 · 315 字 · 八戒