139
Infrastruktur als Code

Rex - Infrastruktur als Code

Embed Size (px)

DESCRIPTION

Vortrag auf den Kieler Linuxtagen 2013

Citation preview

Infrastruktur als Code

Wer bin ich?

● Jan Gehring● System Architekt● Bei inovex seit 2008

Um was geht’s?

● Von der Turnschuh-Administration● Zur automatisierten Umgebung

Turnschuh-Administration

● Der unglückliche Umstand, sämtliche Rechner im Netzwerk zu Fuß abklappern zu müssen, um vor Ort administrative Tätigkeiten durchzuführen.

http://www.wikiservice.at

Turnschuh-Administration

● Funktioniert bei wenigen Servern● Kann funktionieren wenn jeder Server

unterschiedlich ist● Skaliert nur durch mehr Mitarbeiter

Turnschuh-Administration

Oft gehört...

● Wirklich?● Was ist mit

– ntp

– Benutzer und Gruppen

– ssh

– logrotate

– resolv.conf

– profile

– Apache Security Einstellungen

– ...

Jeder Server ist unterschiedlich

● Wieviele Server kann eine Person administrieren?– 10

– 25

– 50

● Krankheit oder Urlaub?

Skaliert?

● Der Tag hat 24 Stunden● Naja, eigentlich nur 8 …

Zeit ist knapp

● Mails lesen und beantworten

Zeit ist knapp

● Mails lesen und beantworten (1-2h)● Mailinglisten verfolgen (Security, ...)

Zeit ist knapp

Sicherheitsproblem im Apache... auch bei uns.

Infrastruktur als Code

Muss heute mittag gepatched werden!

Infrastruktur als Code

● Mails lesen und beantworten (1-2h)● Mailinglisten verfolgen (1h)● Updates aus der IT Welt (heise, golem, ...)

Zeit ist knapp

● Mails lesen und beantworten (1-2h)● Mailinglisten verfolgen (1h)● Updates aus der IT Welt (1h)

Zeit ist knapp

Vormittag ist rum

Zeit ist knapp

Bleiben noch 4 Stunden

Zeit ist knapp

Ein Kollege von der Entwicklung schaut rein, hat ein Problem...

Zeit ist knapp

3 Stunden

Zeit ist knapp

Neue Softwarerelease soll deployed werden...

Zeit ist knapp

Funktioniert nicht!

Zeit ist knapp

Hotfix 1...

Zeit ist knapp

Hotfix 2...

Zeit ist knapp

-2 Stunden

Zeit ist knapp

Feierabend!

Zeit ist knapp

Security Update kommt morgen...

Zeit ist knapp

.oO( Vielleicht )

Zeit ist knapp

Kommt das bekannt vor?

Infrastruktur als Code

Wenn ja, dann seid ihr hier richtig!

Infrastruktur als Code

Was braucht man um zu Automatisieren?

Infrastruktur als Code

Tools!

Infrastruktur als Code

Infrastruktur als Code

http://www.immobilienwirtschaft360.de/

Continuous Integration Tool

Infrastruktur als Code

Infrastruktur als Code

http://jenkins-ci.org/http://travisci.com/

Bild von: https://www.openshift.com

Configuration/Deployment Management Tool

Infrastruktur als Code

Infrastruktur als Code

http://www.opscode.com/

Bild von: http://developer.rackspace.com/

Source Control Management

Infrastruktur als Code

Infrastruktur als Code

http://git-scm.com/

http://mercurial.selenic.com/

http://subversion.tigris.org/

Infrastruktur als Code

Was bedeutet das?

Infrastruktur als Code

● Das Wissen über die Infrastruktur ist nicht in eurem Kopf sondern in einem Script.

● Wenn ihr mal krank seid kann ein Kollege kucken wie es funktioniert und muss nicht stören.

● Das gilt auch für den Urlaub.● Oder wenn ihr mal die Firma gewechselt habt.

Was bedeutet das?

Außerdem macht es sich gut bei einem Bewerbungsgespräch...

Inovex sucht übrigens Linux System Engineers!

● Wenn es nur von einem verwendet / upgedatet wird?

● Wenn es nur auf einer Plattform läuft?

Was bringt uns ein Script?

● Dezentrale Versionsverwaltung● Funktioniert auch offline● Schöne Oberflächen

Git – und jeder macht mit

Git – und jeder macht mit

http://gitlab.org/

● Dezentrale Versionsverwaltung● Funktioniert auch offline● Schöne Oberflächen● Scripte sind versioniert● Vielleicht machen auch die Entwickler mit● Man lernt dazu

Git – und jeder macht mit

● Unterschiedliche Entwicklungszweige● Für Testsysteme ein Entwicklungszweig

(branch)● Für Livesysteme ein „Production“ Branch

