Use case of OpenShift + buildConfig + ConfigMaps



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

C:OpenShift>oc version
Client Version: 4.5.31
Kubernetes Version: v1.18.3+65bd32d
  • 背景:-

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

要拥有一组单独的配置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:variables。

我这样做是为了有更大的灵活性,比如说我在docker文件中引入了一个新变量,我不需要更改buildconfig.yml我只是将新的密钥:值对添加到属性文件中,重新构建,我们就可以进行了

这就是我接下来要做的;

第二版

Dockerfile

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:MyRepoMyTemplatesMyApp.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。你说的第一件事是

为每个应用程序分别设置一组配置yml文件(映像流、构建配置、部署配置、服务、路由(似乎效率很低。

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

相反,我希望有一组参数化的yml文件,我可以将自定义参数传递给这些文件,以设置每个单独的应用程序

是的,我认为Openshift模板就是你想要的。如果您将模板上传到服务目录,那么每当您有新的应用程序要部署时,都可以在UI中添加一些变量,然后单击部署。

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

但也可以看看Openshift的Source to Image解决方案(我不确定你使用的是什么版本,所以你必须在谷歌上搜索一些资源(。它可以构建和部署您的应用程序,而无需编写自己的资源文件。

最新更新