15th August Newsletter
1. Kubernetes tip of the week.
Kubetail
In a production or pre-production environment usually, we have multiple pods of the same microservice. if we want to debug one of the issues in logs generally we stream the logs on the local machine, but sometimes we have multiple pods and for that, we have to open multiple terminals to stream the logs from each pod. this is eventually time-consuming as we have to monitor multiple terminal windows. Kubetail is a bash script that enables you to aggregate (tail/follow) logs from multiple pods into one stream. This is the same as running "kubectl logs -f " but for multiple pods.
Installation
Just download the kubetail file and you're good to go.
Homebrew
You can also install kubetail using brew:
$ brew tap johanhaleby/kubetail && brew install kubetail
It's also possible to install kubetail abbreviated to kt by using the --with-short-names suffix:
$ brew tap johanhaleby/kubetail && brew install kubetail --with-short-names
Usage
First Find the name of your pods
$ kubectl get pods
To tail the logs of the busybox pods in one go simply do:
$ kubetail busybox
To tail only a specific container from multiple pods specify the container like this:
Kubetail Command
$ kubetail app2 -c container1
You can repeat -c to tail multiple specific containers:
Kubetail Command
$ kubetail app2 -c container1 -c container2
To tail multiple applications at the same time separate them by comma:
Kubetail Command
$ kubetail app1,app2
To tail logs within a specific namespace, make sure to append the namespace flag after you have provided values for containers and applications:
Kubetail Command
$ kubetail app2 -c container1 -n namespace1
Supply -h for help and additional options:
Kubetail Command
$ kubetail -h
2. Java Tip of the Week
The try-with-resources Statement
The try-with-resources statement (introduced in java 7) is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.
Try-with-resource
static String readFirstLineFromFile(String path) throws IOException {
try (FileReader fr = new FileReader(path);
BufferedReader br = new BufferedReader(fr)) {
return br.readLine();
}
}
Advantages of using try-with-resources
finally block is not required to close the resource.
try-with-resources can be used with multiple resources
Try-with-resource
public static void main(String[] args) throws IOException{
try (Scanner scanner = new Scanner(new File("testRead.txt"));
PrintWriter writer = new PrintWriter(new File("testWrite.txt"))) {
while (scanner.hasNext()) {
writer.print(scanner.nextLine());
}
}
}
3. Short Course of the Week
Learning Vim
Vim is one of the greatest text editor which makes this life of developer is easy. In unix environment most of the time we connect the servers via SSH, so Vim is important to create or edit the text file. Below course from Linkedin Learning gives a good knowledge how to use Vim.
the infamous “How do I exit Vim?” question on StackOverflow turns 10 years old this week. It has been viewed over 2.7 million times so far. If you want to learn Vim or are stuck in Vim yourself then below is the course that you can pick