How Do You Make an Environmental Variable Persist through a Reboot on a Linux server?

Problem scenario
Every time a server is rebooted, you have to manually set environment variables with the "export" command. You want a variable to persist through a reboot so you do not have to do this manual work. You want a variable to be in the environment of the OS every time you log in. You want the solution to be specific to one user but you want it to work for any distribution of Linux (a Debian or Red Hat derivative including SUSE, CentOS/RHEL/Fedora, and Ubuntu). How do you get a variable to be in the environment of a Linux OS for your user every time you log in?

Solution

  1. Modify the .bashrc file in your home directory: vi ~/.bashrc
  2. At the very end of the file add a new line. Place the "export …" command without the initial "export " (export and space). Here is an example:
    JAVA_HOME=/usr/lib/jvm/java-9-openjdk
  3. The above is specific was one user. If you want every user to get this variable, see this link titled How Do You Set an Environment Variable for Every User Upon Rebooting a Linux Server?.

How Do You Troubleshoot the Groovy Error “JAVA_HOME is not defined correctly”?

One of the following situations apply to you:

Problem scenario #1
You try to run a Groovy command (e.g., groovy foobar.groovy), but you get this error:
"groovy: JAVA_HOME is not defined correctly, can not execute: /usr/lib/jvm/java-9-openjdk/bin/java"

Problem scenario #2
You get this error when you run an "hdfs" command:

ERROR: /usr/bin//bin/java is not executable.

What should you do?

Solution

  1. Set the JAVA_HOME variable. Run this command: which java
  2. Mentally eliminate the trailing "/bin/java" from the above command. If Java was not found, follow these directions to install Java.
  3. Draft this command but replace "foobar" with the results of step #2 (with the elimination of "/bin/java" from the end of step #1's results): export JAVA_HOME=foobar
  4. Run the drafted command from above.
  5. Try to use Groovy again.

If you want the solution to persist a reboot, we recommend rebooting the server and seeing if your original problem is reproducible. Then run a command like this but replace "foobar" with the results of step #2 (with the elimination of "/bin/java" from the end of step #1's results):

echo 'export JAVA_HOME=/foobar' >> ~/.bashrc

How Do You Install Helm on Any Type of Linux?

Problem scenario
You want to use Helm to manage Kubernetes applications. Helm helps you with packages for changes to Kubernetes (in ways that are similar to yum or apt). Helm uses what are called Charts (.yaml files) that enable you to do more with Kubernetes with less trouble. Helm consists of these two things: a CLI tool and a server component that runs as a pod in a Kubernetes cluster (page 531 of Kubernetes in Action by Luksa).

You want to install Helm. You want a script that will work on any type of Linux. You have access to the internet. What should you do?

Solution
Run these commands:

curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
chmod 700 get_helm.sh
./get_helm.sh

(The above commands were taken from this website.)

The script below no longer works as of January 2022.
1. Create this script /tmp/helm.sh:

version=2.9.1

curl -Lk https://kubernetes-helm.storage.googleapis.com/helm-v$version-linux-amd64.tar.gz > /tmp/helm-v$version-linux-amd64.tar.gz

mv -i /tmp/helm-v$version-linux-amd64.tar.gz /bin/
cd /bin/
tar -zxvf helm-v$version-linux-amd64.tar.gz
cp linux-amd64/helm /usr/local/bin/helm

2. Run it like this: sudo bash /tmp/helm.sh

If you are using Debian/Ubuntu Linux without internet access, you can try this posting.

How Do You Create a Server in Azure with Terraform?

Problem scenario
You want to use Terraform to create a virtual machine in Azure. What do you do?

Solution
Prerequisites

i. Terraform must be installed on a Linux server. If you need assistance with this, see this posting.
ii. The Azure CLI must be installed and configured on the Linux server. If you need assistance with this, see this posting. (Normally you would need a client_id and client_secret in the provider block of the .tf file; you do not need these if your Azure CLI has been configured properly.)
iii. You have run ssh-keygen on the Linux server with Terraform, and you have the id_rsa.pub file. You'll need the content of the file for the directions below.

