alphine镜像的使用技巧

用 alphine 镜像的一些常用技巧: 会随时增加: 一、修改源,用国外的源非常慢,替换成国内的中科大源 sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories 二、更新apk库 apk update 三、安装软件 #安装telnet apk add busybox-extras #安装curl apk add curl #安装时间组件 apk add tzdata #更新并且安装软件 apk add --update tzdata 四、进入容器一步执行换源、更新、安装 sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && apk update && apk add curl && apk add busybox-extras 五、解决缺少glibc库的问题 如果不ln会报错,原因是缺少glibc库!!!解决方法如下: RUN mkdir /lib64 && \ ln -s /lib/libc.musl-x86_64.so.1 /lib64/ld-linux-x86-64.so.2 六、一些调试的CMD CMD ["/bin/bash", "-c", "while true; do echo hi; sleep 10; done"] kubectl run curlpod --image=radial/busyboxplus:curl --command -- /bin/sh -c "while true; do echo hi; sleep 10; done" 七、pod的等待技巧 这儿里启动正式的pod之前,先临时起了两个容器等待其他服务的完成 ...

2021年11月4日

alphine镜像中timezone的设定

我们都喜欢用 alphine 的镜像做底包,来生产自己的镜像 alphine 的底包的时间设定就非常重要了 直接给出 Dockerfile FROM alpine:3.12 # latest certs RUN apk add ca-certificates --no-cache && update-ca-certificates # timezone support ENV TZ=Asia/Shanghai RUN apk add --update tzdata --no-cache &&\ cp /usr/share/zoneinfo/${TZ} /etc/localtime &&\ echo $TZ > /etc/timezone # install chrony and place default conf which can be overridden with volume RUN apk add --no-cache chrony && mkdir -p /etc/chrony COPY chrony.conf /etc/chrony/. # port exposed EXPOSE 123/udp # start CMD [ "/usr/sbin/chronyd", "-d", "-s"] 时间设定重要的就是下面这几行 ...

2021年11月3日

生产环境Proxmox 7.02的安装和配置

其实我们的生产环境一直是 KVM ,然后用 shell 脚本控制虚机的生成,也是用到了 Cloud-init 的标准镜像。 听说 Proxmox 也很不错,于是想看看能否也在生产环境中用上 如果在生产环境中用,必须要让 proxmox 支持 cloud-init ,否则无意义,下面也说一下跑在生产的注意事项 首先我们用光盘安装: 然后第一个注意的地方就是硬盘,选 Options 后: 会冒出一堆选项,公司的生产环境,服务器如果没有 raid 卡是很奇怪的,所以 zfs 反而不是标配,因为我们会事先在 raid 卡上划分好硬盘,生产环境基本必然是 raid10 ,接下来就是 ext4 和 xfs 二选一了,八戒选 ext4 ,因为坏了好修理,xfs_repair 用起来相当龟毛: 那么,选定了 ext4 ,接下来就比较重要了 hdsize 1116.0 ,单位是G,这个是自动收集上来的,不用改 swapsize,交换分区大小,这个给 8 G(最大8G) maxroot,这个分区是第一个分区,存放 iso 和 template 的,需要给够,100 G minfree,第一个分区最小留多大,给 10 G(缺省16G) maxvz,这个分区是第二个分区,存放实际的虚机文件,全都用上,什么也不填写 然后继续,国家选 china,Hostname 填写 proxmox-168-86-103.local,再填写好其他信息,就安装成功了。 打开网页,我们可以看到一个 local,100G,对应上面的 maxroot 然后 local-lvm ,就是剩余放虚机的空间 ...

2021年11月3日

k8s中nginx ingress的性能优化

kubernetes 中 nginx ingress 的优化分两部分 一、系统sysctl部分优化 首先是对nginx启动前的系统性能进行优化,这部分调整网络的缓冲区,减小闲置 socket 关闭的时间 以阿里 ACK 为例,我们可以编辑 deployments 的 nginx-ingress-controller initContainers: - command: - /bin/sh - -c - | mount -o remount rw /proc/sys sysctl -w net.core.somaxconn=65535 sysctl -w net.ipv4.ip_local_port_range="1024 65535" sysctl -w net.ipv4.tcp_tw_reuse=1 sysctl -w fs.file-max=1048576 sysctl -w net.ipv4.tcp_keepalive_time = 300 sysctl -w net.ipv4.tcp_keepalive_probes = 5 sysctl -w net.ipv4.tcp_keepalive_intvl = 15 二、nginx ingress 参数优化 大家制动,nginx ingree 其实是做为一个中间代理,所以上下游的socket参数也需要优化 ...

2021年11月2日

Linux内核sysctl内核参数优化

这篇是纯配置篇,解释都在配置里了,是生产服务器 sysctl.conf 的配置 ### KERNEL ### # Reboot after 10sec. on kernel panic kernel.panic = 10 ### IMPROVE SYSTEM MEMORY MANAGEMENT ### # Increase size of file handles and inode cache fs.file-max = 2097152 # Insure we always have enough memory vm.min_free_kbytes = 8192 # Do less swapping vm.swappiness = 10 vm.dirty_ratio = 10 vm.dirty_background_ratio = 2 ### GENERAL NETWORK SECURITY OPTIONS ### # Avoid a smurf attack net.ipv4.icmp_echo_ignore_broadcasts = 1 # Turn on protection for bad icmp error messages net.ipv4.icmp_ignore_bogus_error_responses = 1 # Turn on syncookies for SYN flood attack protection net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_syn_backlog = 8192 # Turn on timestamping net.ipv4.tcp_timestamps = 1 # Turn on and log spoofed, source routed, and redirect packets net.ipv4.conf.all.log_martians = 1 net.ipv4.conf.default.log_martians = 1 # No source routed packets here net.ipv4.conf.all.accept_source_route = 0 net.ipv4.conf.default.accept_source_route = 0 # Turn on reverse path filtering net.ipv4.conf.all.rp_filter = 1 net.ipv4.conf.default.rp_filter = 1 # Make sure no one can alter the routing tables net.ipv4.conf.all.accept_redirects = 0 net.ipv4.conf.default.accept_redirects = 0 net.ipv4.conf.all.secure_redirects = 0 net.ipv4.conf.default.secure_redirects = 0 # Don't act as a router net.ipv4.ip_forward = 0 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 # Number of times SYNACKs for passive TCP connection. net.ipv4.tcp_synack_retries = 2 # Allowed local port range net.ipv4.ip_local_port_range = 1024 65000 # Protect Against TCP Time-Wait net.ipv4.tcp_rfc1337 = 1 # Decrease the time default value for tcp_fin_timeout connection net.ipv4.tcp_fin_timeout = 15 # Decrease the time default value for connections to keep alive net.ipv4.tcp_keepalive_time = 300 net.ipv4.tcp_keepalive_probes = 5 net.ipv4.tcp_keepalive_intvl = 15 # This means that the keepalive process waits 300 seconds for socket # activity before sending the first keepalive probe, and then resend # it every 15 seconds. If no ACK response is received for 5 consecutive # times (75s in this case), the connection is marked as broken. ### TUNING NETWORK PERFORMANCE ### # Disable IPv6 net.ipv6.conf.all.disable_ipv6 = 1 net.ipv6.conf.default.disable_ipv6 = 1 net.ipv6.conf.lo.disable_ipv6 = 1 # Default Socket Receive Buffer net.core.rmem_default = 31457280 # Maximum Socket Receive Buffer net.core.rmem_max = 12582912 # Default Socket Send Buffer net.core.wmem_default = 31457280 # Maximum Socket Send Buffer net.core.wmem_max = 12582912 # Increase number of incoming connections net.core.somaxconn = 5000 # Increase number of incoming connections backlog net.core.netdev_max_backlog = 65536 # Enable TCP window scaling net.ipv4.tcp_window_scaling = 1 # Increase the maximum amount of option memory buffers net.core.optmem_max = 25165824 # Increase the maximum total buffer-space allocatable # This is measured in units of pages (4096 bytes) net.ipv4.tcp_mem = 65536 131072 262144 net.ipv4.udp_mem = 65536 131072 262144 # Increase the read-buffer space allocatable net.ipv4.tcp_rmem = 8192 87380 16777216 net.ipv4.udp_rmem_min = 16384 # Increase the write-buffer-space allocatable net.ipv4.tcp_wmem = 8192 65536 16777216 net.ipv4.udp_wmem_min = 16384 # Increase the tcp-time-wait buckets pool size to prevent simple DOS attacks net.ipv4.tcp_max_tw_buckets = 1800000 # TIME_WAIT socket policy # Note: if both enabled then disable # net.ipv4.tcp_timestamps for servers # behind NAT to prevent dropped incoming connections #net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 # Enable TCP MTU probing (in case of Jumbo Frames enabled) #net.ipv4.tcp_mtu_probing = 1 # Speedup retrans (Google recommended) net.ipv4.tcp_slow_start_after_idle = 0 net.ipv4.tcp_early_retrans = 1 # Conntrack # 288bytes x 131072 = 37748736 (~38MB) max memory usage #net.netfilter.nf_conntrack_max = 131072 #net.netfilter.nf_conntrack_tcp_loose = 1 #TCP的直接拥塞通告(tcp_ecn)关掉 net.ipv4.tcp_ecn = 0 #路由缓存刷新频率,当一个路由失败后多长时间跳到另一个路由,默认是300。 net.ipv4.route.gc_timeout = 100 #设定系统中最多允许在多少TCP套接字不被关联到任何一个用户文件句柄上。 #如果超过这个数字,没有与用户文件句柄关联的TCP 套接字将立即被复位 #防简单Dos net.ipv4.tcp_max_orphans = 655360 # NOTE: Enable this if machine support it # -- 10gbe tuning from Intel ixgb driver README -- # # turn off selective ACK and timestamps #net.ipv4.tcp_sack = 0 #net.ipv4.tcp_timestamps = 1 ** 注意,net.ipv4.tcp_tw_recycle 不要打开,在 NAT 环境中会出错,而且在 K8S 中也会因 NAT 导致 pod 出错,切记!!!** ...

