Skip to content

Instantly share code, notes, and snippets.

@alexnederlof
Last active March 15, 2018 10:52
Show Gist options
  • Save alexnederlof/8ef8477cce49a4b532c79d39eaae77f5 to your computer and use it in GitHub Desktop.
Save alexnederlof/8ef8477cce49a4b532c79d39eaae77f5 to your computer and use it in GitHub Desktop.
Local development with Kubernetes
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: bar
spec:
template:
metadata:
labels:
app: bar
spec:
containers:
- name: bar
image: paddycarey/go-echo
ports:
- name: http
containerPort: 8000
---
kind: Service
apiVersion: v1
metadata:
name: bar
spec:
selector:
app: bar
ports:
- protocol: "TCP"
port: 8081
targetPort: 8000
type: LoadBalancer
#!/usr/bin/env bash
#
# Creates a service with an endpoint that is your computer's IP so services can route out of the cluster to your
# IDE
#
set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TEMPLATE="${DIR}/ide-template.yaml"
service=$1
port=$2
if [ -z $service ] ; then
echo "Specify the service as the first param"
exit 1
fi
if ! [[ $port =~ ^-?[0-9]+$ ]] ; then
echo "Second argument must be the port number but was $port"
exit 1
fi
# now get the IP address
if [ "$(uname)" == "Darwin" ] ; then
interface=$(route -n get default | grep interface | awk '{print $2}')
#echo "You're on a mac and your default interface seems to be $interface"
ip=$(ipconfig getifaddr $interface)
else
ip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/')
fi
echo "Creating service ${service} on ${ip}:${port}"
tmpFile="$DIR/$service.tmp.ide.yml"
sed "s/{{port}}/${port}/g" $TEMPLATE > $tmpFile
if [ "$(uname)" == "Darwin" ] ; then
# Sed is different on OS-X And requires an argument for the -i flag.
sed -i '' "s/{{ip}}/${ip}/g" $tmpFile
sed -i '' "s/{{service}}/${service}/" $tmpFile
else
sed -i "s/{{ip}}/${ip}/g" $tmpFile
sed -i "s/{{service}}/${service}/" $tmpFile
fi
echo "Created:"
cat $tmpFile
if kubectl get services -o=name | grep -q $service ; then
echo "Deleting previous service"
kubectl delete service $service
sleep 1 # Give it some time to take care of that...
fi
echo "Posting the new service"
kubectl create -f $tmpFile
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: foo
spec:
template:
metadata:
labels:
app: foo
spec:
containers:
- name: foo
image: paddycarey/go-echo
ports:
- name: http
containerPort: 8000
---
kind: Service
apiVersion: v1
metadata:
name: foo
spec:
selector:
app: foo
ports:
- protocol: "TCP"
port: 8080
targetPort: 8000
type: LoadBalancer
kind: Service
apiVersion: v1
metadata:
name: foo
spec:
ports:
- protocol: TCP
port: 8080
targetPort: 8080
---
kind: Endpoints
apiVersion: v1
metadata:
name: foo
subsets:
- addresses:
- ip: 192.168.0.2
ports:
- port: 8080
kind: Service
apiVersion: v1
metadata:
name: {{service}}
spec:
ports:
- protocol: TCP
port: {{port}}
targetPort: {{port}}
---
kind: Endpoints
apiVersion: v1
metadata:
name: {{service}}
subsets:
- addresses:
- ip: {{ip}}
ports:
- port: {{port}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment