Script for creating EBS persistent volumes in OpenShift/Kubernetes

If you aren't using the automated dynamic volume provisioning (which you should!). Here is a short bash script to help you automatically create both the EBS volume and Kubernetes persistent volume:

#!/bin/bash

if [ $# -ne 2 ]; then  
    echo "Usage: sh create-volumes.sh SIZE COUNT"
    exit
fi

for i in `seq 1 $2`; do  
  size=$1
  vol=$(ec2-create-volume --size $size --region ap-southeast-2 --availability-zone ap-southeast-2a --type gp2 --encrypted | awk '{print $2}')
  size+="Gi"

  echo "
  apiVersion: v1
  kind: PersistentVolume
  metadata:
    labels:
      failure-domain.beta.kubernetes.io/region: ap-southeast-2
      failure-domain.beta.kubernetes.io/zone: ap-southeast-2a
    name: pv-$vol
  spec:
    capacity:
      storage: $size
    accessModes:
      - ReadWriteOnce
    awsElasticBlockStore:
      fsType: ext4
      volumeID: aws://ap-southeast-2a/$vol
    persistentVolumeReclaimPolicy: Delete" | oc create -f -
done  

WordPress editor missing when using CloudFront

We often put CloudFront in front of our WordPress sites to increase the load times of the website significantly.

CloudFront and WordPress have a few quirks, the main one will be the missing rich post/page editor that suddenly goes missing from your wp-admin.

The issue comes down to the UA sniffing that WordPress does.

Adding this into your functions.php will be a good quick fix for you

/**
* Ignore UA Sniffing and override the user_can_richedit function
* and just check the user preferences
*
* @return bool
*/
function user_can_richedit_override() {  
    global $wp_rich_edit;

    if (get_user_option('rich_editing') == 'true' || !is_user_logged_in()) {
        $wp_rich_edit = true;
        return true;
    }

    $wp_rich_edit = false;
    return false;
}

add_filter('user_can_richedit', 'user_can_richedit_override');  

Quickly build your own CentOS 6 & 7 AMI

Following on from my previous post Roll your own CentOS 6.5 HVM AMI in less than 15 minutes here's the snippet to for booting into an automated kickstart install for building your new AMI.

This also works for other VM providers such as Azure.

Don't forget to replace the version and appropriate snippets with your own.

version=<%= @osver %>  
mirror=http://mirror.centos.org/centos/

# Detect primary root drive
if [ -e /dev/xvda ]; then  
  drive=xvda
elif [ -e /dev/vda ]; then  
  drive=vda
elif [ -e /dev/sda ]; then  
  drive=sda
fi

yum -y install wget  
mkdir /boot/centos  
cd /boot/centos  
wget ${mirror}/${version}/os/x86_64/isolinux/vmlinuz  
wget ${mirror}/${version}/os/x86_64/isolinux/initrd.img

cat > /boot/centos/kickstart.ks << EOL  
<%= snippet 'centos_kickstart' %>

%post
<%= snippet 'centos_post' %>  
<%= snippet 'centos_post_finish' %>  
%end
EOL

if [ ${version} == 6 ]; then  
  echo "
  default         0
  timeout         0
  hiddenmenu

  title CentOS 6 Installation
          root (hd0,0)
          kernel /boot/centos/vmlinuz ip=dhcp ksdevice=eth0 ks=hd:${drive}1:/boot/centos/kickstart.ks method=${mirror}/${version}/os/x86_64/ lang=en_US keymap=us
  initrd /boot/centos/initrd.img " > /boot/grub/grub.conf
else  
  echo "menuentry 'centosinstall' {
          set root='hd0,msdos1'
      linux /boot/centos/vmlinuz ip=dhcp ksdevice=eth0 ks=hd:${drive}1:/boot/centos/kickstart.ks method=${mirror}/${version}/os/x86_64/ lang=en_US keymap=us
          initrd /boot/centos/initrd.img
  }" >> /etc/grub.d/40_custom

  echo 'GRUB_DEFAULT=saved
  GRUB_HIDDEN_TIMEOUT=
  GRUB_TIMEOUT=2
  GRUB_RECORDFAIL_TIMEOUT=5
  GRUB_CMDLINE_LINUX_DEFAULT="quiet nosplash vga=771 nomodeset"
  GRUB_DISABLE_LINUX_UUID=true' > /etc/default/grub

  grub2-set-default 'centosinstall'
  grub2-mkconfig -o /boot/grub2/grub.cfg
fi  
reboot  

Cleaning Up Docker Host

Sometimes testing docker images consumes a lot of diskspace depending on what you're using, if you're like me and don't have a brand new 1TB SSD then we can't afford to have diskspace wasted.

Here's a quick 3 line command to:
1. Stop all docker contaienrs
2. Remove all docker containers
3. Remove all docker images

docker stop $(docker ps -a -q)  
docker rm $(docker ps -a -q)  
docker rmi $(docker images -q)  

Now we can start fresh and try or build new docker containers. Also note, if you created external volume mount folders these commands won't remove them.

Error response from daemon: Cannot start container (....) (exit status 1)

This is a fairly generic error message, but it may help some that their iptable rules may be conflicting.

Stop using lokkit!

If you use lokkit for your iptable rules, you'll overwrite the docker rules so you'll have to service docker restart and try again.

Now the docker firewall rules should be loaded and you can start your docker container!

FATA[0000] Error response from daemon: Cannot start container a788e23879a4257918008b62bd6bfdaceb69cb6364180d1259c1348df0a4bd91: failed to find the cgroup root

Today after a reboot on a fresh CentOS 6.6 docker host, the containers were failing to startup with the error message:

FATA[0000] Error response from daemon: Cannot start container xxxxxxx: failed to find the cgroup root

Fix seems simple enough, the cgroup services aren't running:

[[email protected] ~]# /etc/init.d/cgred status
cgred is stopped  
[[email protected] ~]# /etc/init.d/cgred start
Starting CGroup Rules Engine Daemon:                       [  OK  ]  
[[email protected] ~]# /etc/init.d/cgconfig status
Stopped  
[[email protected] ~]# /etc/init.d/cgconfig start
Starting cgconfig service:                                 [  OK  ]

[[email protected] ~]# /etc/init.d/docker restart
Stopping docker:                                           [  OK  ]  
Starting docker:                                       [  OK  ]

[[email protected] ~]# docker run ....

Now run your containers and you should be all good to go :)

Hope this may help someone who stumbles across this. Remember to enable the services so it doesn't happen on next reboot!

chkconfig cgconfig on  
chkconfig cgred on  

Docker is fun :)