Commit a4139c83 by chenxx

init

0 parents
## Requirements
| Name | Version |
|------|---------|
| <a name="requirement_kubectl"></a> [kubectl](#requirement\_kubectl) | 1.14.0 |
## Providers
| Name | Version |
|------|---------|
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | n/a |
## Modules
| Name | Source | Version |
|------|--------|---------|
| <a name="module_deployment"></a> [deployment](#module\_deployment) | ./modules/deployment/kubernetes | n/a |
| <a name="module_ingress"></a> [ingress](#module\_ingress) | ./modules/ingress/kubernetes | n/a |
| <a name="module_service"></a> [service](#module\_service) | ./modules/service/kubernetes | n/a |
## Resources
| Name | Type |
|------|------|
| [kubernetes_service.service](https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/service) | data source |
## Inputs
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | 域名 | `string` | n/a | yes |
| <a name="input_env"></a> [env](#input\_env) | Name and value pairs to set as the environment variables | `map(string)` | `{}` | no |
| <a name="input_image"></a> [image](#input\_image) | Docker image name | `string` | n/a | yes |
| <a name="input_limit_cpu"></a> [limit\_cpu](#input\_limit\_cpu) | CPU limit. e.g. 0.5, 1, 2 | `string` | `"0.5"` | no |
| <a name="input_limit_memory"></a> [limit\_memory](#input\_limit\_memory) | Memory limit. e.g. 128Mi, 512Mi, 1Gi, 2Gi, 4Gi | `string` | `"512Mi"` | no |
| <a name="input_name"></a> [name](#input\_name) | Name of the deployment resource. Auto-generated if empty. | `string` | `""` | no |
| <a name="input_namespace"></a> [namespace](#input\_namespace) | Namespace to deploy. Auto-generated if empty. | `string` | `"default"` | no |
| <a name="input_path"></a> [path](#input\_path) | 访问路径 | `string` | `"/"` | no |
| <a name="input_path_type"></a> [path\_type](#input\_path\_type) | Path type for ingress rule | `string` | `"ImplementationSpecific"` | no |
| <a name="input_port"></a> [port](#input\_port) | Service ports to expose | `string` | `"7777"` | no |
| <a name="input_probe_http_path"></a> [probe\_http\_path](#input\_probe\_http\_path) | (Optional)http path for probe | `string` | `"/"` | no |
| <a name="input_probe_http_port"></a> [probe\_http\_port](#input\_probe\_http\_port) | (Optional)http port for probe | `string` | `"7777"` | no |
| <a name="input_replicas"></a> [replicas](#input\_replicas) | Replicas to deploy | `number` | `1` | no |
| <a name="input_request_cpu"></a> [request\_cpu](#input\_request\_cpu) | CPU request. e.g. 0.5, 1, 2 | `string` | `"0.1"` | no |
| <a name="input_request_memory"></a> [request\_memory](#input\_request\_memory) | Memory request. e.g. 128Mi, 512Mi, 1Gi, 2Gi, 4Gi | `string` | `"128Mi"` | no |
| <a name="input_restart_policy"></a> [restart\_policy](#input\_restart\_policy) | Restart policy for all containers within the pod. One of Always, OnFailure, Never | `string` | `"Always"` | no |
| <a name="input_walrus_metadata_namespace_name"></a> [walrus\_metadata\_namespace\_name](#input\_walrus\_metadata\_namespace\_name) | Walrus metadata namespace name. | `string` | `""` | no |
| <a name="input_walrus_metadata_service_name"></a> [walrus\_metadata\_service\_name](#input\_walrus\_metadata\_service\_name) | Walrus metadata service name. | `string` | `""` | no |
## Outputs
| Name | Description |
|------|-------------|
| <a name="output_ingress"></a> [ingress](#output\_ingress) | 访问入口ingress |
| <a name="output_ports"></a> [ports](#output\_ports) | Service Ports |
| <a name="output_service_ip"></a> [service\_ip](#output\_service\_ip) | Service IP |
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 24.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 94.6 94.6" style="enable-background:new 0 0 94.6 94.6;" xml:space="preserve">
<style type="text/css">
.st0{fill:none;}
.st1{fill:#262261;}
</style>
<g>
<path class="st1" d="M50.3,73.7v20.2c0.4,0,0.7-0.1,1.1-0.1c5.3-4.7,9.7-10.9,13-18.2C60,74.6,55.3,73.9,50.3,73.7z"/>
<path class="st1" d="M18.4,50.3H0.7c0.7,10.3,4.7,19.8,11,27.2c3-2.2,6.6-4.1,10.7-5.7C20.1,65.2,18.7,57.9,18.4,50.3z"/>
<path class="st1" d="M93.9,44.3C93.3,34,89.3,24.5,83,17.1c-2.9,2.2-6.5,4.1-10.6,5.6c2.2,6.7,3.6,13.9,3.8,21.5H93.9z"/>
<path class="st1" d="M15.8,81.8c4.9,4.5,10.9,8,17.4,10c-3.4-4.2-6.4-9.1-8.7-14.5C21.2,78.7,18.2,80.2,15.8,81.8z"/>
<path class="st1" d="M30.1,18.9c4.3,1.1,9.1,1.8,14.2,2V0.7c-0.5,0-1,0.1-1.5,0.1C37.6,5.5,33.3,11.7,30.1,18.9z"/>
<path class="st1" d="M0.7,44.3h17.7c0.3-7.6,1.6-14.9,3.8-21.5c-4.1-1.5-7.7-3.4-10.6-5.6C5.3,24.5,1.3,34,0.7,44.3z"/>
<path class="st1" d="M32.8,2.9c-6.4,2.1-12.1,5.5-17,9.9c2.4,1.7,5.3,3.1,8.6,4.4C26.6,11.9,29.5,7.1,32.8,2.9z"/>
<path class="st1" d="M70.2,50.3H50.3v17.4c5.8,0.2,11.3,1,16.2,2.3C68.6,63.9,69.9,57.3,70.2,50.3z"/>
<path class="st1" d="M28.1,70c4.9-1.3,10.4-2.1,16.2-2.3V50.3H24.4C24.7,57.3,26,63.9,28.1,70z"/>
<path class="st1" d="M72.3,71.8c4.1,1.5,7.8,3.5,10.7,5.7c6.3-7.4,10.3-16.9,11-27.2H76.2C75.9,57.9,74.5,65.2,72.3,71.8z"/>
<path class="st1" d="M61.4,91.9c6.5-2.1,12.5-5.5,17.4-10c-2.4-1.7-5.4-3.2-8.7-4.4C67.7,82.8,64.8,87.6,61.4,91.9z"/>
<path class="st1" d="M66.7,24.6c-5,1.3-10.5,2.1-16.4,2.3v17.4h19.9C69.9,37.3,68.7,30.7,66.7,24.6z"/>
<path class="st1" d="M24.4,44.3h19.9V26.9c-5.9-0.2-11.4-1-16.4-2.3C25.9,30.7,24.7,37.3,24.4,44.3z"/>
<path class="st1" d="M43.2,93.9c0.4,0,0.7,0.1,1.1,0.1V73.7c-5,0.2-9.7,0.9-14,2C33.5,82.9,38,89.1,43.2,93.9z"/>
<path class="st1" d="M70.2,17.1c3.3-1.2,6.2-2.7,8.6-4.4c-4.8-4.4-10.6-7.8-17-9.9C65.1,7.1,68,11.9,70.2,17.1z"/>
<path class="st1" d="M51.8,0.8c-0.5,0-1-0.1-1.5-0.1v20.2c5-0.2,9.8-0.9,14.2-2C61.3,11.7,57,5.5,51.8,0.8z"/>
</g>
</svg>
terraform {
required_providers {
kubectl = {
source = "gavinbunney/kubectl"
version = "1.14.0"
}
}
}
module "deployment" {
# disable wait for all pods be ready.
#
wait_for_rollout = false
# always pull latest version avoid fake updating.
#
image_pull_policy = "Always"
# Use local paths to avoid accessing external networks
# This module comes from terraform registry "terraform-iaac/deployment/kubernetes 1.4.2"
source = "./modules/deployment/kubernetes"
name = local.name
namespace = local.namespace
image = var.image
replicas = var.replicas
restart_policy = var.restart_policy
resources = {
request_cpu = var.request_cpu == "" ? null : var.request_cpu
limit_cpu = var.limit_cpu == "" ? null : var.limit_cpu
request_memory = var.request_memory == "" ? null : var.request_memory
limit_memory = var.limit_memory == "" ? null : var.limit_memory
}
env = var.env
readiness_probe = {
http_get = {
path = var.probe_http_path
port = var.probe_http_port
scheme = "HTTP"
}
success_threshold = 1
failure_threshold = 3
initial_delay_seconds = 30
period_seconds = 30
timeout_seconds = 3
}
# start time 4 minute failure_threshold * period_seconds = 4m
startup_probe = {
http_get = {
path = var.probe_http_path
port = var.probe_http_port
scheme = "HTTP"
}
success_threshold = 1
failure_threshold = 3
initial_delay_seconds = 10
period_seconds = 10
timeout_seconds = 5
}
}
module "service" {
# Use local paths to avoid accessing external networks
# This module comes from terraform registry "terraform-iaac/service/kubernetes 1.0.4"
source = "./modules/service/kubernetes"
app_name = local.name
app_namespace = local.namespace
type = "NodePort"
port_mapping = [
{
name = "port-${var.port}"
internal_port = var.port
external_port = var.port
protocol = "TCP"
}
]
}
module "ingress" {
source = "./modules/ingress/kubernetes"
ingress_name = local.name
service_namespace = local.namespace
service_name = local.name
rule = [
{
domain = var.domain_name
external_port = var.port // Service port
path = var.path
path_type = var.path_type
}
]
}
data "kubernetes_service" "service" {
depends_on = [module.service]
metadata {
name = local.name
namespace = local.namespace
}
}
locals {
name = coalesce(var.name, "${var.walrus_metadata_service_name}")
namespace = coalesce(var.namespace, var.walrus_metadata_namespace_name)
}
module "deploy" {
source = "../"
name = var.app_name
namespace = var.app_namespace
image = var.image
internal_port = var.ports
volume_nfs = [
{
path_on_nfs = "/"
nfs_endpoint = "10.10.0.100"
volume_name = var.volume_data
}
]
volume_host_path = [
{
volume_name = var.volume_logs
path_on_node = "/var/log"
},
{
volume_name = var.volume_tmp
path_on_node = "/tmp"
}
]
volume_mount = [
{
mount_path = "/var/jenkins_home"
sub_path = "jenkins"
volume_name = var.volume_data
},
{
mount_path = "/logs"
volume_name = var.volume_logs
},
{
mount_path = "/tmps"
volume_name = var.volume_tmp
},
{
mount_path = "/var/jenkins_home/config.conf"
volume_name = var.volume_config
sub_path = "key1" // Key from configmap
}
]
volume_config_map = [
{
mode = "0777" // Must be a value between 0 and 0777. Defaults to 0644
name = "config-jenkins" // Config map name
volume_name = var.volume_config
}
]
readiness_probe = {
http_get = {
path = "/healthcheck"
port = 80
scheme = "HTTP"
http_header = [
{
name = "test"
value = "test"
},
{
name = "tes"
value = "asd"
}
]
}
success_threshold = 1
failure_threshold = 3
initial_delay_seconds = 10
period_seconds = 30
timeout_seconds = 3
}
}
\ No newline at end of file
variable "app_name" {
default = "jenkins"
}
variable "app_namespace" {
default = "jenkins-ci"
}
variable "image" {
default = "jenkins/jenkins:latest"
}
variable "ports" {
default = [
{
name = "web-access"
internal_port = "8080"
}
]
}
variable "volume_data" {
default = "data"
}
variable "volume_logs" {
default = "logs-from-node"
}
variable "volume_tmp" {
default = "tmp-from-node"
}
variable "volume_config" {
default = "jenkins-configmap"
}
\ No newline at end of file
locals {
labels = var.custom_labels == null ? { app = var.name } : var.custom_labels
env = flatten([
for name, value in var.env : {
name = tostring(name)
value = tostring(value)
}
])
env_field = flatten([
for name, field_path in var.env_field : {
name = tostring(name)
field_path = tostring(field_path)
}
])
env_secret = flatten([
for name, secret in var.env_secret : {
name = tostring(name)
secret_key = try(tostring(secret["key"]), name)
secret_name = try(tostring(secret["name"]), secret)
}
])
}
\ No newline at end of file
output "name" {
value = var.name
}
output "namespace" {
value = var.namespace
}
\ No newline at end of file
variable "name" {
type = string
description = "(Required) Name of the deployment"
}
variable "namespace" {
type = string
description = "(Optional) Namespace in which to create the deployment"
default = "default"
}
variable "deployment_annotations" {
description = "Annotations for deployment"
type = map(string)
default = null
}
variable "template_annotations" {
description = "Annotations for pod (template)"
type = map(string)
default = null
}
variable "image" {
type = string
description = "(Required) Docker image name"
}
variable "image_pull_policy" {
default = "IfNotPresent"
description = "One of Always, Never, IfNotPresent"
}
variable "args" {
type = list(string)
description = "(Optional) Arguments to the entrypoint"
default = []
}
variable "command" {
type = list(string)
description = " (Optional) Entrypoint array. Not executed within a shell"
default = []
}
variable "env" {
type = map(string)
description = "(Optional) Name and value pairs to set in the container's environment"
default = {}
}
variable "env_field" {
type = map(string)
description = "(Optional) Get field from k8s and add as environment variables to pods"
default = {}
}
variable "env_secret" {
description = "(Optional) Get secret keys from k8s and add as environment variables to pods"
default = {}
}
variable "resources" {
description = "(Optional) Compute Resources required by this container. CPU/RAM requests/limits"
default = {}
}
variable "internal_port" {
description = "(Optional) List of ports to expose from the container"
default = []
}
variable "volume_mount" {
description = "(Optional) Mount path from pods to volume"
default = []
}
variable "volume_nfs" {
type = list(object({ path_on_nfs = string, nfs_endpoint = string, volume_name = string }))
description = "(Optional) Represents an NFS mounts on the host"
default = []
}
variable "volume_host_path" {
description = "(Optional) Represents a directory from node on the host"
default = []
}
variable "volume_config_map" {
type = list(object({ mode = string, name = string, volume_name = string }))
description = "(Optional) The data stored in a ConfigMap object can be referenced in a volume of type configMap and then consumed by containerized applications running in a Pod"
default = []
}
variable "volume_empty_dir" {
type = list(object({ volume_name = string }))
default = []
}
variable "volume_gce_disk" {
description = "(Optional) Represents an GCE Disk resource that is attached to a kubelet's host machine and then exposed to the pod"
default = []
}
variable "volume_secret" {
description = "(Optional) Create volume from secret"
default = []
}
variable "volume_aws_disk" {
description = "(Optional) Represents an AWS Disk resource that is attached to a kubelet's host machine and then exposed to the pod"
default = []
}
variable "volume_claim" {
description = "(Optional) Represents an Persistent volume Claim resource that is attached to a kubelet's host machine and then exposed to the pod"
default = []
}
variable "toleration" {
description = "(Optional) Pod node tolerations"
default = []
}
variable "hosts" {
type = list(object({ hostname = list(string), ip = string }))
description = "(Optional) Add /etc/hosts records to pods"
default = []
}
variable "security_context" {
description = "(Optional) SecurityContext holds pod-level security attributes and common container settings"
default = []
}
variable "security_context_capabilities" {
description = "(Optional) Security context in pod. Only capabilities."
default = []
}
variable "security_context_container" {
description = "(Optional) Security context in pod."
default = []
}
variable "custom_labels" {
description = "(Optional) Add custom label to pods"
default = null
type = map(string)
}
variable "tty" {
description = "Whether this container should allocate a TTY for itself"
type = bool
default = true
}
variable "termination_grace_period_seconds" {
type = number
description = "Duration in seconds the pod needs to terminate gracefully"
default = null
}
variable "service_account_token" {
type = bool
description = "Indicates whether a service account token should be automatically mounted"
default = null
}
variable "service_account_name" {
type = string
description = "(Optional) Is the name of the ServiceAccount to use to run this pod"
default = null
}
variable "restart_policy" {
type = string
description = "Restart policy for all containers within the pod. One of Always, OnFailure, Never"
default = "Always"
}
variable "replicas" {
type = number
description = "(Optional) Count of pods"
default = 1
}
variable "min_ready_seconds" {
type = number
description = "(Optional) Field that specifies the minimum number of seconds for which a newly created Pod should be ready without any of its containers crashing, for it to be considered available"
default = null
}
variable "liveness_probe" {
description = "(Optional) Periodic probe of container liveness. Container will be restarted if the probe fails. Cannot be updated. "
default = []
}
variable "readiness_probe" {
description = "(Optional) Periodic probe of container service readiness. Container will be removed from service endpoints if the probe fails. Cannot be updated. "
default = []
}
variable "startup_probe" {
description = "(Optional) Periodic probe of container service startup. Container will be removed from service endpoints if the probe fails. Cannot be updated. "
default = []
}
variable "lifecycle_events" {
description = "(Optional) Actions that the management system should take in response to container lifecycle events"
default = []
}
variable "image_pull_secrets" {
description = "(Optional) Specify list of pull secrets"
type = map(string)
default = {}
}
variable "node_selector" {
description = "(Optional) Specify node selector for pod"
type = map(string)
default = null
}
variable "strategy_update" {
description = "(Optional) Type of deployment. Can be 'Recreate' or 'RollingUpdate'"
default = "RollingUpdate"
}
variable "rolling_update" {
description = "Rolling update config params. Present only if strategy_update = RollingUpdate"
default = []
}
variable "wait_for_rollout" {
default = true
}
variable "prevent_deploy_on_the_same_node" {
description = "Pod pod_anti_affinity rule, which prevents deploy same pod on one node."
type = bool
default = false
}
\ No newline at end of file
terraform {
required_version = ">= 0.14.8"
required_providers {
kubernetes = ">= 2.1.0"
}
}
\ No newline at end of file
# Add any directories, files, or patterns you don't want to be tracked by version control
**/credentials
.idea
.terraform
*.tfstate
*.tfstate.backup
*.tfstate.lock.info
\ No newline at end of file
Terraform module for Kubernetes Ingress
==========================================
## Usage (more in example direcotry)
With default labels, backend finds pods with `app` label which equal to `app_name`
### Example with ClusterIP
```
module "ingress" {
source = "../"
app_name = module.service.name
app_namespace = var.namespace
annotations = {
"app" = "test"
}
rule = [
{
domain = local.url
external_port = var.ports_mapping.0.external_port // Service port
}
]
tls_hosts = [
{
secret_name = "cert-manager-ingress"
hosts = [example.com]
}
]
}
```
## Terraform Requirements
| Name | Version |
|------|---------|
| terraform | >= 0.12.26 |
| kubernetes | >= 2.0.0 |
## Inputs
See in example directory & variables.tf
## Outputs
| Name | Description |
|------|:-----------:|
| id | Kubernetes resource id |
| urls | URLs in ingress ||
\ No newline at end of file
module "url_jenkins" {
source = "../"
domain_name = var.domain_name
service_name = var.service_name
service_namespace = var.service_namespace
rule = var.rule_ports
annotations = {
"kubernetes.io/ingress.class" = "nginx" // Require if used addional annontations. By default exist.
"nginx.ingress.kubernetes.io/whitelist-source-range" = "10.10.10.0/24, 30.0.0.10/25"
}
tls = [ // You can define few TLS. Kube will automatically choose correct certificate for url. If you want to use http traffit, please don't write tls option.
kubernetes_secret.wilcart-tls.metadata[0].name,
kubernetes_secret.domain-tls.metadata[0].name
]
}
\ No newline at end of file
variable "rule_ports" {
default = [
{
sub_domain = "jenkins." //(Optional) ( subdomain must consist " . " in the end)
path = "/jenkins-dev" //(Optional) (Path in url)
internal_port = "8080" //(Required) Internal pod port
},
{
sub_domain = "jenkins." //(Optional) ( subdomain must consist " . " in the end)
path = "/jenkins-prod" //(Optional) (Path in url)
internal_port = "9090" //(Required) Internal pod port
},
{
domain = "another.com" //(Optional) (Redefine domain for this url) (Important! by default will use var.domain_name, if this field empty)
internal_port = "9090" //(Required) Internal pod port
},
]
}
variable "domain_name" {
default = "example.com"
}
variable "service_name" {
default = "jenkins"
}
variable "service_namespace" {
default = "ci-tools"
}
resource "kubernetes_ingress_v1" "ingress" {
count = var.ingress_v1_enable ? 1 : 0
metadata {
name = var.ingress_name != null ? var.ingress_name : var.service_name
namespace = var.service_namespace
annotations = var.annotations
}
spec {
ingress_class_name = var.ingress_class_name
dynamic "rule" {
for_each = var.rule
content {
host = "${lookup(rule.value, "sub_domain", "")}${lookup(rule.value, "domain", var.domain_name)}"
http {
path {
path = lookup(rule.value, "path", null)
path_type = lookup(rule.value, "path_type", var.path_type)
backend {
service {
name = lookup(rule.value, "service_name", var.service_name)
port {
number = rule.value.external_port
}
}
}
}
}
}
}
dynamic "tls" {
for_each = var.tls
content {
secret_name = tls.value
}
}
dynamic "tls" {
for_each = var.tls_hosts
content {
secret_name = tls.value.secret_name
hosts = tls.value.hosts
}
}
}
}
resource "kubernetes_ingress" "ingress" {
count = var.ingress_v1_enable ? 0 : 1
metadata {
name = var.ingress_name != null ? var.ingress_name : var.service_name
namespace = var.service_namespace
annotations = var.annotations
}
spec {
ingress_class_name = var.ingress_class_name
dynamic "rule" {
for_each = var.rule
content {
host = "${lookup(rule.value, "sub_domain", "")}${lookup(rule.value, "domain", var.domain_name)}"
http {
path {
path = lookup(rule.value, "path", null)
backend {
service_name = lookup(rule.value, "service_name", var.service_name)
service_port = rule.value.external_port
}
}
}
}
}
dynamic "tls" {
for_each = var.tls
content {
secret_name = tls.value
}
}
dynamic "tls" {
for_each = var.tls_hosts
content {
secret_name = tls.value.secret_name
hosts = tls.value.hosts
}
}
}
}
\ No newline at end of file
output "id" {
value = var.ingress_v1_enable ? kubernetes_ingress_v1.ingress[0].id : kubernetes_ingress.ingress[0].id
}
output "urls" {
value = var.ingress_v1_enable ? kubernetes_ingress_v1.ingress[0].spec[*].rule[*].host : kubernetes_ingress.ingress[0].spec[*].rule[*].host
}
\ No newline at end of file
variable "service_name" {
type = string
description = "(Required) Application name for attach to ingress. Service name."
}
variable "ingress_name" {
type = string
description = "Specific ingress name instead name as service"
default = null
}
variable "ingress_v1_enable" {
type = bool
description = "Enable ingress v1 tf resource type"
default = true
}
variable "service_namespace" {
type = string
description = "(Required) Namespace where located service"
}
variable "rule" {
description = "(Required) External Service port, ingress will redirect request to this service port. Also could add subdomain ( example: subdomain.domainname.com). And path for access ( example: domain.com/path ). And redefine domain."
}
variable "domain_name" {
type = string
description = "(Otional) Global domain name for all URL ( example: google.com)."
default = "example.com"
}
variable "tls" {
type = list(string)
description = "(Optional) Enable https traffic & and include SSL Certificate"
default = []
}
variable "annotations" {
description = "(Optional) Custom Annotations"
default = null
}
variable "ingress_class_name" {
description = "Ingress Class name"
type = string
default = "nginx"
}
variable "tls_hosts" {
description = "(Optional) Enable https traffic & and include SSL Certificate with hosts match"
default = []
}
variable "path_type" {
description = "Path type for ingress rule"
default = "ImplementationSpecific"
}
\ No newline at end of file
Terraform module for Kubernetes Service
==========================================
## Usage (more in example direcotry)
With default labels, selector finds pods with `app` label which equal to `app_name`
### Example with ClusterIP
```
module "service_jenkins_ClusterIP" {
source = "../"
app_name = "jenkins1"
app_namespace = "jenkins1"
port_mapping = var.port_mapping_CI
}
```
### Example with Load Balancer
```
module "service_jenkins_LB" {
source = "../"
app_name = "jenkins2"
app_namespace = "jenkins2"
port_mapping = var.port_mapping_LB
type = "LoadBalancer"
load_balancer_ip = "134.13.13.24" // This field will be ignored if the cloud-provider does not support the feature
load_balancer_ips_whitelist = [
"10.10.10.0/24",
"34.23.145.91/32"
]
}
```
### Example with Node Port
```
module "service_jenkins_NP" {
source = "../"
app_name = "jenkins3"
app_namespace = "jenkins3"
port_mapping = var.port_mapping_NP
type = "NodePort"
}
```
## Terraform Requirements
| Name | Version |
|------|---------|
| terraform | >= 0.12.26 |
| kubernetes | >= 2.0.0 |
## Inputs
See in example directory & variables.tf
## Outputs
| Name | Description |
|------------|:--------------------------------------:|
| name | Name of the service |
| namespace | Namespace in which created the service |
| id | Kubernetes resource id |
| node_ports | All Node Ports generated & entered |
\ No newline at end of file
# Example with ClusterIP
module "service_jenkins_ClusterIP" {
source = "../"
app_name = "jenkins1"
app_namespace = "jenkins1"
port_mapping = var.port_mapping_CI
}
# Example with Load Balancer
module "service_jenkins_LB" {
source = "../"
app_name = "jenkins2"
app_namespace = "jenkins2"
port_mapping = var.port_mapping_LB
type = "LoadBalancer"
load_balancer_ip = "134.13.13.24" // Also you can get from google_compute_address.static_external_ip.address
load_balancer_ips_whitelist = [
"10.10.10.0/24",
"34.23.145.91/32"
]
}
# Example with Node Port
module "service_jenkins_NP" {
source = "../"
app_name = "jenkins3"
app_namespace = "jenkins3"
port_mapping = var.port_mapping_NP
type = "NodePort"
}
\ No newline at end of file
variable "port_mapping_CI" {
default = [
{
name = "web-access" // (Required) Port name
internal_port = "8080" // (Required) Pod port
external_port = "80" // (Required) Port for others container's
},
{
name = "service-port" // (Required) Port name
internal_port = "3000" //(Required) Pod port
external_port = "90" //(Required) Port for others container's
portocol = "UDP" // (Optional) The IP protocol for this port. Supports TCP and UDP. Default is TCP.
}
]
}
variable "port_mapping_LB" {
default = [
{
name = "web-access" // (Required) Port name
internal_port = "8080" // (Required) Pod port
external_port = "80" // (Required) Port for others container's
node_port = "30000" // (Optional) set Node Port. By default K8S automatically set free Node Port
},
{
name = "service-port" // (Required) Port name
internal_port = "3000" // (Required) Pod port
external_port = "90" // (Required) Port for others container's
portocol = "UDP" // (Optional) The IP protocol for this port. Supports TCP and UDP. Default is TCP.
}
]
}
variable "port_mapping_NP" {
default = [
{
name = "web-access" // (Required) Port name
internal_port = "8080" // (Required) Pod port
external_port = "80" // (Required) Port for others container's
node_port = "30000" // (Optional) set Node Port. By default K8S automatically set free Node Port
},
{
name = "service-port" // (Required) Port name
internal_port = "3000" // (Required) Pod port
external_port = "90" // (Required) Port for others container's
portocol = "UDP" // (Optional) The IP protocol for this port. Supports TCP and UDP. Default is TCP.
}
]
}
\ No newline at end of file
resource "kubernetes_service" "service" {
metadata {
name = var.app_name
namespace = var.app_namespace
annotations = var.annotations
labels = local.labels
}
spec {
selector = local.labels
type = var.type
load_balancer_ip = var.type == "LoadBalancer" ? var.load_balancer_ip : null
load_balancer_source_ranges = var.type == "LoadBalancer" ? var.load_balancer_ips_whitelist : null
dynamic "port" {
iterator = port
for_each = var.port_mapping
content {
name = port.value.name
port = port.value.external_port
protocol = lookup(port.value, "protocol", "TCP")
node_port = lookup(port.value, "node_port", null)
target_port = port.value.internal_port
}
}
}
}
\ No newline at end of file
# K8S Resource id
output "id" {
value = kubernetes_service.service.id
}
# K8S Service Name
output "name" {
value = kubernetes_service.service.metadata[0].name
}
output "namespace" {
value = kubernetes_service.service.metadata[0].namespace
}
# Ports
output "node_ports" {
value = kubernetes_service.service.spec[0].port[*].node_port
}
\ No newline at end of file
variable "app_name" {
type = string
description = "(Required) Application name for attach to service"
}
variable "app_namespace" {
type = string
description = "(Required) Namespace where located application"
}
variable "port_mapping" {
description = "(Required) Mapping ports from container to expose port"
}
variable "type" {
type = string
default = null
description = "(Optional) Default is ClusterIP. Can accept: NodePort , LoadBalancer ."
}
variable "load_balancer_ip" {
type = string
description = "(Optional) !!!Only applies with type = LoadBalancer!!! Define External Static IP address."
default = null
}
variable "load_balancer_ips_whitelist" {
type = list(string)
default = null
description = "(Optional) !!!Only applies with type = LoadBalancer!!! Define IPs cidr range for whitelist in Load Balancer. By default accept all IPs."
}
variable "annotations" {
description = "(Optional) Add annotations"
default = {}
}
locals {
labels = var.custom_labels == null ? { app = var.app_name } : var.custom_labels
}
variable "custom_labels" {
description = "(Optional) Custom labels & selector"
default = null
}
\ No newline at end of file
output "service_ip" {
description = "Service IP"
value = data.kubernetes_service.service.spec.0.cluster_ip
}
output "ports" {
description = "Service Ports"
value = var.ports
}
output "ingress" {
description = "访问入口ingress"
value = var.domain_name
}
# @label "Image Name"
# @group "Basic"
variable "image" {
type = string
description = "Docker image name"
}
# @label "Ports"
# @group "Basic"
variable "port" {
type = string
description = "Service ports to expose"
default = "7777"
}
# @label "Environment Variables"
# @options ["dev", "test", "release", "beta"]
# @group "Basic"
variable "env" {
type = map(string)
description = "Name and value pairs to set as the environment variables"
default = {}
}
# @label "Replicas"
# @group "Basic"
variable "replicas" {
type = number
description = "Replicas to deploy"
default = 1
}
# @group "Resources"
# @options ["0.2","0.3", "0.5","1", "2"]
# @label "CPU Request"
variable "request_cpu" {
type = string
description = "CPU request. e.g. 0.5, 1, 2"
default = "0.1"
}
# @group "Resources"
# @options ["128Mi","512Mi", "1Gi"]
# @label "Memory Request"
variable "request_memory" {
type = string
description = "Memory request. e.g. 128Mi, 512Mi, 1Gi, 2Gi, 4Gi"
default = "128Mi"
}
# @group "Resources"
# @options ["0.2","0.3", "0.5","1", "2", "3", "4"]
# @label "CPU Limit"
variable "limit_cpu" {
type = string
description = "CPU limit. e.g. 0.5, 1, 2"
default = "0.5"
}
# @group "Resources"
# @options ["128Mi","512Mi", "1Gi","2Gi", "3Gi"]
# @label "Memory Limit"
variable "limit_memory" {
type = string
description = "Memory limit. e.g. 128Mi, 512Mi, 1Gi, 2Gi, 4Gi"
default = "512Mi"
}
# @label "Namespace"
# @group "Advanced"
variable "namespace" {
type = string
description = "Namespace to deploy. Auto-generated if empty."
default = "default"
}
# @label "Deployment Name"
# @group "Advanced"
variable "name" {
type = string
description = "Name of the deployment resource. Auto-generated if empty."
default = ""
}
# @hidden
variable "walrus_metadata_service_name" {
type = string
description = "Walrus metadata service name."
default = ""
}
# @hidden
variable "walrus_metadata_namespace_name" {
type = string
description = "Walrus metadata namespace name."
default = ""
}
# @label "probe_port"
# @group "Advanced"
variable "probe_http_port" {
description = "(Optional)http port for probe"
type = string
default = "7777"
}
# @label "probe_path"
# @group "Advanced"
variable "probe_http_path" {
description = "(Optional)http path for probe"
type = string
default = "/"
}
# @label "重启策略"
# @group "Advanced"
variable "restart_policy" {
type = string
description = "Restart policy for all containers within the pod. One of Always, OnFailure, Never"
default = "Always"
}
# @label "域名名字"
# @group "ingress"
variable "domain_name" {
type = string
description = "域名"
}
# @label "path_type"
# @options ["ImplementationSpecific","Exact","Prefix"]
# @group "ingress"
variable "path_type" {
type = string
description = "Path type for ingress rule"
default = "ImplementationSpecific"
}
# @label "path"
# @group "ingress"
variable "path" {
type = string
description = "访问路径"
default = "/"
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!