Procedures

  1. Create a file called instance.tf with the following content (this was mostly taken from https://docs.microsoft.com/en-us/azure/virtual-machines/linux/terraform-create-complete-vm) :
# Configure the Microsoft Azure Provider
provider "azurerm" {
    subscription_id = "abcdefghijklmnopqrstuvwxyz1234567891"
    tenant_id       = "777777777777777777777777777777777777"
    version = "=1.22.0"
}

# Create a resource group if it doesn’t exist
resource "azurerm_resource_group" "myterraformgroup" {
    name     = "Coolthing"
    location = "eastus"

    tags {
        environment = "Terraform Demo"
    }
}

# Create virtual network
resource "azurerm_virtual_network" "myterraformnetwork" {
    name                = "myVnet"
    address_space       = ["10.0.0.0/16"]
    location            = "eastus"
    resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"

    tags {
        environment = "Terraform Demo"
    }
}

# Create subnet
resource "azurerm_subnet" "myterraformsubnet" {
    name                 = "mySubnet"
    resource_group_name  = "${azurerm_resource_group.myterraformgroup.name}"
    virtual_network_name = "${azurerm_virtual_network.myterraformnetwork.name}"
    address_prefix       = "10.0.1.0/24"
}

# Create public IPs
resource "azurerm_public_ip" "myterraformpublicip" {
    name                         = "myPublicIP"
    location                     = "eastus"
    resource_group_name          = "${azurerm_resource_group.myterraformgroup.name}"
    allocation_method            = "Dynamic"

    tags {
        environment = "Terraform Demo"
    }
}

# Create Network Security Group and rule
resource "azurerm_network_security_group" "myterraformnsg" {
    name                = "myNetworkSecurityGroup"
    location            = "eastus"
    resource_group_name = "${azurerm_resource_group.myterraformgroup.name}"

    security_rule {
        name                       = "SSH"
        priority                   = 1001
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    }

    tags {
        environment = "Terraform Demo"
    }
}

# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
    name                      = "myNIC"
    location                  = "eastus"
    resource_group_name       = "${azurerm_resource_group.myterraformgroup.name}"
    network_security_group_id = "${azurerm_network_security_group.myterraformnsg.id}"

    ip_configuration {
        name                          = "myNicConfiguration"
        subnet_id                     = "${azurerm_subnet.myterraformsubnet.id}"
        private_ip_address_allocation = "Dynamic"
        public_ip_address_id          = "${azurerm_public_ip.myterraformpublicip.id}"
    }

    tags {
        environment = "Terraform Demo"
    }
}

# Generate random text for a unique storage account name
resource "random_id" "randomId" {
    keepers = {
        # Generate a new ID only when a new resource group is defined
        resource_group = "${azurerm_resource_group.myterraformgroup.name}"
    }

    byte_length = 8
}

# Create storage account for boot diagnostics
resource "azurerm_storage_account" "mystorageaccount" {
    name                        = "diag${random_id.randomId.hex}"
    resource_group_name         = "${azurerm_resource_group.myterraformgroup.name}"
    location                    = "eastus"
    account_tier                = "Standard"
    account_replication_type    = "LRS"

    tags {
        environment = "Terraform Demo"
    }
}

# Create virtual machine
resource "azurerm_virtual_machine" "myterraformvm" {
    name                  = "myVM"
    location              = "eastus"
    resource_group_name   = "Coolthing"
    network_interface_ids = ["${azurerm_network_interface.myterraformnic.id}"]
    vm_size               = "Standard_DS1_v2"

    storage_os_disk {
        name              = "myOsDisk"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Premium_LRS"
    }

    storage_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = "16.04.0-LTS"
        version   = "latest"
    }

    os_profile {
        computer_name  = "myvm"
        admin_username = "azureuser"
    }

    os_profile_linux_config {
        disable_password_authentication = true
        ssh_keys {
            path     = "/home/azureuser/.ssh/authorized_keys"
            key_data = "ssh-rsa AAAAB...ec2-instance5.azure.gcp.com"
        }
    }

    boot_diagnostics {
        enabled = "true"
        storage_uri = "${azurerm_storage_account.mystorageaccount.primary_blob_endpoint}"
    }

    tags {
        environment = "Terraform Demo"
    }
}

2.a. Replace "ssh-rsa AAAAB…ec2-instance5.azure.gcp.com" in the above file with the content of the id_rsa.pub file. To find this file run sudo find / -name id_rsa.pub. To create this file if you do not have it, run ssh-keygen -t rsa -P ""
2.b. Change "abcdefghijklmnopqrstuvwxyz1234567891" to your subscription ID. If you need assistance finding your subscription ID, see this posting.
2.c. Change "777777777777777777777777777777777777" in the file above to your tenant ID.
2.d. (Optional.) Change "eastus" to the region of your choice (e.g., "westus").

3. Run these commands:

terraform init
terraform apply

(Respond with "yes" to the prompt.)

