Skip to content

Instantly share code, notes, and snippets.

@akurkin
Forked from nathanborror/instructions.txt
Created July 13, 2018 15:41
Show Gist options
  • Save akurkin/3f0f5fda9aa6eeea407a64b68800b25d to your computer and use it in GitHub Desktop.
Save akurkin/3f0f5fda9aa6eeea407a64b68800b25d to your computer and use it in GitHub Desktop.
Example Kubernetes setup with Postgres and two Services for serving an API and a static site using Ingress. Also have a CronJob example for kicks.
*** Cluster Setup for Google Container Engine ***
0/ Install and configure local gcloud and kubectl: https://cloud.google.com/sdk/docs/
> gcloud components install kubectl
1/ Configure Google Cloud account:
> gcloud config set account YOUR_EMAIL_ADDRESS
> gcloud config set project YOUR_PROJECT_ID
> gcloud config set compute/zone us-west1-a
> gcloud config set container/cluster example
> gcloud container clusters get-credentials example
2/ Create cluster on Google Cloud Console:
> gcloud container clusters create example --machine-type=f1-micro --num-nodes=3 --scopes=cloud-platform --zone=us-west1-a
3/ Create a static IP for Ingress:
> gcloud compute addresses create example-ingress-ip --global
4/ Create persistant disk for database:
> gcloud compute disks create --size 200GB example-disk
5/ Create configmap:
> kubectl create configmap example-config -f kubernetes-config-map.yaml
6/ Create deployments:
> kubectl create -f kubernetes.yaml
7/ Create database:
> kubectl get pods
> kubectl exec postgres-POD_IDENTIFIER --stdin --tty -- createdb -U postgres example
---
98/ Destroy cluster:
> kubectl delete -f kubernetes-config-map.yaml
> kubectl delete -f kubernetes.yaml
> gcloud container clusters delete example
> gcloud compute disks delete example-disk
> gcloud compute addresses delete example-ingress-ip
99/ Web UI
> gcloud container clusters get-credentials example --zone us-west1-a --project example
> kubectl proxy
> open http://localhost:8001/ui
---
apiVersion: v1
kind: ConfigMap
metadata:
name: example-config
data:
EXAMPLE_DB_HOST: postgres://postgres@postgres/example?sslmode=disable
EXAMPLE_DB_KIND: postgres
PGDATA: /var/lib/postgresql/data/pgdata
POSTGRES_USER: postgres
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: example-ingress
annotations:
kubernetes.io/ingress.class: "gce"
kubernetes.io/ingress.global-static-ip-name: example-ingress-ip
spec:
backend:
serviceName: example-site
servicePort: 8080
rules:
- http:
paths:
- path: /graphql
backend:
serviceName: example-api-svc
servicePort: 8080
- path: /*
backend:
serviceName: example-site-svc
servicePort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
ports:
- port: 5432
selector:
app: postgres
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: postgres
spec:
template:
metadata:
labels:
app: postgres
spec:
containers:
- image: "postgres:10.1"
name: postgres
envFrom:
- configMapRef:
name: example-config
ports:
- containerPort: 5432
name: postgres
volumeMounts:
- name: postgres-storage
mountPath: /var/lib/postgresql/data
volumes:
- name: postgres-storage
gcePersistentDisk:
fsType: ext4
pdName: postgres-disk
---
apiVersion: v1
kind: Service
metadata:
name: example-api-svc
labels:
app: example-api
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30050
selector:
app: example-api
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: example-api
spec:
replicas: 1
template:
metadata:
labels:
app: example-api
spec:
containers:
- name: example-api
image: gcr.io/YOUR_PROJECT_ID/example:b8886066cfa761cf858ce526b71513abbe1ac7c0
command: ["/example", "serve", "--schema=schema.graphql"]
envFrom:
- configMapRef:
name: example-config
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: example-site-svc
labels:
app: example-site
spec:
type: NodePort
ports:
- port: 8080
nodePort: 30051
selector:
app: example-site
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: example-site
spec:
replicas: 1
template:
metadata:
labels:
app: example-site
spec:
containers:
- name: example-site
image: gcr.io/YOUR_PROJECT_ID/example:b8886066cfa761cf858ce526b71513abbe1ac7c0
command: ["/example", "site"]
envFrom:
- configMapRef:
name: example-config
ports:
- containerPort: 8080
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: crawler
spec:
schedule: "@hourly"
successfulJobsHistoryLimit: 0
jobTemplate:
spec:
template:
spec:
containers:
- name: crawler
image: gcr.io/YOUR_PROJECT_ID/example:b8886066cfa761cf858ce526b71513abbe1ac7c0
command: ["/example", "crawl"]
envFrom:
- configMapRef:
name: example-config
restartPolicy: Never
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment