Skip to content

Latest commit

 

History

History
72 lines (53 loc) · 2.49 KB

File metadata and controls

72 lines (53 loc) · 2.49 KB

Init Container

In this example, we will see a simple Init Container in action. As in the other examples, we assume that you have a running Kubernetes cluster to your avail. Checkout INSTALL documentation for some options to access a Kubernetes cluster.

In this example, we are creating a Pod with an init container and a main container:

  • The Init Container will start first and check out a GitHub repository containing a static example website.

  • The application container started after the Init Container has finished will then serve these pages over port 80

Let’s create this bare Pod into the current namespace with the following:

kubectl create -f https://k8spatterns.io/InitContainer/pod.yml

Check the startup of the Pod by watching the Pod by executing the following command right after the creation of your Pod:

kubectl get pods -w

You will see some output like

NAME   READY   STATUS            RESTARTS   AGE
www    0/1     Init:0/1          0          8s
www    0/1     PodInitializing   0          10s
www    1/1     Running           0          12s

Finally, let’s check how we can access the data from the Init Container retrieved. For this, we create a nodePort Service with

kubectl apply -f https://k8spatterns.io/InitContainer/service.yml

This service will open a port on every node so that we can access the static HTTP server from there.

If you are using minikube, you can quickly fire up your browser to point to the exposed port with

minikube service www

For any other cluster, you can find out the service port.

port=$(kubectl get svc www -o jsonpath='{.spec.ports[0].nodePort}')

And then access it from every node of your cluster. You would need to find out the address of such a node first, like with kubectl get node -o wide For more ways to expose that service, see the "Service Discovery" pattern.