Skip to main content

Posts

2022

Git Config

·8 words·1 min
Git Config commands git config –global init.defaultBranch main

Kotlin Enum

·150 words·1 min
Let’s have a look at enums in kotlin. Unlike Java, where enum is a Type; in Kotlin, enums are classes Defining an enum # enum class Direction { NORTH, SOUTH, WEST, EAST } Defining an enum with a variable # enum class Direction(val shortName: Char) { NORTH('N'), SOUTH('S'), WEST('W'), EAST('E') } Template Method pattern in Enum # enum class Direction(val shortName: Char) { NORTH('N') { override fun move() = "Moving in North Direction" }, SOUTH('S') { override fun move() = "Moving in South Direction" }, WEST('W'){ override fun move() = "Moving in West Direction" }, EAST('E'){ override fun move() = "Moving in East Direction" }; abstract fun move(): String } Companion methods in enum # enum class Direction(val shortName: String) { NORTH("N"), SOUTH("S"), WEST("W"), EAST("E"); companion object { fun valueOfIgnoreCase(name: String) = valueOf(name.uppercase(Locale.getDefault())) fun valueOfShortName(shortName: String): Direction? = Direction.values().find { it.shortName == shortName } } } Links # https://kotlinlang.org/docs/enum-classes.html https://www.baeldung.com/kotlin/enum

Bastion Pod in Kubernetes

·384 words·2 mins
Background # When deploying applications to kubernetes, we should restrict access to resources like databases, queues, cache, etc. From a security prospective, we should not be able to connect to storage/database from outside the kubernetes deployment(applications). Only applications deployed to kubernetes cluster should be allowed access to databases,queues, etc. Problem # But for debugging purpose its essential to be able to connect to such resources. For e.g. we might need to check some data in the database or verify if an item is getting stored in redis cache, etc

Springdoc OpenApi3 Swagger

When working as a backend engineer, it is essential to document the REST APIs. It also helps in providing a UI(swagger-ui) to test the REST calls. Let us try to integrate springdoc-openapi to provide swagger documentation for a spring boot project using spring-security(OAuth2). Add the dependencies to build.gradle # implementation("org.springdoc:springdoc-openapi-ui:1.6.8") implementation("org.springdoc:springdoc-openapi-security:1.6.8") implementation("org.springdoc:springdoc-openapi-kotlin:1.6.8") Kotlin code example # The following example showcases spring configuration to create OpenAPI bean which supports two authentication mechanism:

2019

GitOps

·488 words·3 mins
GitOps # Three pillars of GitOps # Pipelines Observability Control ##Joined Up Pipelines CI CD Release Automation Git as source of truth Service Deployments are controlled using operator pattern in jubernetes Kubernetes pattern - Git config -> Deploy Operator > service/deployments Similarly now in gitops config is code and everything is config code and config must be version controlled kubectl get <object> -o yaml --export to extract existing configuration to bootstrap config repo in git Sometimes export is not perfect. So review it properly

Ansible introduction

·623 words·3 mins
Ansible introduction # Modules # Command module: Takes the command and executes it Shell module: Executes through a shell like /bin/sh Script module: Runs a local script on a remote node after transferring it. raw: executes a ssh command. useful for installation python3 Adhoc commands # Examples:

Kubernetes introduction

·574 words·3 mins
Kubernetes Introduction # Build a docker image # Dockerfile default filename FROM base image to be used for container ADD copies files/directories/remote file urls to container filesystem. Tarball and Remote URL (git, http) handling COPY same as ADD without tar and remote url handling CMD kubectl –namespace <DEV|STAGING|PROD> get deployments,pods,svc,ingress

Prometheus

·1007 words·5 mins
Prometheus # Type of logs # Transaction logs Request logs Application logs Debug logs Using Expression browser # Metrics: up Gauges: process_resident_memory - metric type gauges . For a gauge, its current absolute value is important from a monitoring point of view Counters: prometheus_tsdb_head_samples_appended_total metric type - counter. Number of samples prometheus has ingested Rate: rate(prometheus_tsdb_head_samples_appended_total[1m]) to compute rate per minute. The rate function automatically handles counters resetting due to processes restarting and samples not being exactly aligned. This can lead to rates on integers returning noninteger results, but the results are correct on average. Debug logs Running the Node Exporter # Exposes kernal and machine level metrics on Unix systems. Provides all standard metrics such as CPU, memory, sick space, disk IP, network bandwidth.

Docker introduction

·768 words·4 mins
Docker for Java Developers # Basic Docker commands # docker images ls to list docker images docker container ls to list containers Containers and Images # Docker CLI # docker info shows basic docker infor about containers/images/volumes, runtimes, kernal version, OS details, etc docker version client and server version for docker api docker --help list of commands available prefer to use management commands like: docker image ls docker container ls Run a container # docker container run jboss/wildfly download wildfly and run it. this will be run in the same terminal docker container -d run jboss/wildfly run container in background (detached mode) docker container stop containername/containerid to stop container docker container rm containername/id to remove the container docker container run -d --name web jboss/wildfly to run a container with desired name docker container rm -f web stop and remove the container. forced docker container run -it --name web jboss/wildfly bash to start a container named web in interactive mode an log in to bash. exit to exit container docker image ls -aq list all image ids. docker image rm -f $(docker image ls -aq) to remove all images. similarly all containers can be removed docker image tag helloworld:1 helloworld:latest to tag version 1 as latest. we need to explicitly tag an image as latest if we want it to be latest. docker image push helloworld:latest to push image to docker hub -> it wont work. docker image tag helloworld:2 ravikrsingh/helloworld:latest then enter credentials to push image to docker hub docker run -d -p 5000:5000 --restart always --name registry registry:2.6.0. docker image tag helloworld:latest localhost:5000/ravikrsingh20/helloworld:latest. docker image push localhost:5000/ravikrsingh20/helloworld Export ports and attach volumes # docker container run -d --name web -P jboss/wildfly publish the port. Maps localhost:32768 to wildfly’s 8080 port docker container logs web shows all the logs of container named web docker container run -d --name web -p 8085:8080 jboss/wildfly maps localhost:8085 to wildfly 8080 port docker container run -d --name web -p 8085:8080 -v /path/to/source/war/file/webapp.war:/opt/jboss/wildfly/standalone/deployments/webapp.war jboss/wildfly take a war file from local machine and mount it as a point in container. Browse to localhost:8085/webapp to see your webapplication docker container logs containername/id -f to tail logs from docker container Creating a docker image # Reference docs for creating docker file docker build image -t helloworld . build a docker image tagged as helloworld using current directory. docker image build can also be used for docker build. docker history helloworld shows how image is built. docker container run helloworld to run the container. Java docker image # Simple Dockerfile for java: