Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rosskukulinski committed May 8, 2017
0 parents commit da3cc4b
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM rethinkdb:2.3.5

MAINTAINER Ross Kukulinski <ross@kukulinski.com>

RUN apt-get update && \
apt-get install -yq curl && \
rm -rf /var/cache/apt/* && rm -rf /var/lib/apt/lists/*

ADD http://stedolan.github.io/jq/download/linux64/jq /usr/bin/jq
RUN chmod +x /usr/bin/jq

ADD https://github.com/Yelp/dumb-init/releases/download/v1.2.0/dumb-init_1.2.0_amd64 /usr/local/bin/dumb-init
RUN chmod +x /usr/local/bin/dumb-init

COPY ./run.sh ./probe ./ready-probe.sh /
RUN chmod u+x /run.sh /probe /ready-probe.sh

ENTRYPOINT ["/usr/local/bin/dumb-init", "/run.sh"]
10 changes: 10 additions & 0 deletions Dockerfile.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM golang:1.6
MAINTAINER Chris Dornsife <chris@applariat.com>

# Build container to have a consistent go build environment

COPY . /go/src/probe
WORKDIR /go/src/probe

RUN go get ./... \
&& CGO_ENABLED=0 GOOS=linux go build -a -ldflags '-w -s' -o /target/probe .
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
IMAGE=rosskukulinski/rethinkdb-kubernetes
TAG=2.3.5-v1

probe:
./build-probe.sh

image: probe
docker build -t ${IMAGE}:${TAG} .

push: image
docker push ${IMAGE}:${TAG}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# RethinkDB Image for Kubernetes

This is the Docker image that's the basis for a [Kubernetes RethinkDB Cluster](https://github.com/rosskukulinski/kubernetes-rethinkdb-cluster).
18 changes: 18 additions & 0 deletions build-probe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

# Author: Chris Dornsife chris@dornsife.com
# This will run a build container and extract a binary.
# This doesn't require the use of a mount so it can be used
# in a build pipeline such as bitbucket.
PROJ=probe

HASH=`date +%s`
BUILD_NAME=${PROJ}-build-${HASH}

docker build -t ${BUILD_NAME} -f Dockerfile.build . || exit 1
docker create --name ${BUILD_NAME} ${BUILD_NAME} /bin/true || exit 1
docker cp ${BUILD_NAME}:/target/$PROJ ./$PROJ || exit 1
docker rm ${BUILD_NAME} || exit 1
docker rmi -f ${BUILD_NAME} || exit 1

chmod +x $PROJ
Binary file added probe
Binary file not shown.
40 changes: 40 additions & 0 deletions probe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"log"
"os"
r "gopkg.in/gorethink/gorethink.v2"
)


func main() {

url := os.Getenv("RETHINKDB_URL")

if url == "" {
url = "localhost:28015"
}

session, err := r.Connect(r.ConnectOpts{
Address: url,
Database: "rethinkdb",
})

if err != nil {
log.Fatalln(err.Error())
}

res, err := r.Table("server_status").Pluck("id", "name").Run(session)
if err != nil {
log.Fatalln(err.Error())
}
defer res.Close()

if res.IsNil() {
log.Fatalln("no server status results found")
}

log.Printf("A-OK!")

}

11 changes: 11 additions & 0 deletions ready-probe.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# Fail with first error
set -e

# Checks if the rethinkdb instance is ready to operate.
# This will be used to direct client traffic
# And, more instances will not be created by the petset until it returns success.

# For now, try to hit the app at 8080
curl localhost:8080
93 changes: 93 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/bin/bash

# Copyright 2015 The Kubernetes Authors All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o pipefail

POD_NAMESPACE=${POD_NAMESPACE:-default}
POD_IP=${POD_IP:-127.0.0.1}
RETHINK_CLUSTER=${RETHINK_CLUSTER:-"rethinkdb"}
POD_NAME=${POD_NAME:-"NO_POD_NAME"}

# Transform - to _ to comply with requirements
SERVER_NAME=$(echo ${POD_NAME} | sed 's/-/_/g')

echo "Using additional CLI flags: ${@}"
echo "Pod IP: ${POD_IP}"
echo "Pod namespace: ${POD_NAMESPACE}"
echo "Using service name: ${RETHINK_CLUSTER}"
echo "Using server name: ${SERVER_NAME}"

echo "Checking for other nodes..."
if [[ -n "${KUBERNETES_SERVICE_HOST}" && -z "${USE_SERVICE_LOOKUP}" ]]; then
echo "Using endpoints to lookup other nodes..."
URL="https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT}/api/v1/namespaces/${POD_NAMESPACE}/endpoints/${RETHINK_CLUSTER}"
echo "Endpoint url: ${URL}"
echo "Looking for IPs..."
token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
# try to pick up first different ip from endpoints
IP=$(curl -s ${URL} --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt --header "Authorization: Bearer ${token}" \
| jq -s -r --arg h "${POD_IP}" '.[0].subsets | .[].addresses | [ .[].ip ] | map(select(. != $h)) | .[0]') || exit 1
[[ "${IP}" == null ]] && IP=""
JOIN_ENDPOINTS="${IP}"
else
echo "Using service to lookup other nodes..."
# We can just use ${RETHINK_CLUSTER} due to dns lookup
# Instead though, let's be explicit:
JOIN_ENDPOINTS=$(getent hosts "${RETHINK_CLUSTER}.${POD_NAMESPACE}.svc.cluster.local" | awk '{print $1}')

# Let's filter out our IP address if it's in there...
JOIN_ENDPOINTS=$(echo ${JOIN_ENDPOINTS} | sed -e "s/${POD_IP}//g")
fi

# xargs echo removes extra spaces before/after
# tr removes extra spaces in the middle
JOIN_ENDPOINTS=$(echo ${JOIN_ENDPOINTS} | xargs echo | tr -s ' ')

if [ -n "${JOIN_ENDPOINTS}" ]; then
echo "Found other nodes: ${JOIN_ENDPOINTS}"

# Now, transform join endpoints into --join ENDPOINT:29015
# Put port after each
JOIN_ENDPOINTS=$(echo ${JOIN_ENDPOINTS} | sed -r 's/([0-9.])+/&:29015/g')

# Put --join before each
JOIN_ENDPOINTS=$(echo ${JOIN_ENDPOINTS} | sed -e 's/^\|[ ]/&--join /g')
else
echo "No other nodes detected, will be a single instance."
if [ -n "$PROXY" ]; then
echo "Cannot start in proxy mode without endpoints."
exit 1
fi
fi

if [[ -n "${PROXY}" ]]; then
echo "Starting in proxy mode"
set -x
exec rethinkdb \
proxy \
--canonical-address ${POD_IP} \
--bind all \
${JOIN_ENDPOINTS} \
${@}
else
set -x
exec rethinkdb \
--server-name ${SERVER_NAME} \
--canonical-address ${POD_IP} \
--bind all \
${JOIN_ENDPOINTS} \
${@}
fi

0 comments on commit da3cc4b

Please sign in to comment.