首页 > Java > java教程 > 正文

在 Kubernetes 上设置 JFrog Artifactory 并连接 Spring Boot 应用程序

王林
发布: 2024-08-22 06:37:32
原创
558 人浏览过

本文档提供了有关在 Kubernetes 集群中设置 JFrog Artifactory 的指南。它作为开发人员在本地计算机上运行的 Kubernetes 环境中安装和配置 JFrog 的基本教程。

设置本地环境来构建DevOps资源

我使用 Docker 容器为多个应用程序设置工作环境(Setup Environment)。这种方法确保了应用程序开发的完全隔离和可维护的环境,使我们能够轻松启动和终止这些环境。下面是创建环境的 Docker 命令。

docker run -it --name test-jfrog-envornment-box -v ${HOME}/root/ -v ${PWD}/work -w /work --net host developerhelperhub/kub-terr-work-env-box sh
登录后复制

容器包含 Docker、Kubectl、Helm、Terraform、Kind、Git

在 Kubernetes 集群上设置 Jenkins

我已经创建了所有 Terraform 脚本,这些脚本可在 GitHub 存储库中找到。您可以在 Kubernetes 集群上下载并设置 Jenkins,该集群在 Docker 容器中本地运行。

将存储库克隆到本地 Linux 计算机上即可开始。

git clone https://github.com/developerhelperhub/kuberentes-help.git
cd kuberentes-help/kubenretes/tutorials/sections/0009/
登录后复制

使用 Kind 在 Docker 容器中设置 Kubernetes 集群,将集群命名为“devops-jfrog-cluster-control-plane” 该集群支持入口并公开端口 80 和 443 以允许访问服务来自集群外部。

集群创建 terraform 脚本在 kind 文件夹下可用

cd kind
terraform init #Install the required providers to set up the necessary resources.
terraform plan #Verify the resources that will be installed on the system.
terraform apply #Install resources on the system
登录后复制

以下命令验证集群

kubectl cluster-info
kubectl get nodes -o wide
登录后复制

在 Kubernetes 集群上设置 Nginx 入口控制器
NGINX Ingress Controller 用于管理 Kubernetes 集群中服务的外部访问。它充当 Kubernetes 应用程序的入口点,将外部 HTTP 和 HTTPS 流量路由到集群内的适当服务。

以下命令在集群上安装入口控制器

Ingress 创建脚本位于“ingress”文件夹下

cd ingress
kubectl apply -f ingress-nginx.yaml
登录后复制

以下命令验证 nginx 入口控制器

kubectl get -n ingress-nginx pod
登录后复制

输出

NAME                                       READY   STATUS      RESTARTS   AGE
ingress-nginx-admission-create-5mt2k       0/1     Completed   0          53s
ingress-nginx-admission-patch-w2rlk        0/1     Completed   0          53s
ingress-nginx-controller-d45d995d4-gl65h   1/1     Running     0          53s
登录后复制

设置 JFrog Artifactory OSS(开源)

添加 JFrog Artifactory OSS 社区 Helm 存储库:首先,您需要将 JFrog Artifactory OSS 社区 Helm 存储库添加到您的 Helm 客户端:

注意:JFrog 在“jfrog”文件夹下创建可用脚本

helm repo add jfrog https://charts.jfrog.io
helm repo update
登录后复制

以下命令可用于验证集群中安装的服务的app版本和chart版本:

helm search repo  artifactory-oss
登录后复制

在此示例中,我安装的购物车版本为“107.90.8”,应用程序版本为“7.90.8”

NAME                         CHART VERSION        APP VERSION        DESCRIPTION
jfrog/artifactory-oss        107.90.8             7.90.8             JFrog Artifactory OSS
登录后复制

创建一个名为 helm-value.yaml 的 Helm 值 YAML 文件,该文件将用于配置 JFrog Artifactory OSS 服务。 Helm 将使用此配置文件在集群中安装服务。

artifactory:
    postgresql:
    postgresqlPassword: postgres_password
    nginx:
    enabled: false
    ingress:
    enabled: false
登录后复制

在上面的配置中,我配置了以下几点:

  • Helm 使用默认的 PostgreSQL 数据库,我在其中指定了数据库密码。
  • NGINX 和 Ingress 资源已禁用。

以下 Helm 命令用于在集群中安装 Artifactory。它在集群中创建必要的资源,包括名为“artifactory-oss”的命名空间,并安装该命名空间内的所有资源。

helm install artifactory-oss -f helm-value.yaml jfrog/artifactory-oss --namespace artifactory-oss --create-namespace
登录后复制

Helm 图表值:https://github.com/jfrog/charts/blob/master/stable/artifactory-oss/values.yaml

我们可以验证所有资源都已安装在“artifactory-oss”命名空间中。 Artifactory 服务 UI 运行在端口 8082 上,而 Artifactory 本身运行在端口 8081 上。

kubectl get -n artifactory-oss all

NAME                               READY   STATUS    RESTARTS   AGE
pod/artifactory-oss-0              0/7     Running   0          3m19s
pod/artifactory-oss-postgresql-0   1/1     Running   0          3m19s

NAME                                          TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)                      AGE
service/artifactory-oss                       ClusterIP   10.96.170.63   <none>        8082/TCP,8025/TCP,8081/TCP   3m19s
service/artifactory-oss-postgresql            ClusterIP   10.96.95.36    <none>        5432/TCP                     3m19s
service/artifactory-oss-postgresql-headless   ClusterIP   None           <none>        5432/TCP                     3m19s