2021年11月2日

Linux内核TCP连接Keep-Alive timeout的配置

Custom Configuration of TCP Socket Keep-Alive Timeouts 这是个古老的话题,我们在机器的优化中,需要设置 TCP Socket 的 Timeout 参数 用来加快 TCP 关闭无用闲置连接的时间 Linux 内核中有三个缺省参数: tcp_keepalive_time 缺省是 7200 秒 tcp_keepalive_probes 缺省是 9 tcp_keepalive_intvl 缺省是 75 秒 处理流程如下: 一、客户端打开一个 TCP socket 连接,开始跟服务器通讯 二、如果这条 socket 连接空闲没有任何数据传输,静默了 tcp_keepalive_time 秒后,那么客户端会主动发送一个空的 ACK 包到服务器 三、那么,根据服务器是否回应了一个相应的 ACK 包来判断 ACK 未回应 等待 tcp_keepalive_intvl 秒,然后再发一个 ACK 包 重复以上等待并发送 ACK 包的过程,直到次数等于 tcp_keepalive_probes 如果第2步做完还收不到任何回应,发送一个 RST 包并关闭连接 回应了: 回到上述第二步 那么缺省情况下,7200+75×9,一个没有任何数据传输的 socket 才会被关闭,大概是2小时11分钟。 ...

2021年11月2日

shell以及find的多线程执行

这属于Shell的高级技巧了,我们可能需要在 bash 中并发 wget rsync 文件,下面就讨论一下这个问题。 首先从简单的单线程开始: $ for i in $(seq 1 2); do echo $i; done 1 2 可以看到是顺序执行的,下面变多线程: $ for i in $(seq 1 2); do echo $i & done [1] 245505 1 [2] 245506 2 [1] Done echo $i [2] Done echo $i 可以看到我们只把 ; 号改成了 & 号,程序就变成了多线程执行。 区别在于 ; 号会等待之前的命令执行完毕再执行下一条,而 & 不等待,直接继续执行下一条;相当于后台运行了前一条命令。 下面说说 find 的单线程和多线程: find 的 exec 用法 $ find /path [args] -exec [cmd] {} \; {} 占位符号,存放find找到的记录 ; 对于每一条找到的单独记录,执行的cmd是一条一条单独执行的 执行的顺序如下: cmd result1; cmd result2; …; cmd result N $ find /path [args] -exec [cmd] {} \+ {} 占位符号,存放find找到的记录 + 对于找到的所有记录,执行的cmd是合并了所有记录集执行的 执行顺序如下: cmd result1 result2 … result N 多个exec可以串起来: ...

2021年10月29日

Linux下的程序限速软件Trickle

之前讲过如何对 opnvpn 总体限速,这次来了一个更严格的程序限速需求: 场景如下: 两个机房间有一条专线 100M 两个机房间需要同步数据,同步需要限制到60M,给别的程序留出带宽空间 传输是多个文件,用 rsync 并发传送 分析了一下脚本的核心部分 find . -type f | grep $(date -d"-1 day" +'%Y%m%d') |xargs -I % -P 30 rsync -auvPR % 192.168.9.17::mysql 发现是利用 xargs 的并发,-P 30 最大并发30个,启动了 rsync 同步 rsync 没有限速,这就麻烦了。 一、单文件单独限速 首先是使用 rsync 的 –bwlimit=600 参数,把速度限制为 600KB/s ,600×8=4800,单进程基本是5M的速度,最多只能跑12个了,就会跑到60M。 这样也不太对,尤其是 rsync 并发进程逐渐减少,少于12个的时候,这样就出现跑不满60M的现象。 二、多文件整体限速 那么 rsync 支持多文件传输 ,使用如下格式整体限速 rsync --bwlimit=7200 -auvPR 文件1 文件2 文件3 192.168.9.17::mysql 问题又来了,文件1 文件2 文件3 的路径非常长,而文件个数不定,有撑爆命令行单行长度限制的可能,也不可行 三、tc 使用 tc 可以控制源ip或者目的ip的带宽,但是本机网卡是万兆光卡,生产环境,每时每刻都有数据读写。 一旦错了,就直接完蛋了。也不太可行 四、杀器trickle 寻找了半天,终于找到了个大杀器trickle,可以对程序单独限速,也可以对一堆程序整体限速 安装: ...

2021年10月28日

Dockerfile的编写与调试技巧

Dockerfile 是造出镜像的基础,是必须熟知并了解的知识: 一、编写Dockerfile 先给个例子,是 minio 代理访问阿里的 OSS FROM alpine:3.12 RUN apk add --update bash && rm -rf /var/cache/apk/* COPY minio.RELEASE.2020-04-15T19-42-18Z /data/minio.RELEASE.2020-04-15T19-42-18Z ENV MINIO_ACCESS_KEY=LTAI5tFFTbsxxxxxuLb ENV MINIO_SECRET_KEY=t78PyGnHZilxxxxxdxBCjvNgtVC5Y WORKDIR /data EXPOSE 9000 CMD ["/data/minio.RELEASE.2020-04-15T19-42-18Z","gateway","oss","http://oss-cn-shanghai-internal.aliyuncs.com"] # CMD /bin/sh -c "while true; do echo hi; sleep 10; done" 详细解释每一条语句: FROM 基板,alpine 3.12 是个比较微小的版本,注意它的毛病,/bin/sh其实是busybox,没有/bin/bash,某些bash的函数功能支持不全,比如for循环 RUN 在容器中运行命令,上例中我们添加了 bash ,并清理了缓存。命令间用 && 可以避免镜像过多分层。 RUN分两种模式shell和exec模式: 我们只用 exec 模式,因为在 image 里装入多个 shell,没什么意义。 COPY 和 ADD ...

2021年10月28日

PHP程序如何发送syslog到远程服务器

给同事做了个 PHP 接口,转发发送短信的请求,同时要把发送记录发送到远程的 cacti 的 syslog 去 很简单,但是也不简单 首先是 PHP 服务器,是最简化编译的,php -m 查了一下 php -m [PHP Modules] Core ctype curl date dom fileinfo filter gettext hash iconv json libxml openssl pcre PDO pdo_sqlite Phar posix Reflection session SimpleXML SPL sqlite3 standard tokenizer xml xmlreader xmlwriter [Zend Modules] 居然没有 socket 模块,没办法,找到源代码,编译一个安装,原有的 php 安装路径是 /export/servers/php $ tar zxvf php-7.4.0.tar.gz $ cd php-7.4.0/sockets $ /export/servers/php/bin/phpize $ ./configure --enable-sockets --with-php-config=/export/servers/php/bin/php-config $ make $ make install 又看了一眼,是 php-fpm,居然没有 php.ini ,得,再生成一个,放在 /export/servers/php/lib/php.ini ...

2021年10月28日