mirror of
https://github.com/openshift/openshift-docs.git
synced 2026-02-07 00:48:01 +01:00
121 lines
2.2 KiB
Plaintext
121 lines
2.2 KiB
Plaintext
// Module included in the following assemblies:
|
|
//
|
|
// * nodes/nodes-containers-init.adoc
|
|
|
|
[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`. Once both containers complete, the Pod begins.
|
|
|
|
.Procedure
|
|
|
|
. Create a YAML file for the Init Container:
|
|
+
|
|
[source,yaml]
|
|
----
|
|
apiVersion: v1
|
|
kind: Pod
|
|
metadata:
|
|
name: myapp-pod
|
|
labels:
|
|
app: myapp
|
|
spec:
|
|
containers:
|
|
- name: myapp-container
|
|
image: busybox
|
|
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
|
|
initContainers:
|
|
- name: init-myservice
|
|
image: busybox
|
|
command: ['sh', '-c', 'until nslookup myservice; do echo waiting for myservice; sleep 2; done;']
|
|
- name: init-mydb
|
|
image: busybox
|
|
command: ['sh', '-c', 'until nslookup mydb; do echo waiting for mydb; sleep 2; done;']
|
|
----
|
|
|
|
. Create a YAML file for the `myservice` service.
|
|
+
|
|
[source,yaml]
|
|
----
|
|
kind: Service
|
|
apiVersion: v1
|
|
metadata:
|
|
name: myservice
|
|
spec:
|
|
ports:
|
|
- protocol: TCP
|
|
port: 80
|
|
targetPort: 9376
|
|
----
|
|
|
|
. Create a YAML file for the `mydb` service.
|
|
+
|
|
[source,yaml]
|
|
----
|
|
kind: Service
|
|
apiVersion: v1
|
|
metadata:
|
|
name: mydb
|
|
spec:
|
|
ports:
|
|
- protocol: TCP
|
|
port: 80
|
|
targetPort: 9377
|
|
----
|
|
|
|
. Run the following command to create the `myapp-pod`:
|
|
+
|
|
[source,terminal]
|
|
----
|
|
$ oc create -f myapp.yaml
|
|
----
|
|
+
|
|
.Example output
|
|
[source,terminal]
|
|
----
|
|
pod/myapp-pod created
|
|
----
|
|
|
|
. 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
|
|
----
|
|
+
|
|
Note that the pod status indicates it is waiting
|
|
|
|
. Run the following commands to create the services:
|
|
+
|
|
[source,terminal]
|
|
----
|
|
$ oc create -f mydb.yaml
|
|
----
|
|
+
|
|
[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 1/1 Running 0 2m
|
|
----
|
|
|