kubernetes下定制服务器503以及其他的403消息

kubernetes下定制 http 50x 以及 40x 服务器返回信息的话,如果用 Nginx 做 ingress,大家会很自然想到直接用 nginx 来定制: error_page 500 502 503 504 /50x.html; location = /50x.html { root /export/html; } 这样做是不行的,因为 Nginx 作为 ingress 来使用,就不能落地,只能做转发;如果你落地了,访问了本地文件,就违背了初衷。而且在 ingress 的 pod 上放页面,会导致 ingress 的配置错乱。 那么正确 50x 重定位的方法如下:首先建立一个 deploy 和对应的 svc,就是建立一个 web 服务器,能提供服务返回 50x 和 40x 的定制页面;然后在 ingress 内指定 custom-http-errors 和 default-backend 指向它就可以了。 一、建立miniserve的deploy和svc 我们选用 rust 的 miniserve 作为 web 服务器,准备好定制好的 index.html,写个 Dockerfile 网址:https://github.com/svenstaro/miniserve FROM alpine:3.12 RUN apk add --update bash && rm -rf /var/cache/apk/* COPY . /data/ RUN rm -rf /data/Dockerfile WORKDIR /data EXPOSE 8080 CMD ["/data/miniserve","--index","index.html"] #CMD /bin/sh -c "while true; do echo hi; sleep 10; done" 作出镜像后推送到阿里镜像:registry.cn-shanghai.aliyuncs.com/rendoumi/miniserve:lastest ...

2021年11月29日 · 2 分钟 · 236 字 · 八戒

kubernetes下nginx ingress的限制

在 kubernetes 中,ingress 负责转发、融断和限流。 我们以 Nginx ingress 为例,讨论一下这方面的问题。 一、Nginx ingress黑白名单 这个很简单了,丢进黑名单或者白名单,在入口前拦一刀。 我们只要在 annotations 声明即可: apiVersion: networking.k8s.io/v1beta1 kind: Ingress metadata: name: www-com-ingress annotations: kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/ssl-redirect: "true" nginx.ingress.kubernetes.io/block-cidrs: a.b.c.d/32 #nginx.ingress.kubernetes.io/whitelist-source-range: a.b.c.d/32 spec: tls: - hosts: - www.huabbao.com secretName: www-huabbao-com-cert rules: - host: www.huabbao.com http: paths: - path: / backend: serviceName: nginx-svc 注意,多个ip的话,之间用逗号隔开(该值是逗号分隔的CIDR列表) : ...

2021年11月29日 · 1 分钟 · 165 字 · 八戒

kubernetes搭建minio作为阿里OSS的Gateway

阿里的云的 OSS 并不是完全版本的 AWS S3 兼容。 我们如果需要用 S3 协议访问 OSS,就比较麻烦了。 所以搭建一个 minio 来做网关,代理OSS,minio 是基本兼容S3的,所以这样曲线救国,通过 S3 协议访问 minio 来访问最后端的 OSS 这里还有一段故事: Minio 中间有一版是支持 oss 的,但是后来 oss 改了协议,所以现在的最新版本 minio 反而是不支持代理 oss 的,我们必须手动作出镜像,放到镜像库里,然后阿里 ACK 再使用 首先去下载那一版直接支持oss的 wget http://dl.minio.org.cn/server/minio/release/linux-amd64/archive/minio.RELEASE.2020-04-15T19-42-18Z chmod 755 minio.RELEASE.2020-04-15T19-42-18Z 这个文件比较宝贵,给个本地备份链接下载: minio.RELEASE.2020-04-15T19-42-18Z 然后在当前目录编辑 Dockerfile ,因为 K8S 和 OSS 同一地域,所以用 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" 注意上面,MINIO_ACCESS_KEY 和 MINIO_SECRET_KEY 对应的是阿里云 OSS的 AccessKey ID 和 AccessKey Secret ...

2021年11月26日 · 2 分钟 · 233 字 · 八戒

替代kubernetes crontab的神器kala

