This sample demonstrates binding a simple Appsody Spring boot application to DB2 in Kubernetes.
This sample makes use of Appsody for cloud-native development and deployment, it is bootstrapped into existence with Appsody, using the Appsody Spring boot stack.
- Install Appsody
brew install appsody
- Install Appsody operator
appsody install operator
Appsody generates a AppsodyApplication
CR called app-deploy.yaml
when you run appsody build
, which can be used to deploy the application to Kubernetes. In this sample we have made app-deploy.yaml
available for simplicity purposes.
There are few defaults in this configuration file, that we can change or add.
- Application name can be changed by changing
metdata.name
- Application image name can be changed by changing
spec.applicationImage
- You can also add volume mounts for Kubernetes secrets acquisition by specifying the
volumes
andvolumeMounts
More info on the parameters that can be configured in AppsodyApplication
CR can be found here
Add the DB2 credentials to db2-secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: db2-secrets
stringData:
db2.url: <<url>>
db2.username: <<username>>
db2.password: <<password>>
and run
kubectl apply -f db2-secret.yaml
To deploy the application to Kubernetes, run the following command
appsody deploy -t <<mynamespace>>/sample-service-binding-db2:[tag] --push
The above command performs the following actions:
- Calls
appsody build
and creates a deployment image. - The
-t mynamespace/myrepository[:tag]
flag tags the image. - The --push flag tells the Appsody CLI to push the image to Docker Hub. You can push the image to private registry by specifying
--push-url
and the registry url. - The
app-deploy.yaml
file is used to issue akubectl apply -f
command against the target Kubernetes cluster.
More info on how to deploy appsody application can be found here
- To determine the IP address
a. Identify the node on which your pod has been scheduled
kubectl get pods -o wide
b. Use the node name to get the IP address
kubectl get node <<nodename>> -o wide
- To determine the mapped service port, run
kubectl get service -o wide sample-service-binding-db2
- To access the sample application service endpoint
curl http://<<ipaddress>>:<<port>>/rest/v1/books
This sample application acquires the Secrets via K8s volume mounts and this is done use Spring Cloud Kubernetes(SCK). Check app-deploy.yaml
for info on volumes
and volumeMounts
.
The application enables SCK K8s Secrets mapping, and declares a set of Secrets paths, as shown here (from src/main/resources/bootstrap.yml
)
spring:
cloud:
kubernetes:
secrets:
enabled: true
paths: /etc/secrets/db2
SCK then maps the contents of each file under the directory /etc/secrets/db2
to resolve placeholders found within application.properties
.
spring.datasource.url= ${db2.url}
spring.datasource.username: ${db2.username}
spring.datasource.password: ${db2.password}