<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Christopher Jansson</title>
    <description>The latest articles on DEV Community by Christopher Jansson (@stoffe).</description>
    <link>https://dev.to/stoffe</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F736123%2F2ffca9c5-7269-4729-b15b-ea3ceaf28ba8.jpg</url>
      <title>DEV Community: Christopher Jansson</title>
      <link>https://dev.to/stoffe</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stoffe"/>
    <language>en</language>
    <item>
      <title>Optimizely on Kubernetes</title>
      <dc:creator>Christopher Jansson</dc:creator>
      <pubDate>Mon, 25 Oct 2021 12:55:16 +0000</pubDate>
      <link>https://dev.to/stoffe/optimizely-on-kubernetes-4k9n</link>
      <guid>https://dev.to/stoffe/optimizely-on-kubernetes-4k9n</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This article shows you how to set up the Optimizely CMS sample site using Kubernetes. The article also covers topics such as scaling and load balancing, and some gotchas found when installing.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The article is based on Google Cloud for hosting the database &lt;br&gt;
and provisioning a Kubernetes &lt;br&gt;
cluster, but you can of course use Azure or an on-premise&lt;br&gt;
installation if it suits you better.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Pre-requisites
&lt;/h2&gt;

&lt;p&gt;This assumes you has the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kubernetes Cluster (In this article i use Google Kubernetes Engine)&lt;/li&gt;
&lt;li&gt;Azure Servicebus (Standard or Premium tier)&lt;/li&gt;
&lt;li&gt;Azure Storage Account&lt;/li&gt;
&lt;li&gt;SQL Server (2012 or later)&lt;/li&gt;
&lt;li&gt;Linux or WSL2&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Getting the sample site
&lt;/h2&gt;

&lt;p&gt;The sample site used in this article is &lt;a href="https://github.com/episerver/netcore-preview" rel="noopener noreferrer"&gt;found here&lt;/a&gt;.&lt;br&gt;
Start by cloning the repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone git@github.com:episerver/netcore-preview.git
cd netcore-preview
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The netcore-preview repository contains 2 sample web sites: Quicksilver and Alloy. This article focus on running the Alloy web site. To speed up building the solution, remove the Quicksilver project by running the following command in the same directory as the solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet sln remove  Quicksilver/EPiServer.Reference.Commerce.Site/EPiServer.Reference.Commerce.Site.csproj
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating the database
&lt;/h2&gt;

&lt;p&gt;To create a new Optimizely database we first need to install the new EPiserver CLI using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dotnet tool install EPiServer.Net.Cli --global --add-source https://pkgs.dev.azure.com/EpiserverEngineering/netCore/_packaging/beta-program/nuget/v3/index.json --version 1.0.0-pre-020034
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the CLI is done installing you can run the following command to create the Optimizely CMS database:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dotnet-episerver create-cms-database Alloy/AlloyMvcTemplates.csproj -S {SQL SERVER ADDRESS} -U {USER} -P {PASSWORD}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When the installer is done, the EPiServerDB connectionstring is appended to the appSettings.json &lt;br&gt;
configuration file located in the root of the project.&lt;/p&gt;

&lt;p&gt;To create an admin user run the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;dotnet-episerver add-admin-user Alloy/AlloyMvcTemplates.csproj -u username -p password -e user@email.com -c EPiServerDB&lt;/code&gt; &lt;/p&gt;
&lt;h2&gt;
  
  
  Configuring the website to run multiple instances
&lt;/h2&gt;

&lt;p&gt;There are some things we must do in order to make the site able to run multiple instances&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configuring EventProvider&lt;/li&gt;
&lt;li&gt;Configuring BlobStorage &lt;/li&gt;
&lt;li&gt;Adding Dataprotection&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Adding Azure EventProvider &amp;amp; BlobProvider
&lt;/h3&gt;

&lt;p&gt;In a loadbalanced website events must be distributed to all instances, this is carried out by the EventProvider. &lt;br&gt;
In this article I will use Azure Servicebus to distribute the events. Optimizely provides a implementation for replicating events, AzureEventProvider found in the EPiServer.Azure NuGet package.&lt;/p&gt;

