Skip to content

Instantly share code, notes, and snippets.

@aledbf
Last active February 19, 2018 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aledbf/2985931789f3c182f22b to your computer and use it in GitHub Desktop.
Save aledbf/2985931789f3c182f22b to your computer and use it in GitHub Desktop.
apiVersion: v1
kind: ReplicationController
metadata:
name: service-loadbalancer
labels:
app: service-loadbalancer
version: v1
spec:
replicas: 1
selector:
app: service-loadbalancer
version: v1
template:
metadata:
labels:
app: service-loadbalancer
version: v1
spec:
containers:
- image: aledbf/servicelb:0.2
imagePullPolicy: Always
livenessProbe:
httpGet:
path: /healthz
port: 8081
scheme: HTTP
initialDelaySeconds: 30
timeoutSeconds: 5
name: haproxy
ports:
# All http services
- containerPort: 80
hostPort: 80
protocol: TCP
# haproxy stats
- containerPort: 1936
hostPort: 1936
protocol: TCP

If you already have a running cluster skip next step (please change the IP address used in the commands)

From 0 to k8s -> https://coreos.com/kubernetes/docs/latest/kubernetes-on-vagrant-single.html

kubectl run example-go --image=deis/example-go --replicas=1 --port=8080
kubectl expose rc example-go --port=8080
kubectl scale --replicas=3 replicationcontrollers example-go

kubectl annotate svc example-go "serviceloadbalancer/lb.host=www.k8s.io"

wget https://gist.githubusercontent.com/aledbf/2985931789f3c182f22b/raw/53ff53f49a598fd7ee67e5a6cc0d12526313c2fe/haproxy-rc.yaml

kubectl create -f haproxy-rc.yaml

(wait)

kubectl describe pods -l app=service-loadbalancer

#Routing using the service name:

http://172.17.4.99/example-go:8080

#Routing using Host header:

kubectl annotate svc example-go "serviceloadbalancer/lb.host=www.k8s.io"

curl -v 172.17.4.99 -H "Host: www.k8s.io"

##Session Affinity:

If non-empty, set the session affinity for the service to this; legal values: 'None', 'ClientIP'

kubectl expose example-go --session-affinity="ClusterIP"

if the service already exists

kubectl patch svc example-go -p '{"spec":{"sessionAffinity":"ClientIP"}}'
kubectl expose example-go --session-affinity="ClientIP"
kubectl annotate svc example-go "serviceloadbalancer/lb.cookie-sticky-session=true"

if the service already exists

kubectl patch svc example-go -p '{"spec":{"sessionAffinity":"ClientIP"}}'
kubectl annotate svc example-go "serviceloadbalancer/lb.cookie-sticky-session=true"

Multiple namespaces

  • --namespace different namespace than "default"
  • --all-namespaces all available namespaces

kubectl 1.1.0 is required (because of the annotate command)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment