|
| 1 | +"""A Google Cloud Python Pulumi program""" |
| 2 | + |
| 3 | +import pulumi |
| 4 | +import pulumi_gcp as gcp |
| 5 | +import pulumi_kubernetes as k8s |
| 6 | +from pulumi_gcp import storage |
| 7 | + |
| 8 | +# SET YOUR VARIABLES |
| 9 | +db_instance_name = "instance" |
| 10 | +region = "us-central1" |
| 11 | +db_tier = "db-f1-micro" |
| 12 | +db_user = "myuser" |
| 13 | +db_password = "mypassword" |
| 14 | + |
| 15 | + |
| 16 | +# See versions at https://registry.terraform.io |
| 17 | +# /providers/hashicorp/google/latest/docs/resources |
| 18 | +# /sql_database_instance#database_version |
| 19 | +instance = gcp.sql.DatabaseInstance( |
| 20 | + db_instance_name, |
| 21 | + region=region, |
| 22 | + database_version="POSTGRES_15", |
| 23 | + settings=gcp.sql.DatabaseInstanceSettingsArgs( |
| 24 | + tier=db_tier, |
| 25 | + ), |
| 26 | + deletion_protection=True, |
| 27 | +) |
| 28 | + |
| 29 | +database_user = gcp.sql.User( |
| 30 | + db_user, instance=instance.name, password=db_password |
| 31 | +) |
| 32 | + |
| 33 | +database = gcp.sql.Database("database", instance=instance.name) |
| 34 | + |
| 35 | +# Create a GCP resource (Storage Bucket) |
| 36 | +bucket = storage.Bucket("my-bucket", location="US") |
| 37 | + |
| 38 | +# Export the DNS name of the bucket |
| 39 | +pulumi.export("bucket_name", bucket.url) |
| 40 | + |
| 41 | + |
| 42 | +# Define your Google Cloud project |
| 43 | +project = "your-gcp-project" |
| 44 | +gcp_config = pulumi.Config("gcp") |
| 45 | +project = gcp_config.get("project") or project |
| 46 | + |
| 47 | +# Configure the GKE cluster |
| 48 | +cluster_name = "my-gke-cluster" |
| 49 | +cluster = gcp.container.Cluster( |
| 50 | + cluster_name, |
| 51 | + initial_node_count=1, |
| 52 | + min_master_version="latest", |
| 53 | + node_version="latest", |
| 54 | + project=project, |
| 55 | + location="us-central1", |
| 56 | +) |
| 57 | + |
| 58 | +# Use gcp.container.getClusterCredentials to get Kubernetes config |
| 59 | +kubeconfig = pulumi.Output.all(cluster.name, project).apply( |
| 60 | + lambda args: gcp.container.get_cluster_credentials( |
| 61 | + cluster_name=args[0], project=args[1] |
| 62 | + ) |
| 63 | +) |
| 64 | + |
| 65 | +# Define the configuration for your Kubernetes services |
| 66 | + |
| 67 | +# App Deployment |
| 68 | +app_deployment = k8s.apps.v1.Deployment( |
| 69 | + "app-deployment", |
| 70 | + metadata={ |
| 71 | + "name": "app-deployment", |
| 72 | + }, |
| 73 | + spec={ |
| 74 | + "replicas": 2, |
| 75 | + "selector": { |
| 76 | + "matchLabels": {"app": "app"}, |
| 77 | + }, |
| 78 | + "template": { |
| 79 | + "metadata": {"labels": {"app": "app"}}, |
| 80 | + "spec": { |
| 81 | + "containers": [ |
| 82 | + { |
| 83 | + "name": "app-container", |
| 84 | + "image": "your-app-image:latest", |
| 85 | + "ports": [{"containerPort": 8000}], |
| 86 | + # You can add additional |
| 87 | + # configurations and resources here |
| 88 | + } |
| 89 | + ] |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | +) |
| 94 | + |
| 95 | +# Celery Deployment |
| 96 | +celery_deployment = k8s.apps.v1.Deployment( |
| 97 | + "celery-deployment", |
| 98 | + metadata={ |
| 99 | + "name": "celery-deployment", |
| 100 | + }, |
| 101 | + spec={ |
| 102 | + "replicas": 2, # Adjust as needed |
| 103 | + "selector": { |
| 104 | + "matchLabels": {"app": "celery"}, |
| 105 | + }, |
| 106 | + "template": { |
| 107 | + "metadata": {"labels": {"app": "celery"}}, |
| 108 | + "spec": { |
| 109 | + "containers": [ |
| 110 | + { |
| 111 | + "name": "celery-container", |
| 112 | + "image": "your-celery-image:latest", |
| 113 | + # Add any necessary command and args here |
| 114 | + # You can add additional configurations |
| 115 | + # and resources here |
| 116 | + } |
| 117 | + ] |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | +) |
| 122 | + |
| 123 | +# Redis Deployment |
| 124 | +redis_deployment = k8s.apps.v1.Deployment( |
| 125 | + "redis-deployment", |
| 126 | + metadata={ |
| 127 | + "name": "redis-deployment", |
| 128 | + }, |
| 129 | + spec={ |
| 130 | + "replicas": 1, # Redis is typically deployed with a single node |
| 131 | + "selector": { |
| 132 | + "matchLabels": {"app": "redis"}, |
| 133 | + }, |
| 134 | + "template": { |
| 135 | + "metadata": {"labels": {"app": "redis"}}, |
| 136 | + "spec": { |
| 137 | + "containers": [ |
| 138 | + { |
| 139 | + "name": "redis-container", |
| 140 | + "image": "redis:latest", |
| 141 | + "ports": [{"containerPort": 6379}], |
| 142 | + # You can add additional configurations |
| 143 | + # and resources here |
| 144 | + } |
| 145 | + ] |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | +) |
| 150 | + |
| 151 | +# RabbitMQ Deployment |
| 152 | +rabbitmq_deployment = k8s.apps.v1.Deployment( |
| 153 | + "rabbitmq-deployment", |
| 154 | + metadata={ |
| 155 | + "name": "rabbitmq-deployment", |
| 156 | + }, |
| 157 | + spec={ |
| 158 | + "replicas": 1, # RabbitMQ is typically deployed with a single node |
| 159 | + "selector": { |
| 160 | + "matchLabels": {"app": "rabbitmq"}, |
| 161 | + }, |
| 162 | + "template": { |
| 163 | + "metadata": {"labels": {"app": "rabbitmq"}}, |
| 164 | + "spec": { |
| 165 | + "containers": [ |
| 166 | + { |
| 167 | + "name": "rabbitmq-container", |
| 168 | + "image": "rabbitmq:3.6-management", |
| 169 | + "ports": [ |
| 170 | + {"containerPort": 5672}, |
| 171 | + {"containerPort": 15672}, |
| 172 | + ], # RabbitMQ ports |
| 173 | + # You can add additional configurations |
| 174 | + # and resources here |
| 175 | + } |
| 176 | + ] |
| 177 | + }, |
| 178 | + }, |
| 179 | + }, |
| 180 | +) |
| 181 | + |
| 182 | +# Export useful information |
| 183 | +pulumi.export("cluster_name", cluster.name) |
| 184 | +pulumi.export("kubeconfig", cluster.kube_config) |
| 185 | + |
| 186 | +# Deploy the infrastructure |
| 187 | +pulumi.export("app_deployment", app_deployment.metadata) |
| 188 | +pulumi.export("celery_deployment", celery_deployment.metadata) |
| 189 | +pulumi.export("redis_deployment", redis_deployment.metadata) |
| 190 | +pulumi.export("rabbitmq_deployment", rabbitmq_deployment.metadata) |
0 commit comments