1.2. 创建 Source-to-image 构建
您可以创建 Source-to-image
构建,并将创建的镜像推送到自定义 Quay 存储库。
先决条件
- 您已在 OpenShift Container Platform 集群中安装了 Red Hat OpenShift Operator 的构建。
-
已安装
oc
CLI。 -
可选: 已安装
shp
CLI。
步骤
创建
Build
资源,并使用其中一个 CLI 将它应用到 OpenShift Container Platform 集群:示例: 使用
oc
CLIoc apply -f - <<EOF apiVersion: shipwright.io/v1beta1 kind: Build metadata: name: s2i-nodejs-build spec: source: 1 type: Git git: url: https://github.com/redhat-openshift-builds/samples contextDir: s2i-build/nodejs strategy: 2 name: source-to-image kind: ClusterBuildStrategy paramValues: 3 - name: builder-image value: quay.io/centos7/nodejs-12-centos7:master output: image: quay.io/<repo>/s2i-nodejs-example 4 pushSecret: registry-credential 5 EOF
Copy to clipboardCopied$ oc apply -f - <<EOF apiVersion: shipwright.io/v1beta1 kind: Build metadata: name: s2i-nodejs-build spec: source: 1 type: Git git: url: https://github.com/redhat-openshift-builds/samples contextDir: s2i-build/nodejs strategy: 2 name: source-to-image kind: ClusterBuildStrategy paramValues: 3 - name: builder-image value: quay.io/centos7/nodejs-12-centos7:master output: image: quay.io/<repo>/s2i-nodejs-example 4 pushSecret: registry-credential 5 EOF
示例: 使用
shp
CLIshp build create s2i-nodejs-build \ --source-url="https://github.com/redhat-openshift-builds/samples" --source-context-dir="s2i-build/nodejs" \1
Copy to clipboardCopied$ shp build create s2i-nodejs-build \ --source-url="https://github.com/redhat-openshift-builds/samples" --source-context-dir="s2i-build/nodejs" \1 --strategy-name="source-to-image" \2 --builder-image="quay.io/centos7/nodejs-12-centos7" \3 --output-image="quay.io/<repo>/s2i-nodejs-example" \4 --output-credentials-secret="registry-credential" 5
使用其中一个 CLI 检查是否创建了
Build
资源:示例: 使用
oc
CLIoc get builds.shipwright.io s2i-nodejs-build
Copy to clipboardCopied$ oc get builds.shipwright.io s2i-nodejs-build
示例: 使用
shp
CLIshp build list
Copy to clipboardCopied$ shp build list
创建
BuildRun
资源,并使用其中一个 CLI 将它应用到 OpenShift Container Platform 集群:示例: 使用
oc
CLIoc apply -f - <<EOF apiVersion: shipwright.io/v1beta1 kind: BuildRun metadata: name: s2i-nodejs-buildrun spec: build: name: s2i-nodejs-build 1 EOF
Copy to clipboardCopied$ oc apply -f - <<EOF apiVersion: shipwright.io/v1beta1 kind: BuildRun metadata: name: s2i-nodejs-buildrun spec: build: name: s2i-nodejs-build 1 EOF
- 1
spec.build.name
字段表示要运行的相应构建,该构建应该在同一个命名空间中可用。
示例: 使用
shp
CLIshp build run s2i-nodejs-build --follow 1
Copy to clipboardCopied$ shp build run s2i-nodejs-build --follow 1
- 1
- 可选:通过使用-
follow
标志,您可以在输出结果中查看构建日志。
运行以下命令之一检查
BuildRun
资源是否已创建:示例: 使用
oc
CLIoc get buildrun s2i-nodejs-buildrun
Copy to clipboardCopied$ oc get buildrun s2i-nodejs-buildrun
示例: 使用
shp
CLIshp buildrun list
Copy to clipboardCopied$ shp buildrun list
BuildRun
资源创建一个TaskRun
资源,然后创建 Pod 来执行构建策略步骤。
验证
所有容器完成其任务后,验证以下内容:
检查 pod 是否显示
STATUS
字段为Completed
:oc get pods -w
Copy to clipboardCopied$ oc get pods -w
输出示例
NAME READY STATUS RESTARTS AGE s2i-nodejs-buildrun-phxxm-pod 2/2 Running 0 10s s2i-nodejs-buildrun-phxxm-pod 1/2 NotReady 0 14s s2i-nodejs-buildrun-phxxm-pod 0/2 Completed 0 2m
Copy to clipboardCopiedNAME READY STATUS RESTARTS AGE s2i-nodejs-buildrun-phxxm-pod 2/2 Running 0 10s s2i-nodejs-buildrun-phxxm-pod 1/2 NotReady 0 14s s2i-nodejs-buildrun-phxxm-pod 0/2 Completed 0 2m
检查对应的
TaskRun
资源是否显示SUCCEEDED
字段为True
:oc get tr
Copy to clipboardCopied$ oc get tr
输出示例
NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME s2i-nodejs-buildrun-phxxm True Succeeded 2m39s 13s
Copy to clipboardCopiedNAME SUCCEEDED REASON STARTTIME COMPLETIONTIME s2i-nodejs-buildrun-phxxm True Succeeded 2m39s 13s
检查对应的
BuildRun
资源是否将SUCCEEDED
字段显示为True
:oc get br
Copy to clipboardCopied$ oc get br
输出示例
NAME SUCCEEDED REASON STARTTIME COMPLETIONTIME s2i-nodejs-buildrun True Succeeded 2m41s 15s
Copy to clipboardCopiedNAME SUCCEEDED REASON STARTTIME COMPLETIONTIME s2i-nodejs-buildrun True Succeeded 2m41s 15s
在验证过程中,如果构建失败,您可以检查
BuildRun
资源中的status.failureDetails
字段,以识别 pod 或容器中故障的确切点。注意pod 可能会切换到
NotReady
状态,因为其中一个容器完成了其任务。这是预期的行为。
验证镜像是否已推送到
build.spec.output.image
字段中指定的 registry。在登录到 registry 后,您可以尝试拉取镜像:podman pull quay.io/<repo>/<image> 1
Copy to clipboardCopied$ podman pull quay.io/<repo>/<image> 1
- 1
- 创建
Build
资源时使用的存储库名称和镜像名称。例如,您可以使用s2i-nodejs-example
作为镜像名称。
其他资源
1.2.1. 在网络限制环境中创建 Source-to-image 构建
您可以通过镜像 Source-to-image
构建策略,在网络限制环境中创建 Source-to-image
构建。
先决条件
- 集群可以连接并与可用于创建 Source-to-image 构建的 git 源进行交互。
-
您有在本地 registry 中创建
source-to-image
构建所需的 builder-to-image。如果您在本地 registry 中没有 builder-image,则镜像源镜像。
步骤
运行以下命令镜像
source-to-image
构建策略所需的镜像:oc image mirror --insecure -a <registry_authentication> registry.redhat.io/source-to-image/source-to-image-rhel8@sha256:d041c1bbe503d152d0759598f79802e257816d674b342670ef61c6f9e6d401c5 <mirror_registry>/<repo>/source-to-image-source-to-image-rhel8
Copy to clipboardCopied$ oc image mirror --insecure -a <registry_authentication> registry.redhat.io/source-to-image/source-to-image-rhel8@sha256:d041c1bbe503d152d0759598f79802e257816d674b342670ef61c6f9e6d401c5 <mirror_registry>/<repo>/source-to-image-source-to-image-rhel8
- 执行"创建 source-to-image 构建"部分中所述的步骤。