nodejs多版本的安装与管理

最近在搞 JavaScripts,nodejs的版本满天飞,之前用的 nvm 管理的方法突然不能用了。 这里记录一下,比较新的 nvm 的安装使用方法: 下载安装: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/refs/heads/master/install.sh | bash nvm install 24 以上的脚本安装到底做了什么呢? 一、它 clone 了 nvm 到 $HOME/.nvm目录 二、它在 .bashrc 中添加了三行 事实上它是根据当前的 bash 选择在 .bash_profile、.zshrc、.profile、.bashrc中的某一个进行添加 然后我们退出终端,并重新进入,看看 nvm list nvm ls-remote 然后选择最新的版本安装: nvm install v16.13.1 如果想使用不同版本 nvm use 14.0.0 设置缺省版本或者取消 nvm alias defaut <your_nodejs_default_version> nvm alias default 14.0.0 node -v # prints 14.0.0 参考地址:https://github.com/nvm-sh/nvm

2021年12月17日 · 1 分钟 · 58 字 · 八戒

Backblaze类S3免费免备案对象存储

S3 的桶存储可以用 minio 来模拟。 网上有个 Backblaze 的网站,提供 10GB 的免费空间,且如果是公网访问,1GB流量是免费的,如果前面套了 Cloudflare 的CDN,那基本是全免了。 说下使用方式: 首先去 https://www.backblaze.com/ 注册个帐号: 然后直接建个桶(Create a Bucket) 桶文件类型选择 public,否则无法公网访问: 然后 gen 出 master app key 来并记录好: 随后最大的问题就来了,如果你用它页面自带的上传下载工具,彻底完蛋,拖进去的文件夹会变成扁平的,完全丧失目录结构。 必须要找一个好用的上传工具了,推荐 b2 。 b2 工具下载: https://github.com/Backblaze/B2_Command_Line_Tool 下载后直接改名为 b2 ,然后放到 /usr/local/bin 中 wget -O /usr/local/bin/b2 https://github.com/Backblaze/B2_Command_Line_Tool/releases/download/v3.1.0/b2-linux chmod 755 /usr/lcoal/b2 运行一下,有很多命令参数: 详细的使用文档: https://b2-command-line-tool.readthedocs.io/en/master/ 先去配置帐号,输入applicationKeyId和applicationKey b2 authorize-account 然后就可以往桶里传东西了,把当前目录下的东西传到 rendoumi 这个桶里,并且不要传 .git 目录 b2 sync --excludeDirRegex .git . b2://rendoumi/ 套 Cloudflare 就随意了。full ssl,建个 CNAME 且用 Page rule 做 url 转发就可以了。 ...

2021年12月08日 · 1 分钟 · 74 字 · 八戒

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 字 · 八戒