OpenShift + buildConfig + ConfigMaps 的用例

Use case of OpenShift + buildConfig + ConfigMaps

我正在尝试创建 运行 buildconfig yml 文件。

C:\OpenShift>oc version
Client Version: 4.5.31
Kubernetes Version: v1.18.3+65bd32d

我有多个需要在 OpenShift 上部署的 Springboot WebUI 应用程序

拥有一组单独的配置 yml 文件(图像流、构建配置、部署配置、服务、路由), 对于每一个应用程序似乎都非常低效。

相反,我想要一组参数化的 yml 文件 我可以向其传递自定义参数以设置每个单独的应用程序

第一版

Dockerfile-

FROM org/rhelImage
USER root
# Install Yum Packages
RUN yum -y install\
net-tools\
&& yum -y install nmap-ncat\

RUN curl -s --create-dirs --insecure -L ${ARTIFACTURL} -o ${APPPATH}/${ARTIFACT}

# Add docker-entrypoint.sh to the image
ADD docker-entrypoint.sh /docker-entrypoint.sh

RUN chmod -Rf 775 /app && chmod 775 /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
RUN chmod -R g+rx /app


# Expose port
EXPOSE $MY_PORT

# Set working directory when container starts
WORKDIR $APPPATH

# Starting the applicaiton using ENTRYPOINT
#ENTRYPOINT ["sh","/docker-entrypoint.sh"]


$ oc create configmap myapp-configmap --from-env-file=MyApp.properties
configmap/myapp-configmap created

$ oc describe cm myapp-configmap
Name:         myapp-configmap
Namespace:    1234
Labels:       <none>
Annotations:  <none>

Data
====
APPPATH:
----
/app
ARTIFACT:
----
myapp.jar
ARTIFACTURL:
----
"https://myorg/1.2.3.4/myApp-1.2.3.4.jar"
MY_PORT:
----
12305
Events:  <none> 

buildconfig.yaml 片段

 strategy:
        dockerStrategy:
          env:
            - name: GIT_SSL_NO_VERIFY
              value: "true"
            - name: ARTIFACTURL
              valueFrom:
                configMapKeyRef:
                  name: "myapp-configmap"
                  key: ARTIFACTURL
            - name: ARTIFACT
              valueFrom:
                configMapKeyRef:
                  name: "myapp-configmap"
                  key: ARTIFACT

这很好用。但是我不知何故需要在文件中包含那些 env: 变量。

我这样做是为了获得更大的灵活性,也就是说,我在 docker 文件中引入了一个新变量,我不需要更改 buildconfig.yml 我只是将新的 key:value 对添加到 属性 文件,重建,我们就可以开始了

这就是我接下来要做的;

版本二

Docker 文件

FROM org/rhelImage
USER root
# Install Yum Packages
RUN yum -y install\
net-tools\
&& yum -y install nmap-ncat\

#Intializing the variables file;
RUN ["sh", "-c", "source ./MyApp.properties"]

RUN curl -s --create-dirs --insecure -L ${ARTIFACTURL} -o ${APPPATH}/${ARTIFACT}

# Add docker-entrypoint.sh to the image
ADD docker-entrypoint.sh /docker-entrypoint.sh

RUN chmod -Rf 775 /app && chmod 775 /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh
RUN chmod -R g+rx /app


# Expose port
EXPOSE $MY_PORT

# Set working directory when container starts
WORKDIR $APPPATH

# Starting the applicaiton using ENTRYPOINT
#ENTRYPOINT ["sh","/docker-entrypoint.sh"]


$ oc create configmap myapp-configmap --from-env-file=MyApp.properties=C:\MyRepo\MyTemplates\MyApp.properties
configmap/myapp-configmap created

C:\OpenShift>oc describe configmaps test-configmap
Name:         myapp-configmap
Namespace:    1234
Labels:       <none>
Annotations:  <none>

Data
====
MyApp.properties:
----
APPPATH=/app
ARTIFACTURL="https://myorg/1.2.3.4/myApp-1.2.3.4.jar"
ARTIFACT=myapp.jar
MY_PORT=12035
Events:  <none> 

buildconfig.yaml 片段

 source:
        contextDir: "${param_source_contextdir}"
        configMaps:
          - configMap:
              name: "${param_app_name}-configmap"

但是构建失败

STEP 9: RUN ls ./MyApp.properties
ls: cannot access ./MyApp.properties: No such file or directory
error: build error: error building at STEP "RUN ls ./MyApp.properties": error while running runtime: exit status 2 
 

这意味着配置映射文件没有复制到文件夹。

你能建议下一步做什么吗?

我认为您对 Openshift 有点误解。 你说的第一句话是

To have separate set of config yml files ( image stream, buildconfig, deployconfig, service, routes), for each and every application seems to be very inefficient.

但这就是 kubernetes/openshift 的工作原理。如果您的资源文件看起来相同,但仅使用不同的 git 资源或图像,那么您可能正在寻找 Openshift 模板。

Instead i would like to have a single set of parameterized yml files to which i can pass on custom parameters to setup each individual application

是的,我认为 Openshift 模板正是您要找的。如果您将模板上传到服务目录,每当您有一个新的应用程序要部署时,您可以在 UI 中添加一些变量并单击部署。

Openshift 模板只是所有 openshift 资源(configmap、服务、buildconfig 等)的参数化文件。 如果您的应用程序需要从某些 git 存储库构建,使用一些凭据,您可以参数化这些变量。

但也可以看看 Openshift 的 Source-to-Image 解决方案(我不确定您使用的是哪个版本,因此您必须 google 一些资源)。它可以构建和部署您的应用程序,而无需您编写自己的资源文件。