Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recipe: Splunk #16

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ output "bastion-public_dns" {
output "bastion-public_ip" {
value = "${module.openshift.bastion-public_ip}"
}
output "splunk-private_ip" {
value = "${module.openshift.splunk-private_ip}"
}
output "splunk-console-url" {
value = "http://${module.openshift.splunk-public_dns}:8000"
}
7 changes: 7 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ openshift:
# Open the console.
browse-openshift:
open $$(terraform output master-url)
browse-splunk:
open $$(terraform output splunk-console-url)

# SSH onto the master.
ssh-bastion:
Expand All @@ -42,4 +44,9 @@ sample:
oc new-project sample
oc process -f ./sample/counter-service.yml | oc create -f -

# Setup splunk.
splunk:
cat ./recipes/splunk/setup-cluster.sh | ssh -A ec2-user@$$(terraform output bastion-public_dns) ssh master.openshift.local
sed "s/\$${SPLUNK_FORWARD_SERVER}/$$(terraform output splunk-private_ip)/" ./recipes/splunk/splunk-forwarder.template.yml | ssh -A ec2-user@$$(terraform output bastion-public_dns) ssh master.openshift.local oc create -f -

.PHONY: sample
8 changes: 8 additions & 0 deletions modules/openshift/99-outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,11 @@ output "bastion-private_dns" {
output "bastion-private_ip" {
value = "${aws_instance.bastion.private_ip}"
}

# Output some information about the Splunk host.
output "splunk-private_ip" {
value = "${aws_instance.splunk.private_ip}"
}
output "splunk-public_dns" {
value = "${aws_instance.splunk.public_dns}"
}
51 changes: 51 additions & 0 deletions modules/openshift/files/setup-splunk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

# This script template is expected to be populated during the setup of a
# OpenShift node. It runs on host startup.

# Log everything we do.
set -x
exec > /var/log/user-data.log 2>&1

# Create initial logs config.
cat > ./awslogs.conf << EOF
[general]
state_file = /var/awslogs/state/agent-state

[/var/log/messages]
log_stream_name = openshift-splunk-{instance_id}
log_group_name = /var/log/messages
file = /var/log/messages
datetime_format = %b %d %H:%M:%S
buffer_duration = 5000
initial_position = start_of_file

[/var/log/user-data.log]
log_stream_name = splunk-{instance_id}
log_group_name = /var/log/user-data.log
file = /var/log/user-data.log
EOF

# Download and run the AWS logs agent.
curl https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py -O
python ./awslogs-agent-setup.py --non-interactive --region us-east-1 -c ./awslogs.conf

# Start the awslogs service, also start on reboot.
# Note: Errors go to /var/log/awslogs.log
service awslogs start
chkconfig awslogs on

# Download splunk.
aws s3 cp s3://dwmkerr-public/splunk-7.0.0-c8a78efdd40f-Linux-x86_64.tgz ./splunk.tgz
tar xvzf splunk.tgz -C /opt

# Everything else we do now is with the splunk binary.
cd /opt/splunk/bin

# Start splunk on reboot, then start splunk. Set the admin password.
./splunk enable boot-start --accept-license
./splunk start --accept-license
./splunk edit user admin -password 123 -role admin -auth admin:changeme

# Enable receiving of events on 9997.
./splunk enable listen 9997 -auth admin:123
53 changes: 53 additions & 0 deletions modules/openshift/recipe-splunk.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# This security group allows public ingress on port 8000, which is Splunk's
# management console port.
resource "aws_security_group" "splunk-public-management-ingress" {
name = "openshift-public-management-"
description = "Security group that allows ingress on port 8000"
vpc_id = "${aws_vpc.openshift.id}"

ingress {
from_port = 8000
to_port = 8000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

tags {
Name = "OpenShift Splunk Management Public Access"
Project = "openshift"
}
}

// Create the userdata script.
data "template_file" "setup-splunk" {
template = "${file("${path.module}/files/setup-splunk.sh")}"

// Currently, no vars needed.
}

resource "aws_instance" "splunk" {
ami = "${data.aws_ami.amazonlinux.id}"
instance_type = "t2.medium"
subnet_id = "${aws_subnet.public-subnet.id}"
# The profile below provides access to cloudwatch.
iam_instance_profile = "${aws_iam_instance_profile.openshift-instance-profile.id}"
user_data = "${data.template_file.setup-splunk.rendered}"

security_groups = [
"${aws_security_group.openshift-vpc.id}",
"${aws_security_group.openshift-public-egress.id}",
"${aws_security_group.splunk-public-management-ingress.id}",
]

# Give ourselves a bit more space...
root_block_device {
volume_size = 50
}

key_name = "${aws_key_pair.keypair.key_name}"

tags {
Name = "OpenShift Splunk"
Project = "openshift"
}
}
5 changes: 5 additions & 0 deletions recipes/splunk/setup-cluster.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Create the forwarder service account.
# Add priviledges to mount volumes and run as root.
oc create sa splunk-forwarder
oadm policy add-scc-to-user anyuid system:serviceaccount:default:splunk-forwarder
oadm policy add-scc-to-user privileged system:serviceaccount:default:splunk-forwarder
69 changes: 69 additions & 0 deletions recipes/splunk/splunk-forwarder.template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: splunk-forwarder
spec:
selector:
matchLabels:
name: splunk-forwarder
template:
metadata:
labels:
name: splunk-forwarder
spec:
serviceAccountName: splunk-forwarder
restartPolicy: Always
nodeSelector:
region: primary
containers:
- name: splunk-forwarder
image: splunk/universalforwarder:6.5.2-monitor
securityContext:
privileged: true
env:
- name: SPLUNK_START_ARGS
value: "--accept-license --answer-yes"
- name: SPLUNK_USER
value: root
- name: SPLUNK_FORWARD_SERVER
value: "${SPLUNK_FORWARD_SERVER}:9997"
# Monitor the containers logs.
- name: SPLUNK_ADD_1
value: "monitor /var/log/containers"
volumeMounts:
- mountPath: /var/run/docker.sock
readOnly: true
name: docker-socket
- mountPath: /var/lib/docker/containers
readOnly: true
name: var-lib-docker-containers
- mountPath: /var/log/containers
readOnly: true
name: var-log-containers
- mountPath: /var/log/pods
readOnly: true
name: var-log-pods
# Having some weird issues with chown permissions here, skipping for now...
#- mountPath: /opt/splunk/etc
# name: opt-splunk-etc
# - mountPath: /opt/splunk/var
# name: opt-splunk-var
volumes:
- name: docker-socket
hostPath:
path: /var/run/docker.sock
- name: var-lib-docker-containers
hostPath:
path: /var/lib/docker/containers
- name: var-log-containers
hostPath:
path: /var/log/containers
- name: var-log-pods
hostPath:
path: /var/log/pods
- name: opt-splunk-etc
hostPath:
path: /opt/splunk/etc
- name: opt-splunk-var
hostPath:
path: /opt/splunk/var