本来 k8s 的 crontab 是启动一个容器来运行的,很简单,如下: apiVersion: batch/v1beta1 kind: CronJob metadata: name: curl-cron spec: schedule: "*/1 * * * *" jobTemplate: spec: template: spec: containers: - name: curl-cron image: radial/busyboxplus:curl imagePullPolicy: IfNotPresent command: - /bin/sh - -c - date;echo "run crontab";curl http://www.baidu.com restartPolicy: OnFailure successfulJobsHistoryLimit: 2 failedJobsHistoryLimit: 2 上面就跑了一个 busybox 的 pod,每分钟去访问百度,然后在 stdout 输出结果 这个没什么,注意上面文件的最后两行。限制成功以及失败 job 的 History 数量,如果不加限制,kubectl get pods 会看到无穷无尽的completed 状态的 curl-cron pod。 ...

2021年11月25日 · 2 分钟 · 416 字 · 八戒

kubernetes的hpa和自定义指标hpa

kubernetes 的动态伸缩 HPA 是非常有用的特性。 我们的服务器托管在阿里云的 ACK 上,k8s 根据 cpu 或者 内存的使用情况,会自动伸缩关键 pod 的数量,以应对大流量的情形。而且更妙的是,动态扩展的 pod 并不是使用自己的固定服务器,而是使用阿里动态的 ECI 虚拟节点服务器,这样就真的是即开即用,用完即毁。有多大流量付多少钱,物尽其用。 我们先明确一下概念: k8s 的资源指标获取是通过 api 接口来获得的,有两种 api,一种是核心指标,一种是自定义指标。 核心指标:Core metrics,由metrics-server提供API,即 metrics.k8s.io,仅提供Node和Pod的CPU和内存使用情况。api 是 metrics.k8s.io 自定义指标:Custom Metrics,由Prometheus Adapter提供API,即 custom.metrics.k8s.io,由此可支持任意Prometheus采集到的自定义指标。api 是 custom.metrics.k8s.io 和 external.metrics.k8s.io 一、核心指标metrics-server 阿里的 ACK 缺省是装了 metrics-server 的,看一下,系统里有一个metrics-server kubectl get pods -n kube-system 再看看 api 的核心指标能拿到什么,先看 Node 的指标: kubectl get --raw "/apis/metrics.k8s.io" | jq . kubectl get --raw "/apis/metrics.k8s.io/v1beta1" | jq . kubectl get --raw "/apis/metrics.k8s.io/v1beta1/nodes" | jq . 可以看到阿里 eci 虚拟节点的 cpu 和 memory 资源。 ...

2021年11月25日 · 6 分钟 · 1263 字 · 八戒

ansible vault加密的使用

公司已经由 saltstack 全面转向了 ansible 。 用 ansible-playbook 执行各种任务的时候,需要登录主机,就必然涉及到主机 ssh 密码的输入。 最早我们是在 inventory 里做了定义: [deqin:vars] ansible_ssh_common_args="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" host_key_checking=False ansible_ssh_user="peadmin" ansible_ssh_pass="Fuck2021!" [deqin] 192.168.1.19 太直白了,所有看到这文件内容的人都会知道密码了。完全没有安全性,这样行不通啊! 好在 ansible-vault 提供了一种方法来解决:那就是生成一个密文放进去,然后解开它必须再输入一个密码。这样看到的人也不知道实际的密码到底是什么 具体的做法如下,首先生成 key –> 加密字符串的键值对: ansible-vault encrypt_string 'Fuck2021!' --name 'ansible_ssh_pass' 输入密码,会得到下面一串字符 ansible_ssh_pass: !vault | $ANSIBLE_VAULT;1.1;AES256 37393235646234613332646366306233346330656666623862313339313861393239646261366237 6663343263363161643634653266343466356634656539650a393834663938636165336431656433 66333761643538623434363334316661653035313166333137373562363436613636366162353239 3661623733323933350a373164626131646235616361356638653733646534616163393362373135 6139 这个就是密文了,必须用输入的密码才能解开。 注意:这里的键值 name 不可改变,如果你想把字符串拷贝下来,改掉 ansible_ssh_pass 的名字,改成别的,想改名引用,是不行的。 这一大长串密文有以下两种用法: 一、ini格式的inventory引用 最原始的 inventory.ini 内容如下: [deqin:vars] ansible_ssh_common_args="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" host_key_checking=False ansible_ssh_user="peadmin" [deqin] 192.168.1.19 我们定义 playbook 文件 shenji.yml: - hosts: deqin become: yes vars_files: - pass.yml vars: ansible_ssh_pass: '{{ ansible_ssh_pass }}' tasks: - name: mkdirs file: path="{{ item }}" state=directory with_items: - "OS.05" - "OS.06" 把密文放进 pass.yml 文件 ...

