Writing a startup script to google container engine

4/28/2017

I found that startup scripts can be added to Google compute instances using either the console or cli(gcloud). I want to add the startup scripts to google container engine.

The goal is to notify me when the google container engine has changed its state to Running. I though one efficient way is to use startup scripts in container engine, as these scripts will only be executed when the container's status is changed to running.

Any idea how to add startup scripts to container engine or any other way of notifying when the container's status changes to running.

-- Lakshman Diwaakar
gcp
google-cloud-platform
google-compute-engine
google-kubernetes-engine
kubernetes

3 Answers

10/29/2019

Project metadata works for this, here's a terraform example:

resource "google_compute_project_metadata_item" "main" {
    project = abcdefg # this is optional
    key = "startup-script"
    value = "#! /bin/sh\necho hello > /tmp/world"
}
-- Roman
Source: StackOverflow

5/8/2017

You can approximate the behavior of startup scripts using a DaemonSet with a simple pod that runs in privileged mode. For example code, see https://github.com/kubernetes/contrib/tree/master/startup-script.

-- Robert Bailey
Source: StackOverflow

4/28/2017

First of all your question is fairly complicated. The concept of startup scripts do not belong to the containers world. As far as I know you can't add startup scripts in Google Container Engine. This is because Container Engine instances are immutable (e.g. you can't or you are not supposed to modify the operating system, you should just run containers).

If you're trying to run scripts when a container starts/stops you need to forget about startup scripts concept in the Compute Engine world. You can use container lifecycle hooks in Kubernetes (the orchestrator running in Container Engine).

Here's documentation and tutorial about it: https://kubernetes.io/docs/concepts/containers/container-lifecycle-hooks/ https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/

-- AhmetB - Google
Source: StackOverflow