There are multiple ways to interact with the API. The most common will be to use the command kubectl. Using this command there are also several ways to start things inside Kubernetes. In the first example we will start a container containing the webserver nginx using a single commandline, also called an imperative command:
kubectl create deployment nginx --image nginx
This created a deployment, that started a pod. We concentrate on the started pod. Deployments are described here. You can see this container running by typing on the commandline:
kubectl get pods
You can also use the dashboard to take a look at the pod.
The pod can be deleted by:
kubectl delete deployment nginx
Everything Kubernetes API Object can also be described in a declarative file. A declarative file for an nginx pod can be found at nginx.yaml.
To apply it to the kubernetes cluster use:
kubectl apply -f nginx.yaml
Multiple Containers in a pod share the filesystem and the network address. All containers will run on the same cluster node. There are a number of patterns how this can be used. The following examples are already using services so that you can see the effects. Services are described here
Init containers are run before the service containers are started. There can be multiple of them but the example only shows one.
In our example we are starting an example container, that changes the content of the website shown by the nginx server started later on. See the full definition in init_nginx.yaml. To start it use:
kubectl apply -f init_nginx.yaml
This file also creates a service so you can see the results of the init in your browser. If you have not read about services yet, ignore this and just use the following to see the result:
minikube service nginx
A sidecar container runs in parallel to the main service of a pod. There are many possible usages to this pattern. One of them would be to update the static content of a website on a regular basis. The example in sidecar.yaml does this by writing the current date into the index.html every 5 seconds. Apply this configuration and watch the results using:
kubectl apply -f sidecar.yaml
minikube service nginx
A reload should give you a new timestamp every 5 seconds.