Skip to content

Commit

Permalink
Initial port to EC2
Browse files Browse the repository at this point in the history
  • Loading branch information
sebgoa committed Sep 30, 2016
1 parent 6df797d commit 2ea4e26
Show file tree
Hide file tree
Showing 27 changed files with 272 additions and 196 deletions.
10 changes: 10 additions & 0 deletions k8s-exo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
- hosts: localhost
connection: local
vars_files:
- vars/local_config.yml
- vars/secrets.yml

roles:
- common
- k8s
4 changes: 2 additions & 2 deletions k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
- vars/secrets.yml

roles:
- common
- k8s
- common
- k8s
6 changes: 6 additions & 0 deletions roles/common-exo/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- name: install ssh key
copy:
content: "{{ key.private_key }}"
dest: "~/.ssh/id_rsa_{{ ssh_key }}"
mode: 0600
tags: sshkey
19 changes: 19 additions & 0 deletions roles/common-exo/tasks/create_sshkey.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- name: check if ssh key exists
stat:
path: "~/.ssh/id_rsa_{{ ssh_key }}"
register: stat_ssh_key
tags: sshkey

- name: Create SSH Key
cs_sshkeypair:
name: "{{ ssh_key }}"
register: key
when: "{{ not stat_ssh_key.stat.exists }}"
notify: install ssh key
tags: sshkey

- debug:
msg: 'private key is {{ key.private_key }}'
verbosity: 2
when: not key|skipped
tags: sshkey
2 changes: 2 additions & 0 deletions roles/common-exo/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
- include: create_sshkey.yml
34 changes: 17 additions & 17 deletions roles/common/tasks/create_sshkey.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
- name: check if ssh key exists
stat:
path: "~/.ssh/id_rsa_{{ ssh_key }}"
register: stat_ssh_key
tags: sshkey
- name: Create EC2 SSH Key
ec2_key:
name: "{{ ssh_key }}"
region: "{{ aws_region }}"
state: present
register: ec2key
tags: sshkey

- name: Create SSH Key
cs_sshkeypair:
name: "{{ ssh_key }}"
register: key
when: "{{ not stat_ssh_key.stat.exists }}"
notify: install ssh key
tags: sshkey
- debug: msg='private key is {{ ec2key.key.private_key }}'
when: ec2key.changed
tags: sshkey

- local_action: copy content="{{ ec2key.key.private_key }}" dest="~/.ssh/id_rsa_{{ ssh_key }}"
when: ec2key.changed
tags: sshkey

- debug:
msg: 'private key is {{ key.private_key }}'
verbosity: 2
when: not key|skipped
tags: sshkey
- file: path="~/.ssh/id_rsa_{{ ssh_key }}" mode=0600
when: ec2key.changed
tags: sshkey
2 changes: 1 addition & 1 deletion roles/common/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
- include: create_sshkey.yml
- include: create_sshkey.yml
14 changes: 14 additions & 0 deletions roles/k8s-exo/tasks/create_context.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Create k8s context

- name: Set context cluster
command: kubectl config set-cluster exo --server=https://{{ k8s_master.default_ip }}:443 --insecure-skip-tls-verify=true
tags: context

- name: Set context user
command: kubectl config set-credentials exo --username={{ k8s_username }} --password={{ k8s_password}}

- name: Create context
command: kubectl config set-context exo --cluster=exo --user=exo

- name: Use context
command: kubectl config use-context exo
7 changes: 7 additions & 0 deletions roles/k8s-exo/tasks/create_inv.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Create inventory file

- name: Create inventory file
template:
src: inventory.j2
dest: ./inventory
tags: inventory
6 changes: 6 additions & 0 deletions roles/k8s-exo/tasks/create_secgroup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Create k8s security group

