How Do You Find what VPC an EC-2 Instance Is In?

Problem scenario
You want to determine what VPC a given AWS server is in. How do you find this out?

Solution
Prerequisite

You must have the AWS CLI installed and configured with a user that has the ability to view EC-2 databases (e.g., an administrator of your AWS account). If you need assistance, see this posting.

Procedure
Run this command: aws rds describe-db-instances | grep -i vpc

How Do You Install Molecule on RHEL v. 8.x with Python 3?

Problem scenario
You want to make and test Ansible roles, and therefore you want to use Molecule. You are using RHEL version 8.x. You want to use Python 3 and Molecule. What should you do?

Solution
Prerequisites
i. You should install Docker. If you need assistance, see this posting.
ii. You should install these packages before hand:

sudo yum -y install gcc python3-devel
sudo dnf install -y redhat-rpm-config

Procedures
1. Run these commands:

sudo python3 -m pip install virtualenv
sudo python3 -m virtualenv my_env
source my_env/bin/activate
sudo yum install -y python3-devel 
sudo python3 -m pip install molecule docker
molecule init role -r ansible-apache -d docker
cd ansible-apache
molecule test

2.a. Run this command: vi tasks/main.yml
2.b. Delete what is there. Put this there instead:

---
- name: "Ensure required packages are present"
  yum:
    name: "{{ pkg_list }}"
    state: present

- name: "Ensure latest index.html is present"
  template:
    src: index.html.j2
    dest: /var/www/html/index.html

- name: "Ensure httpd service is started and enabled"
  service:
    name: "{{ item }}"
    state: started
    enabled: true
  with_items: "{{ svc_list }}"

- name: "Whitelist http in firewalld"
  firewalld:
    service: http
    state: enabled
    permanent: true
    immediate: true

3. Run this command: mkdir templates

4.a. Run this command: vi templates/index.html.j2
4.b. Put this there and save the file:

<div style="text-align: center">
    <h2>Managed by Ansible</h2>
</div>

5.a. Run this command: vi vars/main.yml
5.b. Delete what is there. Place this code starting at the first non-blank line:

---
pkg_list:
  - httpd
  - firewalld
svc_list:
  - httpd
  - firewalld

6.a. Run this command: vi molecule/default/tests/test_default.py
6.b. Eliminate the content that you find. Place these lines in the place of the old content:

import os
import pytest

import testinfra.utils.ansible_runner

testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
    os.environ['MOLECULE_INVENTORY_FILE']).get_hosts('all')


@pytest.mark.parametrize('pkg', [
  'httpd',
  'firewalld'
])
def test_pkg(host, pkg):
    package = host.package(pkg)

    assert package.is_installed


@pytest.mark.parametrize('svc', [
  'httpd',
  'firewalld'
])
def test_svc(host, svc):
    service = host.service(svc)

    assert service.is_running
    assert service.is_enabled


@pytest.mark.parametrize('file, content', [
  ("/etc/firewalld/zones/public.xml", "<service name=\"http\"/>"),
  ("/var/www/html/index.html", "Managed by Ansible")
])
def test_files(host, file, content):
    file = host.file(file)

    assert file.exists
    assert file.contains(content)

7. Run this command: molecule test

8. You are done. The above directions were adapted from a DigitalOcean tutorial here.

How Do You Find what VPC an Aurora Database Is In?

Problem scenario
You want to be sure that a given Aurora database is in a specific VPC. How do you determine what VPC it is in?

Solution
Prerequisite
You must have the AWS CLI installed and configured with a user that has the ability to view Aurora databases (e.g., an administrator of your AWS account). If you need assistance, see this posting.

Procedure
Run this command: aws rds describe-db-instances | grep -i vpc

How Do You Invoke the main Section of a Groovy Program?

Problem scenario
Your Groovy code has a section like this:

class Example {
static void main(String[] args) {
…

But this section above is not executing when you run your Groovy program. There are no errors however. How do you get it to execute?

Possible Solution #1
Remove lines of code (or statements) that are outside the "class Example {…"

Possible Solution #2
See this posting: How Do You Invoke a Class in a Groovy Program?

How Do You Use the aws update-kubeconfig Command?

Problem scenario
You want to manage your Amazon Kubernetes (i.e., EKS) cluster. You have installed the AWS CLI version on a Linux server, and you want to use the update-config or update-kubeconfig command.

But when you try you see messages like "error argument command: Invalid choice…" or "update-config: command not found". You could not readily find an example. What should you do?

Solution
Background
The command is update-kubeconfig, not update-config.

Procedures
1. Verify you have a version of the AWS CLI at or higher than 1.16. Use this command: aws --version
To install a newer version, see this set of directions if you can install pip; if you cannot install pip, see this set of directions.

2. Use this syntax (where foobar is the name of the cluster you want to create the configuration file for):
aws eks update-kubeconfig --name foobar

(If you need to find the name of your cluster(s), run this command: aws eks list-clusters)

How Do You Interpret the dstat Utility’s Results on a Linux Server in AWS?

Problem scenario
You want to monitor network traffic on an EC-2 server's NICs. You are not sure what to look for or what a baseline should look like. What do you do?

Solution
1. Install dstat. With a CentOS/RHEL/Fedora server, run this: sudo yum -y install dstat
2. Run this command: dstat -nt
3. With no network activity beyond the PuTTy session to the EC-2 server, you will see numbers like these: recv send| time
0 0 |13-12 01:05:06
40B 310B|13-12 01:05:07
40B 198B|13-12 01:05:08
40B 198B|13-12 01:05:09
40B 198B|13-12 01:05:10
82B 240B|13-12 01:05:11

The above represents minimal activity across the NIC of the server.
4. If you want to learn more about the dstat tool, see these links:
https://linux.die.net/man/1/dstat
https://linoxide.com/monitoring-2/dstat-monitor-linux-performance/

How Do You Terminate Pods in Kubernetes?

Problem scenario
You have some Pods in Kubernetes that you want deleted. What should you do?

Solution
1. Find the names. You can run this to find out: kubectl get pods

2. Run this command, but replace "foobar" with the name of the pod you want deleted: kubectl delete pods foobar

3. That should be it. Run kubectl get pods to verify deletion was successful. If the Pods keep coming back, run this: kubectl get deployments

4. With the results you may need to run kubectl delete deployments foobar (where foobar is the name of the Deployment responsible for the Pods self-healing automatically).

How Do You Troubleshoot the Java Program Message “java.sql.SQLException: No database selected”

Problem scenario
Your Java program returns this message: "java.sql.SQLException: No database selected"
What should you do?

Possible Solution #1
Your SQL statement was appropriate for a database but not for the circumstance of connecting to a SQL instance. Try show databases; instead of the SQL you were running.

Possible Solution #2
Did you connect to the SQL instance you thought you connected to? Double check that.

Possible Solution #3
Verify the way your program connects to a database. There may be a syntax error that does not manifest itself until your SQL command is executed.