A Google Cloud Project is required in order to run the sample.
The project should have the following API's enabled:
- Cloud Run
- Secret Manager
gcloud services enable secretmanager.googleapis.com run.googleapis.com
Instead of packaging the nginx config into the container image, the config will be mounted as a volume at runtime using Secret Manager. This allows for separation of config from code.
In Kubernetes, while you are able to mount different volume types,
Cloud Run currently provides secret
volume as a lightweight volume mount. If you need a full filesystem, see Using network file systems.
In service.yaml
, look for nginx-conf-secret
volume mount and nginx_config
secret name references.
Follow along either using the gcloud
commands in your terminal or the Google Cloud Console site to add the nginx_config
secret.
The following creates a new secret in Secret Manager and adds value (new version) from local file nginx.conf
.
gcloud secrets create nginx_config --replication-policy="automatic" --data-file="./nginx.conf"
Grant your compute service account to have access to your newly created secret.
export PROJECT_NUMBER=$(gcloud projects describe $(gcloud config get-value project) --format='value(projectNumber)')
gcloud secrets add-iam-policy-binding nginx_config --member=serviceAccount:$PROJECT_NUMBER[email protected] --role='roles/secretmanager.secretAccessor'
OR
- Go to the Secret Manager UI
- Select
+ Create Secret
and name itnginx_config
with the contents ofnginx.conf
- Click
Create Secret
From inside the hello-nginx-sample
directory, declare an environment variable MC_SERVICE_NAME
to
store your custom service name string.
export MC_SERVICE_NAME=<service-name>
export REGION = us-central1
# Substituting above env vars
sed -i -e s/MC_SERVICE_NAME/${MC_SERVICE_NAME}/g -e s/REGION/${REGION}/g service.yaml
# Deploy your service
gcloud run services replace service.yaml
By default, the above command will deploy the following containers into a single service:
nginx
:serving
ingress container (entrypoint)hello
:sidecar
container
The Cloud Run Multi-container service will default access to port 8080
,
where nginx
container will be listening and proxy request over to hello
container at port 8888
.
Use curl to send an authenticated request:
curl --header "Authorization: Bearer $(gcloud auth print-identity-token)" <cloud-run-mc-service-url>
To allow un-authenticated access to containers:
gcloud run services add-iam-policy-binding $MC_SERVICE_NAME \
--member="allUsers" \
--role="roles/run.invoker"
Visit the Cloud Run url or use curl to send a request:
curl <cloud-run-mc-service-url>