Kubernetes Nexus搭建Docker私有仓库

一.创建命名空间

1.命令方式

  • 通过命令方式创建一个名为nexus3的namespace

    kubectl create namespace nexus3

2.yaml方式

  • 通过yaml文件方式创建一个名为nexus3的namespace

    apiVersion: v1
    kind: Namespace
    metadata:
       name: nexus3
       labels:
         name: nexus3
    kubectl apply repo-nexus-ns.yaml 

二.创建PV/PVC

1.创建存储类

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: nexus3-nfs
provisioner: example.com/external-nfs
parameters:
  server: 10.23.23.45 #nfs服务器IP地址
  path: /usr1/nfs-share/nexus #nfs共享路径
  readOnly: "false"
kubectl apply nexus_class.yaml

注意:/usr1/nfs-share/nexus #nfs共享路径 设置成777权限

2.创建PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nexus-pv
  namespace: nexus3
spec:
  capacity:
    storage: 600Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteOnce
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: nexus3-nfs
  nfs:
    path: /usr1/nfs-share/nexus #nfs共享路径
    server: 10.23.23.45 #nfs服务器IP地址
kubectl apply nexus_pv.yaml

3.创建PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nexus-pvc
  namespace: nexus3
spec:
  storageClassName: nexus3-nfs
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 600Gi
kubectl apply nexus_pvc.yaml

4.查看PV和PVC创建情况

kubectl get pv

三.K8S部署Nexus

1.创建配置文件

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nexus3
  name: nexus3
  namespace: nexus3
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nexus3
  template:
    metadata:
      labels:
        app: nexus3
      name: nexus3
      namespace: nexus3
    spec:
      containers:
      - name: nexus3
        image: sonatype/nexus3
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8081
          protocol: TCP
          name: web
        - containerPort: 8082
          protocol: TCP
          name: docker-hosted
        - containerPort: 8083
          protocol: TCP
          name: docker-group
        volumeMounts:
        - name: nexus-data
          mountPath: /nexus-data
      volumes:
        - name: nexus-data
          persistentVolumeClaim:
            claimName: nexus-pvc
​
---
apiVersion: v1
kind: Service
metadata:
  name: nexus3
  namespace: nexus3
  labels:
    app: nexus3
spec:
  selector:
    app: nexus3
  type: NodePort
  sessionAffinity: None
  ports:
    - name: web
      protocol: TCP
      port: 8081
      targetPort: 8081
      nodePort: 30005
    - name: docker-hosted
      protocol: TCP
      port: 8082
      targetPort: 8082
      nodePort: 30006
    - name: docker-group
      protocol: TCP
      port: 8083
      targetPort: 8083
      nodePort: 30007

2.执行命令使其生效

kubectl apply -f nexus.yaml 

四.配置Nexus服务器

1.查询初始密码

kubectl exec XXXXXXXX -n nexus3 -- cat /nexus-data/admin.password

2.修改初始密码

五.创建自定义docker私有仓库

1.创建docker本地仓库

  • 进入Repositories ,点击Create Repository,选择docker(hosted)

  • 配置空闲端口8082 (注意:这里端口配置的是pod里面配置的空闲端口,不是service的暴露端口)

  • 点击Create Repository,创建docker本地仓库成功

  • 设置dcoker仓权限,点击Realms,选择Docker Bearer Token Realm配置到右边,点击save即可

2.创建docker代理仓库

  • 点击create Repository,选择docker(proxy)进行创建

  • 配置加速器地址(如:https://docker.mirrors.ustc.edu.cn),选择Use Docker Hub

  • 点击Create Repository,创建代理仓库成功

3.创建docker仓库组

用于pull镜像至本地使用,可下载外网镜像和从本地push到私库的镜像

  • 点击create Repository,选择docker(group)进行创建

  • 配置空闲端口8083

  • 配置group成员docker_hosteddocker_proxy,点击Create Repository,创建仓库组成功

六.修改docker配置

1.修改daemon.json文件

cat >> /etc/docker/daemon.json << EOF
{ "insecure-registries":["10.169.6.150:30006","10.169.6.150:30007"] }
EOF

2.重启docker服务

sudo systemctl daemon-reload
sudo systemctl restart docker

3.检查是否生效

sudo docker info

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值