Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 3. Creating build inputs
Use the following sections for an overview of build inputs, instructions on how to use inputs to provide source content for builds to operate on, and how to use build environments and create secrets.
3.1. Build inputs Link kopierenLink in die Zwischenablage kopiert!
A build input provides source content for builds to operate on. You can use the following build inputs to provide sources in OpenShift Container Platform, listed in order of precedence:
- Inline Dockerfile definitions
- Content extracted from existing images
- Git repositories
- Binary (Local) inputs
- Input secrets
- External artifacts
You can combine multiple inputs in a single build. However, as the inline Dockerfile takes precedence, it can overwrite any other file named Dockerfile provided by another input. Binary (local) input and Git repositories are mutually exclusive inputs.
You can use input secrets when you do not want certain resources or credentials used during a build to be available in the final application image produced by the build, or want to consume a value that is defined in a secret resource. External artifacts can be used to pull in additional files that are not available as one of the other build input types.
When you run a build:
- A working directory is constructed and all input content is placed in the working directory. For example, the input Git repository is cloned into the working directory, and files specified from input images are copied into the working directory using the target path.
-
The build process changes directories into the , if one is defined.
contextDir - The inline Dockerfile, if any, is written to the current directory.
-
The content from the current directory is provided to the build process for reference by the Dockerfile, custom builder logic, or script. This means any input content that resides outside the
assembleis ignored by the build.contextDir
The following example of a source definition includes multiple input types and an explanation of how they are combined. For more details on how each input type is defined, see the specific sections for each input type.
source:
git:
uri: https://github.com/openshift/ruby-hello-world.git
ref: "master"
images:
- from:
kind: ImageStreamTag
name: myinputimage:latest
namespace: mynamespace
paths:
- destinationDir: app/dir/injected/dir
sourcePath: /usr/lib/somefile.jar
contextDir: "app/dir"
dockerfile: "FROM centos:7\nRUN yum install -y httpd"
- 1
- The repository to be cloned into the working directory for the build.
- 2
/usr/lib/somefile.jarfrommyinputimageis stored in<workingdir>/app/dir/injected/dir.- 3
- The working directory for the build becomes
<original_workingdir>/app/dir. - 4
- A Dockerfile with this content is created in
<original_workingdir>/app/dir, overwriting any existing file with that name.
3.2. Dockerfile source Link kopierenLink in die Zwischenablage kopiert!
When you supply a
dockerfile
dockerfile
The source definition is part of the
spec
BuildConfig
source:
dockerfile: "FROM centos:7\nRUN yum install -y httpd"
- 1
- The
dockerfilefield contains an inline Dockerfile that is built.
3.3. Image source Link kopierenLink in die Zwischenablage kopiert!
You can add additional files to the build process with images. Input images are referenced in the same way the
From
To
The source path can be any absolute path within the image specified. The destination must be a relative directory path. At build time, the image is loaded and the indicated files and directories are copied into the context directory of the build process. This is the same directory into which the source repository content is cloned. If the source path ends in
/.
Image inputs are specified in the
source
BuildConfig
source:
git:
uri: https://github.com/openshift/ruby-hello-world.git
ref: "master"
images:
- from:
kind: ImageStreamTag
name: myinputimage:latest
namespace: mynamespace
paths:
- destinationDir: injected/dir
sourcePath: /usr/lib/somefile.jar
- from:
kind: ImageStreamTag
name: myotherinputimage:latest
namespace: myothernamespace
pullSecret: mysecret
paths:
- destinationDir: injected/dir
sourcePath: /usr/lib/somefile.jar
- 1
- An array of one or more input images and files.
- 2
- A reference to the image containing the files to be copied.
- 3
- An array of source/destination paths.
- 4
- The directory relative to the build root where the build process can access the file.
- 5
- The location of the file to be copied out of the referenced image.
- 6
- An optional secret provided if credentials are needed to access the input image.Note
If your cluster uses an
object to configure repository mirroring, you can use only global pull secrets for mirrored registries. You cannot add a pull secret to a project.ImageContentSourcePolicy
Images that require pull secrets
When using an input image that requires a pull secret, you can link the pull secret to the service account used by the build. By default, builds use the
builder
$ oc secrets link builder dockerhub
This feature is not supported for builds using the custom strategy.
Images on mirrored registries that require pull secrets
When using an input image from a mirrored registry, if you get a
build error: failed to pull image
- Create an input secret that contains the authentication credentials for the builder image’s repository and all known mirrors. In this case, create a pull secret for credentials to the image registry and its mirrors.
-
Use the input secret as the pull secret on the object.
BuildConfig
3.4. Git source Link kopierenLink in die Zwischenablage kopiert!
When specified, source code is fetched from the supplied location.
If you supply an inline Dockerfile, it overwrites the Dockerfile in the
contextDir
The source definition is part of the
spec
BuildConfig
source:
git:
uri: "https://github.com/openshift/ruby-hello-world"
ref: "master"
contextDir: "app/dir"
dockerfile: "FROM openshift/ruby-22-centos7\nUSER example"
- 1
- The
gitfield contains the Uniform Resource Identifier (URI) to the remote Git repository of the source code. You must specify the value of thereffield to check out a specific Git reference. A validrefcan be a SHA1 tag or a branch name. The default value of thereffield ismaster. - 2
- The
contextDirfield allows you to override the default location inside the source code repository where the build looks for the application source code. If your application exists inside a sub-directory, you can override the default location (the root folder) using this field. - 3
- If the optional
dockerfilefield is provided, it should be a string containing a Dockerfile that overwrites any Dockerfile that may exist in the source repository.
If the
ref
git fetch
FETCH_HEAD
When no
ref
--depth=1
master
git clone
ref
main
Git clone operations that go through a proxy that is performing man in the middle (MITM) TLS hijacking or reencrypting of the proxied connection do not work.
3.4.1. Using a proxy Link kopierenLink in die Zwischenablage kopiert!
If your Git repository can only be accessed using a proxy, you can define the proxy to use in the
source
NoProxy
Your source URI must use the HTTP or HTTPS protocol for this to work.
source:
git:
uri: "https://github.com/openshift/ruby-hello-world"
ref: "master"
httpProxy: http://proxy.example.com
httpsProxy: https://proxy.example.com
noProxy: somedomain.com, otherdomain.com
For Pipeline strategy builds, given the current restrictions with the Git plugin for Jenkins, any Git operations through the Git plugin do not leverage the HTTP or HTTPS proxy defined in the
BuildConfig
3.4.2. Source Clone Secrets Link kopierenLink in die Zwischenablage kopiert!
Builder pods require access to any Git repositories defined as source for a build. Source clone secrets are used to provide the builder pod with access it would not normally have access to, such as private repositories or repositories with self-signed or untrusted SSL certificates.
The following source clone secret configurations are supported:
- .gitconfig File
- Basic Authentication
- SSH Key Authentication
- Trusted Certificate Authorities
You can also use combinations of these configurations to meet your specific needs.
3.4.2.1. Automatically adding a source clone secret to a build configuration Link kopierenLink in die Zwischenablage kopiert!
When a
BuildConfig
To use this functionality, a secret containing the Git repository credentials must exist in the namespace in which the
BuildConfig
build.openshift.io/source-secret-match-uri-
BuildConfig
BuildConfig
Prerequisites
A URI pattern must consist of:
-
A valid scheme: ,
*://,git://,http://orhttps://ssh:// -
A host: *` or a valid hostname or IP address optionally preceded by
*. -
A path: or
/*followed by any characters optionally including/characters*
In all of the above, a
*
URI patterns must match Git source URIs which are conformant to RFC3986. Do not include a username (or password) component in a URI pattern.
For example, if you use
ssh://git@bitbucket.atlassian.com:7999/ATLASSIAN jira.git
ssh://bitbucket.atlassian.com:7999/*
ssh://git@bitbucket.atlassian.com:7999/*
$ oc annotate secret mysecret \
'build.openshift.io/source-secret-match-uri-1=ssh://bitbucket.atlassian.com:7999/*'
Procedure
If multiple secrets match the Git URI of a particular
BuildConfig
The following fragment shows two partial source clone secrets, the first matching any server in the domain
mycorp.com
mydev1.mycorp.com
mydev2.mycorp.com
kind: Secret
apiVersion: v1
metadata:
name: matches-all-corporate-servers-https-only
annotations:
build.openshift.io/source-secret-match-uri-1: https://*.mycorp.com/*
data:
...
---
kind: Secret
apiVersion: v1
metadata:
name: override-for-my-dev-servers-https-only
annotations:
build.openshift.io/source-secret-match-uri-1: https://mydev1.mycorp.com/*
build.openshift.io/source-secret-match-uri-2: https://mydev2.mycorp.com/*
data:
...
Add a
annotation to a pre-existing secret using:build.openshift.io/source-secret-match-uri-$ oc annotate secret mysecret \ 'build.openshift.io/source-secret-match-uri-1=https://*.mycorp.com/*'
3.4.2.2. Manually adding a source clone secret Link kopierenLink in die Zwischenablage kopiert!
Source clone secrets can be added manually to a build configuration by adding a
sourceSecret
source
BuildConfig
basicsecret
apiVersion: "build.openshift.io/v1"
kind: "BuildConfig"
metadata:
name: "sample-build"
spec:
output:
to:
kind: "ImageStreamTag"
name: "sample-image:latest"
source:
git:
uri: "https://github.com/user/app.git"
sourceSecret:
name: "basicsecret"
strategy:
sourceStrategy:
from:
kind: "ImageStreamTag"
name: "python-33-centos7:latest"
Procedure
You can also use the
oc set build-secret
To set the source clone secret on an existing build configuration, enter the following command:
$ oc set build-secret --source bc/sample-build basicsecret
3.4.2.3. Creating a secret from a .gitconfig file Link kopierenLink in die Zwischenablage kopiert!
If the cloning of your application is dependent on a
.gitconfig
BuildConfig
Procedure
-
To create a secret from a file:
.gitconfig
$ oc create secret generic <secret_name> --from-file=<path/to/.gitconfig>
SSL verification can be turned off if
sslVerify=false
http
.gitconfig
[http]
sslVerify=false
3.4.2.4. Creating a secret from a .gitconfig file for secured Git Link kopierenLink in die Zwischenablage kopiert!
If your Git server is secured with two-way SSL and user name with password, you must add the certificate files to your source build and add references to the certificate files in the
.gitconfig
Prerequisites
- You must have Git credentials.
Procedure
Add the certificate files to your source build and add references to the certificate files in the
.gitconfig
-
Add the ,
client.crt, andcacert.crtfiles to theclient.keyfolder in the application source code./var/run/secrets/openshift.io/source/ In the
file for the server, add the.gitconfigsection shown in the following example:[http]# cat .gitconfigExample output
[user] name = <name> email = <email> [http] sslVerify = false sslCert = /var/run/secrets/openshift.io/source/client.crt sslKey = /var/run/secrets/openshift.io/source/client.key sslCaInfo = /var/run/secrets/openshift.io/source/cacert.crtCreate the secret:
$ oc create secret generic <secret_name> \ --from-literal=username=<user_name> \1 --from-literal=password=<password> \2 --from-file=.gitconfig=.gitconfig \ --from-file=client.crt=/var/run/secrets/openshift.io/source/client.crt \ --from-file=cacert.crt=/var/run/secrets/openshift.io/source/cacert.crt \ --from-file=client.key=/var/run/secrets/openshift.io/source/client.key
To avoid having to enter your password again, be sure to specify the source-to-image (S2I) image in your builds. However, if you cannot clone the repository, you must still specify your user name and password to promote the build.
3.4.2.5. Creating a secret from source code basic authentication Link kopierenLink in die Zwischenablage kopiert!
Basic authentication requires either a combination of
--username
--password
Prerequisites
- User name and password to access the private repository.
Procedure
Create the secret first before using the
and--usernameto access the private repository:--password$ oc create secret generic <secret_name> \ --from-literal=username=<user_name> \ --from-literal=password=<password> \ --type=kubernetes.io/basic-authCreate a basic authentication secret with a token:
$ oc create secret generic <secret_name> \ --from-literal=password=<token> \ --type=kubernetes.io/basic-auth
3.4.2.6. Creating a secret from source code SSH key authentication Link kopierenLink in die Zwischenablage kopiert!
SSH key based authentication requires a private SSH key.
The repository keys are usually located in the
$HOME/.ssh/
id_dsa.pub
id_ecdsa.pub
id_ed25519.pub
id_rsa.pub
Procedure
Generate SSH key credentials:
$ ssh-keygen -t ed25519 -C "your_email@example.com"NoteCreating a passphrase for the SSH key prevents OpenShift Container Platform from building. When prompted for a passphrase, leave it blank.
Two files are created: the public key and a corresponding private key (one of
,id_dsa,id_ecdsa, orid_ed25519). With both of these in place, consult your source control management (SCM) system’s manual on how to upload the public key. The private key is used to access your private repository.id_rsaBefore using the SSH key to access the private repository, create the secret:
$ oc create secret generic <secret_name> \ --from-file=ssh-privatekey=<path/to/ssh/private/key> \ --from-file=<path/to/known_hosts> \1 --type=kubernetes.io/ssh-auth- 1
- Optional: Adding this field enables strict server host key check.
WarningSkipping the
file while creating the secret makes the build vulnerable to a potential man-in-the-middle (MITM) attack.known_hostsNoteEnsure that the
file includes an entry for the host of your source code.known_hosts
3.4.2.7. Creating a secret from source code trusted certificate authorities Link kopierenLink in die Zwischenablage kopiert!
The set of Transport Layer Security (TLS) certificate authorities (CA) that are trusted during a Git clone operation are built into the OpenShift Container Platform infrastructure images. If your Git server uses a self-signed certificate or one signed by an authority not trusted by the image, you can create a secret that contains the certificate or disable TLS verification.
If you create a secret for the CA certificate, OpenShift Container Platform uses it to access your Git server during the Git clone operation. Using this method is significantly more secure than disabling Git SSL verification, which accepts any TLS certificate that is presented.
Procedure
Create a secret with a CA certificate file.
If your CA uses Intermediate Certificate Authorities, combine the certificates for all CAs in a
file. Enter the following command:ca.crt$ cat intermediateCA.crt intermediateCA.crt rootCA.crt > ca.crtCreate the secret:
$ oc create secret generic mycert --from-file=ca.crt=</path/to/file>1 - 1
- You must use the key name
ca.crt.
3.4.2.8. Source secret combinations Link kopierenLink in die Zwischenablage kopiert!
You can combine the different methods for creating source clone secrets for your specific needs.
3.4.2.8.1. Creating a SSH-based authentication secret with a .gitconfig file Link kopierenLink in die Zwischenablage kopiert!
You can combine the different methods for creating source clone secrets for your specific needs, such as a SSH-based authentication secret with a
.gitconfig
Prerequisites
- SSH authentication
- .gitconfig file
Procedure
To create a SSH-based authentication secret with a
file, run:.gitconfig$ oc create secret generic <secret_name> \ --from-file=ssh-privatekey=<path/to/ssh/private/key> \ --from-file=<path/to/.gitconfig> \ --type=kubernetes.io/ssh-auth
3.4.2.8.2. Creating a secret that combines a .gitconfig file and CA certificate Link kopierenLink in die Zwischenablage kopiert!
You can combine the different methods for creating source clone secrets for your specific needs, such as a secret that combines a
.gitconfig
Prerequisites
- .gitconfig file
- CA certificate
Procedure
To create a secret that combines a
file and CA certificate, run:.gitconfig$ oc create secret generic <secret_name> \ --from-file=ca.crt=<path/to/certificate> \ --from-file=<path/to/.gitconfig>
3.4.2.8.3. Creating a basic authentication secret with a CA certificate Link kopierenLink in die Zwischenablage kopiert!
You can combine the different methods for creating source clone secrets for your specific needs, such as a secret that combines a basic authentication and certificate authority (CA) certificate.
Prerequisites
- Basic authentication credentials
- CA certificate
Procedure
Create a basic authentication secret with a CA certificate, run:
$ oc create secret generic <secret_name> \ --from-literal=username=<user_name> \ --from-literal=password=<password> \ --from-file=ca-cert=</path/to/file> \ --type=kubernetes.io/basic-auth
3.4.2.8.4. Creating a basic authentication secret with a .gitconfig file Link kopierenLink in die Zwischenablage kopiert!
You can combine the different methods for creating source clone secrets for your specific needs, such as a secret that combines a basic authentication and
.gitconfig
Prerequisites
- Basic authentication credentials
-
file
.gitconfig
Procedure
To create a basic authentication secret with a
file, run:.gitconfig$ oc create secret generic <secret_name> \ --from-literal=username=<user_name> \ --from-literal=password=<password> \ --from-file=</path/to/.gitconfig> \ --type=kubernetes.io/basic-auth
3.4.2.8.5. Creating a basic authentication secret with a .gitconfig file and CA certificate Link kopierenLink in die Zwischenablage kopiert!
You can combine the different methods for creating source clone secrets for your specific needs, such as a secret that combines a basic authentication,
.gitconfig
Prerequisites
- Basic authentication credentials
-
file
.gitconfig - CA certificate
Procedure
To create a basic authentication secret with a
file and CA certificate, run:.gitconfig$ oc create secret generic <secret_name> \ --from-literal=username=<user_name> \ --from-literal=password=<password> \ --from-file=</path/to/.gitconfig> \ --from-file=ca-cert=</path/to/file> \ --type=kubernetes.io/basic-auth
3.5. Binary (local) source Link kopierenLink in die Zwischenablage kopiert!
Streaming content from a local file system to the builder is called a
Binary
BuildConfig.spec.source.type
Binary
This source type is unique in that it is leveraged solely based on your use of the
oc start-build
Binary type builds require content to be streamed from the local file system, so automatically triggering a binary type build, like an image change trigger, is not possible. This is because the binary files cannot be provided. Similarly, you cannot launch binary type builds from the web console.
To utilize binary builds, invoke
oc start-build
-
: The contents of the file you specify are sent as a binary stream to the builder. You can also specify a URL to a file. Then, the builder stores the data in a file with the same name at the top of the build context.
--from-file -
and
--from-dir: The contents are archived and sent as a binary stream to the builder. Then, the builder extracts the contents of the archive within the build context directory. With--from-repo, you can also specify a URL to an archive, which is extracted.--from-dir -
: The archive you specify is sent to the builder, where it is extracted within the build context directory. This option behaves the same as
--from-archive; an archive is created on your host first, whenever the argument to these options is a directory.--from-dir
In each of the previously listed cases:
-
If your already has a
BuildConfigsource type defined, it is effectively ignored and replaced by what the client sends.Binary -
If your has a
BuildConfigsource type defined, it is dynamically disabled, sinceGitandBinaryare mutually exclusive, and the data in the binary stream provided to the builder takes precedence.Git
Instead of a file name, you can pass a URL with HTTP or HTTPS schema to
--from-file
--from-archive
--from-file
Content-Disposition
When using
oc new-build --binary=true
BuildConfig
Binary
BuildConfig
oc start-build
--from
The Dockerfile and
contextDir
Dockerfile can be used with any binary build source. If Dockerfile is used and the binary stream is an archive, its contents serve as a replacement Dockerfile to any Dockerfile in the archive. If Dockerfile is used with the
--from-file
In the case of the binary stream encapsulating extracted archive content, the value of the
contextDir
3.6. Input secrets and config maps Link kopierenLink in die Zwischenablage kopiert!
To prevent the contents of input secrets and config maps from appearing in build output container images, use build volumes in your Docker build and source-to-image build strategies.
In some scenarios, build operations require credentials or other configuration data to access dependent resources, but it is undesirable for that information to be placed in source control. You can define input secrets and input config maps for this purpose.
For example, when building a Java application with Maven, you can set up a private mirror of Maven Central or JCenter that is accessed by private keys. To download libraries from that private mirror, you have to supply the following:
-
A file configured with the mirror’s URL and connection settings.
settings.xml -
A private key referenced in the settings file, such as .
~/.ssh/id_rsa
For security reasons, you do not want to expose your credentials in the application image.
This example describes a Java application, but you can use the same approach for adding SSL certificates into the
/etc/ssl/certs
3.6.1. What is a secret? Link kopierenLink in die Zwischenablage kopiert!
The
Secret
dockercfg
YAML Secret Object Definition
apiVersion: v1
kind: Secret
metadata:
name: test-secret
namespace: my-namespace
type: Opaque
data:
username: <username>
password: <password>
stringData:
hostname: myapp.mydomain.com
- 1
- Indicates the structure of the secret’s key names and values.
- 2
- The allowable format for the keys in the
datafield must meet the guidelines in theDNS_SUBDOMAINvalue in the Kubernetes identifiers glossary. - 3
- The value associated with keys in the
datamap must be base64 encoded. - 4
- Entries in the
stringDatamap are converted to base64 and the entry are then moved to thedatamap automatically. This field is write-only. The value is only be returned by thedatafield. - 5
- The value associated with keys in the
stringDatamap is made up of plain text strings.
3.6.1.1. Properties of secrets Link kopierenLink in die Zwischenablage kopiert!
Key properties include:
- Secret data can be referenced independently from its definition.
- Secret data volumes are backed by temporary file-storage facilities (tmpfs) and never come to rest on a node.
- Secret data can be shared within a namespace.
3.6.1.2. Types of Secrets Link kopierenLink in die Zwischenablage kopiert!
The value in the
type
opaque
Specify one of the following types to trigger minimal server-side validation to ensure the presence of specific key names in the secret data:
-
. Uses a service account token.
kubernetes.io/service-account-token -
. Uses the
kubernetes.io/dockercfgfile for required Docker credentials..dockercfg -
. Uses the
kubernetes.io/dockerconfigjsonfile for required Docker credentials..docker/config.json -
. Use with basic authentication.
kubernetes.io/basic-auth -
. Use with SSH key authentication.
kubernetes.io/ssh-auth -
. Use with TLS certificate authorities.
kubernetes.io/tls
Specify
type= Opaque
opaque
key:value
You can specify other arbitrary types, such as
example.com/my-secret-type
3.6.1.3. Updates to secrets Link kopierenLink in die Zwischenablage kopiert!
When you modify the value of a secret, the value used by an already running pod does not dynamically change. To change a secret, you must delete the original pod and create a new pod, in some cases with an identical
PodSpec
Updating a secret follows the same workflow as deploying a new container image. You can use the
kubectl rolling-update
The
resourceVersion
Currently, it is not possible to check the resource version of a secret object that was used when a pod was created. It is planned that pods report this information, so that a controller could restart ones using an old
resourceVersion
3.6.2. Creating secrets Link kopierenLink in die Zwischenablage kopiert!
You must create a secret before creating the pods that depend on that secret.
When creating secrets:
- Create a secret object with secret data.
- Update the pod service account to allow the reference to the secret.
-
Create a pod, which consumes the secret as an environment variable or as a file using a volume.
secret
Procedure
Use the create command to create a secret object from a JSON or YAML file:
$ oc create -f <filename>For example, you can create a secret from your local
file:.docker/config.json$ oc create secret generic dockerhub \ --from-file=.dockerconfigjson=<path/to/.docker/config.json> \ --type=kubernetes.io/dockerconfigjsonThis command generates a JSON specification of the secret named
and creates the object.dockerhubYAML Opaque Secret Object Definition
apiVersion: v1 kind: Secret metadata: name: mysecret type: Opaque1 data: username: <username> password: <password>- 1
- Specifies an opaque secret.
Docker Configuration JSON File Secret Object Definition
apiVersion: v1 kind: Secret metadata: name: aregistrykey namespace: myapps type: kubernetes.io/dockerconfigjson1 data: .dockerconfigjson:bm5ubm5ubm5ubm5ubm5ubm5ubm5ubmdnZ2dnZ2dnZ2dnZ2dnZ2dnZ2cgYXV0aCBrZXlzCg==2
3.6.3. Using secrets Link kopierenLink in die Zwischenablage kopiert!
After creating secrets, you can create a pod to reference your secret, get logs, and delete the pod.
Procedure
Create the pod to reference your secret:
$ oc create -f <your_yaml_file>.yamlGet the logs:
$ oc logs secret-example-podDelete the pod:
$ oc delete pod secret-example-pod
3.6.4. Adding input secrets and config maps Link kopierenLink in die Zwischenablage kopiert!
To provide credentials and other configuration data to a build without placing them in source control, you can define input secrets and input config maps.
In some scenarios, build operations require credentials or other configuration data to access dependent resources. To make that information available without placing it in source control, you can define input secrets and input config maps.
Procedure
To add an input secret, config maps, or both to an existing
BuildConfig
Create the
object, if it does not exist:ConfigMap$ oc create configmap settings-mvn \ --from-file=settings.xml=<path/to/settings.xml>This creates a new config map named
, which contains the plain text content of thesettings-mvnfile.settings.xmlTipYou can alternatively apply the following YAML to create the config map:
apiVersion: core/v1 kind: ConfigMap metadata: name: settings-mvn data: settings.xml: | <settings> … # Insert maven settings here </settings>Create the
object, if it does not exist:Secret$ oc create secret generic secret-mvn \ --from-file=ssh-privatekey=<path/to/.ssh/id_rsa> --type=kubernetes.io/ssh-authThis creates a new secret named
, which contains the base64 encoded content of thesecret-mvnprivate key.id_rsaTipYou can alternatively apply the following YAML to create the input secret:
apiVersion: core/v1 kind: Secret metadata: name: secret-mvn type: kubernetes.io/ssh-auth data: ssh-privatekey: | # Insert ssh private key, base64 encodedAdd the config map and secret to the
section in the existingsourceobject:BuildConfigsource: git: uri: https://github.com/wildfly/quickstart.git contextDir: helloworld configMaps: - configMap: name: settings-mvn secrets: - secret: name: secret-mvn
To include the secret and config map in a new
BuildConfig
$ oc new-build \
openshift/wildfly-101-centos7~https://github.com/wildfly/quickstart.git \
--context-dir helloworld --build-secret “secret-mvn” \
--build-config-map "settings-mvn"
During the build, the
settings.xml
id_rsa
WORKDIR
Dockerfile
destinationDir
source:
git:
uri: https://github.com/wildfly/quickstart.git
contextDir: helloworld
configMaps:
- configMap:
name: settings-mvn
destinationDir: ".m2"
secrets:
- secret:
name: secret-mvn
destinationDir: ".ssh"
You can also specify the destination directory when creating a new
BuildConfig
$ oc new-build \
openshift/wildfly-101-centos7~https://github.com/wildfly/quickstart.git \
--context-dir helloworld --build-secret “secret-mvn:.ssh” \
--build-config-map "settings-mvn:.m2"
In both cases, the
settings.xml
./.m2
id_rsa
./.ssh
3.6.5. Source-to-image strategy Link kopierenLink in die Zwischenablage kopiert!
When using a
Source
destinationDir
destinationDir
The same rule is used when a
destinationDir
destinationDir
destinationDir
Input secrets are added as world-writable, have
0666
assemble
Input config maps are not truncated after the
assemble
3.6.6. Docker strategy Link kopierenLink in die Zwischenablage kopiert!
When using a docker strategy, you can add all defined input secrets into your container image using the ADD and COPY instructions in your Dockerfile.
If you do not specify the
destinationDir
destinationDir
Example of a Dockerfile referencing secret and config map data
FROM centos/ruby-22-centos7
USER root
COPY ./secret-dir /secrets
COPY ./config /
# Create a shell script that will output secrets and ConfigMaps when the image is run
RUN echo '#!/bin/sh' > /input_report.sh
RUN echo '(test -f /secrets/secret1 && echo -n "secret1=" && cat /secrets/secret1)' >> /input_report.sh
RUN echo '(test -f /config && echo -n "relative-configMap=" && cat /config)' >> /input_report.sh
RUN chmod 755 /input_report.sh
CMD ["/bin/sh", "-c", "/input_report.sh"]
Users normally remove their input secrets from the final application image so that the secrets are not present in the container running from that image. However, the secrets still exist in the image itself in the layer where they were added. This removal is part of the Dockerfile itself.
To prevent the contents of input secrets and config maps from appearing in the build output container images and avoid this removal process altogether, use build volumes in your Docker build strategy instead.
3.6.7. Custom strategy Link kopierenLink in die Zwischenablage kopiert!
When using a Custom strategy, all the defined input secrets and config maps are available in the builder container in the
/var/run/secrets/openshift.io/build
There is no technical difference between existing strategy secrets and the input secrets. However, your builder image can distinguish between them and use them differently, based on your build use case.
The input secrets are always mounted into the
/var/run/secrets/openshift.io/build
$BUILD
If a pull secret for the registry exists in both the namespace and the node, builds default to using the pull secret in the namespace.
3.7. External artifacts Link kopierenLink in die Zwischenablage kopiert!
It is not recommended to store binary files in a source repository. Therefore, you must define a build which pulls additional files, such as Java
.jar
For a Source build strategy, you must put appropriate shell commands into the
assemble
.s2i/bin/assemble File
#!/bin/sh
APP_VERSION=1.0
wget http://repository.example.com/app/app-$APP_VERSION.jar -O app.jar
.s2i/bin/run File
#!/bin/sh
exec java -jar app.jar
For a Docker build strategy, you must modify the Dockerfile and invoke shell commands with the RUN instruction:
Excerpt of Dockerfile
FROM jboss/base-jdk:8
ENV APP_VERSION 1.0
RUN wget http://repository.example.com/app/app-$APP_VERSION.jar -O app.jar
EXPOSE 8080
CMD [ "java", "-jar", "app.jar" ]
In practice, you may want to use an environment variable for the file location so that the specific file to be downloaded can be customized using an environment variable defined on the
BuildConfig
assemble
You can choose between different methods of defining environment variables:
-
Using the file] (only for a Source build strategy)
.s2i/environment -
Setting in
BuildConfig -
Providing explicitly using (only for builds that are triggered manually)
oc start-build --env
3.8. Using docker credentials for private registries Link kopierenLink in die Zwischenablage kopiert!
You can supply builds with a .
docker/config.json
You can supply credentials for multiple repositories within the same registry, each with credentials specific to that registry path.
For the OpenShift Container Platform container image registry, this is not required because secrets are generated automatically for you by OpenShift Container Platform.
The
.docker/config.json
auths:
index.docker.io/v1/:
auth: "YWRfbGzhcGU6R2labnRib21ifTE="
email: "user@example.com"
docker.io/my-namespace/my-user/my-image:
auth: "GzhYWRGU6R2fbclabnRgbkSp=""
email: "user@example.com"
docker.io/my-namespace:
auth: "GzhYWRGU6R2deesfrRgbkSp=""
email: "user@example.com"
You can define multiple container image registries or define multiple repositories in the same registry. Alternatively, you can also add authentication entries to this file by running the
docker login
Kubernetes provides
Secret
Prerequisites
-
You must have a file.
.docker/config.json
Procedure
Create the secret from your local
file:.docker/config.json$ oc create secret generic dockerhub \ --from-file=.dockerconfigjson=<path/to/.docker/config.json> \ --type=kubernetes.io/dockerconfigjsonThis generates a JSON specification of the secret named
and creates the object.dockerhubAdd a
field into thepushSecretsection of theoutputand set it to the name of theBuildConfigthat you created, which in the previous example issecret:dockerhubspec: output: to: kind: "DockerImage" name: "private.registry.com/org/private-image:latest" pushSecret: name: "dockerhub"You can use the
command to set the push secret on the build configuration:oc set build-secret$ oc set build-secret --push bc/sample-build dockerhubYou can also link the push secret to the service account used by the build instead of specifying the
field. By default, builds use thepushSecretservice account. The push secret is automatically added to the build if the secret contains a credential that matches the repository hosting the build’s output image.builder$ oc secrets link builder dockerhubPull the builder container image from a private container image registry by specifying the
field, which is part of the build strategy definition:pullSecretstrategy: sourceStrategy: from: kind: "DockerImage" name: "docker.io/user/private_repository" pullSecret: name: "dockerhub"You can use the
command to set the pull secret on the build configuration:oc set build-secret$ oc set build-secret --pull bc/sample-build dockerhubNoteThis example uses
in a Source build, but it is also applicable in Docker and Custom builds.pullSecretYou can also link the pull secret to the service account used by the build instead of specifying the
field. By default, builds use thepullSecretservice account. The pull secret is automatically added to the build if the secret contains a credential that matches the repository hosting the build’s input image. To link the pull secret to the service account used by the build instead of specifying thebuilderfield, run:pullSecret$ oc secrets link builder dockerhubNoteYou must specify a
image in thefromspec to take advantage of this feature. Docker strategy builds generated byBuildConfigoroc new-buildmay not do this in some situations.oc new-app
3.9. Build environments Link kopierenLink in die Zwischenablage kopiert!
As with pod environment variables, build environment variables can be defined in terms of references to other resources or variables using the Downward API. There are some exceptions, which are noted.
You can also manage environment variables defined in the
BuildConfig
oc set env
Referencing container resources using
valueFrom
3.9.1. Using build fields as environment variables Link kopierenLink in die Zwischenablage kopiert!
You can inject information about the build object by setting the
fieldPath
JsonPath
Jenkins Pipeline strategy does not support
valueFrom
Procedure
Set the
environment variable source to thefieldPathof the field from which you are interested in obtaining the value:JsonPathenv: - name: FIELDREF_ENV valueFrom: fieldRef: fieldPath: metadata.name
3.9.2. Using secrets as environment variables Link kopierenLink in die Zwischenablage kopiert!
You can make key values from secrets available as environment variables using the
valueFrom
This method shows the secrets as plain text in the output of the build pod console. To avoid this, use input secrets and config maps instead.
Procedure
To use a secret as an environment variable, set the
syntax:valueFromapiVersion: build.openshift.io/v1 kind: BuildConfig metadata: name: secret-example-bc spec: strategy: sourceStrategy: env: - name: MYVAL valueFrom: secretKeyRef: key: myval name: mysecret
3.10. Service serving certificate secrets Link kopierenLink in die Zwischenablage kopiert!
Service serving certificate secrets are intended to support complex middleware applications that need out-of-the-box certificates. It has the same settings as the server certificates generated by the administrator tooling for nodes and masters.
Procedure
To secure communication to your service, have the cluster generate a signed serving certificate/key pair into a secret in your namespace.
Set the
annotation on your service with the value set to the name you want to use for your secret.service.beta.openshift.io/serving-cert-secret-nameThen, your
can mount that secret. When it is available, your pod runs. The certificate is good for the internal service DNS name,PodSpec.<service.name>.<service.namespace>.svcThe certificate and key are in PEM format, stored in
andtls.crtrespectively. The certificate/key pair is automatically replaced when it gets close to expiration. View the expiration date in thetls.keyannotation on the secret, which is in RFC3339 format.service.beta.openshift.io/expiry
In most cases, the service DNS name
<service.name>.<service.namespace>.svc
<service.name>.<service.namespace>.svc
Other pods can trust cluster-created certificates, which are only signed for internal DNS names, by using the certificate authority (CA) bundle in the
/var/run/secrets/kubernetes.io/serviceaccount/service-ca.crt
The signature algorithm for this feature is
x509.SHA256WithRSA
3.11. Secrets restrictions Link kopierenLink in die Zwischenablage kopiert!
To use a secret, a pod needs to reference the secret. A secret can be used with a pod in three ways:
- To populate environment variables for containers.
- As files in a volume mounted on one or more of its containers.
- By kubelet when pulling images for the pod.
Volume type secrets write data into the container as a file using the volume mechanism.
imagePullSecrets
When a template contains a secret definition, the only way for the template to use the provided secret is to ensure that the secret volume sources are validated and that the specified object reference actually points to an object of type
Secret
Secret API objects reside in a namespace. They can only be referenced by pods in that same namespace.
Individual secrets are limited to 1MB in size. This is to discourage the creation of large secrets that would exhaust apiserver and kubelet memory. However, creation of a number of smaller secrets could also exhaust memory.