Setup Kubernetes Local-machine

Minikube is the recommended method for creating a local, single-node Kubernetes cluster for development and testing. Setup is completely automated and doesn’t require a cloud provider account.

Windows 10

  1. Enable Hyper V in your BIOS and turn on the windows feature ‘Hyper-V’
    1. Also see: Install Hyper-V on Windows 10
  2. Install chocolatey package manager
  3. Install kubectl
1
2
3
4
5
6
choco install kubernetes-cli
kubectl version
cd C:\users\Carl (where Carl is your home dir)
mkdir .kube
cd .kube
New-Item config -type file
  1. Install
    1. minikube
  2. At the time of writing this the latest version was v0.28.0

TBC \ :D /

References

PostgreSQL on a Linux VM

You will need to install Ubuntu on your virtual host, I used VMware Workstation Player and ubuntu-17.10.1-server-amd64.iso

I called my instance ‘Ubuntu PSQL’ and used carl/carl username and password for authentication. Once the VM is running you can install postgresql:

1
2
3
4
5
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install postgresql postgresql-contrib
sudo apt-get install openssh-server
ip addr show

You can then putty to the server, this is easier as you can paste commands.

To check the PostgreSQL server is running you can query it with psql –version

All of my examples below were on version 9.6

1
2
3
4
5
6
7
8
9
10
11
12
#Check the PSQL version
psql --version

#Check status
service --status-all
sudo pg_lsclusters

#Other service commands
sudo service postgresql status
sudo service postgresql restart
sudo service postgresql stop
sudo service postgresql start

Configuration updates

The following configuration updates will be needed, I used nano to edit the files but you can use any other editor such as vi

pg_hba.conf

Set the METHOD to ‘trust’ under ‘# Database administrative login by Unix domain socket’

1
2
3
4
sudo nano /etc/postgresql/9.6/main/pg_hba.conf

# Database administrative login by Unix domain socket
local all postgres trust

You can also restrict access by IP or IP range:

Example:

1
2
192.168.0.254/32 = ip address of 192.168.0.254
192.168.0.1/24 = range of ip's from 192.168.0.1 to 192.168.0.1.255

Also see Subnet mask

Allow all connections:

1
2
3
4
5
-- allow the lot
host all all all md5

-- allow by username
host all USERNAME 0.0.0.0/0 md5

postgresql.conf

Set the the following:

1
2
3
4
5
6
sudo nano /etc/postgresql/9.6/main/postgresql.conf

listen_addresses = '*'
log_statement = 'all'
log_hostname = on
log_line_prefix = '%m [%p] [%r] %q%u@%d '

Update password

You can update the root password

1
2
3
4
5
6
7
8
9
10
#set password
sudo -u postgres psql postgres
\password postgres
[newpassword]
\q
#test user/pass
(Requires pg_hba.conf change to trust)

psql -U postgres -W
[newpassword]

Tail logs

1
2
tail -f /var/log/postgresql/postgresql-9.6-main.log
CRTL C ~ to quit

Backup & Restore

See putty if you need to copy a dump file to the server from your windows PC.

1
2
3
4
5
6
7
8
//Create the dump file
sudo -Su postgres PGCLUSTER="9.6/main" pg_dump -v -Fc productiondbname -f /tmp/production20180503.dump

//Create a new database
sudo -u postgres psql -c "CREATE DATABASE production20180503 OWNER = postgres TABLESPACE pg_default;"

//Restore
sudo -u postgres pg_restore --dbname "production20180503" --verbose /tmp/production20180503.dump

References

NLog

Creates and manages instances of Logger objects.

Works well with .net core. Install from nuget NLog 4.5.11

Configuration

Set the config file NLog.config

1
2
3
4
5
6
7
8
9
10
11
12
13
<targets>
<target name="logfile"
xsi:type="File"
archiveNumbering="Date"
archiveEvery="Day"
maxArchiveFiles="50"
bufferSize="102400"
fileName="appname.log" />
</targets>

<rules>
<logger name="*" minlevel="Debug" writeTo="logfile" />
</rules>

Example use

1
2
3
4
5
6
7
var logger = NLog.LogManager.GetCurrentClassLogger();

logger.Debug("Some debug");
logger.Debug("Some other debug");
logger.Info("Some info.");

~ this will then create `appname.log` in your executing bin directory.

References

Simple text logging for a C# console application

Simple logging to text file into a directory called ‘logs’ inside the directory of the executing image.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
private static object objLock = new object();

public static void LogInfo(string d)
{
var sb = new StringBuilder();
sb.AppendFormat("INFO : {0} {1}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"), Environment.NewLine);
sb.Append(d);
Log(sb.ToString());
}

public static void LogError(Exception ex)
{
var sb = new StringBuilder();
sb.AppendFormat("EXCEPTION : {0} {1}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss"), Environment.NewLine);
sb.AppendFormat("{0}{1}", ex.Message, Environment.NewLine);
sb.AppendFormat("{0}{1}", ex.StackTrace, Environment.NewLine);
Log(sb.ToString());
}

static void Log(string d)
{
Console.WriteLine(d);
var _logsDir = "logs";

if (!Directory.Exists(_logsDir))
Directory.CreateDirectory(_logsDir);

lock (objLock)
{
var path = Path.Combine("logs", LogFileName());
using (StreamWriter sw = new StreamWriter(path, true))
{
sw.WriteLine(d);
sw.WriteLine(" ");
sw.Close();
}
}
}

static string LogFileName()
{
return DateTime.Now.ToString("ddMMyyyy") + ".log";
}