Deploy an application to a managed Kubernetes cluster.
As a pre-req, you should have obtained a cluster. This is a managed Kubernetes cluster hosted on an IBM organization, with access granted to your personal IBM Cloud account. Let's enable kubectl
, the primary method of working with any Kubernetes cluster, to communicate with this cluster.
Recall the name of the cluster you obtained in the prereq steps. You can run ibmcloud ks clusters
if you need to find it again.
Run the following:
# Replace XX with your cluster number!
$ ibmcloud ks cluster-config jri-customer-kubeXX
Set the KUBECONFIG
env var. You'll see output like this:
The configuration for jri-customer-kube03 was downloaded successfully.
Export environment variables to start using Kubernetes.
export KUBECONFIG=/Users/svennam/.bluemix/plugins/container-service/clusters/jri-customer-kube03/kube-config-sjc04-jri-customer-kube03.yml
Copy/paste the output from your terminal. Save this output in a notepad/note for future reference. When opening a new terminal, you will have to set this env var again. If you're feeling savvy with your environment, you can also edit your .bashrc
or similar file to include this env var every time you open a new terminal.
Once your client is configured, you are ready to deploy your first application, guestbook
.
Now, you will deploy an application called guestbook
that has already been built and uploaded to DockerHub under the name svennam92/guestbook:v1
.
-
Start by creating a deployment of
guestbook
:
$ kubectl create deployment guestbook --image=svennam92/guestbook:v1
This action will take a bit of time. To check the status of the pods being spun up, you can use
$ kubectl get pods --watch
. (Ctrl+C
to exit the watch process)You should see output similar to the following:
$ kubectl get pods NAME READY STATUS RESTARTS AGE guestbook-59bd679fdc-bxdg7 0/1 ContainerCreating 0 1m
Eventually, the status should show up as
Running
.$ kubectl get pods NAME READY STATUS RESTARTS AGE guestbook-59bd679fdc-bxdg7 1/1 Running 0 1m
The end result of the run command is a Pod containing our application container.
Pro Tip: This step also created a
Deployment
resource. While Pods are ephemeral, Deployments continue to live on. That means even if you delete this pod (kubectl delete pod <pod_name>
), Kubernetes will bring up another in its place because the Deployment enforces it!Once the status reads
Running
, we need to expose that deployment as a service so we can access it through the IP of the worker nodes. -
The
guestbook
application listens on port 8080. Expose it as a service, run:$ kubectl expose deployment guestbook --type="NodePort" --port=8080
-
To find the port used on that worker node, examine your new service:
$ kubectl get service guestbook NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE guestbook NodePort 10.10.10.253 <none> 8080:31208/TCP 1m
We can see that our
<nodeport>
is31208
. We can see in the output the port mapping from 8080 inside the pod exposed to the cluster on port 31208. This port in the 31000 range is automatically chosen, and could be different for you. -
guestbook
is now running on your cluster, and exposed to the internet. We need to find out where it is accessible. The worker nodes running in the container service get external IP addresses. Run$ ibmcloud ks workers <name-of-cluster>
, and note the public IP listed on the<public-IP>
line.$ ibmcloud ks workers jri-customer-kubeXX OK ID Public IP Private IP Machine Type State Status Zone Version kube-hou02-pa1e3ee39f549640aebea69a444f51fe55-w1 173.193.99.136 10.76.194.30 free normal Ready hou02 1.5.6_1500*
We can see that our
<public-IP>
is173.193.99.136
. If you have multiple workers, choose any of them. -
Now that you have both the address and the port, you can now access the application in the web browser at
<public-IP>:<nodeport>
. In the example case this is173.193.99.136:31208
.
Congratulations, you've now deployed an application to Kubernetes! In the next portion of the lab, you'll learn how to update a Pod and scale it up.