Commit a88437f2 by chenxx

add

1 parent 13a04ace
.idea
.terraform
*.tfstate
*.tfstate.backup
*.tfstate.lock.info
\ No newline at end of file
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
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 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
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!