4. You have now created a VM in Azure. You will be able to log in with SSH to the new VM using the "azureuser" account from the server you ran the Terraform commands from.

Using Python How Do You Write a Palindrome Tester?

Problem scenario
You want to write a Python program that tests if a string is a palindrome. You want the calling function to have five lines of code or fewer. How do you do this?

Solution
Here is a program that interactively prompts you for a string and will tell you if it is palindromic or not.

# You may want to see this documentation page: https://docs.python.org/2.3/whatsnew/section-slices.html

stra = input("Enter a string: ")

def goodone(stra):
  if(str(stra) == str(stra)[::-1]):
    print("It is palindromic.")
  else:
    print("The entered string was not a palindrome.")

goodone(stra)

What is Calico?

Question
In the context of container orchestration, you have read or seen "Calico." What is it?

Answer
"Calico provides a highly scalable networking and network policy solution for connecting Kubernetes pods based on the same IP networking principles as the internet. … Calico also provides fine-grained, intent based network security policy for Kubernetes pods via its distributed firewall." Taken from https://kubernetes.io/docs/concepts/cluster-administration/networking/

"Calico provides secure network connectivity for containers and virtual machine workloads. Calico creates and manages a flat layer 3 network, assigning each workload a fully routable IP address." Taken from https://docs.projectcalico.org/v3.3/introduction/

"Project Calico is an open source container networking provider and network policy engine." taken from https://kubernetes.io/docs/concepts/cluster-administration/networking/

What is Flannel?

Question
You have heard about flannel in the context of container orchestration. What is flannel?

Answer
"Flannel is a simple and easy way to configure a layer 3 network fabric designed for Kubernetes." Taken from https://github.com/coreos/flannel

"Flannel is a networking technology used to connect Linux Containers. It is distributed and maintained by CoreOS, the producer of the stripped-down CoreOS Linux operating system for containers, as well as the rkt container system that competes with Docker." Taken from
https://www.sdxcentral.com/containers/definitions/what-is-coreos-flannel-definition/

"Flannel is a very simple overlay network that satisfies the Kubernetes requirements." taken from https://kubernetes.io/docs/concepts/cluster-administration/networking/

How Do You Find (Or Determine) What Product Key Id Is Associated with Your Windows Computer?

Problem scenario
You lost your product key to your Windows computer. You find command prompt and PowerShell commands do not work to display the key ID. How do you display it (e.g., for upgrade purposes)?

Solution
Use this GUI (.exe) tool found here:
https://gallery.technet.microsoft.com/scriptcenter/Get-Windows-Product-6b5e6f6e

What Is The Difference between git merge and git rebase?

Question
What is the difference between git merge and git rebase?

Answer
git merge takes code from one branch and copies it into another branch. One branch will prevail to the extent there are conflicts. (The details of the syntax are beyond the scope of this solution. The git merge operation can be done in a GUI (e.g., via BitBucket).) The published source code would usually change.

git rebase takes code from a committed branch and overwrites your local branch. git rebase brings down code with the trunk code always prevailing. The published source code does not change.

git merge is for committing code upstream and git rebase is for bring new code downstream. If production is the most upstream you can go and development is the most downstream you can go, the public Git repository holds code. As you develop a new feature, you may find another developer committed code for something you were working on. You may want to bring that change upstream back down into development. This way when you integrate your code, you can manually reconcile any potential conflicts.

"The golden rule of git rebase is to never use it on public branches." (Taken from this link.)

git merge operations are usually for making public your code. git rebase is usually for getting a new baseline to develop from.

If you want to learn more about the differences between the two, see these postings:
https://www.atlassian.com/git/tutorials/merging-vs-rebasing
https://stackoverflow.com/questions/804115/when-do-you-use-git-rebase-instead-of-git-merge

How Do You Install the Azure CLI on a Debian/Ubuntu Linux Server?

Problem scenario
You want to use the Azure CLI on a Debian/Ubuntu Linux server.

Solution

1. Run these commands:

sudo apt-get -y update sudo apt-get install -y curl apt-transport-https lsb-release gpg curl -sL https://packages.microsoft.com/keys/microsoft.asc | \ gpg --dearmor | \ sudo tee /etc/apt/trusted.gpg.d/microsoft.asc.gpg > /dev/null AZ_REPO=$(lsb_release -cs) echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | \ sudo tee /etc/apt/sources.list.d/azure-cli.list sudo apt-get -y update sudo apt-get -y install azure-cli

2. Test it with this command:
az login

(You will need to go to a web browser and enter a code.)