cwonrails
12/24/2015 - 5:04 AM

5 minute local Kubernetes Cluster on Mac

# Reference:
https://www.cloudgear.net/blog/2015/5-minutes-kubernetes-setup/

# install homebrew and cask
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

# install virtualbox
brew cask install virtualbox

# install dockertoolbox
brew cask install dockertoolbox

# add this to your bash profile and source into current shell
eval $(docker-machine env dev)

# clone demo kubernetes
cd ~/Code
git clone https://github.com/cloudgear-net/demo-kubernetes.git

# bring up kubernetes
cd demo-kubernetes
docker-compose up

# install kubernetes
brew install kubernetes-cli

# setup ssh tunnel for kubectl which uses port 8080
docker-machine ssh default -L 8080:localhost:8080

# use kubectl to list the nodes
kubectl get nodes
###########################################################
# NAME      LABELS                           STATUS    AGE
# default   kubernetes.io/hostname=default   Ready     36m
###########################################################

# Create demo service
kubectl run service-demo --image=geku/go-app:0.1 --port=5000  
kubectl get pods -l run=service-demo

# Scale service to 3 instances
kubectl scale rc service-demo --replicas=3  
kubectl get pods -l run=service-demo  

# list replication controllers
kubectl get rc
######################################################################################
# CONTROLLER     CONTAINER(S)   IMAGE(S)          SELECTOR           REPLICAS   AGE
# service-demo   service-demo   geku/go-app:0.1   run=service-demo   3          11m
######################################################################################

# expose our service on port 80
kubectl expose rc service-demo --port=80 --target-port=5000 --type=NodePort 

# This creates a load balancer and assigns our service a virtual IP where we can reach a random instance of our service. 
# Additionally it maps it to a random port on our host server.
# get the node port on your host server
kubectl get -o yaml service/service-demo | grep nodePort
# this returns nodePort: 31538

# now you can curl the load balanced service multiple times and get results from the different machines
curl $(docker-machine ip default):32382/json
################################################################################
# {"hostname":"service-demo-qk3kf","env":["PATH=/usr/local/sbin:/usr/local/bin
################################################################################

curl $(docker-machine ip default):32382/json
################################################################################
# {"hostname":"service-demo-i1tdt","env":["PATH=/usr/local/sbin:/usr/local/bin
################################################################################

# By sending multiple requests you can see that they are answered by different instances (varying hostname).

# remove all pods and the service
kubectl delete service/service-demo  
kubectl delete rc/service-demo