暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

云原生利器 -- Nexus3

devops运维先行者 2020-12-15
1090






1 Nexus3 简介

Nexus3 是一个统一的仓库系统,常见的有诸如apt、docker、maven2、npm、pypi、yum等repositories。如果开发工程师在开发软件时没有一个统一的repositories地址,那么将会受到不必要的影响,降低开发迭代效率。而Nexus3 就是这么一个平台,可以统一管理repositories仓库。这里文章介绍如何在k8s环境部署Nexus3,并快速使用,作为docker images 私有仓库。

2 K8s部署Nexus3

nexus-nm.yaml

    #创建namespace - nexus
    apiVersion: v1
    kind: Namespace
    metadata:
    name: nexus

    nexus-pv.yaml

      #创建 pv,这里使用aliyun的nas作为pv
      apiVersion: v1
      kind: PersistentVolume
      metadata:
      name: nexus-pv
      name: nexus
      labels:
      alicloud-nfs: nexus-pv
      spec:
      capacity:
      storage: 500Gi
      accessModes:
      - ReadWriteMany
      persistentVolumeReclaimPolicy: Retain
      nfs:
      server: "xxxxx.cn-hangzhou.nas.aliyuncs.com"
      path: nexus/

      nexus-pvc.yaml

        #创建pvc,注意labels对齐
        kind: PersistentVolumeClaim
        apiVersion: v1
        metadata:
        name: nexus-pvc
        namespace: nexus
        spec:
        accessModes:
        - ReadWriteMany
        resources:
        requests:
        storage: 500Gi
        selector:
        matchLabels:
        alicloud-nfs: nexus-pv

        nexus-deployment.yaml

          #创建nexus deployment,这里containers端口开放了8081、8082、8083分别作为web管理页面、docker repo、maven repo,且通过traefik2将web、docker域名开放
          apiVersion: apps/v1
          kind: Deployment
          metadata:
          name: nexus-deployment
          namespace: nexus
          spec:
          replicas: 1
          selector:
          matchLabels:
          app: nexus
          template:
          metadata:
          labels:
          app: nexus
          spec:
          containers:
          - name: nexus
          image: sonatype/nexus3
          ports:
          - containerPort: 8081
          - containerPort: 8082
          - containerPort: 8083
          resources:
          requests:
          memory: "128Mi"
          limits:
          memory: "2Gi"
          cpu: 4
          volumeMounts:
          - mountPath: nexus-data
          name: nexus-data
          volumes:
          - name: nexus-data
          persistentVolumeClaim:
          claimName: nexus-pvc
          ---
          apiVersion: v1
          kind: Service
          metadata:
          name: nexus-svc
          namespace: nexus
          spec:
          selector:
          app: nexus
          ports:
          - name: web
          protocol: TCP
          port: 8081
          targetPort: 8081
          - name: docker
          protocol: TCP
          port: 8082
          targetPort: 8082
          - name: maven
          protocol: TCP
          port: 8083
          targetPort: 8083
          ---
          apiVersion: traefik.containo.us/v1alpha1
          kind: IngressRoute
          metadata:
          name: nexus
          namespace: nexus
          spec:
          entryPoints:
          - http
          routes:
          - match: Host(`nexus3.domain.com`) && PathPrefix(`/`)
          kind: Rule
          priority: 1
          middlewares:
          - name: compress
          namespace: default
          services:
          - name: nexus-svc
          namespace: nexus
          port: 8081
          - match: Host(`repo-nexus.domain.com`) && PathPrefix(`/`)
          kind: Rule
          priority: 1
          middlewares:
          - name: compress
          namespace: default
          services:
          - name: nexus-svc
          namespace: nexus
          port: 8082

          按顺序分别kubectl apply部署nexus3,部署完成后可查看相关nexus资源。

          3 Nexus3使用

          3.1 登陆

          当我们首次登陆 nexus3.domain.com 的web界面的时候,需要输入admin的用户密码,这里需要通过查看nexus pods下面的/nexus-data/admin.password文件,

            #kubectl exec -n nexus nexus-deployment-697b6f5945-jkzst cat nexus-data/admin.password
            msNPSKNbX05BwV9V

            将输出的msNPSKNbX05BwV9V
            作为admin密码。

            3.2 添加Docker repo

            可以看到有个感叹号提醒,Nexus3建议至少使用4核CPU,这里使用2C,并无太大关系,如果有强迫症的同学可以增大CPU核数 >= 4C。

            1.点击系统配置 - Repositories - Create repository

            2.选择docker(hosted)

            3.创建一个name为docker-repo,选择8082作为http入口(如果需要https,可以在k8s上在增加一个Nexus端口作为https端口,同nexus deployment中的 ports.docker)

            4.点击save,创建完docker repo后,就可以通过Browse看到docker-repo已经online在线。

            4 Decker repo 使用

            4.1 调整docker配置

            由于使用的http协议,需要修改/etc/docker/daemon.json
            配置文件,

              {"insecure-registries": ["repo-nexus.k8s.stdomai.com"]}

              4.2 上传push

              1.docker login repo-nexus.domain.com
              输入admin用户密码,如果不想用admin用户登陆的,可以在Nexus3创建一个新的使用用户。

              2.docker tag 172.16.10.13/library/java:latest repo-nexus.domain.com/java:simba_latest

              3.docker push repo-nexus.domain.com/java:simba_latest

              4.3 下载pull

              docker pull repo-nexus.domain.com/java:simba_latest

              往期推荐

              K8S secret怎么友好更新?

              更好用的Kubernetes 桌面IDE -- Lens

              Python Linux自动化运维 -- Openpyxl Excel处理

              Traefik - Kubernetes 配置TCP/HTTP服务

              TSDB -- M3DB Prometheus远端存储方案

              运维神器 -- ELK

              Prometheus 监控架构  -- 生产级别

              Traefik版本升级与生产使用



              文章转载自devops运维先行者,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

              评论