Skip to content

Instantly share code, notes, and snippets.

@vukhanhtruong
Last active February 19, 2021 07:08
Show Gist options
  • Save vukhanhtruong/01ec81af25d0de62e64dd95315c14a10 to your computer and use it in GitHub Desktop.
Save vukhanhtruong/01ec81af25d0de62e64dd95315c14a10 to your computer and use it in GitHub Desktop.
Terraform, Helm, Nginx Ingress and Hello-world app

Usage

Start minikube

minikube start --driver=kvm2

Terraform apply

tf apply --auto-approve

LoadBalancer access

A LoadBalancer service is the standard way to expose a service to the internet. With this method, each service gets its own IP address.

Services of type LoadBalancer can be exposed via the minikube tunnel command. It must be run in a separate terminal window to keep the LoadBalancer running.

minikube tunnel --cleanup

Get exposed IP

$ kubectl get svc

Sample result

NAME                                     TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)                      AGE
kubernetes                               ClusterIP      10.96.0.1       <none>          443/TCP                      20h
reverse-proxy-nginx-ingress-controller   LoadBalancer   10.105.192.35   10.105.192.35   80:31654/TCP,443:31351/TCP   29m

Try in your browser

Open in your browser

http://REPLACE_WITH_EXTERNAL_IP:8080

http://REPLACE_WITH_EXTERNAL_IP:8080/hello-world

Reference

terraform {
required_providers {
kubernetes = {
source = "hashicorp/kubernetes"
version = ">= 2.0.2"
}
helm = {
source = "hashicorp/helm"
version = ">= 2.0.2"
}
}
}
provider "kubernetes" {
config_path = "~/.kube/config"
}
resource "kubernetes_namespace" "develop" {
metadata {
name = "develop"
}
}
resource "kubernetes_deployment" "hello_world" {
metadata {
name = "hello-world"
namespace = kubernetes_namespace.develop.metadata.0.name
}
spec {
replicas = 2
selector {
match_labels = {
app = "hello-world"
}
}
template {
metadata {
labels = {
app = "hello-world"
}
}
spec {
container {
image = "tutum/hello-world"
name = "hello-world"
port {
container_port = 80
}
}
}
}
}
}
resource "kubernetes_service" "hello_world" {
metadata {
name = "hello-world"
namespace = kubernetes_namespace.develop.metadata.0.name
}
spec {
selector = {
app = kubernetes_deployment.hello_world.spec.0.template.0.metadata.0.labels.app
}
port {
port = 80
target_port = 80
}
}
}
resource "kubernetes_ingress" "hello_world" {
metadata {
name = "hello-world"
namespace = kubernetes_namespace.develop.metadata.0.name
annotations = {
"kubernetes.io/ingress.class" = "nginx"
}
}
spec {
backend {
service_name = "hello-world"
service_port = 80
}
rule {
http {
path {
backend {
service_name = "hello-world"
service_port = 80
}
path = "/hello-world"
}
}
}
}
}
provider "helm" {
debug = true
kubernetes {
config_path = "~/.kube/config"
}
}
resource "helm_release" "reverse-proxy" {
name = "reverse-proxy"
repository = "https://charts.bitnami.com/bitnami"
chart = "nginx-ingress-controller"
//@see https://github.com/bitnami/charts/tree/master/bitnami/nginx-ingress-controller
set {
name = "service.type"
value = "LoadBalancer"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment