Data Management Global

 View Only

Deploy Db2 .NET 6 application and capture db2trc on Red Hat OpenShift Platform

By Tapan Kumar Ghosh posted Mon September 11, 2023 08:27 AM

  

This blog explains all the below important aspects of deployment of Db2 .NET 6 application and about capturing db2trc for troubleshooting driver issue on Red Hat OpenShift Platform.

Part-1: Develop .NET 6 application and upload into GitHub

Part-2: Deploy the application in Red Hat OpenShift using Git Repository

Part-3: Capture db2trc in OpenShift platform

Part - 1 : Develop .NET 6 application and upload into GitHub

  1.   Open Visual Studio 2022 and then click on Create a new project option.



  2. Select Console App and click Next.



  3. Provide project name as “ConsoleDb2DotNET6App” and location of the project. And Click Next.



  4. Select framework: .NET 6.0 (Long-term support) and click Create.



  5. After clicking Create button, it creates console app which looks like below.



  6. Now, modify the application to target x64 platform using Configuration Manager. Click on the Debug → select Configuration Manager from the dropdown list.



  7. In Configuration Manager window click on the drop-down list for Active solution Configuration and select as Release. Then click on the drop-down list for Active solution platform, select New and create a new solution platform as x64 and copy the settings from Any CPU and click OK.



  8. Now you should be able to see the project as Release x64. Then click on Close.



  9. Right Click on ConsoleDb2DotNET6App project and click on Manage Nuget Packages option. And then install 'IBM Data Server provider for .NET6 on Linux' package into your application. 



  10. In the Nuget Package Manager screen, go to Package source drop down list and select 'nuget.org'. Click on Browse and search for Net.IBM.Data.Db2-lnx. Then select Net.IBM.Data.Db2-lnx package and choose 6.0.0.400 version from the drop down list. Now, click on Install and then click on OK in the next window and later accept the license agreement by clicking on I Accept in License Acceptance window.



  11. Now IBM Data Server provider for .NET6 on Linux package gets installed as a part of your application.



  12. Modify the Program.cs file to create a sample .NET 6 application to connect to Db2 database.

    // See https://aka.ms/new-console-template for more information

    using IBM.Data.Db2;

    Console.WriteLine("Using DB2 .NET6 provider");

    string uid=Environment.GetEnvironmentVariable("uid");

    string pwd=Environment.GetEnvironmentVariable("pwd");

    string server=Environment.GetEnvironmentVariable("server");

    string db=Environment.GetEnvironmentVariable("db");

    string security =Environment.GetEnvironmentVariable("security");

    //Connection String

    string connString = "uid=" + uid + ";pwd=" + pwd +";server=" + server + ";database=" + db + ";Security=" + security;

    DB2Connection con = new DB2Connection(connString);

    con.Open();

    Console.WriteLine("Connection Opened successfully");

    con.Close();

    Console.WriteLine("Connection Closed");

  13. Add a docker file into the project by right-clicking on ConsoleDb2DotNET6App project -> Add-> Docker Support option.



  14. Choose Linux as Target OS and Click OK.



  15. Then default Dockerfile gets created. However, you need to modify DockerFile with below contents. The last line of the docker file is command ‘tail -f /dev/null’ to keep the container running/alive, else pod will crash in OpenShift platform.

    FROM mcr.microsoft.com/dotnet/runtime:6.0 AS base

    WORKDIR /app

    ENV LD_LIBRARY_PATH="/app/clidriver/lib/"

    RUN apt-get -y update && apt-get install -y libxml2

    FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

    WORKDIR /src

    COPY ["ConsoleDb2DotNET6App.csproj", "."]

    RUN dotnet restore "./ConsoleDb2DotNET6App.csproj"

    COPY . .

    WORKDIR "/src/."

    RUN dotnet build "ConsoleDb2DotNET6App.csproj" -c Release -o /app/build

    FROM build AS publish

    RUN dotnet publish "ConsoleDb2DotNET6App.csproj" -c Release -o /app/publish

    FROM base AS final

    WORKDIR /app

    COPY --from=publish /app/publish .

    ENV PATH=$PATH:/app/clidriver/lib:/app/clidriver/adm

    CMD tail -f /dev/null


  16. Now upload this application to GitHub repository. For that, right click on ConsoleDb2DotNET6App Solution -> click on Create Git Repository option.



  17. In Create a Git repository window, you need to provide your GitHub credentials to connect to GitHub repository. Then your account will show in Account and owner section. The Local Path option needs to be set to the location where project’s csproj file is present. In this blog, public GitHub repository is being created for demo purpose.



  18. Then Click on Create and Push, to upload the application in the GitHub repository.
  19. Once application gets uploaded into GitHub repository, you need to deploy that application from GitHub repository into Red Hat OpenShift using steps mentioned in Part - 2.

Part - 2 : Deploy the application in Red Hat OpenShift using Git Repository

  1. Login to Red Hat OpenShift Service on AWS cluster using your credentials.
  2. In the +Add view, click From Git to see the Import from Git form.



  3. In the Git section, provide the Git repository URL for the codebase you uploaded.



  4. In the Application section choose Create application. Provide your application name and other details.


  5. Click on Build Configuration option.



  6. In Build Configuration section, you can provide the confidential details like credentials, server IP, port etc. in either secrets/Configmap or environment variables, to keep these sensitive details away from the git source code.



  7. You can choose server, database, userid, password etc. details in connection string from Secrets for environment variables which you are going to use in your application.



  8. Click Create to create the application and see its build status in the Topology view.



  9. You can view your application in the Topology view.
  10. Click the application and check the Resources tab of the application details pane. Look for a success message under Builds, for example, Build #1 is complete.
  11. If you need to modify the application in GitHub repository, then you need to build it by clicking on Start build option. If build fails, identify the reason for failure by seeing the View logs and fix it. After successful build, get inside the running pod by clicking on it.



  12. Then click on the Terminal tab of the running pod.



  13. You need to run the application using dotnet command i.e. dotnet Consoledb2Dotnet6app.dll. You will see the successful connection message. You can also capture the db2trc dump file for troubleshoot any driver related issue as mentioned in Part - 3.


Part - 3 : Capture db2trc in Red Hat OpenShift
Platform

By default, you do not have root access on any of the pods created on OpenShift. To capture db2trc, you need to have root access. For root access, you need to follow the below simple steps.

  1. You need to install OpenShift command-line interface (CLI) on Windows. You can refer OpenShift installation documentation to install it.
  2. After you install the OpenShift CLI, it is available using the oc command.
        C:\> oc <command>
  3. You can authenticate from the command line using the CLI command oc login with token and server option to establish a session to an OpenShift Container Platform server.
        C:\> oc login --token=xxxxxxxx --server=xxxxxxxx

  4. Then switch to the project you would like to work with.
        C:\> oc project <projectname>


  5. You need to create a service account. Since you deployed db2dotnet6app application hence you can create db2dotnet6app-sa as service account name.

        C:\>oc create sa db2dotnet6app-sa

  6. Now, give privileged access to service account.
        C:\>oc adm policy add-scc-to-user privileged -z db2dotnet6app-sa

    NOTE: You need sufficient privilege to execute the above command.

  7. To see all deployed application, you need to execute the below command.
       
      C:\>oc get deployment

  8. Once the policy is added to your service account db2dotnet6app-sa. Add service account to your deployed application.

        C:>oc set sa deployment db2dotnet6app db2dotnet6app-sa


  9. You need to do the relevant changes in Deployment Config YAML file. To do these changes, you need to click on the Topology. Then right click on deployed application name db2dotnet6app D icon, choose -> Edit Deployment option.



  10. In Edit deployment screen choose to configure via YAML view option.



  11. Modify the deployment yaml config file by updating the below line.

     securityContext: { }  to

     securityContext:

     <4 blankspace>runAsUser:<blankspace>0   

    NOTE: This strict format should be maintained, else desired changes will not take place.

    After modification, click on Save. It will restart the pod and new pod with new name gets created. These changes will reflect on the YAML tab of running pod. You can verify the securityContext section of deployment by clicking on YAML tab  to see the above changes.



  12. Now, you navigate to the Terminal tab of your running pod. And execute whoami command from terminal prompt to verify if you have root access. Command output root indicates that you have root access.



  13. Capture the db2trc dump file using db2trc on -f t.dmp command in terminal prompt before executing the application. Execute the application using dotnet ConsoleDb2DotNET6App.dll and then turn off db2trc using db2trc off command. You will see t.dmp dump file gets created.



  14. Now, it is time to format the db2trc dump file using below db2trc format commands.

    db2trc flw t.dmp t.flw
    db2trc fmt t.dmp t.fmt
    db2trc fmt -cli t.dmp t.cli
    db2trc fmt -c t.dmp t.fmtc



  15. You can share these formatted db2trc files for further investigation to troubleshoot any driver related issue.

    NOTE: Above security context constraints(SCC) settings were done for capturing db2trc. Once it is captured, you can revert to your original configuration settings.

    Disclaimer: This configuration, setup and settings are for demonstration purpose only. Please follow the recommendations and best practices by Red Hat OpenShift, other providers and your company policies and guidelines.

    0 comments
    28 views

    Permalink