Git – und jeder macht mit

Git – und jeder macht mit

http://erickryski.com/

● Standard Aufgaben kann man hier hinterlegen– Testen des Development Branchs

– Automatische Prüfung von Backup/Restore

– Deployment von neuen Software-Releases

– Erstellen von Test-VMs

– …

● Gibt auch eine Android / iPhone App

Jenkins – Ein-Klick Administration

Jenkins – Ein-Klick Administration

http://devcentral.f5.com/

● Keine selbst geschriebenen Scripte– Auf die Arbeit von anderen aufbauen

● Ganz einfaches Perl– Kennt eigentlich jeder

– Lesbar

● Benötigt (fast) nichts auf dem Zielsystem● Kein Agent nötig● Verwendet SSH● Logik läuft auf dem lokalen Rechner

Rex – Remote Execution

● Wird entwickelt seid 2010● Genau für dieses Einsatzgebiet● Bruch der Kompatibilität = Bug● Aktive Community● Professioneller Support verfügbar

Rex – Remote Execution

Bis hier hin Fragen?

Weiter geht’s mit: Rex

● Begrifflichkeiten● Rex Interna● Aus der Praxis

● Installation● 1. Schritte● ...

Infrastruktur als Code

BegrifflichkeitenWas ist was?

Infrastruktur als Code

WKS

Rex

Workstation

WKS

Rex

SSH

Workstation

SRV02SRV01 SRV03 SRV04 SRV05 SRV06

WKS

Rex

SSH

Workstation & Server

Gruppen

LB01 LB02

FE01 FE03FE02

MW01 MW02

DBS01 DBS02

DBM01

Gruppen

DB Write

DB Read

Middleware

Frontend

Loadbalancer LB01 LB02

FE01 FE03FE02

MW01 MW02

DBS01 DBS02

DBM01

Funktions-Gruppen

group “ourgroup“ => “server1“, “server2“, “server3“;

Funktions-Gruppen

group “ourgroup“ => “server1“, “server2“, “server3“;

Funktions-Gruppen

group “ourgroup“ => “server1“, “server2“, “server3“;

Funktions-Gruppen

group “ourgroup“ => “server[1..3]“;

Funktions-Gruppen

group “ourgroup“ => “server[01..03]“;

Funktions-Gruppen

group “ourgroup“ => get_servers_from_db();

Funktions-Gruppen

Environments

LIVE STAGE TEST

Loadbalancer

Frontend

Middleware

DB Read

DB Write

LB01 LB02

FE01 FE03FE02

MW01 MW02

DBS01 DBS02

DBM01

LB01

FE01

MW01

DBS01

DBM01

FE01

MW01

DBM01

environment “live“ => sub { group “frontend“ => “fe01[01..03].live“;};

environment “stage“ => sub { group “frontend“ => “fe01.stage“;};

environment “test“ => sub { group “frontend“ => “fe01.test“;};

Environments

environment “live“ => sub { group “frontend“ => “fe01[01..03].live“;};

environment “stage“ => sub { group “frontend“ => “fe01.stage“;};

environment “test“ => sub { group “frontend“ => “fe01.test“;};

Environments

environment “live“ => sub { group “frontend“ => “fe01[01..03].live“;};

environment “stage“ => sub { group “frontend“ => “fe01.stage“;};

environment “test“ => sub { group “frontend“ => “fe01.test“;};

Environments

Tasks

task “mytask“, group => “ourgroup“, make {};

Tasks

task “mytask“, group => “ourgroup“, make {};

Tasks

task “mytask“, group => “ourgroup“, make {};

Tasks

task “mytask“, group => “ourgroup“, make {};

Tasks

task “mytask“, group => “ourgroup“, make {};

Tasks

task “mytask“, group => “ourgroup“, make {};

Tasks

task “mytask“, group => “ourgroup“, make { run “unzip -q /tmp/my.zip“;};

Tasks

task “mytask“, group => “ourgroup“, make { run “unzip -q /tmp/my.zip“;};

Tasks

task “mytask“, group => “ourgroup“, make { run “unzip -q /tmp/my.zip“;};

Tasks

Bis hier hin Fragen?

Weiter geht’s mit: Rex

● Begrifflichkeiten● Rex Interna● Aus der Praxis

● Installation● 1. Schritte● ...

Aufteilung in Module und Services

● Unabhängig● Wiederverwendbar● Für einen Einsatzzweck gemacht

Module sind

... ein spezieller Dienst

Services sind ...

z.b.: der Intranet-Server

Services sind ...

oder: der Proxy-Server

Services sind ...

oder: das Frontend

Services sind ...

Bis hier hin Fragen?

Weiter geht’s mit: Rex

● Begrifflichkeiten● Rex Interna● Aus der Praxis

