carlessanagustin
11/25/2018 - 8:52 PM

Hello World Ingress in Kubernetes

Hello World Ingress in Kubernetes

#!/usr/bin/env bash

: '
We'll see...
* Pod replicas
* Helm: The Kubernetes Package Manager
* Ingress resource: Exposes HTTP and HTTPS routes from outside the cluster to services within the cluster.
'

NS_HELLO=hello
NS_INGRESS=ingress

# check nodes
kubectl get nodes

# helm init
kubectl -n kube-system create serviceaccount tiller
kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller
helm init --service-account tiller --upgrade
helm repo update

# launch ingress
kubectl create namespace $NS_INGRESS
helm install stable/nginx-ingress --name ingress --namespace $NS_INGRESS

kubectl -n $NS_INGRESS get pod,deploy,rs,svc,ep,ingress -o wide
sleep 10

# launch backend
kubectl create namespace $NS_HELLO
kubectl -n $NS_HELLO create deploy hostnames --image=nginxdemos/hello
kubectl -n $NS_HELLO scale --replicas=3 deploy hostnames
kubectl -n $NS_HELLO create service clusterip hostnames --tcp=8080:80

kubectl -n $NS_HELLO get pod,deploy,rs,svc,ep,ingress -o wide
sleep 10

# add ingress rule for backend
cat << EOF > ingress-hostnames.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: hello
  namespace: $NS_HELLO
spec:
  rules:
    - host: www.example.com
      http:
        paths:
          - backend:
              serviceName: hostnames
              servicePort: 8080
            path: /hello
EOF
kubectl create -f ingress-hostnames.yaml

# cloud
EXTERNAL_IP=$(kubectl -n ingress get service/ingress-nginx-ingress-controller -o jsonpath='{.status.loadBalancer.ingress.ip}')
EXTERNAL_PORT=80
## inhouse
#EXTERNAL_IP=$(kubectl get nodes -o jsonpath='{.items[1].status.addresses[0].address}')
#EXTERNAL_PORT=$(kubectl -n ingress get service/ingress-nginx-ingress-controller -o jsonpath='{.spec.ports[0].nodePort}')

sudo -- sh -c 'echo "$EXTERNAL_IP  www.example.com" >> /etc/hosts'

curl http://www.example.com:$EXTERNAL_PORT/hostnames | grep -i "name:" | awk '{print $2}' | awk '{gsub(/<[^>]*>/,"")};1'


destroy (){
	helm delete --purge ingress && kubectl delete ns $NS_INGRESS
	kubectl -n $NS_HELLO delete deploy,svc,ingress --all && kubectl delete ns $NS_HELLO
}