NAME                                          READY   AGE
statefulset.apps/artifactory-oss              0/1     3m19s
statefulset.apps/artifactory-oss-postgresql   1/1     3m19s
登录后复制

我们需要创建一个 Ingress 资源来将请求路由到 Artifactory 服务。在 ingress-resource.yaml 文件中添加以下配置。

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
    name: jfrog-ingress
    namespace: artifactory-oss
    annotations:
    nginx.ingress.kubernetes.io/proxy-body-size: "0"
spec:
    ingressClassName: nginx
    rules:
    - host: jfrog.artifactory.devops.myapp.com
        http:
        paths:
            - path: /
            pathType: ImplementationSpecific
            backend:
                service:
                name: artifactory-oss
                port:
                    number: 8082
登录后复制

我们需要配置 proxy-body-size=0。此设置指示 Ingress 在上传文件到 Artifactory 时不要对文件大小施加限制。

执行以下命令在集群中应用ingress

kubectl apply -f ingress-resource.yaml
登录后复制

验证 IP 地址是否已设置:

kubectl -n artifactory-oss get ingress

NAME            CLASS   HOSTS                                ADDRESS     PORTS   AGE
jfrog-ingress   nginx   jfrog.artifactory.devops.myapp.com   localhost   80      2m53s
登录后复制

将我们的域添加到本地计算机上的 /etc/hosts 文件的底部。此配置不应位于我们工作的 Linux 盒子“test-jfrog-enornment-box”内;它应该应用于您个人计算机的 /etc/hosts 文件。
(您需要管理员权限):

127.0.0.1 jfrog.artifactory.devops.myapp.com
登录后复制

我们可以在浏览器中打开artifactory UI“http://jfrog.artifactory.devops.myapp.com/”

Setup JFrog Artifactory on Kubernetes and Connect Spring Boot Application

You can log in using the default username "admin" and password "password." Upon your first login, Artifactory will prompt you to change the default password. Be sure to update the password, set the base URL to http://jfrog.artifactory.devops.myapp.com (the domain configured in the Artifactory Ingress resource), and skip any other initial configuration steps.

We can create the initial repositories configurations to push the dependencies and binary in the artifactory.

Navigate to “Artifactory → Artifacts → Manage Repositories → Create Repository” and create the following repositories:

  • Local: This repository manages your application binaries. For example “my-app-snapshot”
  • Remote: This repository stores all dependencies used in your application, which will be downloaded from central repositories and stored in repository. For example “my-app-central-snapshot”
  • Virtual: This virtual repository provides a common endpoint that aggregates the “Local” and “Remote” repositories. This endpoint will be configured in your application. “my-app-virtual-snapshot”

I am using maven repository to maintain the repository. Following configuration we have to give “my-app-snapshot” local repository

Setup JFrog Artifactory on Kubernetes and Connect Spring Boot Application

Following configuration we have to give “my-app-central-snapshot” local repository

Setup JFrog Artifactory on Kubernetes and Connect Spring Boot Application

Following configuration we have to give “my-app-virtual-snapshot” local repository

Setup JFrog Artifactory on Kubernetes and Connect Spring Boot Application

Setup JFrog Artifactory on Kubernetes and Connect Spring Boot Application

Add the local and remote repositories to the virtual repository and select the local repository in the “Default Deployment Repository”.

Once all the repositories are created, you can view them in the main section under “Artifactory → Artifacts.” The virtual URL http://jfrog.artifactory.devops.myapp.com/artifactory/my-app-virtual-snapshot/ will be used for your Maven application.

Setup JFrog Artifactory on Kubernetes and Connect Spring Boot Application

We need to configure the authentication details in the Maven settings configuration file “~/.m2/settings.xml” to enable your Maven application to authenticate with Artifactory.

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
        <server>
            <id>my-app-virtual-snapshot</id>
            <username>admin</username>
            <password>Give your artifactory admin passoword</password>
        </server>
    </servers>
</settings>
登录后复制

Note: The admin user should not be used for UI and Artifactory access. Instead, create a custom user with appropriate permissions for reading and writing in Artifactory.

We have configure the maven repository and distribution management tags inside our maven application POM XML file

<distributionManagement>
    <repository>
        <uniqueVersion>false</uniqueVersion>
        <id>my-app-virtual-snapshot</id>
        <name>my-app-virtual-snapshot</name>
        <url>http://jfrog.artifactory.devops.myapp.com/artifactory/my-app-virtual-snapshot/</url>
        <layout>default</layout>
    </repository>
</distributionManagement>

<repositories>
    <repository>
        <id>my-app-virtual-snapshot</id>
        <name>my-app-virtual-snapshot</name>
        <url>http://jfrog.artifactory.devops.myapp.com/artifactory/my-app-virtual-snapshot/</url>
        <layout>default</layout>
    </repository>
</repositories>
登录后复制

The we can deploy the maven application with following command

mvn clean deploy
登录后复制

We can the following output of maven :

Setup JFrog Artifactory on Kubernetes and Connect Spring Boot Application

Refernece Git Repo

  • https://github.com/developerhelperhub/spring-boot-jfrog-artifact-app
  • https://github.com/developerhelperhub/kuberentes-help/tree/main/kubenretes/tutorials/sections/0009

以上是在 Kubernetes 上设置 JFrog Artifactory 并连接 Spring Boot 应用程序的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!