Kubernetes Tips: ConfigMap

This is how to update a config map with 1 line:

kubectl create configmap foo --from-file foo.properties -o yaml --dry-run | kubectl replace -f -

I found it here: https://stackoverflow.com/questions/38216278/update-k8s-configmap-or-secret-without-deleting-the-existing-one

And this is how to mount a config map created from a file as file(not super intuitive but a config map can only be mounted as a volume, and the mount point has to be a directory):

containers:
- volumeMounts:
 - name: demo-config
 mountPath: /app/settings.json
 subPath: settings.json
volumes:
- name: demo-config
 configMap:
 name: demo

I think this is because when creating a config map from a file, the file name becomes the key and the content becomes the value. A config map can have multiple key-value pairs just like a directory can have more than 1 file. So by using the subPath the key will be mounted as a file.

Found it here: https://github.com/kubernetes/kubernetes/issues/44815#issuecomment-297077509

🙂

Gotcha AWS NAT instance

It’s quite straight forward when creating an NAT instance for a private subnet in AWS, eg. search for amzn-ami-vpc-nat-hvm for the AMI then launch it into a public subnet.

However I need to disable source/destination check before the NAT instance becomes available in the drop down list of destinations of a route table:

🙂

Mining With Solar Power

Here’s a Python script I wrote to check today’s sun rise/set times given a coordinate:

https://github.com/raynix/solar-hours/blob/master/solar_hours.py

It uses api.sunrise-sunset.org free API to get local sun rise/set time of today. It also takes time zone into consideration as the API only returns times in UTC time zone. It also cache the API result for a day so I’m not abusing the free API.

I have solar power at home however the invertor isn’t API friendly, otherwise it will be best to get solar power output directly from the invertor.

Using this python script I can turn on my Zcash miner when there’s solar power. This saves me money, more importantly the ZEC I mined is carbon free. Here’s a sample bash script to turn on/off the miner according to sun rise/set:

#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
MINER=<miner's IP>
pushd $DIR
python solar-hours/solar_hours.py
# 0 means solar on, 1 means off
if [ $? -eq 0 ]; then
 ping $MINER -c 1 || wakeonlan <miner's MAC address>
else
 ssh $MINER sleep 1 && ssh -t $MINER 'sudo shutdown -h now'
fi
popd

If you want to try this too please read the Wake On LAN part I’ve done a while ago.

🙂