&lt;p&gt;However we cannot use AzureEventProvider directly when we run on top of Kubernetes since Servicebus SubscriptionNames has a limitation of 50 characters and AzureEventProvider sets SubscriptionName using the following code snippet: &lt;code&gt;MachineName + Guid.NewGuid().ToString("N")&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;When running a Kubernetes Deployment, pod names get a hash suffix. This name is used as the machine name, so when using AzureEventProvider the SubscriptionName would be: podname + hash + 32 character guid which is to many characters due to the 50 character restriction in &lt;a href="https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-quotas" rel="noopener noreferrer"&gt;Azure Service Bus&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To circumvent this, we create a custom implementation which inherits AzureEventProvider and uses System.Reflection magic to set SubscriptionName. The implementation can be found &lt;a href="https://github.com/crikke/epi-ha-k8s-article/blob/master/CustomAzureEventProvider.cs" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Install the required NuGet package by running the following command:&lt;br&gt;
&lt;code&gt;dotnet add package EPiServer.Azure&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When the NuGet package(s) has been installed you need to add the following code shown below:&lt;br&gt;
Register services in Startup.cs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void ConfigureServices(IServiceCollection services)
{
  ...
        // register blob storage
        services.AddAzureBlobProvider(o =&amp;gt; {
            o.ConnectionString = Environment.GetEnvironmentVariable("BLOBSTORAGE_CONNECTIONSTRING");
        });

        // register event provider
        services.AddEventProvider&amp;lt;CustomAzureEventProvider&amp;gt;();
        services.Configure&amp;lt;CustomAzureEventProviderOptions&amp;gt;(o =&amp;gt; {
            o.ConnectionString = Environment.GetEnvironmentVariable("SERVICEBUS_CONNECTIONSTRING");
            o.TopicName = "siteevents";
            o.SubscriptionName =  Guid.NewGuid().ToString("N");
        });

  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring Dataprotection
&lt;/h3&gt;

&lt;p&gt;In order to encrypt/decrypt and validate view-state, a shared key on each instance of our application needs to be used. This was previously done in ASP.NET Framework by setting the &lt;code&gt;&amp;lt;machineKey&amp;gt;&lt;/code&gt; element under &lt;code&gt;&amp;lt;system.web&amp;gt;&lt;/code&gt;. In ASP.NET Core, this has been replaced by DataProtection.&lt;/p&gt;

&lt;p&gt;To distribute and persist our key to each instance we will use Azure Blobstorage.&lt;br&gt;
Note that in production, you probably want to encrypt the keys at rest aswell. You can read more about this &lt;a href="https://docs.microsoft.com/en-us/aspnet/core/security/data-protection/configuration/overview?view=aspnetcore-5.0" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Install required NuGet by running the following command: &lt;code&gt;dotnet add package Azure.Extensions.AspNetCore.DataProtection.Blobs&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Add the code to Startup.cs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void ConfigureServices(IServiceCollection services)
{
  ...
  services
    .AddDataProtection()
    .PersistKeysToAzureBlobStorage(Environment.GetEnvironmentVariable("BLOBSTORAGE_CONNECTIONSTRING"), "keys", "epidemo");

  ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring License
&lt;/h3&gt;

&lt;p&gt;Getting a developer license for Optimizely can be done &lt;a href="https://license.episerver.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqwsmt8hd2wkbziuit4l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuqwsmt8hd2wkbziuit4l.png" alt="License"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Instead of bundling container specfic configuration with our application, a better way is to mount configuration either as files in an attached volume or as environment variables.&lt;br&gt;
By default, Optimizely checks for the Licence.config file in the app root directory. We need to configure the application to check for the license in another directory where the license will be mounted. &lt;br&gt;
To configure this add the following to Startup.cs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public void ConfigureServices(IServiceCollection services)
{
    ...

    services.Configure&amp;lt;Licensing.LicensingOptions&amp;gt;(o =&amp;gt; {
        o.LicenseFilePath = "/etc/app/License.config";
    });

    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Dockerizing the project
&lt;/h2&gt;

&lt;p&gt;The final thing we need to do in order to run the site on Kubernetes is to dockerize the application. &lt;/p&gt;

&lt;p&gt;Create a new Dockerfile in the root directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM mcr.microsoft.com/dotnet/sdk:5.0-buster-slim AS build-env
WORKDIR /app

COPY NuGet.config .
COPY dependencies.props .
COPY ./Alloy/AlloyMvcTemplates.csproj   ./Alloy/
COPY *.sln .
RUN dotnet restore

COPY . .
RUN dotnet publish -c Release -o out

FROM mcr.microsoft.com/dotnet/aspnet:5.0-buster-slim
WORKDIR /app
COPY --from=build-env /app/out .

EXPOSE 80

ENTRYPOINT ["dotnet", "AlloyMvcTemplates.dll"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Deploying the site on Kubernetes
&lt;/h2&gt;

&lt;p&gt;Since our docker image does not contain any ConnectionStrings or Licensefile we need to inject them into the container. This is done by using Kubernetes Secrets &amp;amp; ConfigMap. &lt;br&gt;
By separating configuration and application the configuration could be reused acros multiple applications. &lt;/p&gt;
&lt;h3&gt;
  
  
  Creating Connection string Secret
&lt;/h3&gt;

&lt;p&gt;Create a new secret containg our connectionstrings by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create secret generic epi-connectionstring \ 
--from-literal=eventprovider='{EVENT_PROVIDER_CONNECTION_STRING}' \
--from-literal=blobstorage='{BLOB_STORAGE_CONNECTION_STRING}' \
--from-literal=database='{DB_CONNECTION_STRING}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating a License ConfigMap
&lt;/h3&gt;

&lt;p&gt;Create a new configmap by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create configmap epi-demo-license --from-file=License.config=PATH_TO_License.config 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating Deployment Manifest
&lt;/h3&gt;

&lt;p&gt;Deployments are used to run a specified number of replicas at any given time. &lt;/p&gt;

&lt;p&gt;Create a new deployment by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f https://raw.githubusercontent.com/crikke/epi-ha-k8s-article/master/deployment.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run &lt;code&gt;kubectl get deployments epi-k8s&lt;/code&gt; to verify that the deploy is created&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME      READY   UP-TO-DATE   AVAILABLE   AGE
epi-k8s   2/2     2            2           3h11m
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Loadbalancing and making the application reachable outside the cluster
&lt;/h3&gt;

&lt;p&gt;Currently, the application is only reachable within the cluster. In order to expose it to the internet, a Service is needed which routes incomming traffic to pods. Selecting which pod a service should route traffic to is done by using matching Pod labels.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Labels are key/value pairs that are attached to objects, such as pods. Labels are intended to be used to specify &lt;br&gt;
identifying attributes of objects that are meaningful and relevant to users, but do not directly imply semantics to the&lt;br&gt;
core system. Labels can be used to organize and to select subsets of objects.&lt;/p&gt;

&lt;p&gt;Read more about labels at Kubernetes official documentation &lt;a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Create a new service by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl apply -f https://github.com/crikke/epi-ha-k8s-article/blob/master/service.yml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify that the service is created by running &lt;code&gt;kubectl get service epi-k8s-demo&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You should get similar output as the example shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME           TYPE           CLUSTER-IP   EXTERNAL-IP    PORT(S)                      AGE
epi-k8s-demo   LoadBalancer   10.44.7.49   34.88.54.254   80:30080/TCP,443:30443/TCP   155m
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And thats it, you now have Optimizely CMS running in a Kubernetes cluster!&lt;/p&gt;

&lt;p&gt;Your application should now be reachable by the &lt;code&gt;EXTERNAL-IP&lt;/code&gt; field from your service. Note that if it´s &lt;code&gt;&amp;lt;PENDING&amp;gt;&lt;/code&gt;, wait some minutes and check again. &lt;/p&gt;

&lt;p&gt;If you visit the site through &lt;code&gt;EXTERNAL-IP&lt;/code&gt;, you could get an 404 error. This is because you first need to add a new site inside the Optimizely Admin UI. Visit EXTERNAL-IP/episerver/cms and go to Admin &amp;gt; Configuration &amp;gt; Manage Sites &amp;gt; Add Site&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;The Optimizely Devteam has done massive work of migrating Optimizely from .NET Framework and all underlying dependencies. Not only does this open many possibilities on hosting your application, but brings performance boosts aswell.&lt;/p&gt;

&lt;p&gt;I hope this post helped you get some insight how to run Optimizely CMS on Kubernetes. &lt;br&gt;
Happy coding!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
