Skip to content

Terraform GCP Tutorial (Getting Started | Create GCP VPC)

  • To support my channel, I’d like to offer Mentorship/On-the-Job Support/Consulting - me@antonputra.com
  • You can find the source code for this video in my GitHub Repo.

Terraform GCP Tutorial (Getting Started)

When you create resources in GCP such as VPC, Terraform needs a way to keep track of them. If you simply apply terraform right now, it will keep all the state locally on your computer. It's very hard to collaborate with other team members and easy to accidentally destroy all your infrastructure. You can declare Terraform backend to use remote storage instead. Since we're creating infrastructure in GCP, the logical approach would be to use Google Storage Bucket to store Terraform state. You need to provide a bucket name and a prefix.

operator.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: mongodb
  name: mongodb-kubernetes-operator
spec:
  replicas: 1
  selector:
    matchLabels:
      name: mongodb-kubernetes-operator
  strategy:
    rollingUpdate:
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        name: mongodb-kubernetes-operator
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: name
                    operator: In
                    values:
                      - mongodb-kubernetes-operator
              topologyKey: kubernetes.io/hostname
      containers:
        - command:
            - /usr/local/bin/entrypoint
          env:
            - name: WATCH_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: OPERATOR_NAME
              value: mongodb-kubernetes-operator
            - name: AGENT_IMAGE
              value: quay.io/mongodb/mongodb-agent:11.0.5.6963-1
            - name: VERSION_UPGRADE_HOOK_IMAGE
              value: quay.io/mongodb/mongodb-kubernetes-operator-version-upgrade-post-start-hook:1.0.3
            - name: READINESS_PROBE_IMAGE
              value: quay.io/mongodb/mongodb-kubernetes-readinessprobe:1.0.6
            - name: MONGODB_IMAGE
              value: mongo
            - name: MONGODB_REPO_URL
              value: docker.io
          image: quay.io/mongodb/mongodb-kubernetes-operator:0.7.2
          imagePullPolicy: Always
          name: mongodb-kubernetes-operator
          resources:
            limits:
              cpu: 1100m
              memory: 1Gi
            requests:
              cpu: 500m
              memory: 200Mi
          securityContext:
            readOnlyRootFilesystem: true
            runAsUser: 2000
      serviceAccountName: mongodb-kubernetes-operator
  • Now let's move to the terminal and apply all of these files. I assume that you already have Kubernetes provisioned and kubectl configured to talk to the cluster.
kubectl apply -f k8s/mongodb/namespace.yaml
kubectl apply -f k8s/mongodb/crd.yaml
kubectl apply -f k8s/mongodb/rbac
kubectl apply -f k8s/mongodb/operator.yaml