OPS435 Python Lab 8

From CDOT Wiki
Revision as of 12:27, 30 December 2017 by Andrew (talk | contribs) (INVESTIGATION 1: Extra VM Setup)
Jump to: navigation, search

THIS LAB IS NOT READY YET!!!

PLEASE WAIT UNTIL THE LAB IS PUBLISHED. IT IS CURRENTLY UNDER CONSTRUCTION.

LAB OBJECTIVES

Completing this course will give you the prerequisites for getting into the DevOps field. A DevOps professional is a system/network administrator with programming skills. As an introduction to that field, we will look at Fabric in this lab. Using Fabric you can automate deploying software, monitoring, and updating many systems at the same time, without using a terminal to connect to each of them separately.

REFERENCE

These links may be helpful for extra reading:
Category Resource Link
Official Fabric website
[1]
Official Fabric tutorial
[2]
Better Fabric tutorial
[3]

INVESTIGATION 1: Extra VM Setup

In order to experience Fabric's features in a realistic way, we're going to set up several virtual machines. To begin with they are all going to have the same configuration.

PART 1 - Set up your controller

In this lab you will use your existing vm centos7 as a workstation to control other VMs which we'll call workers.

Install fabric using yum. Once it's installed you should have a fab command available.

PART 2 - Create master Worker image

Create a new virtual machine, and allocate for it 1GB or RAM and 8GB of disk space. Install a Basic Web Server configuration of CentOS in that VM using the same CentOS .iso file you used for your first machine in this course.

Make sure that:

  • The hostname of the system is worker1.
  • It has a static IP address appropriate for your virtual network.
  • You don't need to create any extra user.
  • After installation ensure that you can access worker1 from your main vm using the static IP address you've assigned to it.

Set up SSH key login

In order for an automated system to be able to connect to your VM and administer it - you will need to be able to connect to it using SSH keys. You've done this in both OPS235 and OPS335.

Create a new SSH key on your main VM with your regular user. Please avoid using root. Then set things up so that your regular user on your main VM can SSH to the slave1 VM as root without putting in a password.

PART 3 - Clone the Workers

We're only simulating the real world where you'd have hundreds of VMs in one or more clouds, but you can just imagine that the VMs you're creating on your computer are actually being created on an Amazon or Microsoft server.

Make four clones of the master worker image you've just created. Then make sure that each of them has a unique IP address. That's all you're required to change manually. All the other configuration on the workers (inlcuding the hostnames) will be set by Fabric. Normally you would have some kind of automation doing all this cloning and IP address assignment as well, but we don't have time for that this semester.

Make snapshots of all your workers so that you can easily restore them to the original state after you modify them.

INVESTIGATION 2: Fabric practice

We will start with some basics. Fabric runs python programs on the controller and the workers. You create an "instruction" file on your controller, and execute it on the controller using the fab program. When you do that - you specify which workers you want your instructions to be executed on.

The instructions are stored in a python file. Let's start with a simple one named fabfile.py (the default filename for fab):

from fabric.api import *

# Will get the hostname of this worker:
def getHostname():
    name = run("hostname")
    print(name)

All this will do is get the hostname of the worker and print it (on the controller). We run it on the controller like this:

fab --fabfile=fabfile.py -H 192.168.56.11 getHostname

In the command above we're using the fab program to read the file fabfile.py and execute the getHostname function on the worker 192.168.56.11. Note that the IP address of your first worker will likely be different.

If you did all the setup right and you try to execute the command above - you will get a password prompt. That shouldn't happen, the point is for everything to be automated, and that's why you've set up SSH keys. To fix it, add the following to your fab file:

env.user = 'root'

LAB 7 SIGN-OFF (SHOW INSTRUCTOR)

Have Ready to Show Your Instructor:


LAB REVIEW