2021年11月24日 · 1 分钟 · 182 字 · 八戒

使用FreeIPA和FreeRadius搭建双因子认证服务器

审计啊审计,公司使用的华为防火墙需要配置双因子登录认证,这下麻烦了。 查了一下华为手册,支持 Radius 认证,那么没办法,最省钱的办法就是用 FreeIPA 和 FreeRadius 搭一套 OTP 双因子认证了。 系统是 CentOS 7 ,已关闭防火墙服务,方法如下: 一、搭建FreeIPA 首先设置 hostname hostnamectl set-hostname freeipa.rendoumi.local echo "192.168.1.5 freeipa.rendoumi.local" >> /etc/hosts 然后安装 FreeIPA,注意要回答的几个问题 不装bind,无论是 dnsmasq 或 coredns,都比 bind 轻,要装也装那两个。 server hostname 是 freeipa.rendoumi.local domian name 是 rendoumi.local realm name 是大写的 RENDOUMI.LOCAL 有两个密码,第一个是 LDAP 的密码,第二个是 IPA 的密码 yum -y install deltarpm yum update yum -y install freeipa-server sysctl net.ipv6.conf.all.disable_ipv6=0 ipa-server-install This program will set up the IPA Server. This includes: * Configure a stand-alone CA (dogtag) for certificate management * Configure the Network Time Daemon (ntpd) * Create and configure an instance of Directory Server * Create and configure a Kerberos Key Distribution Center (KDC) * Configure Apache (httpd) To accept the default shown in brackets, press the Enter key. WARNING: conflicting time&date synchronization service 'chronyd' will be disabled in favor of ntpd Do you want to configure integrated DNS (BIND)? [no]:no Server host name [freeipa.rendoumi.local]: Please confirm the domain name [rendoumi.local]: Please provide a realm name [RENDOUMI.LOCAL]: Directory Manager password: Password (confirm): ... IPA admin password: Password (confirm): The IPA Master Server will be configured with: Hostname: freeipa.rendoumi.local IP address(es): 192.168.1.5 Domain name: rendoumi.local Realm name: RENDOUMI.LOCAL Continue to configure the system with these values? [no]: yes The following operations may take some minutes to complete. Please wait until the prompt is returned. Configuring NTP daemon (ntpd) ... Setup complete Next steps: 1. You must make sure these network ports are open: TCP Ports: * 80, 443: HTTP/HTTPS * 389, 636: LDAP/LDAPS * 88, 464: kerberos UDP Ports: * 88, 464: kerberos * 123: ntp 2. You can now obtain a kerberos ticket using the command: 'kinit admin' This ticket will allow you to use the IPA tools (e.g., ipa user-add) and the web user interface. Be sure to back up the CA certificate stored in /root/cacert.p12 This file is required to create replicas. The password for this file is the Directory Manager password 以上,就装好了 FreeIPA,配置文件在 /etc/ipa/default.conf ...

2021年11月23日 · 4 分钟 · 689 字 · 八戒

KVM下附加硬盘的passthrough直通

这个比较有意思,同事要存放 2TB 的数据,但是系统是 1.7TB 的 4 块 600G 盘组成的 Raid10。 很明显盘空间不够了,去库房找两块 10TB 的大盘组成 Raid1 给他用好了。 问题来了,系统是 KVM 虚机,怎样把这个 10TB 的大盘给送进虚机呢? 这里面还真有要注意的问题: Important Guest virtual machines should not be given write access to whole disks or block devices (for example, /dev/sdb). Guest virtual machines with access to whole block devices may be able to modify volume labels, which can be used to compromise the host physical machine system. Use partitions (for example, /dev/sdb1) or LVM volumes to prevent this issue. ...

