Deploying your DB to Google Container Engine with Kubernetes

  • setup a pool of nodes in GKE that only database container is allowed

    gcloud container node-pools create db-pool \
        --machine-type=n1-highmem-2 \
        --num-nodes=1
    
  • Creating a persistent disk for DB datastore

gcloud compute disks create --size 50GB --type pd-ssd NAME # pd-standard
  • encode your secret as base64 and save this for later
% echo -n 'root' | base64
cm9vdA==
% echo -n 'my secure password' | base64 
bXkgc2VjdXJlIHBhc3N3b3Jk
% echo -n 'some secret string' | base64
c29tZSBzZWNyZXQgc3RyaW5n
  • create a file named db-secrets.yml, then run kubectl create -f db-secret.yaml
#db-secret.yaml
apiVersion: v1
data:
  pg_user: cm9vdA==
  pg_password: bXkgc2VjdXJlIHBhc3N3b3Jk
  secret_key_base: c29tZSBzZWNyZXQgc3RyaW5n
kind: Secret
type: Opaque
metadata:
  name: db-secrets
  • Create a file named db-deploy.yml, for your db deployment looks like this, then run kubectl create -f db-deploy.yaml
#db-deploy.yml
apiVersion: extensions/v1
kind: Deployment
metadata:
  name: postgresql
  labels:
    name: postgresql
spec:
  # means I want 1 pod running at all times
  replicas: 1
  template:
    metadata:
      labels:
        name: postgresql
    spec:
      nodeSelector:
        cloud.google.com/gke-nodepool: db-pool
      containers:
        - image: postgres:9.4
          name: postgresql
          resources:
            # 80% of one CPU core
            requests:
              cpu: 800m
            limits:
              cpu: 800m
          env:
            - name: POSTGRES_DB
              value: my-app
            - name: POSTGRES_USER
              valueFrom:
                secretKeyRef:
                  name: db-secrets
                  key: pg_user
            - name: POSTGRES_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: db-secrets
                  key: pg_password
          ports:
            - containerPort: 5432
              name: postgresql
          volumeMounts:
              # This name must match the volumes.name below.
            - mountPath: /var/lib/mysql
              name: pg-db-data

      volumes:
        - name: pg-db-data
          gcePersistentDisk:
            # This disk must already exist.
            pdName: db-data
            fsType: ext4
  • Check db pods status
kubectl get pods
  • Create db service, then run kubectl create -f db-service.yml
#db-service.yml
apiVersion: v1
kind: Service
metadata:
  name: postgresql
  labels:
    name: postgresql
spec:
  ports:
    - port: 5432
  selector:
    name: postgresql

#This defines a service in our cluster named postgresql
#which Kubernetes will make available to all pods in our cluster
#by resolving DNS lookups for postgresql to any pods matching the selector name=postgresql
#and send traffic to the container on port 5432.
  • Check services status
kubectl get services

results matching ""

    No results matching ""