After testing Kubernetes locally to verify that all our pieces work together, we're now
ready to deploy to the cloud.
To do that, we'll need credentials, which generally take the form of a kubeconfig file.
This application is going to have a staging cluster and a production cluster.
For now, we'll use the kubeconfig file for staging.
Any kind of setup would then need to be repeated for production.
Let's check the Kubernetes service to make sure the kubeconfig file is set up correctly.
We'll verify that the Kubernetes service is running,
set the Kubernetes context and namespace, and call kubectl get all to
display any resources, which we can see there are none.
Now that we know we can connect to Kubernetes, the next step is to set up the Docker credentials,
so that Kubernetes will be able to pull the images that we deploy.
When we were running Docker locally, Docker login was how we made sure we could pull images
from the repository.
For our Kubernetes cluster, we'll use a Kubernetes secret instead.
Another one-time setup is to deploy Mongo DB to our cluster.
We do this using the kubectl apply command, and can see that the database is running from
the output of the kubectl get pods command.
Since our database is empty, let's run the setup script to populate it.
We'll do that by executing a command on the Mongo pod.
Our other two containers will be deployed automatically by Wercker.
But first we have to add the deploy pipelines to our wercker.yml files.
Let's start with the API.
We open the wercker.yml file for the API and add the deploy pipeline.
This pipeline is pretty straightforward.
We'll use the kubectl step and run the apply command; the deploy-api.yml file is the same
one we saw before.
The Wercker and token variables come from the kubeconfig file.
These need to be set up in Wercker.
We'll do that next.
We've configured a deploy pipeline in our wercker.yml file, but we also need to add
it using the Wercker UI.
The Workflows tab is where our pipelines are defined.
We've got our build pipeline, and we've been using it to build our code and
push our docker images.
Now that we've configured a deploy pipeline in our
wercker.yml, we can add that pipeline here.
We'll actually create it twice - once to deploy to staging, and again to deploy
to production.
Let's click "Add new pipeline" to continue.
Let's start with staging.
We'll call this pipeline deploy-staging, and of course its name in the YML file is just
deploy.
The default hook type means this pipeline won't automatically run when a git commit
is pushed.
Click Create to create this pipeline.
Now we'll take our kubeconfig file and turn it into the environment variables that Wercker
needs to deploy our application to the right server and with the appropriate credentials.
Let's take a peek at the kubeconfig file.
The first thing we're going to do is create a variable for the server host and port.
And after that we're going to create another variable for the token.
So let's create our first variable, K8S_SERVER.
And now for our second variable, K8S_TOKEN.
We're going to make this one protected, so nobody will be able to see it after it's saved.
These are the only variables we need to define.
Of course, we'll want a separate pipeline called deploy-production with values from
our production kubeconfig file, but we don't need to do that right now.
One last thing we need to do is commit the change we made to wercker.yml.
And when we do - well, let's go and take a look
at what happens.
We check our git status, verify our changes, then add, commit, and push our wercker.yml
file.
When we pushed the commit of wercker.yml, that started a build, and we see the results
on the Runs tab.
The build was done.
This is the pipeline we defined.
Let's run the deploy-staging action.
This doesn't usually take very long to run.
And there's the kubectl step - looks like it worked!
Now let's set up to deploy the front end.
The deployment pipeline is similar to the one for the API, but it's using the NODE box.
It has the same variables we used before.
We'll set those up in the Wercker UI.
We'll go to the Workflows tab of the front end app.
And we'll create another pipeline called deploy-staging.
Now we can create our K8S_SERVER variable.
And our K8S_TOKEN variable.
And that's it, and again, we don't need to set up the deploy-production pipeline right
now.
Now let's commit the change we made to this wercker.yml.
We run the same git commands as before, and push our wercker.yml change.
Once again, the git push started a build.
The build was done.
Let's run the deploy-staging action.
And wait for it to finish.
It shouldn't take too long.
Now if we look at Kubernetes, we should see everything running.
We see our all of our pods are running, and so are their corresponding services.
Our web service has an external IP address.
Let's try connecting to it with a browser.
And it works!
Learn more at cloud.oracle.com/containers/pipelines.