Skip to content

Commit

Permalink
Make test cluster actually work - networking-wise.
Browse files Browse the repository at this point in the history
  • Loading branch information
geerlingguy committed Dec 18, 2019
1 parent 978f556 commit 0be3757
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
hello-go/hello
*/roles/geerlingguy.*
.vagrant
6 changes: 6 additions & 0 deletions cluster-local-vms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ You can log into the master node with the command:
$ vagrant ssh kube1

From there, run `sudo su` to switch to the root user, and then you can use `kubectl` to manage the Kubernetes cluster.

## Running the `test-deployment.yml` playbook

You can run a playbook to run a test deployment and service in the cluster, and verify they are working correctly:

$ ansible-playbook -i inventory test-deployment.yml
1 change: 1 addition & 0 deletions cluster-local-vms/ansible.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[defaults]
roles_path = ./roles
nocows = 1
host_key_checking = False
21 changes: 21 additions & 0 deletions cluster-local-vms/files/hello-k8s-deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-k8s
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: hello-k8s
template:
metadata:
labels:
app: hello-k8s
spec:
containers:
- name: hello-k8s
image: paulbouwer/hello-kubernetes:1.5
ports:
- containerPort: 8080
13 changes: 13 additions & 0 deletions cluster-local-vms/files/hello-k8s-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
apiVersion: v1
kind: Service
metadata:
name: hello-k8s
namespace: default
spec:
type: NodePort
ports:
- port: 8080
targetPort: 8080
selector:
app: hello-k8s
10 changes: 10 additions & 0 deletions cluster-local-vms/files/kube-flannel-patch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--- kube-flannel.yml 2019-12-18 09:29:04.000000000 -0600
+++ kube-flannel-virtualbox.yml 2019-12-18 09:30:01.000000000 -0600
@@ -189,6 +189,7 @@
args:
- --ip-masq
- --kube-subnet-mgr
+ - --iface=enp0s8
resources:
requests:
cpu: "100m"
14 changes: 14 additions & 0 deletions cluster-local-vms/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@
vars_files:
- vars/main.yml

pre_tasks:
# See: https://www.jeffgeerling.com/k8s-cni-virtualbox
- include_tasks: tasks/flannel-setup.yml
when: inventory_hostname == 'kube1'

# See: https://github.com/kubernetes/kubernetes/issues/71305
- name: Use iptables-legacy instead of nftables.
alternatives:
name: '{{ item.name }}'
path: '{{ item.path }}'
with_items:
- { name: iptables, path: /usr/sbin/iptables-legacy }
- { name: ip6tables, path: /usr/sbin/ip6tables-legacy }

roles:
- role: geerlingguy.security
tags: ['security']
Expand Down
14 changes: 14 additions & 0 deletions cluster-local-vms/tasks/flannel-setup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
- name: Retrieve current flannel manifest from GitHub.
get_url:
url: https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
dest: ~/kube-flannel.yml

- name: Patch Flannel manifest with VirtualBox interface.
patch:
src: files/kube-flannel-patch.txt
dest: ~/kube-flannel.yml

- name: Set the correct path for the patched Flannel manifest.
set_fact:
kubernetes_flannel_manifest_file: ~/kube-flannel.yml
42 changes: 42 additions & 0 deletions cluster-local-vms/test-deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
- hosts: kube1
become: true

vars_files:
- vars/main.yml

pre_tasks:
- name: Ensure k8s module dependencies are installed.
pip:
name: openshift
state: present

tasks:
- name: Create hello-k8s Deployment and Service.
k8s:
state: present
definition: "{{ lookup('file', 'files/' + item) }}"
with_items:
- hello-k8s-deployment.yml
- hello-k8s-service.yml

- name: Wait for hello-k8s pods to be ready.
command: >
kubectl wait --for=condition=Ready
pods --selector app=hello-k8s --timeout=60s
changed_when: false

- name: Get hello-k8s service details.
k8s_info:
kind: Service
name: hello-k8s
namespace: default
register: svc

- name: Set the service NodePort as a variable.
set_fact:
port: "{{ svc['resources'][0]['spec']['ports'][0]['nodePort'] }}"

- name: Test a request to the service.
uri:
url: http://{{ ansible_host }}:{{ port }}/
12 changes: 0 additions & 12 deletions cluster-local-vms/vars/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
---
# Ansible configuration.
ansible_python_interpreter: auto

# Docker configuration.
docker_install_compose: false
docker_users:
Expand All @@ -15,12 +12,3 @@ swap_file_path: /dev/mapper/packer--debian--10--amd64--vg-swap_1
kubernetes_allow_pods_on_master: false
kubernetes_kubelet_extra_args: '--node-ip={{ ansible_host }}'
kubernetes_version: '1.16'
kubernetes_packages:
- name: kubelet=1.16.4-00
state: present
- name: kubectl=1.16.4-00
state: present
- name: kubeadm=1.16.4-00
state: present
- name: kubernetes-cni
state: present
3 changes: 2 additions & 1 deletion tests/cluster-local-vms.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ pip install ansible ansible-lint
# Install requirements.
ansible-galaxy install -r requirements.yml

# Lint the Cluster playbook.
# Lint the Cluster playbooks.
ansible-lint main.yml
ansible-lint test-deployment.yml

0 comments on commit 0be3757

Please sign in to comment.