Ktor 2.3.12 Help

Deployment

In this topic, we'll make an overview of how to deploy a Ktor application.

Ktor deployment specifics

The deployment process for a server Ktor application depends on the following specifics:

  • Whether you are going to deploy your application as a self-contained package or inside a servlet container.

  • Which approach do you use to create and configure a server.

Self-contained app vs Servlet container

Ktor allows you to create and start a server with the desired network engine (such as Netty, Jetty, or Tomcat) right in the application. In this case, an engine is a part of your application. Your application has control over engine settings, connection, and SSL options. To deploy your application, you can package it as a fat JAR or an executable JVM application.

In contrast to the approach above, a servlet container should control the application lifecycle and connection settings. Ktor provides a special ServletApplicationEngine engine that delegates control over your application to a servlet container. To deploy inside a servlet container, you need to generate a WAR archive.

Configuration: code vs configuration file

Configuring a self-contained Ktor application for deployment might depend on the approach used to create and configure a server: in code or by using a configuration file. As an example, a hosting provider may require specifying a port used to listen for incoming requests. In this case, you need to configure a port either in code or in the application.conf/application.yaml.

Packaging

Before deploying your application, you need to package it in one of the following ways:

  • Fat JAR

    A fat JAR is an executable JAR that includes all code dependencies. You can deploy it to any cloud service that supports fat JARs. A fat JAR is also required if you need to generate a native binary for GraalVM. To create a fat JAR, you can use the Ktor plugin for Gradle or the Assembly plugin for Maven.

  • Executable JVM application

    An executable JVM application is a packaged application that includes code dependencies and generated start scripts. For Gradle, you can use the Application plugin to generate an application.

  • WAR

    A WAR archive lets you deploy your application inside a servlet container, such as Tomcat or Jetty.

  • GraalVM

    Ktor server applications can make use of GraalVM in order to have native images for different platforms.

Containerizing

After you package your application (for example, to an executable JVM application or a fat JAR), you can prepare a Docker image with this application. This image can then be used to run your application on Kubernetes, Swarm, or a required cloud service container instance.

Publishing

Tutorials below show how to deploy a Ktor application to specific cloud providers:

SSL

If your Ktor server is placed behind a reverse proxy (such as Nginx or Apache) or runs inside a servlet container (Tomcat or Jetty), SSL settings are managed by a reverse proxy or a servlet container. If required, you can configure Ktor to serve SSL directly by using Java KeyStore.

Last modified: 02 April 2024