mirror of
https://github.com/openshift/openshift-docs.git
synced 2026-02-05 12:46:18 +01:00
157 lines
3.2 KiB
Plaintext
157 lines
3.2 KiB
Plaintext
// Module included in the following assemblies:
|
|
//
|
|
// * nodes/nodes-containers-init.adoc
|
|
|
|
:_mod-docs-content-type: PROCEDURE
|
|
[id="nodes-containers-init-creating_{context}"]
|
|
= Creating Init Containers
|
|
|
|
The following example outlines a simple pod which has two Init Containers. The first waits for `myservice` and the second waits for `mydb`. After both containers complete, the pod begins.
|
|
|
|
.Procedure
|
|
|
|
. Create the pod for the Init Container:
|
|
|
|
.. Create a YAML file similar to the following:
|
|
+
|
|
[source,yaml]
|
|
----
|
|
apiVersion: v1
|
|
kind: Pod
|
|
metadata:
|
|
name: myapp-pod
|
|
labels:
|
|
app: myapp
|
|
spec:
|
|
securityContext:
|
|
runAsNonRoot: true
|
|
seccompProfile:
|
|
type: RuntimeDefault
|
|
containers:
|
|
- name: myapp-container
|
|
image: registry.access.redhat.com/ubi9/ubi:latest
|
|
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
|
|
securityContext:
|
|
allowPrivilegeEscalation: false
|
|
capabilities:
|
|
drop: [ALL]
|
|
initContainers:
|
|
- name: init-myservice
|
|
image: registry.access.redhat.com/ubi9/ubi:latest
|
|
command: ['sh', '-c', 'until getent hosts myservice; do echo waiting for myservice; sleep 2; done;']
|
|
securityContext:
|
|
allowPrivilegeEscalation: false
|
|
capabilities:
|
|
drop: [ALL]
|
|
- name: init-mydb
|
|
image: registry.access.redhat.com/ubi9/ubi:latest
|
|
command: ['sh', '-c', 'until getent hosts mydb; do echo waiting for mydb; sleep 2; done;']
|
|
securityContext:
|
|
allowPrivilegeEscalation: false
|
|
capabilities:
|
|
drop: [ALL]
|
|
----
|
|
|
|
.. Create the pod:
|
|
+
|
|
[source,terminal]
|
|
----
|
|
$ oc create -f myapp.yaml
|
|
----
|
|
|
|
.. View the status of the pod:
|
|
+
|
|
[source,terminal]
|
|
----
|
|
$ oc get pods
|
|
----
|
|
+
|
|
.Example output
|
|
[source,terminal]
|
|
----
|
|
NAME READY STATUS RESTARTS AGE
|
|
myapp-pod 0/1 Init:0/2 0 5s
|
|
----
|
|
+
|
|
The pod status, `Init:0/2`, indicates it is waiting for the two services.
|
|
|
|
. Create the `myservice` service.
|
|
|
|
.. Create a YAML file similar to the following:
|
|
+
|
|
[source,yaml]
|
|
----
|
|
kind: Service
|
|
apiVersion: v1
|
|
metadata:
|
|
name: myservice
|
|
spec:
|
|
ports:
|
|
- protocol: TCP
|
|
port: 80
|
|
targetPort: 9376
|
|
----
|
|
|
|
.. Create the pod:
|
|
+
|
|
[source,terminal]
|
|
----
|
|
$ oc create -f myservice.yaml
|
|
----
|
|
|
|
.. View the status of the pod:
|
|
+
|
|
[source,terminal]
|
|
----
|
|
$ oc get pods
|
|
----
|
|
+
|
|
.Example output
|
|
[source,terminal]
|
|
----
|
|
NAME READY STATUS RESTARTS AGE
|
|
myapp-pod 0/1 Init:1/2 0 5s
|
|
----
|
|
+
|
|
The pod status, `Init:1/2`, indicates it is waiting for one service, in this case the `mydb` service.
|
|
|
|
. Create the `mydb` service:
|
|
|
|
.. Create a YAML file similar to the following:
|
|
+
|
|
[source,yaml]
|
|
----
|
|
kind: Service
|
|
apiVersion: v1
|
|
metadata:
|
|
name: mydb
|
|
spec:
|
|
ports:
|
|
- protocol: TCP
|
|
port: 80
|
|
targetPort: 9377
|
|
----
|
|
|
|
.. Create the pod:
|
|
+
|
|
[source,terminal]
|
|
----
|
|
$ oc create -f mydb.yaml
|
|
----
|
|
|
|
.. View the status of the pod:
|
|
+
|
|
[source,terminal]
|
|
----
|
|
$ oc get pods
|
|
----
|
|
+
|
|
.Example output
|
|
[source,terminal]
|
|
----
|
|
NAME READY STATUS RESTARTS AGE
|
|
myapp-pod 1/1 Running 0 2m
|
|
----
|
|
+
|
|
The pod status indicated that it is no longer waiting for the services and is running.
|