在 Kubernetes 集群中使用 Docker 运行 Nexus 3

2023-12-30

最好的运行设置是什么sonatype\nexus3Kubernetes 中允许使用 Docker 存储库吗?

目前我有一个基本设置:

  • 部署sonatype\nexus3
  • 内部服务暴露80和5000端口
  • Ingress + kube-lego 提供对 Nexus UI 的 HTTPS 访问

如何解决不允许多个端口的入口限制?


tl;dr

Nexus 需要通过 SSL 提供服务,否则 docker 将无法连接到它。这可以通过 k8s ingress + 来实现库贝乐高 https://github.com/jetstack/kube-lego/ for a 让我们加密 https://letsencrypt.org/证书。任何其他真实证书也可以。然而,为了通过一个入口(即一个端口)同时为 Nexus UI 和 docker 注册表提供服务,需要在入口后面有一个反向代理来检测 docker 用户代理并将请求转发到注册表。

                                                                             --(IF user agent docker) --> [nexus service]nexus:5000 --> docker registry
                                                                             |
[nexus ingress]nexus.example.com:80/ --> [proxy service]internal-proxy:80 -->|
                                                                             |
                                                                             --(ELSE                ) --> [nexus service]nexus:80   --> nexus UI

启动 Nexus 服务器

Nexus-deployment.yaml这利用了 azureFile 卷,但您可以使用任何卷。此外,由于显而易见的原因,这个秘密没有被展示。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nexus
  namespace: default

spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: nexus
    spec:
      containers:
        - name: nexus
          image: sonatype/nexus3:3.3.1
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8081
            - containerPort: 5000
          volumeMounts:
            - name: nexus-data
              mountPath: /nexus-data
          resources:
            requests:
              cpu: 440m
              memory: 3.3Gi
            limits:
              cpu: 440m
              memory: 3.3Gi
      volumes:
        - name: nexus-data
          azureFile:
            secretName: azure-file-storage-secret
            shareName: nexus-data

添加运行状况和就绪探针始终是一个好主意,以便 Kubernetes 可以检测到应用程序何时出现故障。击中index.html页面并不总是工作得很好,所以我改用 REST API。这需要为用户添加授权标头nx-script-*-browse允许。显然,您必须首先在没有探针的情况下启动系统来设置用户,然后再更新您的部署。

      readinessProbe:
        httpGet:
          path: /service/siesta/rest/v1/script
          port: 8081
          httpHeaders:
            - name: Authorization
              # The authorization token is simply the base64 encoding of the `healthprobe` user's credentials:
              # $ echo -n user:password | base64
              value: Basic dXNlcjpwYXNzd29yZA==
        initialDelaySeconds: 900
        timeoutSeconds: 60
      livenessProbe:
        httpGet:
          path: /service/siesta/rest/v1/script
          port: 8081
          httpHeaders:
            - name: Authorization
              value: Basic dXNlcjpwYXNzd29yZA==
        initialDelaySeconds: 900
        timeoutSeconds: 60

因为 Nexus 有时需要很长时间才能启动,所以我使用了非常慷慨的初始延迟和超时。

Nexus-service.yaml为 UI 暴露端口 80,为注册表暴露端口 5000。这必须与通过 UI 为注册表配置的端口相对应。

apiVersion: v1
kind: Service
metadata:
  labels:
    app: nexus
  name: nexus
  namespace: default
  selfLink: /api/v1/namespaces/default/services/nexus

spec:
  ports:
  - name: http
    port: 80
    targetPort: 8081
  - name: docker
    port: 5000
    targetPort: 5000
  selector:
    app: nexus
  type: ClusterIP

启动反向代理(nginx)

proxy-configmap.yaml The nginx.conf添加为 ConfigMap 数据卷。这包括用于检测 docker 用户代理的规则。这里依赖kubernetes DNS来访问nexus服务作为上游。

apiVersion: v1
data:
  nginx.conf: |
    worker_processes auto;

    events {
        worker_connections 1024;
    }

    http {
        error_log /var/log/nginx/error.log warn;
        access_log  /dev/null;
        proxy_intercept_errors off;
        proxy_send_timeout 120;
        proxy_read_timeout 300;

        upstream nexus {
            server nexus:80;
        }

        upstream registry {
            server nexus:5000;
        }

        server {
            listen 80;
            server_name nexus.example.com;

            keepalive_timeout  5 5;
            proxy_buffering    off;

            # allow large uploads
            client_max_body_size 1G;

            location / {
            # redirect to docker registry
            if ($http_user_agent ~ docker ) {
                proxy_pass http://registry;
            }
            proxy_pass http://nexus;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto "https";
            }
        }
    }
kind: ConfigMap
metadata:
  creationTimestamp: null
  name: internal-proxy-conf
  namespace: default
  selfLink: /api/v1/namespaces/default/configmaps/internal-proxy-conf

代理部署.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: internal-proxy
  namespace: default

spec:
  replicas: 1
  template:
    metadata:
      labels:
        proxy: internal
    spec:
      containers:
        - name: nginx
          image: nginx:1.11-alpine
          imagePullPolicy: IfNotPresent
          lifecycle:
            preStop:
              exec:
                command: ["/usr/sbin/nginx","-s","quit"]
          volumeMounts:
            - name: internal-proxy-conf
              mountPath: /etc/nginx/
          env:
            # This is a workaround to easily force a restart by incrementing the value (numbers must be quoted)
            # NGINX needs to be restarted for configuration changes, especially DNS changes, to be detected
            - name: RESTART_
              value: "0"
      volumes:
        - name: internal-proxy-conf
          configMap:
            name: internal-proxy-conf
            items:
              - key: nginx.conf
                path: nginx.conf

代理服务.yaml代理是故意的类型ClusterIP因为入口会将流量转发给它。本例中未使用端口 443。

kind: Service
apiVersion: v1
metadata:
  name: internal-proxy
  namespace: default

spec:
  selector:
    proxy: internal
  ports:
    - name: http
      port: 80
      targetPort: 80
    - name: https
      port: 443
      targetPort: 443
  type: ClusterIP

创建入口

Nexus-ingress.yaml此步骤假设您有一个 nginx 入口控制器。如果您有证书,则不需要入口,而是可以公开代理服务,但您将无法获得 kube-lego 的自动化优势。

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nexus
  namespace: default
  annotations:
    kubernetes.io/ingress.class: "nginx"
    kubernetes.io/tls-acme: "true"

spec:
  tls:
    - hosts:
      - nexus.example.com
      secretName: nexus-tls
  rules:
    - host: nexus.example.com
      http:
        paths:
        - path: /
          backend:
            serviceName: internal-proxy
            servicePort: 80
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Kubernetes 集群中使用 Docker 运行 Nexus 3 的相关文章

随机推荐