● Installation● 1. Schritte● ...

Installation

echo 'deb http://rex.linux-files.org/ubuntu/ precise rex' >> \ /etc/apt/sources.list

wget -O - \ http://rex.linux-files.org/DPKG-GPG-KEY-REXIFY-REPO \ | apt-key add -

apt-get updateapt-get install rex

Installation / Ubuntu

Installation / CentOS

rpm –import \http://rex.linux-files.org/RPM-GPG-KEY-REXIFY-REPO.CENTOS6

cat >/etc/yum.repos.d/rex.repo <<EOF[rex]name=Fedora \$releasever - \$basearch - Rex Repositorybaseurl=http://rex.linux-files.org/CentOS/\$releasever/rex/\$basearch/enabled=1EOF

yum install rex

Installation / Windows

● Strawberry-Perl (http://strawberryperl.com/)● cmd> cpanm Rex

curl -L get.rexify.org \ | perl - --sudo -n Rex

Installation / Source

Bis hier hin Fragen?

Weiter geht’s mit: Rex

● Installation● 1. Schritte● Templates● 1. Modul

● Verzeichnisstruktur● Das Modul● Ein Service

Ausgangspunkt: das Rexfile

user “root“;password “box“;

Das Rexfile

user “root“;password “box“;pass_auth;

Das Rexfile

user “root“;password “box“;private_key “/home/jan/.ssh/id_rsa_root“;public_key “/home/jan/.ssh/id_rsa_root.pub“;key_auth;

Das Rexfile

user “root“;password “box“;

group “frontend“ => “fe[01..03]";

task “install_ntpd“, group => “frontend“, make { install “ntpd“;};

Das Rexfile

bash# rex install_ntpd

group “frontend“ => “fe[01..03]";

task “install_ntpd“, group => “frontend“, make { install “ntpd“;

file “/etc/ntp.conf“, source => “files/etc/ntp.conf“, owner => “root“, group => “root“, mode => 644;};

Das Rexfile

bash# rex install_ntpd

Das Rexfile

group “frontend“ => “fe[01..03]";

task “install_ntpd“, group => “frontend“, make { install “ntpd“;

file “/etc/ntp.conf“, source => “files/etc/ntp.conf“, owner => “root“, group => “root“, mode => 644;

service ntpd => ensure => “started“;};

Das Rexfile

bash# rex install_ntpd

group “frontend“ => “fe[01..03]";

task “install_ntpd“, group => “frontend“, make { install “ntpd“;

file “/etc/ntp.conf“, source => “files/etc/ntp.conf“, owner => “root“, group => “root“, mode => 644, on_change => make { service ntpd => “restart“ };

service ntpd => ensure => “started“;};

Das Rexfile

bash# rex install_ntpd

● Benutzer & Gruppen● Cron● Filesystem Funktionen

– mkdir, rmdir, ls, ln, rm, chown, chgrp, chmod

– is_file, is_dir, mv, cp, mount, umount, ...

● LVM● Kernel● Process-Management● Virtualization (KVM, Xen)● Cloud (Amazon, Jiffybox, OpenNebula, ...)

Mehr Funktionen

http://rexify.org/api/

Mehr Funktionen

Bis hier hin Fragen?

Weiter geht’s mit: Rex

● Installation● 1. Schritte● Templates● 1. Modul

● Verzeichnisstruktur● Das Modul● Ein Service

Templates

group “frontend“ => “fe[01..03]";

task “install_ntpd“, group => “frontend“, make { install “ntpd“;

file “/etc/ntp.conf“, source => “files/etc/ntp.conf“, owner => “root“, group => “root“, mode => 644, on_change => make { service ntpd => “restart“ };

service ntpd => ensure => “started“;};

Templates

driftfile /var/lib/ntp/ntp.drift

statistics loopstats peerstats clockstatsfilegen loopstats file loopstats type day enablefilegen peerstats file peerstats type day enablefilegen clockstats file clockstats type day enable

server ntp01.intern.lanserver ntp02.intern.lan

restrict -4 default kod notrap nomodify nopeer noqueryrestrict -6 default kod notrap nomodify nopeer noquery

restrict 127.0.0.1restrict ::1

Templates

driftfile /var/lib/ntp/ntp.drift

statistics loopstats peerstats clockstatsfilegen loopstats file loopstats type day enablefilegen peerstats file peerstats type day enablefilegen clockstats file clockstats type day enable

<% for my $server (@{ $servers }) { %>server <%= $server %><% } %>

restrict -4 default kod notrap nomodify nopeer noqueryrestrict -6 default kod notrap nomodify nopeer noquery

restrict 127.0.0.1restrict ::1

Templates

group “frontend“ => “fe[01..03]";

my $servers = [“ntp01.company.lan“, “ntp02.company.lan“];

task “install_ntpd“, group => “frontend“, make { install “ntpd“;

file “/etc/ntp.conf“, content => template(“files/etc/ntp.conf.tpl“, servers => $servers), owner => “root“, group => “root“, mode => 644, on_change => make { service ntpd => “restart“ };

service ntpd => ensure => “started“;};

Templates

Templates

● $eth0_ip● $eth0_netmask● $hostname● $domain● $architecture● $kernel● ...

Bis hier hin Fragen?

Weiter geht’s mit: Rex

● 1. Schritte● Templates● 1. Modul

● Verzeichnisstruktur● Das Modul● Ein Service

Module

bash# rexify NTP --create-module

├── lib│ └── NTP│ ├── meta.yml│ └── __module__.pm└── Rexfile

Verzeichnisstruktur

bash# rexify Common::NTP \ --create-module

├── lib│ └── Common│ └── NTP│ ├── meta.yml│ └── __module__.pm└── Rexfile

Verzeichnisstruktur

├── lib│ └── Common│ └── NTP│ ├── meta.yml│ └── __module__.pm└── Rexfile

Verzeichnisstruktur

> cat lib/NTP/meta.yml Name: NTPDescription: {{ DESCRIPTION }}Author: {{ your name <[email protected]> }}License: {{ THE LICENSE }}

# If you have dependencies to other Rex Modules.Require: - Other::Rex::Module - 2nd::Rex::Module

Verzeichnisstruktur

Bis hier hin Fragen?

Weiter geht’s mit: Rex

● 1. Schritte● Templates● 1. Modul

● Verzeichnisstruktur● Das Modul● Ein Service

group “frontend“ => “fe[01..03]";

my $servers = [“ntp01.company.lan“, “ntp02.company.lan“];

task “install_ntpd“, group => “frontend“, make { install “ntpd“;

file “/etc/ntp.conf“, content => template(“files/etc/ntp.conf.tpl“, servers => $servers), owner => “root“, group => “root“, mode => 644, on_change => make { service ntpd => “restart“ };

service ntpd => ensure => “started“;};

Das Modul

group “frontend“ => “fe[01..03]";

my $servers = [“ntp01.company.lan“, “ntp02.company.lan“];

task “install_ntpd“, group => “frontend“, make { install “ntpd“;

file “/etc/ntp.conf“, content => template(“files/etc/ntp.conf.tpl“, servers => $servers), owner => “root“, group => “root“, mode => 644, on_change => make { service ntpd => “restart“ };

service ntpd => ensure => “started“;};

Das Modul

package Common::NTP;

use Rex -base;

task example => sub { my $output = run "uptime"; say $output;};

1;

Das Modul

package Common::NTP;

use Rex -base;

task “setup“ => make { my $params = shift;

install “ntpd“;

file “/etc/ntp.conf“, content => template(“files/etc/ntp.conf.tpl“, %{ $params }), owner => “root“, group => “root“, mode => 644, on_change => make { service ntpd => “restart“ };

service ntpd => ensure => “started“;};

1;

Das Modul

├── lib│ └── NTP│ ├── files│ │ └── etc│ │ └── ntp.conf.tpl│ ├── meta.yml│ └── __module__.pm└── Rexfile

Das Modul

├── files│ └── etc│ └── ntp.conf.tpl├── lib│ └── NTP│ ├── files│ │ └── etc│ │ └── ntp.conf.tpl│ ├── meta.yml│ └── __module__.pm└── Rexfile

Das Modul

├── files│ └── etc│ └── ntp.conf.tpl├── lib│ └── NTP│ ├── files│ │ └── etc│ │ └── ntp.conf.tpl│ ├── meta.yml│ └── __module__.pm└── Rexfile

Das Modul

├── files│ └── etc│ └── ntp.conf.tpl├── lib│ └── NTP│ ├── files│ │ └── etc│ │ └── ntp.conf.tpl│ ├── meta.yml│ └── __module__.pm└── Rexfile

Das Modul

include “NTP“;

group “frontend“ => “fe[01..03]";

my $servers = [“ntp01.company.lan“, “ntp02.company.lan“];

task “prepare“, group => “frontend“, make { NTP::setup(servers => $servers);};

Das Modul / der Service

include “NTP“;

group “frontend“ => “fe[01..03]";

my $servers = [“ntp01.company.lan“, “ntp02.company.lan“];

task “prepare“, group => “frontend“, make { NTP::setup(servers => $servers);};

Das Modul / der Service

Vielen Dank für Eure Aufmerksamkeit!

Kontakt

Jan GehringSystems Architecture

inovex GmbHOffice PforzheimKarlsruhe Straße 7175179 Pforzheim

+49 (0)173 3181 [email protected]

Fragen?

http://rexify.org/

irc: freenode / #rex