2021年11月19日 · 2 分钟 · 327 字 · 八戒

CentOS7的救援模式和紧急模式

说到 CentOS7 的紧急模式与救援模式,网上可以搜到漫天飞的帖子,说一下区别 RESCUE 救援模式: 救援模式启动的系统没有挂载硬盘,可以将硬盘 mount 出然后拷出数据。 EMERGENCY 紧急模式: 紧急模式启动的系统是一个最小的环境。根目录档案系统将会被挂载为仅能读取,而且将不会做任何的设定。 当然进入的方法也很简单,进入系统的时候按 e 修改 grub 菜单参数,就可以进入不同的模式 本文讨论的重点不是怎么进去,而是那两句命令,在紧急状态下反正我是记不住的 systemd.unit=rescue.target systemd.unit=emergency.target 都没有之前的 single 简单,也完全记不住,既然记不住,那就干脆做到菜单里好了,这才是本文的重点。 现在都是使用 grub2 了,而不是 grub,这很重要。grub2的配置文件是 /boot/grub2/grub.cfg。 修改 grub2 有两个工具,grub2-mkconfig 和 grubby,不要同时使用这两个工具修改,会覆盖的 grub2-mkconfig 会去搜索 /boot 目录下的内核文件,有多少个内核就会生成多少个启动项。那么如果是同一个内核,想修改不同的启动参数,做多个启动项就完蛋,他不能自动生成单内核的多个启动项 grubby 很灵活,可以根据当前 grub2 的配置,生成一个内核,多个不同启动参数的多个启动项。 那么我们要加进去两个只是启动参数不同,内核其实一样的启动项,用 grubby 就好了 grubby --add-kernel=\$(ls -1cat /boot/vmlinuz*|grep rescue) --title="RESCUE BOOT" --initrd=\$(ls -1cat /boot/initramfs*|grep rescue) --args="systemd.unit=rescue.target" --copy-default grubby --add-kernel=\$(ls -1cat /boot/vmlinuz*|grep rescue) --title="EMERGENCY BOOT" --initrd=\$(ls -1cat /boot/initramfs*|grep rescue) --args="systemd.unit=emergency.target" --copy-default 切忌我们之后不能运行 ...

2021年11月19日 · 1 分钟 · 86 字 · 八戒

linux下web pacproxy的用法

说实在话,这个场景非常怪异,客户在 linux 下要动态根据 url 选择代理: 看图,中间的是前端代理,地址是 192.168.1.1:8080,然后客户设置使用这个代理 export http_proxy=http://192.168.1.1:8080 export https_proxy=http://192.168.1.1:8080 然后对应后端有三个代理,两个 http 代理,一个 socks 代理 http 192.168.2.1:3128 socks 192.168.2.2:1080 http 192.168.2.3:3128 我们要根据客户的请求 URL 来决定具体要使用后端的哪个代理 这个如果在浏览器上设置非常容易,设置 PAC 即可。但是偏偏客户端不是浏览器,而是一个程序,那么麻烦就来了。怎么设置呢? 步骤很简单: 一、安装 pacproxy 网址:https://github.com/williambailey/pacproxy wget https://github.com/williambailey/pacproxy/releases/download/v.2.0.4/pacproxy_2.0.4_linux_amd64.tar.gz tar zxvf pacproxy_2.0.4_linux_amd64.tar.gz 拷出来 pacproxy 备用 二、生成配置文件 最主要的就是 PAC 文件的生成 我们给个例子: cat << EOF >> pac function FindProxyForURL(url, host) { if (host == "www.baidu.com") { return "PROXY 192.168.2.1:3128"; } else if (host == "www.sina.com.cn") { return "SOCKS 192.168.2.2:1080"; } else if (host == "www.sohu.com") { return "SOCKS 192.168.2.3:1080"; } else { return "DIRECT"; } } EOF 其实 PAC 文件的内容就是一段 javascript,用来返回代理的地址 ...

2021年11月16日 · 1 分钟 · 117 字 · 八戒