Gregg's MOTD

Tips & Tricks that I've Encountered Over the Years...

Kubernetes Cheatsheet

September 28, 2023 — Gregg Szumowski

The get parameter is a powerful way of discovering your kubenetes resources. You can use it to query: * namespace * pod * node * deployment * service * replicasets

$ kubectl get nodes
$ kubectl get ns # ns is an abreviation for namespace
$ kubectl get pods -n kube-system

The create command can do just that for:

  • service
  • cronjob
  • deployment
  • job
  • namespace (or ns)

$ kubectl create ns hello-world
$ kubectl create cronjob my-cronjob --image=alpine --schedule="*/15 * * * *" -- echo "hi there"

You can also use cj as an abreviation for cronjob

$ kubectl create cj my-cronjob --image=alpine --schedule="*/15 * * * *" -- echo "hi there"

The edit parameter allows you to update resources:

$ kubectr edit my-cronjob

The delete parameter allows you to remove resources:

$ kubectl delete cronjob my-cronjob

The apply parameter allows you to apply configurations from files

$ kubectl apply -f jenkins.yaml

The describe parameter provides details of your resources which could be:

  • nodes
  • pods
  • services
  • deployments
  • replicasets
  • cronjobs

$ kubectl describe cronjob my-cronjob

The logs parameter displays the contents of the resource’s log:

$ kubectl logs my-resource -n charts

The exec parameter allows you to exec into a container:

$ kubectl exec -it my-resource -n charts -- /bin/bash

The cp parameter lets you copy files and directories to and from containers:

$ kubectl cp file1.txt my-resource:file1.txt

Tags: cli, kubernetes, cheatsheet, motd

Extract a Single File from a Tarball

September 27, 2023 — Gregg Szumowski

Suppose I have a tarball (.tar.gz file) which is large and I only want to extract a specific file from it. If I know the name of the file all I have to do is pass the file’s relative path that it is stored under to the command line.

Here is an example of the error you will get if you pass the incorrect file specification:

$ tar zxvf dirtree-tarball.tar.gz file-7-30003.txt
tar: file-7-30003.txt: Not found in archive

Since I don’t have the full path, I can just search for it:

$ tar tf dirtree-tarball.tar.gz | grep 'file-7-30003.txt'
./dir_2/file-7-30003.txt

Now I can pass the full path and extract the file:

$ tar zxvf dirtree-tarball.tar.gz ./dir_2/file-7-30003.txt
./dir_2/file-7-30003.txt
$ ls
dir_2  dirtree-tarball.tar.gz
$ tree
.
├── dir_2
│   └── file-7-30003.txt
└── dirtree-tarball.tar.gz

1 directory, 2 files

Note that it extracts it to the same directory tree but it will only extract the file(s) specified on the command line.

Tags: cli, tar, motd

Updating My Home Lab using Ansible

September 26, 2023 — Gregg Szumowski

I have a variety of Raspberry Pis that I use for various tasks like my Tiny-Tiny RSS server, Gitea server, and Calibre server among other things. In order to keep them updated I use Ansible.

My update script is fairly simple:

$ cat update-pis.sh 
#!/bin/bash
ansible-playbook ./playbooks/apt.yml --user memyselfandi \
     --ask-pass --ask-become-pass -i ./inventory/hosts $@

The YAML playbook is likewise very simple:

$ cat ./playbooks/apt.yml 
- hosts: "*"
  become: yes
  tasks:
  - name: Update System Package Cache (apt)
    apt:
      update_cache: yes
      upgrade: 'yes'
 
  - name: Upgrade System Packages (apt)
    apt: upgrade=full

  - name: Remove unused dependencies
    apt: autoremove=true

  - name: Check if reboot is required
    shell: "[ -f /var/run/reboot-required ]"
    failed_when: False
    register: reboot_required
    changed_when: reboot_required.rc == 0
    notify: reboot

  handlers:
  - name: reboot
    command: /sbin/reboot

Although I can run this in a cronjob, I tend to run it manually (for now). I’m thinking about doing some major revisions to my Pi configuration anyway. Stay tuned for more on that subject.

Tags: cli, ansible, motd

List All MACs on the Local Network

September 25, 2023 — Gregg Szumowski

Per the manpage:

The arp tool manipulates or displays the kernel’s IPv4 network neighbor cache. It can add entries to the table, delete one or display the current content. ARP stands for Address Resolution Protocol, which is used to find the media access control address of a network neighbor for a given IPv4 Address.

