Is there an already built in j2 template processor in kubernetes or docker? I am doing the configuration below and wanted to plugin the values on the template.
Note that using hostPath is not an option since this is using openshift and no pv/pvc can be used.
containers:
- image: some-docker-image:latest
name: some-docker-image
volumeMounts:
- mountPath: /etc/app/conf
name: configuration-volume
.
. Do some j2 template processing here if possible.
.
volumes:
- name: configuration-volume
gitRepo:
repository: "https://gitrepo/repo/example.git
There isn't any templating support built into Kubernetes. You can easily build a templating system on top of the yaml/json files that you pass into kubectl -f create though. I know some folks that are using jsonnet to accomplish this.
The discussion around adding templates is happening in https://github.com/kubernetes/kubernetes/issues/23896 if you'd like to contribute.
Related
I am looking to pass environment variable in my openshift oc process command.
I see option for param file, but nothing for env variable.
Question: Are params = environment variables? I mean can i set env variables using this option? I tried this but I did not get any env variable set after the deployment was done.
I am going through below document.
https://docs.openshift.com/container-platform/3.11/dev_guide/templates.html
Only way I am achieving setting up my env variables is like below
oc process -f helloworld.yaml | oc create -f -
curl http://servertofetchenvironmentvariables:5005/env/dev/helloworld | oc set env dc/helloworld -
This ends up two deployments. Any lead on resolving this and making it as one command will be helpful. I have to use template to create my application.
Question: Are params = environment variables?
No, parameters only apply to your template file. Your template file contains placeholders such as "${MY_PASSWORD}", which are then replaced when using oc process.
I mean can i set env variables using this option?
You can, but you would need to edit your template file to include all these environment variables and the relevant placeholder.
Only way I am achieving setting up my env variables is like below
That should definitely work, as you would then update the created DeploymentConfig (dc/helloworld in your case) with your new environment variables.
A good alternative could be to populate your environment variables using a ConfigMap (so having them totally separate from your Deployment) using envFrom like so:
apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c", "env" ]
envFrom:
- configMapRef:
name: special-config
restartPolicy: Never
This would also decouple your configuration from your Deployment and you could store / change your environment variables in your ConfigMap.
Source: https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#configure-all-key-value-pairs-in-a-configmap-as-container-environment-variables
I am trying to create a Git source build of this Dockerfile: https://github.com/WASdev/ci.docker/blob/master/ga/latest/full/Dockerfile.ubi.ibmjava8
I have the following configuration in my BuildConfig:
source:
git:
uri: "https://github.com/WASdev/ci.docker"
ref: "master"
contextDir: "ga/latest/full"
However, the above assumes the use of the Dockerfile filename while I want to use Dockerfile.ubi.ibmjava8 as in docker build -f Dockerfile.ubi.ibmjava8 ..
How can I use Dockerfile.ubi.ibmjava8 instead of Dockerfile in OpenShift?
TL;DR: Yes. You will not be able to use a Dockerfile with a different name than Dockerfile
On Build Strategy Options on section Dockerfile Path you will find the constrains of OCP regarding the Docker Strategy:
By default, Docker builds use a Dockerfile (named Dockerfile) located
at the root of the context specified in the
BuildConfig.spec.source.contextDir field.
The dockerfilePath field allows the build to use a different path to
locate your Dockerfile, relative to the
BuildConfig.spec.source.contextDir field. It can be simply a different
file name other than the default Dockerfile (for example,
MyDockerfile), or a path to a Dockerfile in a subdirectory (for
example, dockerfiles/app1/Dockerfile).
And they also use an expample:
strategy:
dockerStrategy:
dockerfilePath: dockerfiles/app1/Dockerfile
I would like to to customize settings.xml for s2i maven builds in Openshift 3.10. While this is easily done in version 3.11 using config maps:
https://docs.openshift.com/container-platform/3.11/dev_guide/builds/build_inputs.html#using-secrets-during-build
I did not found any solution for 3.10. Is there a workaround / solution for this?
thank you!
In 3.11, you can create a ConfigMap for your settings.xml file
$ oc create configmap settings-mvn --from-file=settings.xml=<path/to/settings.xml>
And use that to override it in your build. (Source)
source:
git:
uri: https://github.com/wildfly/quickstart.git
contextDir: helloworld
configMaps:
- configMap:
name: settings-mvn
As you point out, 3.10, there is no support for ConfigMaps in BuildConfigs, however, you can create a secret with the same content
$ oc create secret generic settings-mvn --from-file=settings.xml=<path/to/settings.xml>
And use that to override it in your build. (Source)
source:
git:
uri: https://github.com/wildfly/quickstart.git
contextDir: helloworld
secrets:
- secret:
name: settings-mvn
Alternatively, you can also include the settings.xml file in your git repo in order to override the default settings.xml. Simply placing your file at source_dir/configuration/settings.xml should be sufficient. (Source)
I notice that when I create an Ingress on GKE several annotations are automatically generated thus:
annotations:
ingress.kubernetes.io/backends: '{"k8s-be-30266--edf23f6631e3474e":"HEALTHY"}'
ingress.kubernetes.io/forwarding-rule: k8s-fw-default-nginx-ingress--edf23f6631e3474e
ingress.kubernetes.io/target-proxy: k8s-tp-default-nginx-ingress--edf23f6631e3474e
ingress.kubernetes.io/url-map: k8s-um-default-nginx-ingress--edf23f6631e3474e
Is there any way these annotations can be viewed, as this would help me further understand better. TIA
Shaun
For this you need to use get and -o yaml
For example:
kubectl get deployment myapp-deployment -o yaml
result in terminal will be YAML with whole configuration of deployment myapp-deployment
More useful command you can find in official doc page
We have a Java web application that is supposed to be moved from a regular deployment model (install on a server) into an OpenShift environment (deployment as docker container). Currently this application consumes a set of Java key stores (.jks files) for client certificates for communicating with third party web interfaces. We have one key store per interface.
These jks files get manually deployed on production machines and are occasionally updated when third-party certificates need to be updated. Our application has a setting with a path to the key store files and on startup it will read certificates from them and then use them to communicate with the third-party systems.
Now when moving to an OpenShift deployment, we have one docker image with the application that is going to be used for all environments (development, test and production). All configuration is given as environment variables. However we cannot give jks files as environment variables these need to be mounted into the docker container's file system.
As these certificates are a secret we don't want to bake them into the image. I scanned the OpenShift documentation for some clues on how to approach this and basically found two options: using Secrets or mounting a persistent volume claim (PVC).
Secrets don't seem to work for us as they are pretty much just key-value-pairs that you can mount as a file or have handed in as environment variables. They also have a size limit to them. Using a PVC would theoretically work, however we'd need to have some way to get the JKS files into that volume in the first place. A simple way would be to just start a shell container mounting the PVC and copying the files manually into it using the OpenShift command line tools, however I was hoping for a somewhat less manual solution.
Do you have found a clever solution to this or a similar problem where you needed to get files into a container?
It turns out that I misunderstood how secrets work. They are indeed key-values pairs that you can mount as files. The value can however be any base64 encoded binary that will be mapped as the file contents. So the solution is to first encode the contents of the JKS file to base64:
cat keystore.jks| base64
Then you can put this into your secret definition:
apiVersion: v1
kind: Secret
metadata:
name: my-secret
namespace: my-namespace
data:
keystore.jks: "<base 64 from previous command here>"
Finally you can mount this into your docker container by referencing it in the deployment configuration:
apiVersion: v1
kind: DeploymentConfig
spec:
...
template:
spec:
...
container:
- name: "my-container"
...
volumeMounts:
- name: secrets
mountPath: /mnt/secrets
readOnly: true
volumes:
- name: secrets
secret:
secretName: "my-secret"
items:
- key: keystore.jks
path: keystore.jks
This will mount the secret volume secrets at /mnt/secrets and makes the entry with the name keystore.jks available as file keystore.jks under /mnt/secrets.
I'm not sure if this is really a good way of doing this, but it is at least working here.
You can add and mount the secrets like stated by Jan Thomä, but it's easier like this, using the oc commandline tool:
./oc create secret generic crnews-keystore --from-file=keystore.jks=$HOME/git/crnews-service/src/main/resources/keystore.jks --from-file=truststore.jks=$HOME/git/crnews-service/src/main/resources/truststore.jks --type=opaque
This can then be added via UI: Applications->Deployments->-> "Add config files"
where you can choose what secret you want to mount where.
Note, that the name=value pairs (e.g. truststore.jks=) will be used like filename=base64decoded-Content.
My generated base64 was multiline and I was getting the same error.
Trick is, use -w0 argument in base64 so that the whole encode is in 1 line!
base64 -w0 ssl_keystore.jks > test
Above will create a file named test and will contain the base64 in one line, copy paste like this in a secret:
apiVersion: v1
kind: Secret
metadata:
name: staging-ssl-keystore-jks
namespace: staging-space
type: Opaque
data:
keystore.jsk: your-base64-in-one-line
Building upon what both #Frischling and #Jan-Thomä said, and in agreement with Frischling as his way was easier and took care of both the trust cert keystores, after adding the keystores as a secret, under Applications->Deployments->[your deployments name] Click the environment link and add the following system properties:
Name: JAVA_OPTS_APPEND and
Value -Djavax.net.ssl.keyStorePassword=changeme -Djavax.net.ssl.keyStore=/mnt/keystores/your_cert_key_store.jks -Djavax.net.ssl.trustStorePassword=changeme -Djavax.net.ssl.trustStore=/mnt/keystores/your_ca_key_store.jks
This effectively will as indicated, append the keystore file paths, passwords to the java options used by the application, for example JBoss/WildFly or Tomcat.