ingress-nginx.tf 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. resource "kubernetes_namespace" "ingress_nginx" {
  2. metadata {
  3. name = "ingress-nginx"
  4. labels = {
  5. "app.kubernetes.io/part-of" = "ingress-nginx"
  6. "app.kubernetes.io/name" = "ingress-nginx"
  7. }
  8. }
  9. }
  10. resource "kubernetes_config_map" "nginx_configuration" {
  11. metadata {
  12. name = "nginx-configuration"
  13. namespace = "ingress-nginx"
  14. labels = {
  15. "app.kubernetes.io/name" = "ingress-nginx"
  16. "app.kubernetes.io/part-of" = "ingress-nginx"
  17. }
  18. }
  19. }
  20. resource "kubernetes_config_map" "tcp_services" {
  21. metadata {
  22. name = "tcp-services"
  23. namespace = "ingress-nginx"
  24. labels = {
  25. "app.kubernetes.io/name" = "ingress-nginx"
  26. "app.kubernetes.io/part-of" = "ingress-nginx"
  27. }
  28. }
  29. }
  30. resource "kubernetes_config_map" "udp_services" {
  31. metadata {
  32. name = "udp-services"
  33. namespace = "ingress-nginx"
  34. labels = {
  35. "app.kubernetes.io/name" = "ingress-nginx"
  36. "app.kubernetes.io/part-of" = "ingress-nginx"
  37. }
  38. }
  39. }
  40. resource "kubernetes_service_account" "nginx_ingress_serviceaccount" {
  41. metadata {
  42. name = "nginx-ingress-serviceaccount"
  43. namespace = "ingress-nginx"
  44. labels = {
  45. "app.kubernetes.io/name" = "ingress-nginx"
  46. "app.kubernetes.io/part-of" = "ingress-nginx"
  47. }
  48. }
  49. }
  50. resource "kubernetes_cluster_role" "nginx_ingress_clusterrole" {
  51. metadata {
  52. name = "nginx-ingress-clusterrole"
  53. labels = {
  54. "app.kubernetes.io/name" = "ingress-nginx"
  55. "app.kubernetes.io/part-of" = "ingress-nginx"
  56. }
  57. }
  58. rule {
  59. verbs = ["list", "watch"]
  60. api_groups = [""]
  61. resources = ["configmaps", "endpoints", "nodes", "pods", "secrets"]
  62. }
  63. rule {
  64. verbs = ["get"]
  65. api_groups = [""]
  66. resources = ["nodes"]
  67. }
  68. rule {
  69. verbs = ["get", "list", "watch"]
  70. api_groups = [""]
  71. resources = ["services"]
  72. }
  73. rule {
  74. verbs = ["get", "list", "watch"]
  75. api_groups = ["extensions"]
  76. resources = ["ingresses"]
  77. }
  78. rule {
  79. verbs = ["create", "patch"]
  80. api_groups = [""]
  81. resources = ["events"]
  82. }
  83. rule {
  84. verbs = ["update"]
  85. api_groups = ["extensions"]
  86. resources = ["ingresses/status"]
  87. }
  88. }
  89. resource "kubernetes_role" "nginx_ingress_role" {
  90. metadata {
  91. name = "nginx-ingress-role"
  92. namespace = "ingress-nginx"
  93. labels = {
  94. "app.kubernetes.io/name" = "ingress-nginx"
  95. "app.kubernetes.io/part-of" = "ingress-nginx"
  96. }
  97. }
  98. rule {
  99. verbs = ["get"]
  100. api_groups = [""]
  101. resources = ["configmaps", "pods", "secrets", "namespaces"]
  102. }
  103. rule {
  104. verbs = ["get", "update"]
  105. api_groups = [""]
  106. resources = ["configmaps"]
  107. resource_names = ["ingress-controller-leader-nginx"]
  108. }
  109. rule {
  110. verbs = ["create"]
  111. api_groups = [""]
  112. resources = ["configmaps"]
  113. }
  114. rule {
  115. verbs = ["get"]
  116. api_groups = [""]
  117. resources = ["endpoints"]
  118. }
  119. }
  120. resource "kubernetes_role_binding" "nginx_ingress_role_nisa_binding" {
  121. metadata {
  122. name = "nginx-ingress-role-nisa-binding"
  123. namespace = "ingress-nginx"
  124. labels = {
  125. "app.kubernetes.io/name" = "ingress-nginx"
  126. "app.kubernetes.io/part-of" = "ingress-nginx"
  127. }
  128. }
  129. subject {
  130. kind = "ServiceAccount"
  131. name = "nginx-ingress-serviceaccount"
  132. namespace = "ingress-nginx"
  133. }
  134. role_ref {
  135. api_group = "rbac.authorization.k8s.io"
  136. kind = "Role"
  137. name = "nginx-ingress-role"
  138. }
  139. }
  140. resource "kubernetes_cluster_role_binding" "nginx_ingress_clusterrole_nisa_binding" {
  141. metadata {
  142. name = "nginx-ingress-clusterrole-nisa-binding"
  143. labels = {
  144. "app.kubernetes.io/name" = "ingress-nginx"
  145. "app.kubernetes.io/part-of" = "ingress-nginx"
  146. }
  147. }
  148. subject {
  149. kind = "ServiceAccount"
  150. name = "nginx-ingress-serviceaccount"
  151. namespace = "ingress-nginx"
  152. }
  153. role_ref {
  154. api_group = "rbac.authorization.k8s.io"
  155. kind = "ClusterRole"
  156. name = "nginx-ingress-clusterrole"
  157. }
  158. }
  159. resource "kubernetes_deployment" "nginx_ingress_controller" {
  160. metadata {
  161. name = "nginx-ingress-controller"
  162. namespace = "ingress-nginx"
  163. labels = {
  164. "app.kubernetes.io/name" = "ingress-nginx"
  165. "app.kubernetes.io/part-of" = "ingress-nginx"
  166. }
  167. }
  168. spec {
  169. replicas = 1
  170. selector {
  171. match_labels = {
  172. "app.kubernetes.io/name" = "ingress-nginx"
  173. "app.kubernetes.io/part-of" = "ingress-nginx"
  174. }
  175. }
  176. template {
  177. metadata {
  178. labels = {
  179. "app.kubernetes.io/part-of" = "ingress-nginx"
  180. "app.kubernetes.io/name" = "ingress-nginx"
  181. }
  182. annotations = {
  183. "prometheus.io/port" = "10254"
  184. "prometheus.io/scrape" = "true"
  185. }
  186. }
  187. spec {
  188. // Workaround for https://github.com/terraform-providers/terraform-provider-kubernetes/pull/261
  189. volume {
  190. name = kubernetes_service_account.nginx_ingress_serviceaccount.default_secret_name
  191. secret {
  192. secret_name = kubernetes_service_account.nginx_ingress_serviceaccount.default_secret_name
  193. }
  194. }
  195. container {
  196. name = "nginx-ingress-controller"
  197. image = "quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.24.1"
  198. args = [
  199. "/nginx-ingress-controller",
  200. "--configmap=$(POD_NAMESPACE)/nginx-configuration",
  201. "--tcp-services-configmap=$(POD_NAMESPACE)/nginx-ingress-tcp-conf",
  202. "--udp-services-configmap=$(POD_NAMESPACE)/udp-services",
  203. "--publish-service=$(POD_NAMESPACE)/ingress-nginx-service",
  204. "--annotations-prefix=nginx.ingress.kubernetes.io",
  205. ]
  206. port {
  207. name = "http"
  208. container_port = 80
  209. }
  210. port {
  211. name = "https"
  212. container_port = 443
  213. }
  214. env {
  215. name = "POD_NAME"
  216. value_from {
  217. field_ref {
  218. field_path = "metadata.name"
  219. }
  220. }
  221. }
  222. env {
  223. name = "POD_NAMESPACE"
  224. value_from {
  225. field_ref {
  226. field_path = "metadata.namespace"
  227. }
  228. }
  229. }
  230. liveness_probe {
  231. http_get {
  232. path = "/healthz"
  233. port = "10254"
  234. scheme = "HTTP"
  235. }
  236. initial_delay_seconds = 10
  237. timeout_seconds = 10
  238. period_seconds = 10
  239. success_threshold = 1
  240. failure_threshold = 3
  241. }
  242. readiness_probe {
  243. http_get {
  244. path = "/healthz"
  245. port = "10254"
  246. scheme = "HTTP"
  247. }
  248. timeout_seconds = 10
  249. period_seconds = 10
  250. success_threshold = 1
  251. failure_threshold = 3
  252. }
  253. security_context {
  254. run_as_user = 33
  255. allow_privilege_escalation = true
  256. }
  257. // Workaround for https://github.com/terraform-providers/terraform-provider-kubernetes/pull/261
  258. volume_mount {
  259. name = kubernetes_service_account.nginx_ingress_serviceaccount.default_secret_name
  260. mount_path = "/var/run/secrets/kubernetes.io/serviceaccount"
  261. read_only = true
  262. }
  263. }
  264. service_account_name = "nginx-ingress-serviceaccount"
  265. }
  266. }
  267. }
  268. }
  269. resource "kubernetes_service" "nginx-ingress-loadbalancer" {
  270. metadata {
  271. name = "ingress-nginx-service"
  272. namespace = "ingress-nginx"
  273. }
  274. spec {
  275. type = "LoadBalancer"
  276. port {
  277. // Needs to be first as this is where the DO loadbalancer sends its health checks
  278. port = 80
  279. name = "http"
  280. }
  281. port {
  282. port = 443
  283. name = "https"
  284. }
  285. port {
  286. port = 22
  287. name = "ssh"
  288. }
  289. selector = {
  290. "app.kubernetes.io/name" = "ingress-nginx"
  291. "app.kubernetes.io/part-of" = "ingress-nginx"
  292. }
  293. }
  294. }
  295. # A default backend for unmatched routes
  296. resource "kubernetes_deployment" "default-backend" {
  297. metadata {
  298. name = "default-backend"
  299. namespace = "ingress-nginx"
  300. }
  301. spec {
  302. selector {
  303. match_labels = {
  304. "app.kubernetes.io/name" = "default-backend"
  305. "app.kubernetes.io/part-of" = "ingress-nginx"
  306. }
  307. }
  308. replicas = 2
  309. template {
  310. metadata {
  311. labels = {
  312. "app.kubernetes.io/name" = "default-backend"
  313. "app.kubernetes.io/part-of" = "ingress-nginx"
  314. }
  315. }
  316. spec {
  317. termination_grace_period_seconds = 60
  318. container {
  319. name = "default-backend"
  320. image = "gcr.io/google_containers/defaultbackend:1.0"
  321. liveness_probe {
  322. http_get {
  323. path = "/healthz"
  324. port = 8080
  325. scheme = "HTTP"
  326. }
  327. initial_delay_seconds = 30
  328. timeout_seconds = 5
  329. }
  330. port {
  331. container_port = 8080
  332. }
  333. resources {
  334. limits {
  335. cpu = "10m"
  336. memory = "20Mi"
  337. }
  338. requests {
  339. cpu = "10m"
  340. memory = "20Mi"
  341. }
  342. }
  343. }
  344. }
  345. }
  346. }
  347. }
  348. resource "kubernetes_service" "default-backend" {
  349. metadata {
  350. name = "default-backend"
  351. namespace = "ingress-nginx"
  352. }
  353. spec {
  354. port {
  355. name = "http"
  356. port = 80
  357. protocol = "TCP"
  358. target_port = 8080
  359. }
  360. selector = {
  361. "app.kubernetes.io/name" = "default-backend"
  362. "app.kubernetes.io/part-of" = "ingress-nginx"
  363. }
  364. }
  365. }
  366. resource "kubernetes_ingress" "nginx-ingress-default" {
  367. metadata {
  368. name = "nginx-ingress-default"
  369. namespace = "ingress-nginx"
  370. annotations = {
  371. "kubernetes.io/ingress.class" = "nginx"
  372. "nginx.ingress.kubernetes.io/server-snippet" = "if ($host = 'brow.sh' ) {return 301 https://www.brow.sh$request_uri;}"
  373. }
  374. }
  375. spec {
  376. backend {
  377. service_name = "default-backend"
  378. service_port = 80
  379. }
  380. rule {
  381. host = "brow.sh"
  382. http {
  383. path {
  384. path = "/*"
  385. backend {
  386. service_name = "default-backend"
  387. service_port = 443
  388. }
  389. }
  390. }
  391. }
  392. }
  393. }