You can use the arp command to list all of the devices on the local network. It is often useful to find the identities of hidden devices on your network. For example, if you just plugged a Raspberry Pi into your local network and need to find its IP address in order to connect to it via SSH.

# arp -i eth0 -a
? (192.168.1.90) at 42:b9:72:xx:xx:x0 [ether] on eth0
? (192.168.1.70) at 54:04:a6:xx:xx:xd [ether] on eth0
Fios_Quantum_Gateway.fios-router.home (192.168.1.1) at 20:c0:47:xx:xx:x1 [ether] on eth0
? (192.168.1.99) at 34:64:a9:xx:xx:xd [ether] on eth0
? (192.168.1.60) at dc:a6:32:xx:xx:x3 [ether] on eth0

(I’ve masked some of the fields above)

Tags: cli, arp, networking, motd

KVM: Configure libvirt Network

September 24, 2023 — Gregg Szumowski

You can update the network configuration for your KVM installation using the command line using the virsh command.

To list all of the available network enter the following command. The --all will is used to include the inactive networks:

# virsh net-list --all
Name State Autostart
-----------------------------------------
default active yes
NattedNetwork active yes

Then, edit the network you wish to update:

# EDITOR="vi" virsh net-edit NattedNetwork

Add host configuration(s) or whatever changes you wish to make to XML file:

<network>
<name>NattedNetwork</name>
<uuid>8483028d-667b-47e7-9a8e-f269783a8246</uuid>
<forward mode='nat'/>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:ad:b9:ed'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>

Once you’re done, restart the network for the changes to take effect:

# virsh net-destroy NattedNetwork
# virsh net-start NattedNetwork

Tags: cli, libvirt, kvm, network, motd

Install sbopkg on Slackware

September 23, 2023 — Gregg Szumowski

Sbopkg is a command‐line and dialog‐based tool to interact with the SlackBuilds.org repository, a collection of third‐party SlackBuild scripts to build Slackware packages.

Here are the steps for installing the latest version of sbopkg with Slackware.

  1. Download the latest Slackware package from https://www.sbopkg.org/downloads.php using whatever method you find most convenient. At the time of this writing the latest version is https://github.com/sbopkg/sbopkg/releases/download/0.38.2/sbopkg-0.38.2-noarch-1_wsr.tgz. I will use that version in the commands below. If the version has changed, you would use the new filename in the commands. You can also use wget <packagename> to download it directly.

  2. Open your favorite terminal and su to root.

  3. Change to the directory where you downloaded the package.

  4. Run the command installpkg sbopkg-0.38.2-noarch-1_wsr.tgz

If all goes well, sbopkg will now be installed.

To run sbopkg, open your favorite terminal, su - to root, and type sbopkg.

As usual, see the man or info page for more information.

Tags: cli, slackware, sbopkg, motd

Copying a Directory Tree Recursively Using tar

September 22, 2023 — Gregg Szumowski

You can use tar to copy a directory tree to another directory in one shot along with preserving ownership, permissions and timestamps. This also avoids making an intermediary tarfile which may cause problems if the size of the file copy is large and the storage resources are low. Just cd to the top of the directory that you want to copy and begin.

Let’s assume that you want to copy the contents of the source directory to a target directory:

$ cd /path/to/source
$ tar cf - * | (cd /target; tar xfp - )

Tags: cli, tar, copying, motd

Removing Blank Spaces from a Group of File Names

September 21, 2023 — Gregg Szumowski

This set of commands iterate over each file in the current directory and will replace any blank spaces in a filename with an underscore (_).

First, we’ll create a set of sample files:

$ for i in {0..9}; do touch "file-${i} (number-${i}).txt"; done
$ ls
file-0\ (number-0).txt file-4\ (number-4).txt file-8\ (number-8).txt
file-1\ (number-1).txt file-5\ (number-5).txt file-9\ (number-9).txt
file-2\ (number-2).txt file-6\ (number-6).txt
file-3\ (number-3).txt file-7\ (number-7).txt

Now, we’ll execute the set of commands:

$ for f in *\ *; do mv "$f" "${f// /_}"; done

And now you can see that the blanks have been replaced.

$ ls
file-0_(number-0).txt file-4_(number-4).txt file-8_(number-8).txt
file-1_(number-1).txt file-5_(number-5).txt file-9_(number-9).txt
file-2_(number-2).txt file-6_(number-6).txt
file-3_(number-3).txt file-7_(number-7).txt

Tags: cli, bash, rename, motd