To run the server:
- Install dependencies and compile:
mix do deps.get, compile
-
Run the server:
- With developing settings:
mix phoenix.server
- With production settings:
To view the effect for every change you made in production mode, you have to be compiled explicitly.
MIX_ENV=prod mix compile
MIX_ENV=prod mix phoenix.server
Get Content
curl -i http://localhost:8080/poll
Create
curl -i -H "Content-Type:application/json" -d '{"name":"Name"}' http://localhost:8080/poll"
mix Test
To set up applications on Google Container Engine and use Cloud SQL to store your data, you have to set up a pod on GKE, running two containers: your application and a Cloud SQL proxy server. Your application will connect Cloud SQL through this proxy server.
-
Get Kubernetes:
gcloud components install kubectl
-
Create a Cloud SQL instances. Create a user account for you application.
-
Create a GKE container cluster
-
Let
kubectl
could login your clusters:
gcloud doesn’t know you have cluster now. If you use gcloud container clusters list
to get clusters info, you will find gcloud can’t get your clusters. You have to first get credentials for this cluster:
gcloud container clusters get-credentials [YOUR CLUSTER NAME HERE]
gcloud command can now find the clusters. Use kubectl config get-contexts
to query kubernetes context. There is the new context set to default for this cluster.
Before you build the docker image, you have to make sure your application connect to 127.0.0.1:3306
. The Cloud SQL proxy server will run at 127.0.0.1:3306
in the pods, which is local to your application container.
docker build --no-cache -t [IMAGE NAME] .
docker tag [IMAGE NAME] gcr.io/[YOUR PROJECT NAME]/[IMAGE NAME ON gcr.io]
- push your image to Google Container Registry:
gcloud docker push gcr.io/[YOUR PROJECT NAME]/[IMAGE NAME]
-
Create a key for a service account (with Editor role) in JSON format. The key will be downloaded to your computer.
-
Register the key downloaded in previous step with
kubectl
:
kubectl create secret generic cloud-oauth-credentials --from-file=credentials.json=[YOUR KEYFILE PATH]
- Create the secrets to access the database you create. Provide the username and password your set here:
kubectl create secret generic cloudsql --from-literal=username=[YOUR CLOUDSQL USERNAME] --from-literal=password=[YOUR CLOUDSQL PASSWORD]
The last and most important step, you have to create pod for GKE.
- Create a new file for pod config like
deployment.yml
. Assign the image source like:
image:"gcr.io/mirrormedia-1470651750304/mmrest-ex"
- Deploy the proxy server with sidecar pattern. Notice: Do not change the path for
--dir
,-credential_file
or paths in volumneMounts.
- name: cloudsql-proxy
image: b.gcr.io/cloudsql-docker/gce-proxy:1.05
command: ["/cloud_sql_proxy", "--dir=/cloudsql",
"-instances=[YOUR CLOUD SQL INSTANCE NAME]=tcp:3306",
"-credential_file=/secrets/cloudsql/credentials.json"]
volumeMounts:
- name: cloudsql-oauth-credentials
mountPath: /secrets/cloudsql
readOnly: true
- name: ssl-certs
mountPath: /etc/ssl/certs
- name: cloudsql
mountPath: /cloudsql
- Add the volumes as-is:
volumes:
- name: cloudsql-oauth-credentials
secret:
secretName: cloudsql-oauth-credentials
- name: ssl-certs
hostPath:
path: /etc/ssl/certs
- name: cloudsql
emptyDir:
- The full
deployment.yml
should look like:
apiVersion: v1
kind: Pod
metadata:
name: [YOUR POD NAME]
labels:
name: [YOUR POD NAME]
spec:
containers:
- name: [CONTAINER NAME]
image: "gcr.io/[YOUR PROJECT NAME]/[IMAGE NAME]"
ports:
- containerPort: 80
- name: cloudsql-proxy
image: b.gcr.io/cloudsql-docker/gce-proxy:1.05
command: ["/cloud_sql_proxy", "--dir=/cloudsql",
"-instances=[YOUR CLOUD SQL INSTANCE NAME]=tcp:3306",
"-credential_file=/secrets/cloudsql/credentials.json"]
volumeMounts:
- name: cloudsql-oauth-credentials
mountPath: /secrets/cloudsql
readOnly: true
- name: ssl-certs
mountPath: /etc/ssl/certs
- name: cloudsql
mountPath: /cloudsql
volumes:
- name: cloudsql-oauth-credentials
secret:
secretName: cloudsql-oauth-credentials
- name: ssl-certs
hostPath:
path: /etc/ssl/certs
- name: cloudsql
emptyDir:
- Create the pods with
deployment.yml
:
kubectl create -f deployment.yml
If successful, your pods is running. Check it with:
kubectl describe pod [YOUR POD NAME]
Your application is now inaccessible using external IP. You have to forward port, or create service first. Port forwarding:
kubectl port-forward [YOUR CONTEXT NAME] 8080:80
then you could request localhost:8080
to test your application.
To create service, make a new file like service.yml
. IMPORTANT: in selector
make sure you put right pod name you want to expose. service.yml
should like:
apiVersion: v1
kind: Service
metadata:
name: [SERVICE NAME]
spec:
selector:
name: [YOUR POD NAME]
ports:
- protocol: TCP
port: 8080
targetPort: 8080
Create service with:
kubectl expose -f service.yml
You could get the external IP for your app by kubectl get services
. Access your app using this IP and port you set in service.yml
.
To run the server:
- Install dependencies and compile:
mix do deps.get, compile
- Run the server:
- With developing settings:
mix phoenix.server
- With production settings:
To view the effect for every change you made in production mode, you have to be compiled explicitly.
MIX_ENV=prod mix compile
MIX_ENV=prod mix phoenix.server
Get Content
curl -i http://localhost:8080/poll
Create
curl -i -H "Content-Type:application/json" -d '{"name":"Name"}' http://localhost:8080/poll"
mix Test