- name: Create k8s Security Group
cs_securitygroup:
name: "{{ k8s_security_group_name }}"
description: k8s
File renamed without changes.
26 changes: 26 additions & 0 deletions roles/k8s-exo/tasks/create_vm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- name: Start k8s head node
cs_instance:
name: "{{ k8s_node_prefix }}-head"
template: "{{ k8s_template }}"
service_offering: "{{ k8s_instance_type }}"
ssh_key: "{{ ssh_key }}"
security_groups:
- '{{ k8s_security_group_name }}'
user_data: "{{ lookup('template', '../templates/k8s-master.j2') }}"
register: k8s_master

- debug:
msg: 'k8s master IP is {{ k8s_master.default_ip }}'
verbosity: 2

- name: Start k8s nodes
cs_instance:
name: "{{ k8s_node_prefix }}-node-{{ item }}"
template: "{{ k8s_template }}"
service_offering: "{{ k8s_instance_type }}"
ssh_key: "{{ ssh_key }}"
security_groups:
- '{{ k8s_security_group_name }}'
user_data: "{{ lookup('template', '../templates/k8s-node.j2') }}"
with_sequence: count={{ k8s_num_nodes }}
register: k8s_nodes
6 changes: 6 additions & 0 deletions roles/k8s-exo/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- include: create_secgroup.yml
- include: create_secgroup_rules.yml
- include: create_vm.yml
- include: create_inv.yml
- include: create_context.yml
20 changes: 20 additions & 0 deletions roles/k8s-exo/templates/inventory.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[default]
# localhost ansible_python_interpreter=/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python

[master]
master_node ansible_host={{ k8s_master.default_ip }} ansible_ssh_private_key_file=~/.ssh/id_rsa_k8s ansible_ssh_user=centos

[nodes]
{% for item in k8s_nodes.results %}
{{ item.default_ip }}
{% endfor %}

[allnodes]
{{ k8s_master.default_ip }}
{% for item in k8s_nodes.results %}
{{ item.default_ip }}
{% endfor %}

[nodes:vars]
ansible_ssh_user=centos
ansible_ssh_private_key_file=~/.ssh/id_rsa_{{ ssh_key }}
23 changes: 23 additions & 0 deletions roles/k8s-exo/templates/k8s-master.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#cloud-config
write_files:
- path: /etc/yum.repos.d/kubernetes.repo
content: |
[kubernetes]
name=Kubernetes
baseurl=http://yum.kubernetes.io/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg

package_update: true

packages:
- docker
- kubelet
- kubeadm
- kubectl
- kubernetes-cni

power_state:
mode: reboot
23 changes: 23 additions & 0 deletions roles/k8s-exo/templates/k8s-node.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#cloud-config
write_files:
- path: /etc/yum.repos.d/kubernetes.repo
content: |
[kubernetes]
name=Kubernetes
baseurl=http://yum.kubernetes.io/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg

package_update: true

packages:
- docker
- kubelet
- kubeadm
- kubectl
- kubernetes-cni

power_state:
mode: reboot
13 changes: 0 additions & 13 deletions roles/k8s-remove/tasks/delete_context.yml

This file was deleted.

4 changes: 0 additions & 4 deletions roles/k8s-remove/tasks/delete_inv.yml

This file was deleted.

7 changes: 0 additions & 7 deletions roles/k8s-remove/tasks/delete_secgroup.yml

This file was deleted.

95 changes: 0 additions & 95 deletions roles/k8s-remove/tasks/delete_secgroup_rules.yml

This file was deleted.

12 changes: 0 additions & 12 deletions roles/k8s-remove/tasks/delete_vm.yml

This file was deleted.

6 changes: 0 additions & 6 deletions roles/k8s-remove/tasks/main.yml

This file was deleted.

8 changes: 3 additions & 5 deletions roles/k8s/tasks/create_inv.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Create inventory file

- name: Create inventory file
template:
src: inventory.j2
dest: ./inventory
tags: inventory
- name: Create inventory file
template: src=inventory.j2 dest=./inventory
tags: inventory
Loading

0 comments on commit 2ea4e26

Please sign in to comment.