Popular Posts

Kubernetes Ingress with Minikube

1. Using Pod Configuration

Kubernetes ingress is a collection of routing rules that govern how external users access services running in a Kubernetes cluster.

— ThirupathiReddy Vajjala

Kubernetes does not run containers directly; instead it wraps one or more containers into a higher-level structure called a pod.

There are two possible ways we can create pods.

1. Using Pod Configuration

Pod contains one /more different kind of container grouped and assigned under single IP. Any containers in the same pod will share the same resources and local network. Containers can easily communicate with other containers in the same pod as though they were on the same machine while maintaining a degree of isolation from others.

pod_config.yaml
apiVersion: v1
kind: Pod
metadata:
 name: documentation.tvajjala.com
 labels:
  app: documentation
spec:
 containers:
  - name: docuementation
    image: tvajjala/documentation
    ports:
    - containerPort: 8080

2. Using Deployment Configuration

A deployment’s primary purpose is to declare how many replicas of a pod should be running at a time. When a deployment is added to the cluster, it will automatically spin up the requested number of pods, and then monitor them. If a pod dies, the deployment will automatically re-create it.

deployment_config.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: documentation-deployment
spec:
 replicas: 1
 template:
  metadata:
   labels:
    app: documentation
  spec:
   containers:
   - name: documentation
     image: tvajjala/documentation
     ports:
     - name: doc-port
       containerPort: 8080
     livenessProbe:
       httpGet:
          path: /
          port: doc-port
       initialDelaySeconds: 15
       timeoutSeconds: 30
It is highly recommended that you use a Deployment to create your pods. It watches for failed pods and will start up new pods as required to maintain the specified number.

When to use Pods directly?

If you don’t want a Deployment to monitor your pod (e.g. your pod is writing non-persistent data which won’t survive a restart, or your pod is intended to be very short-lived), you can create a pod directly with the create command.

Services

A Service in Kubernetes is an abstraction defining a logical set of Pods and an access policy.

Services can be exposed in different ways by specifying a type in the service spec, and different types determine accessibility from inside and outside of cluster.

  1. ClusterIP (default)

  2. NodePort

  3. LoadBalancer

  4. Ingress

ClusterIP.sh
tvajjala$kubectl get svc nginx-svc
NAME        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
nginx-svc   ClusterIP   10.100.196.54   <none>        80/TCP    4m33s
NodePort.sh
tvajjala$kubectl get svc nginx-svc
NAME        TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx-svc   NodePort   10.100.196.54   <none>        80:32293/TCP   7m25s
LoadBalancer.sh
tvajjala$kubectl get svc
NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP      10.96.0.1        <none>        443/TCP        28h
nginx-svc    LoadBalancer   10.110.188.121   *`<pending>`*     80:31159/TCP   6m42s
If you are running with Minikube EXTERNAL-IP always in pending state.

To know your external IP address run below command

externalIP.sh
tvajjala$minikube service nginx-svc --url

http://192.168.99.102:31159

Run below command to check status

tunnel.sh
tvajjala$minikube tunnel
Password:
Status:
 machine: minikube
 pid: 78370
 route: 10.96.0.0/12 -> 192.168.99.102
 minikube: Running
 services: [nginx-svc]
    errors:
  minikube: no errors
  router: no errors
  loadbalancer emulator: no errors

Ingress

You have to enable ingress addons by following command before creating ingress rules. You can also enable it before executing any other command

ingress.sh
tvajjala$minikube addons enable ingress
✅  ingress was successfully enabled
Wait until the pods are up and running. You can check by executing following command and wait for the similar output
status.sh
tvajjala$kubectl get pods -n kube-system | grep nginx-ingress-controller
nginx-ingress-controller-586cdc477c-j75dz   1/1     Running   0          2m36s
deployment.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
 name: wiremock-server
spec:
 selector:
  matchLabels:
    app: wiremock-server
 replicas: 1
 template:
  metadata:
   labels:
    app: wiremock-server
  spec:
   containers:
   - name: wiremock
     image: tvajjala/wiremock
     ports:
     - name: doc-port # Give name to port so that you can refer by name , instead of hard-coding in services
       containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: wiremock-server
  name: wiremock-service
  namespace: default
spec:
  type: LoadBalancer
  ports:
    - port: 8080
      targetPort: doc-port
  selector:
    app: wiremock-server
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: doc-ingress
  annotations:
    INGRESS.kubernetes.io/rewrite-target: /
spec:
  backend:
    serviceName: default-http-backend
    servicePort: 8080
  rules:
  - host: tvajjala.com
    http:
      paths:
      - path: /
        backend:
          serviceName: wiremock-service
          servicePort: 8080

Run below command to update your host file

host_file.sh
$ echo "$(minikube ip) tvajjala.com" | sudo tee -a /etc/hosts

Verify host file with below command

print_hostfile.sh
$ cat /etc/hosts

Open your browser and point to http://tvajjala.com, which serves the content.

No comments:

Post a Comment