Rootop 服务器运维与web架构

2018-12-11
发表者 Venus
docker exec 进入容器报错connection reset by peer已关闭评论

docker exec 进入容器报错connection reset by peer

在centos7.4 yum安装的docker,拉取一个redis镜像,run以后进入容器报错。

[root@sych vhost]# docker exec -it redis /bin/bash
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "process_linux.go:110: decoding init error from pipe caused \"read parent: connection reset by peer\""

各种查,发现好多资料都提到了版本是1.13.1 。升级到ce版本可以解决。
根据docker官方文档配置yum源,重新安装。
https://docs.docker.com/install/linux/docker-ce/centos/

2018-12-05
发表者 Venus
基于openresty实现服务灰度发布已关闭评论

基于openresty实现服务灰度发布

nginx配置文件:
# 定义3个upstream

upstream up1
{
	server 127.0.0.1:8001;
}

upstream up2
{
	server 127.0.0.2:8002;
}

upstream all
{
	ip_hash;
	server 127.0.0.1:8001;
	server 127.0.0.2:8002;
}

server
{
	server_name localhost;
	listen 80;

	# 请求交给lua脚本处理
	location /
	{
		lua_code_cache off;
		content_by_lua_file /mnt/proxy.lua;
	}

	# location @ 用于nginx内部跳转
	location @up1
	{
		proxy_pass http://up1;
	}

	location @up2
	{
		proxy_pass http://up2;
	}

	location @all
	{
		proxy_pass http://all;
	}

}

lua代码:

ngx.header.content_type="text/html;charset=utf8"
redis = require('resty.redis')
redis = redis.new()
redis:set_timeout(1000)

ok,err =  redis:connect('127.0.0.1', 6379)

if not ok then
	ngx.say('connect to redis failed ! reason: ' .. err)
end

-- 从redis中检查是否存在即将更新的upstream主机
check_up1 = redis:get('update1') --(up1)
check_up2 = redis:get('update2') --(up2)
redis:close()
-- 注意返回的数据类型来判断
if check_up1 == "1" then
	ngx.exec("@up2")
elseif check_up2 == "1" then
	ngx.exec("@up1")
else
	ngx.exec("@all")
end

原理就是利用redis中设置指定key,比如要更新主机1,则redis中添加key=update1,value=1,当浏览器请求进入nginx的content阶段后执行lua脚本,脚本中检查redis中是否存在要更新的主机,如果发现某个主机要更新则通过Nginx API for Lua中的ngx.exec接口内部跳转到另一台主机。
如果两个都不更新,则根据nginx自己的方式(默认轮询)分发请求。

nginx里使用了ip_hash保持会话,主要是为了用户的请求在后端记录的日志中保持完整。

2018-11-30
发表者 Venus
通过dockerfile自定义docker镜像已关闭评论

通过dockerfile自定义docker镜像

通过 dockerfile 构建自定义镜像

# 创建个文件夹,存放dockerfile
[root@localhost ~]# mkdir docker
[root@localhost ~]# cd docker/

# 创建dockerfile(用于基于一个镜像构建一个新镜像,必须是这个文件名,否则提示找不到文件)
[root@localhost docker]# cat dockerfile

# 基础镜像 IMAGE ID
FROM 82a5b82cec98

# 维护者
MAINTAINER venus

# 镜像操作, RUN 用来在构建镜像时运行系统命令
# 注意,每个RUN就会多一层镜像(可通过docker history IMAGE查看已有的镜像层),这样会造成镜像臃肿。可用过"&&"或者换行"\"写多条命令解决。
RUN yum install -y httpd

# 中文支持
RUN yum -y install kde-l10n-Chinese
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
RUN echo 'LC_ALL="zh_CN.utf8"' > /etc/locale.conf
ENV LANG zh_CN.utf8
ENV LC_ALL zh_CN.utf8

# 中国时区
RUN rm -f /etc/localtime
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# 安装jdk ADD发现是一个压缩包会自动解压,不需要手动再解,另外仅支持当前路径下的文件,不支持绝对路径
ADD ./jdk-8u131-linux-x64.tar.gz /usr/local
RUN ln -s /usr/local/jdk1.8.0_131 /usr/local/jdk

# 设置JAVA环境变量
ENV JAVA_HOME /usr/local/jdk
ENV PATH $JAVA_HOME/bin:$PATH
ENV CLASSPATH .:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV TZ Asia/Shanghai

# EXPOSE的作用在docker run --publish-all 参数时有用,宿主机会自动分配随机端口号到EXPOSE的端口
# 如果在docker run时,不指定端口映射 --publish 或 --publish-all,则在docker ps中PORTS列会显示EXPOSE的端口
EXPOSE 80
EXPOSE 8080

RUN mkdir /root/ops
# 添加启动脚本
ADD ./start_httpd.sh /root/ops
RUN chmod 777 /root/ops/start_httpd.sh

# 工作目录,即登录容器后的目录
WORKDIR /root

# 通过CMD执行容器内命令。
# CMD参数是在run一个容器时默认运行的命令,如果run时指定命令,则会覆盖此处的CMD参数。
CMD sh /root/ops/start_httpd.sh

# 通过ENTRYPOINT执行容器内脚本,ENTRYPOINT不会被run容器时指定的命令覆盖,会做为参数传给ENTRYPOINT里的命令。
#ADD ./sh.sh /
#RUN chmod 777 /sh.sh
#ENTRYPOINT ["/sh.sh"]

# 参考cmd与entrypoint区别:https://www.cnblogs.com/lienhua34/p/5170335.html

# 根据 dockerfile 构建一个镜像(镜像名字为java8)
docker build -t java8 .
然后docker images 就可以看到java8的镜像了。

2018-11-29
发表者 Venus
ubuntu挂载共享mount error(95): Operation not supported已关闭评论

ubuntu挂载共享mount error(95): Operation not supported

# 在ubuntu下挂载共享报错
root@venus:~# mount.cifs //192.168.10.5/DB_Backup /mnt/nas/ -o user=admin
Password for admin@//192.168.10.5/DB_Backup: ******
mount error(95): Operation not supported
Refer to the mount.cifs(8) manual page (e.g. man mount.cifs)

根据提示查看mount.cifs帮助文档的第8页
root@venus:~# man mount.cifs 8

原因可能是我nas设备(群晖)里面是SMB2.0的版本,然后ubuntu版本内核为4.15.0-39-generic,就自动协商为2.1版本没成功:
vers=arg
SMB protocol version. Allowed values are:

· 1.0 – The classic CIFS/SMBv1 protocol.

· 2.0 – The SMBv2.002 protocol. This was initially introduced in Windows Vista Service Pack 1, and Windows Server
2008. Note that the initial release version of Windows Vista spoke a slightly different dialect (2.000) that is
not supported.

· 2.1 – The SMBv2.1 protocol that was introduced in Microsoft Windows 7 and Windows Server 2008R2.

· 3.0 – The SMBv3.0 protocol that was introduced in Microsoft Windows 8 and Windows Server 2012.

· 3.1.1 or 3.11 – The SMBv3.1.1 protocol that was introduced in Microsoft Windows Server 2016.

Note too that while this option governs the protocol version used, not all features of each version are available.

The default since v4.13.5 is for the client and server to negotiate the highest possible version greater than or
equal to 2.1. In kernels prior to v4.13, the default was 1.0. For kernels between v4.13 and v4.13.5 the default is
3.0.

# 指定协议2.0版本解决。
root@venus:~# mount.cifs //192.168.10.5/DB_Backup /mnt/nas/ -o user=admin,pass=xxx,vers=2.0

2018-11-28
发表者 Venus
ubuntu下mysql workbench 修改快捷键已关闭评论

ubuntu下mysql workbench 修改快捷键

实现:
通过快捷键执行当前光标所在行的sql语句。

修改配置文件:
root@venus:~# vi /usr/share/mysql-workbench/data/main_menu.xml
大约在2042行,把快捷键修改为F9。保存退出,重启workbench生效。

2036         <value type="object" struct-name="app.MenuItem" id="com.mysql.wb.menu.query.execute_current_statement">
2037           <link type="object" key="owner" struct-name="app.MenuItem">com.mysql.wb.menu.query</link>
2038           <value type="string" key="caption">Execute Current Statement</value>
2039           <value type="string" key="name">query.execute_current_statement</value>
2040           <value type="string" key="command">builtin:query.execute_current_statement</value>
2041           <value type="string" key="itemType">action</value>
2042           <value type="string" key="shortcut">F9</value>
2043           <value type="string" key="platform">macosx,linux</value>
2044         </value>

执行选中的多条sql语句,这里快捷键设置为F5。

2009         <value type="object" struct-name="app.MenuItem" id="com.mysql.wb.menu.query.exec">
2010           <link type="object" key="owner" struct-name="app.MenuItem">com.mysql.wb.menu.query</link>
2011           <value type="string" key="caption">Execute (All or Selection)</value>
2012           <value type="string" key="name">query.execute</value>
2013           <value type="string" key="command">builtin:query.execute</value>
2014           <value type="string" key="itemType">action</value>
2015           <value type="string" key="shortcut">F5</value>
2016         </value>