这篇文章跟我上一篇《k8s搭建pip私有源devpi》过程类似:
准备知识:
需要对构建docker镜像, k8s搭建、部署应用,对deployment (nodeSelect, node亲和性等),service, ingress编写yml文件熟悉,不熟悉的先去打一下基础再来看
1、私有docker镜像仓库(非必要)
2、k8s集群(安装有nginx ingress)(必要)
3、构建verdaccio镜像 ,采用Dockerfile (必要)
(这里使用node:latest镜像,网上说用verdaccio/verdaccio镜像,后面挂载主机共享存储用来存放模块包的时候启动服务报错,提示没有权限写入,因为verdaccio/verdaccio镜像使用的是verdaccio用户运行服务,虽然我的主机共享存储设置了777权限,但还是提示没有权限写入,所以后面我直接用node:latest镜像root权限运行服务,问题解决了)
# cat Dockerfile
FROM node:latest
RUN mkdir -p /verdaccio/storage && \
npm_config_user=root npm install -g verdaccio
WORKDIR /verdaccio
EXPOSE 4873
CMD ["verdaccio","--config","/verdaccio/conf/config.yaml"]
开始build镜像, 创建的镜像名为verdaccio, 注意"."会在当前目录自动寻找Dockerfile
# docker build -t verdaccio .
(构建好镜像之后,在本地就能看到名为verdaccio的镜像 : docker images 命令查看)
(
还可以push到自己的docker镜像仓库(方便k8s在各个节点能够使用公有镜像):
docker login xxx
docker push verdaccio
)
4、创建名称空间 verdaccio-namespaces.yml(namespaces)
(非必要,可用现有默认default名称空间)
# cat devpi-namespaces.yml
apiVersion: v1
kind: Namespace
metadata:
name: verdaccio
labels:
name: verdaccio
5、创建verdaccio-deployment.yml (deployment )
准备前:共享存储
(1)节点直接挂载共享存储 (在这里使用此方法)
(2)使用PV, PVC部署共享存储 (可选)
在要部署devpi的节点挂载共享存储 (挂载分区为 /data/verdaccio)用来存放服务配置文件,和存放模块包,文章后面会提到
(必要)
开始编写yml文件
# cat verdaccio-deployment.yml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: verdaccio
namespace: verdaccio
labels:
app: verdaccio
spec:
replicas: 1
template:
metadata:
labels:
app: verdaccio
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- node162
containers:
- name: verdaccio
image: verdaccio
securityContext:
privileged: true
ports:
- containerPort: 4873
volumeMounts:
- name: hosts
mountPath: /etc/hosts
- name: storage
mountPath: /verdaccio/storage
- name: conf
mountPath: /verdaccio/conf
imagePullSecrets:
- name: verdaccio
volumes:
- name: hosts
hostPath:
path: /etc/hosts
- name: storage
hostPath:
path: /data/verdaccio/storage
- name: conf
hostPath:
path: /data/verdaccio/conf
相关字段解释:
nodeAffinity: values: - node162 (部署pod的节点名为node 162, 根据自身情况修改)
image: verdaccio (使用自己第三步构建的镜像,根据自身情况修改)
volumeMounts:
mountPath: /verdaccio/storage (用于存放模块文件)
mountPath: /verdaccio/conf (用于存放服务启动配置文件(config.yaml)和密码文件(htpasswd), 下面提供配置文件)
volumes:
path: /data/verdaccio/storage (节点物理机路径,用于挂载到pod /verdaccio/storage)
path: /data/verdaccio/conf (节点物理机路径, 用于挂载到pod /verdaccio/conf )
(1)、服务启动配置文件 config.yaml (保存到node 162节点/data/verdaccio/config路径下)
# cat config.yaml
storage: /verdaccio/storage
auth:
htpasswd:
file: /verdaccio/conf/htpasswd
security:
api:
jwt:
sign:
expiresIn: 60d
notBefore: 1
web:
sign:
expiresIn: 7d
uplinks:
# 代理官方源库,当本地没有查找到模块,就会去官方下载到本地缓存
npmjs:
url: https://registry.npmjs.org/
packages:
'@jota/*':
access: $all
publish: $all
'@*/*':
# scoped packages
access: $all
publish: $all
proxy: npmjs
'**':
access: $all
# allow all known users to publish packages
# (anyone can register by default, remember?)
publish: $all
# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs
middlewares:
audit:
enabled: true
logs:
- {type: stdout, format: pretty, level: trace}
#- {type: file, path: verdaccio.log, level: info}
#侦听的端口
listen: 0.0.0.0:4873
(2)、密码文件
请用(htpasswd /data/verdaccio/conf/htpasswd test)命令生成,保存到指定路径,test这个用户就是初始化用来默认登录verdaccio,可自定义用户名
6 、创建verdaccio-service.yml (service)
准备前:
将verdaccio端口服务暴露出来
(1) nodePort (不采用,不能结合ingress使用)
(2) ClusterIP (这里采用此种方式)
# cat verdaccio-service.yml
apiVersion: v1
kind: Service
metadata:
name: verdaccio
namespace: verdaccio
labels:
app: verdaccio
spec:
type: ClusterIP
ports:
- name: http
port: 80
targetPort: 4873
selector:
app: verdaccio
7、创建verdaccio-ingress.yml (通过域名访问verdaccio)
准备前:对npm.test.local域名添加解析
添加verdaccio service的ingress域名
# cat verdaccio-ingress.yml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: verdaccio
namespace: verdaccio
spec:
rules:
- host: npm.test.local
http:
paths:
- path: /
backend:
serviceName: verdaccio
servicePort: 80
8、k8s部署verdaccio
kubectl apply -f verdaccio-namespaces.yml
kubectl apply -f verdaccio-deployment.yml
kubectl apply -f verdaccio-services.yml
kubectl apply -f verdaccio-ingress.yml
9、测试部署完之后就可以用浏览器打开http://npm.test.local
可以使用test用户登录查看私有库
10,进行测试
安装
# npm set registry http://npm.test.local
# npm install pm2 -g
会发现首次会去官方库下载,然后第二次安装就会从本地私有库缓存安装