首頁 > 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學習者快速成長!