<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Realm of Chaos - kubernetes</title>
    <subtitle>Welcome to my notebook! I’m a Site Reliability Engineer who is curious, loves to learn and discover the nature of things.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://realmofchaos.xyz/tags/kubernetes/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://realmofchaos.xyz"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2020-12-02T00:00:00+00:00</updated>
    <id>https://realmofchaos.xyz/tags/kubernetes/atom.xml</id>
    <entry xml:lang="en">
        <title>WordPress Charm for k8s</title>
        <published>2020-12-02T00:00:00+00:00</published>
        <updated>2020-12-02T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Thomas Cuthbert
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://realmofchaos.xyz/tech/wordpress-k8s-charm/"/>
        <id>https://realmofchaos.xyz/tech/wordpress-k8s-charm/</id>
        
        <content type="html" xml:base="https://realmofchaos.xyz/tech/wordpress-k8s-charm/">&lt;p&gt;Canonical uses the WordPress blogging system for all our company blogs. Earlier this year I was tasked with updating our WordPress charm from a Services Framework Juju Charm on OpenStack to a Kubernetes based Operator Framework Charm.&lt;&#x2F;p&gt;
&lt;p&gt;The WordPress Operator Charm is simple by design, the goal of the charm is to just provide the various configuration options for our WordPress Kubernetes image, which does all the heavy lifting. See the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;git.launchpad.net&#x2F;charm-k8s-wordpress&#x2F;tree&#x2F;config.yaml&quot;&gt;config.yaml&lt;&#x2F;a&gt; file for details on what is supported, one option that can be useful during testing is &lt;code&gt;container_config&lt;&#x2F;code&gt;, which gives you the ability to pass through custom Kubernetes spec environment variables. For example, to enable debug level logging and ensuring you always have the latest image, you would set:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;microk8s.juju config wordpress container_config=&amp;#39;WORDPRESS_DEBUG: &amp;quot;1&amp;quot; imagePullPolicy: &amp;quot;always&amp;quot;&amp;#39;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The image build process downloads the latest WordPress codebase and installs it behind an Apache web server, it then downloads the plugins all of our blogs depend on, such as, akismet anti-spam support, SSO with the openid teams plugin, and a variety of Canonical Open Source themes. By default the charm will use the current stable build here, however if you wish to customise the image you can fork the code and update the &lt;code&gt;image&lt;&#x2F;code&gt; charm config option to point to the location of your custom image.&lt;&#x2F;p&gt;
&lt;p&gt;To get started with the Operator Framework WordPress charm you will need a MySQL database running locally. As I write this post there is no Kubernetes MySQL charm, so deploy one to an IaaS model with &lt;code&gt;juju deploy cs:mysql&lt;&#x2F;code&gt;. Initialise the database as follows:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;CREATE DATABASE wordpress CHARACTER SET utf8 COLLATE utf8_unicode_ci;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;CREATE USER &amp;#39;wordpress&amp;#39;@&amp;#39;%&amp;#39; IDENTIFIED BY &amp;#39;wordpress&amp;#39;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;GRANT ALL PRIVILEGES ON wordpress.* TO &amp;#39;wordpress&amp;#39;@&amp;#39;%&amp;#39;;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;FLUSH PRIVILEGES;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Once the database is prepared we are now able to deploy the WordPress charm. The easiest way to get started with a local Kubernetes cluster is to have MicroK8s installed, reference the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;git.launchpad.net&#x2F;charm-k8s-wordpress&#x2F;tree&#x2F;README.md&quot;&gt;README&lt;&#x2F;a&gt; of the charm for details on how to get one setup. Deploy the charm as follows.&lt;&#x2F;p&gt;
&lt;p&gt;Deploy the charm into your Kubernetes Juju model.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;microk8s.juju deploy cs:~wordpress-charmers&#x2F;wordpress&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;The charm requires Kubernetes TLS secrets to be pre-configured to ensure logins are kept secure. Create a self-signed certificate and upload it as a Kubernetes secret.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout server.key -out server.crt&lt;&#x2F;code&gt; &lt;code&gt;microk8s.kubectl create secret tls -n wordpress tls-wordpress --cert=server.crt --key=server.key&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Tell the charm where the database is and provide some initial setup.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;DB_HOST=$IP_OF_YOUR_MYSQL_DATABASE&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;microk8s.juju config wordpress db_host=$DB_HOST db_user=wordpress db_password=wordpress tls_secret_name=tls-wordpress \&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            initial_settings=&amp;quot;user_name: admin&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            admin_email: devnull@example.com&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            weblog_title: Test Blog&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            blog_public: False&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;From there you can test the site by updating your &lt;code&gt;&#x2F;etc&#x2F;hosts&lt;&#x2F;code&gt; file and creating a static entry for the IP address of the Kubernetes ingress gateway.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #F8F8F2; background-color: #272822;&quot;&gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    App        Version                  Status   Scale  Charm      Store  Rev  OS          Address  &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Messagewordpress  wordpress:bionic-stable  waiting      1  wordpress  local    0  kubernetes  10.152.183.140 &lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;echo &#x27;10.152.183.140 myblog.example.com&#x27; | sudo tee -a &#x2F;etc&#x2F;hosts&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;It will take about 5 to 10 minutes for Juju hooks to discover the site is live and perform the initial setup for you. Look for this line in the output of &lt;code&gt;juju debug-log&lt;&#x2F;code&gt; to confirm.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;unit.wordpress&#x2F;0.juju-log Wordpress configured and initialised&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;This is due to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;github.com&#x2F;canonical&#x2F;operator&#x2F;issues&#x2F;166&quot;&gt;issue #166&lt;&#x2F;a&gt; and will be fixed once Juju supports a Kubernetes pod ready hook.&lt;&#x2F;p&gt;
&lt;p&gt;To retrieve the random admin password, run the following.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;microk8s kubectl exec -ti -n wordpress wordpress-operator-0 -- cat &#x2F;root&#x2F;initial.passwd&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;You should now be able to browse to &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;myblog.example.com&#x2F;wp-admin&quot;&gt;https:&#x2F;&#x2F;myblog.example.com&#x2F;wp-admin&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;We’ve been using this charm in production for five months now, but recently updated it to bring it up to date with a current version of the Operator Framework. We’d be interested in any feedback on the charm itself, either here or via bugs against the &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;bugs.launchpad.net&#x2F;charm-k8s-wordpress&quot;&gt;charm project on Launchpad&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
