| copyright | lastupdated | keywords | subcollection | ||
|---|---|---|---|---|---|
|
2025-12-04 |
kubernetes, tutorial, setup |
cloud-databases |
{{site.data.keyword.attribute-definition-list}}
Connect a {{site.data.keyword.databases-for}} deployment to an {{site.data.keyword.containerlong_notm}} application
{: #tutorial-k8s-app}
The {{site.data.keyword.databases-for}} "Hello World" Kubernetes examples repository holds sample {{site.data.keyword.cloud}} applications, which are written in various programming languages, that detail how to connect a {{site.data.keyword.databases-for}} deployment to an {{site.data.keyword.containerlong_notm}} application.
Each Git branch of the examples repository corresponds to samples in a particular programming language, either JavaScript that uses Node.js, or python. The files in each folder correspond either to a database or a message queue. {:shortdesc: .shortdesc}
{: #sample-apps}
Clone the respective repo you want to use. For instance, you can clone the Node repository by selecting the Node branch. Then, click Clone or download to get the URL you need to clone by using SSH or HTTPS. This command looks like:
git clone -b node git@github.com:IBM-Cloud/clouddatabases-helloworld-kubernetes-examples.git{: pre}
Or, clone by using HTTPS:
git clone -b node https://github.com/IBM-Cloud/clouddatabases-helloworld-kubernetes-examples.git{: pre}
After the branch is cloned, select the appropriate directory for the database you want to try. Each database has its own copy of these instructions on how to provision and deploy a database or message queue and an application on {{site.data.keyword.containerlong_notm}}.
{: #running-on-cloud}
-
If you do not already have an {{site.data.keyword.cloud_notm}} account, sign up here.
-
Download and install {{site.data.keyword.cloud_notm}} CLI. The {{site.data.keyword.cloud_notm}} CLI tool enables you to communicate with {{site.data.keyword.cloud_notm}} from your console or CLI.
-
Install the {{site.data.keyword.containershort_notm}} CLI plug-in and the Container Registry CLI plug-in
ibmcloud plugin install container-service ibmcloud plugin install container-registry
{: pre}
To verify their installation, run
ibmcloud plugin list
{: pre}
You receive a response like this:
Listing installed plug-ins... Plugin Name Version Status container-registry 0.1.382 container-service/kubernetes-service 0.3.34
{: screen}
-
Download and install the Kubernetes CLI.
Follow the instructions for downloading and installing the Kubernetes CLI for the platform that you're using.
-
Connect to {{site.data.keyword.cloud_notm}} in the CLI tool and follow the prompts to log in.
ibmcloud login
{: pre}
If you have a federated user ID, use the
ibmcloud login --ssocommand to log in with your single sign-on ID. {: note}
{: #create-database}
This process creates a standard database instance in the service that you specify that might incur extra charges in your selected plan. {: note}
-
You must target a resource group, by using the following command:
ibmcloud target -g <RESOURCE_GROUP>
For more information, see Working with resources and resource groups (ibmcloud resource). {: tip}
-
The database can be created from the CLI by using the
ibmcloud resource service-instance-createcommand. The command takes a service instance name, a service name, plan name, and location. -
The service name is one of the {{site.data.keyword.databases-for}} services,
databases-for-elasticsearch,databases-for-mongodb,databases-for-postgresql,databases-for-redis,messages-for-rabbitmq, ordatabases-for-mysql.ibmcloud resource service-instance-create <INSTANCE_NAME> <SERVICE_NAME> standard <REGION>
{: pre}
Remember the database instance name. Find your region identifier here. {: tip}
The previous example provisions a Shared Compute instance. For more information, see the Hosting models overview. {: note}
{: #config-kubernetes}
-
Create a {{site.data.keyword.containershort_notm}}. Choose the location and resource group in which you'd like to set up your cluster. Select the cluster type that you want to use. This example requires only the lite plan, which comes with one worker node. After a cluster is provisioned, you are given a list of steps to access your cluster and set the environment variables under the Access tab. You are also able to verify that your deployment is provisioned and running normally.
-
Make sure that you are targeting the correct {{site.data.keyword.cloud_notm}} resource group of your {{site.data.keyword.containershort_notm}}.
If your resource group is named anything other than
default, use the following command to target your cluster resource group:ibmcloud target -g <RESOURCE_GROUP_NAME>
{: pre}
This example uses the
defaultresource group. -
Create your own private image repository in {{site.data.keyword.registryshort_notm}} to store your application's Docker image. Since we want the images to be private, we need to create a namespace, which creates a unique URL to your image repository.
ibmcloud cr namespace-add <YOUR_NAMESPACE>
{: pre}
-
Add the Cloud Databases deployment to your cluster.
ibmcloud ks cluster service bind --cluster <YOUR_CLUSTER_NAME> --namespace default --service <INSTANCE_NAME_OR_CRN>
{: pre}
The "default" namespace refers to the Kubernetes instance and not the user-created image store namespace. Likewise, if your database uses both public and private endpoints, your public endpoint is used by default. Therefore, if you want to select the private endpoint, first you need to create a service key for your database so Kubernetes can use it when binding to the database. You set up a service key by using the command: {: note}
ibmcloud resource service-key-create <YOUR-PRIVATE-KEY> --instance-name <INSTANCE_NAME_OR_CRN> --service-endpoint private
{: pre}
The private service endpoint is selected with
--service-endpoint private. After that, you bind the database to the Kubernetes cluster through the private endpoint by using the commandibmcloud ks cluster service bind <YOUR_CLUSTER_NAME> default <INSTANCE_NAME_OR_CRN> --key <YOUR-PRIVATE-KEY>
{: pre}
-
Verify that the Kubernetes secret was created in your cluster namespace. Kubernetes uses secrets to store confidential information like the {{site.data.keyword.IBM_notm}} {{site.data.keyword.iamshort}} (IAM) API key and the URL that the container uses to gain access. To set the cluster as the context for this session and then get the API key for accessing the instance of your deployment, run the following commands
ibmcloud ks cluster config --cluster <CLUSTER_NAME_OR_ID>
{: pre}
Then
kubectl get secrets --namespace=default
{: pre}
Save the name of the secret that was generated when you bound
your_database_nameto your Kubernetes service. {: note} -
If you haven't already, clone the app in one of the available languages to your local environment from your console by using the following command
git clone -b <LANGUAGE> git@github.com:IBM-Cloud/clouddatabases-helloworld-kubernetes-examples.git
{: pre}
-
cdinto this newly created directory, andcdinto the database folder. The code for connecting to the service, and reading from and updating the database can be found inserver.js. See Code Structure and the code comments for information on the app's functions. Apublicdirectory contains the html, stylesheets, and JavaScript for the web app. However, to get the application to work, we first need to push the Docker image of this application to our {{site.data.keyword.registryshort_notm}}. -
Build and push the application's Docker image to your {{site.data.keyword.registryshort_notm}}. Specify the appropriate region and give the container a name.
ibmcloud cr build -t <REGION>.icr.io/<NAMESPACE>/<CONTAINER_NAME> .
{: pre}
You can view the image in container registry by using
ibmcloud cr images
{: pre}
You get something like the following response
REPOSITORY TAG DIGEST NAMESPACE CREATED SIZE SECURITY STATUS <region>.icr.io/mynamespace/container_name latest 81c3959ea657 mynamespace 4 hours ago 28 MB No Issues
{: screen}
-
Update the Kubernetes deployment configuration file
clouddb-deployment.yaml.Change the
imagename to the repository name that you got from the previous step:image: "<REGION>.icr.io/mynamespace/<container_name>" # Edit me
{: pre}
Now, under
secretKeyRef, change the name of<db-secret-name>to match the name of the secret that was created when you bound your database deployment to your Kubernetes cluster.secretKeyRef: name: <DB-SECRET-NAME> # Edit me
{: pre}
As for the
serviceconfiguration at the end of the file,nodePortindicates the port that the application can be accessed from. You have ports in the range 30000 - 32767 that you can use, but we chose 30081. The TCP port is set to 8080, which is the port the Node.js application runs on in the container.
{: #deploy-kubernetes}
-
Deploy the application to {{site.data.keyword.containershort_notm}}. When you deploy the application, it is automatically bound to your Kubernetes cluster.
kubectl apply -f clouddb-deployment.yaml
{: pre}
-
Get the IP for the application.
ibmcloud ks workers -c <CLUSTER_NAME>
{: pre}
The result is something like:
ID Public IP PrivateIP Machine Type State Status Zone Version kube-hou02-pa1a59e9fd92f44af9b4147a27a31db5c4-w1 199.199.99.999 10.76202.188 free normal Ready hou02 1.10.11_1536
{: screen}
Now you can access the application from the Public IP from port 30082.
The clouddatabases-helloworld app displays the contents of an examples database. To demonstrate that the app is connected to your service, add some words to the database. The words are displayed as you add them, with the most recently added words displayed first.
{: #code-structures}
| File | Description |
|---|---|
| server.js | Establishes a connection to the database by using credentials from BINDING (the name that we created in the Kubernetes deployment file to display the credentials) and handles create and read operations on the database. |
| main.js | Handles user input for a PUT command and parses the results of a GET command to output the contents of the database. |
| {: caption="Code structure" caption-side="bottom"} |
The app uses a PUT and a GET operation:
-
PUT
- Takes user input from main.js.
- Adds the user input to the database.
-
GET
- Retrieves the contents of the database.
- Returns the response of the database command to main.js.