diff --git a/dev/cd/after-push-to-branch b/dev/cd/after-push-to-branch new file mode 100755 index 00000000..9f63c476 --- /dev/null +++ b/dev/cd/after-push-to-branch @@ -0,0 +1,37 @@ +#!/bin/bash + +set -o errexit +set -o nounset +set -o pipefail + +# cd to the repo root +REPO_ROOT=$(git rev-parse --show-toplevel) +cd "${REPO_ROOT}" + +if [ -z "${GIT_REF:-}" ]; then + echo "GIT_REF must be set" + exit 1 +fi + +if [[ -z "${REGISTRY_BASE:-}" ]]; then + echo "REGISTRY_BASE must be set" + exit 1 +fi + +if [[ ! "${GIT_REF}" =~ ^refs/heads/.* ]]; then + echo "GIT_REF=${GIT_REF} is not of the expected format refs/heads/*" + exit 1 +fi + +BRANCH=${GIT_REF/refs\/heads\//} +echo "BRANCH is ${BRANCH}" + +GIT_REVISION=$(git rev-parse --short HEAD) +echo "GIT_REVISION is ${GIT_REVISION}" + +export IMAGE_TAG="g${GIT_REVISION}" +echo "IMAGE_TAG is ${IMAGE_TAG}" + +pushd ${REPO_ROOT}/guestbook-go +VERSION=${IMAGE_TAG} REGISTRY=${REGISTRY_BASE} make push +popd \ No newline at end of file diff --git a/dev/cloudbuild/cloudbuild.yaml b/dev/cloudbuild/cloudbuild.yaml new file mode 100644 index 00000000..aaf48fa3 --- /dev/null +++ b/dev/cloudbuild/cloudbuild.yaml @@ -0,0 +1,13 @@ +timeout: 900s +steps: + - name: 'gcr.io/k8s-staging-test-infra/gcb-docker-gcloud:v20230111-cd1b3caf9c' + entrypoint: dev/cd/after-push-to-branch + env: + - GIT_REF=$_GIT_TAG + - PULL_BASE_REF=$_PULL_BASE_REF + - REGISTRY_BASE=gcr.io/k8s-staging-examples +substitutions: + # _GIT_TAG will be filled with a git-based tag for the image, of the form vYYYYMMDD-hash, and + # can be used as a substitution + _GIT_TAG: '12345' + _PULL_BASE_REF: 'master' diff --git a/guestbook-go/Makefile b/guestbook-go/Makefile index 891fcde8..105130ef 100644 --- a/guestbook-go/Makefile +++ b/guestbook-go/Makefile @@ -23,11 +23,11 @@ release: clean build push clean # builds a docker image that builds the app and packages it into a minimal docker image build: - docker build -t ${REGISTRY}/guestbook:${VERSION} . + docker buildx build --load -t ${REGISTRY}/guestbook:${VERSION} . # push the image to an registry push: - gcloud docker -- push ${REGISTRY}/guestbook:${VERSION} + docker buildx build --push -t ${REGISTRY}/guestbook:${VERSION} . # remove previous images and containers clean: diff --git a/guestbook-go/README.md b/guestbook-go/README.md index 3e30df01..f136d479 100644 --- a/guestbook-go/README.md +++ b/guestbook-go/README.md @@ -24,12 +24,12 @@ This example assumes that you have a working cluster. See the [Getting Started G ### Step One: Create the Redis master pod -Use the `examples/guestbook-go/redis-master-controller.json` file to create a [replication controller](https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/) and Redis master [pod](https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/). The pod runs a Redis key-value server in a container. Using a replication controller is the preferred way to launch long-running pods, even for 1 replica, so that the pod benefits from the self-healing mechanism in Kubernetes (keeps the pods alive). +Use the `examples/guestbook-go/redis-master-controller.yaml` file to create a [replication controller](https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/) and Redis master [pod](https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/). The pod runs a Redis key-value server in a container. Using a replication controller is the preferred way to launch long-running pods, even for 1 replica, so that the pod benefits from the self-healing mechanism in Kubernetes (keeps the pods alive). -1. Use the [redis-master-controller.json](redis-master-controller.json) file to create the Redis master replication controller in your Kubernetes cluster by running the `kubectl create -f` *`filename`* command: +1. Use the [redis-master-controller.yaml](redis-master-controller.yaml) file to create the Redis master replication controller in your Kubernetes cluster by running the `kubectl create -f` *`filename`* command: ```console - $ kubectl create -f examples/guestbook-go/redis-master-controller.json + $ kubectl create -f examples/guestbook-go/redis-master-controller.yaml ``` @@ -73,10 +73,10 @@ A Kubernetes [service](https://kubernetes.io/docs/concepts/services-networking/s Services find the pods to load balance based on pod labels. The pod that you created in Step One has the label `app=redis` and `role=master`. The selector field of the service determines which pods will receive the traffic sent to the service. -1. Use the [redis-master-service.json](redis-master-service.json) file to create the service in your Kubernetes cluster by running the `kubectl create -f` *`filename`* command: +1. Use the [redis-master-service.yaml](redis-master-service.yaml) file to create the service in your Kubernetes cluster by running the `kubectl create -f` *`filename`* command: ```console - $ kubectl create -f examples/guestbook-go/redis-master-service.json + $ kubectl create -f examples/guestbook-go/redis-master-service.yaml ``` @@ -96,10 +96,10 @@ Services find the pods to load balance based on pod labels. The pod that you cre The Redis master we created earlier is a single pod (REPLICAS = 1), while the Redis read replicas we are creating here are 'replicated' pods. In Kubernetes, a replication controller is responsible for managing the multiple instances of a replicated pod. -1. Use the file [redis-replica-controller.json](redis-replica-controller.json) to create the replication controller by running the `kubectl create -f` *`filename`* command: +1. Use the file [redis-replica-controller.yaml](redis-replica-controller.yaml) to create the replication controller by running the `kubectl create -f` *`filename`* command: ```console - $ kubectl create -f examples/guestbook-go/redis-replica-controller.json + $ kubectl create -f examples/guestbook-go/redis-replica-controller.yaml ``` @@ -139,10 +139,10 @@ The Redis master we created earlier is a single pod (REPLICAS = 1), while the Re Just like the master, we want to have a service to proxy connections to the read replicas. In this case, in addition to discovery, the Redis replica service provides transparent load balancing to clients. -1. Use the [redis-replica-service.json](redis-replica-service.json) file to create the Redis replica service by running the `kubectl create -f` *`filename`* command: +1. Use the [redis-replica-service.yaml](redis-replica-service.yaml) file to create the Redis replica service by running the `kubectl create -f` *`filename`* command: ```console - $ kubectl create -f examples/guestbook-go/redis-replica-service.json + $ kubectl create -f examples/guestbook-go/redis-replica-service.yaml ``` @@ -164,14 +164,14 @@ Tip: It is helpful to set labels on your services themselves--as we've done here This is a simple Go `net/http` ([negroni](https://github.com/codegangsta/negroni) based) server that is configured to talk to either the replica or master services depending on whether the request is a read or a write. The pods we are creating expose a simple JSON interface and serves a jQuery-Ajax based UI. Like the Redis replica pods, these pods are also managed by a replication controller. -1. Use the [guestbook-controller.json](guestbook-controller.json) file to create the guestbook replication controller by running the `kubectl create -f` *`filename`* command: +1. Use the [guestbook-controller.yaml](guestbook-controller.yaml) file to create the guestbook replication controller by running the `kubectl create -f` *`filename`* command: ```console - $ kubectl create -f examples/guestbook-go/guestbook-controller.json + $ kubectl create -f examples/guestbook-go/guestbook-controller.yaml ``` - Tip: If you want to modify the guestbook code open the `_src` of this example and read the README.md and the Makefile. If you have pushed your custom image be sure to update the `image` accordingly in the guestbook-controller.json. + Tip: If you want to modify the guestbook code open the `_src` of this example and read the README.md and the Makefile. If you have pushed your custom image be sure to update the `image` accordingly in the guestbook-controller.yaml. 2. To verify that the guestbook replication controller is running, run the `kubectl get rc` command: @@ -204,10 +204,10 @@ This is a simple Go `net/http` ([negroni](https://github.com/codegangsta/negroni Just like the others, we create a service to group the guestbook pods but this time, to make the guestbook front end externally visible, we specify `"type": "LoadBalancer"`. -1. Use the [guestbook-service.json](guestbook-service.json) file to create the guestbook service by running the `kubectl create -f` *`filename`* command: +1. Use the [guestbook-service.yaml](guestbook-service.yaml) file to create the guestbook service by running the `kubectl create -f` *`filename`* command: ```console - $ kubectl create -f examples/guestbook-go/guestbook-service.json + $ kubectl create -f examples/guestbook-go/guestbook-service.yaml ``` diff --git a/guestbook-go/guestbook-controller.json b/guestbook-go/guestbook-controller.json deleted file mode 100644 index b069885d..00000000 --- a/guestbook-go/guestbook-controller.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "kind":"ReplicationController", - "apiVersion":"v1", - "metadata":{ - "name":"guestbook", - "labels":{ - "app":"guestbook" - } - }, - "spec":{ - "replicas":3, - "selector":{ - "app":"guestbook" - }, - "template":{ - "metadata":{ - "labels":{ - "app":"guestbook" - } - }, - "spec":{ - "containers":[ - { - "name":"guestbook", - "image":"registry.k8s.io/guestbook:v3", - "ports":[ - { - "name":"http-server", - "containerPort":3000 - } - ] - } - ] - } - } - } -} diff --git a/guestbook-go/guestbook-controller.yaml b/guestbook-go/guestbook-controller.yaml new file mode 100644 index 00000000..ab81bbd3 --- /dev/null +++ b/guestbook-go/guestbook-controller.yaml @@ -0,0 +1,21 @@ +kind: ReplicationController +apiVersion: v1 +metadata: + name: guestbook + labels: + app: guestbook +spec: + replicas: 3 + selector: + app: guestbook + template: + metadata: + labels: + app: guestbook + spec: + containers: + - name: guestbook + image: k8s.gcr.io/guestbook:v3 + ports: + - name: http-server + containerPort: 3000 diff --git a/guestbook-go/guestbook-service.json b/guestbook-go/guestbook-service.json deleted file mode 100644 index cc7640e4..00000000 --- a/guestbook-go/guestbook-service.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "kind":"Service", - "apiVersion":"v1", - "metadata":{ - "name":"guestbook", - "labels":{ - "app":"guestbook" - } - }, - "spec":{ - "ports": [ - { - "port":3000, - "targetPort":"http-server" - } - ], - "selector":{ - "app":"guestbook" - }, - "type": "LoadBalancer" - } -} diff --git a/guestbook-go/guestbook-service.yaml b/guestbook-go/guestbook-service.yaml new file mode 100644 index 00000000..677bde4e --- /dev/null +++ b/guestbook-go/guestbook-service.yaml @@ -0,0 +1,13 @@ +kind: Service +apiVersion: v1 +metadata: + name: guestbook + labels: + app: guestbook +spec: + ports: + - port: 3000 + targetPort: http-server + selector: + app: guestbook + type: LoadBalancer diff --git a/guestbook-go/redis-master-controller.json b/guestbook-go/redis-master-controller.json deleted file mode 100644 index 90619760..00000000 --- a/guestbook-go/redis-master-controller.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "kind":"ReplicationController", - "apiVersion":"v1", - "metadata":{ - "name":"redis-master", - "labels":{ - "app":"redis", - "role":"master" - } - }, - "spec":{ - "replicas":1, - "selector":{ - "app":"redis", - "role":"master" - }, - "template":{ - "metadata":{ - "labels":{ - "app":"redis", - "role":"master" - } - }, - "spec":{ - "containers":[ - { - "name":"redis-master", - "image":"registry.k8s.io/redis:e2e", - "ports":[ - { - "name":"redis-server", - "containerPort":6379 - } - ] - } - ] - } - } - } -} diff --git a/guestbook-go/redis-master-controller.yaml b/guestbook-go/redis-master-controller.yaml new file mode 100644 index 00000000..0dca0f37 --- /dev/null +++ b/guestbook-go/redis-master-controller.yaml @@ -0,0 +1,24 @@ +kind: ReplicationController +apiVersion: v1 +metadata: + name: redis-master + labels: + app: redis + role: master +spec: + replicas: 1 + selector: + app: redis + role: master + template: + metadata: + labels: + app: redis + role: master + spec: + containers: + - name: redis-master + image: k8s.gcr.io/redis:e2e + ports: + - name: redis-server + containerPort: 6379 diff --git a/guestbook-go/redis-master-service.json b/guestbook-go/redis-master-service.json deleted file mode 100644 index 3a7426ea..00000000 --- a/guestbook-go/redis-master-service.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "kind":"Service", - "apiVersion":"v1", - "metadata":{ - "name":"redis-master", - "labels":{ - "app":"redis", - "role":"master" - } - }, - "spec":{ - "ports": [ - { - "port":6379, - "targetPort":"redis-server" - } - ], - "selector":{ - "app":"redis", - "role":"master" - } - } -} diff --git a/guestbook-go/redis-master-service.yaml b/guestbook-go/redis-master-service.yaml new file mode 100644 index 00000000..c69456e3 --- /dev/null +++ b/guestbook-go/redis-master-service.yaml @@ -0,0 +1,14 @@ +kind: Service +apiVersion: v1 +metadata: + name: redis-master + labels: + app: redis + role: master +spec: + ports: + - port: 6379 + targetPort: redis-server + selector: + app: redis + role: master diff --git a/guestbook-go/redis-replica-controller.json b/guestbook-go/redis-replica-controller.json deleted file mode 100644 index 9971c01e..00000000 --- a/guestbook-go/redis-replica-controller.json +++ /dev/null @@ -1,40 +0,0 @@ -{ - "kind":"ReplicationController", - "apiVersion":"v1", - "metadata":{ - "name":"redis-replica", - "labels":{ - "app":"redis", - "role":"replica" - } - }, - "spec":{ - "replicas":2, - "selector":{ - "app":"redis", - "role":"replica" - }, - "template":{ - "metadata":{ - "labels":{ - "app":"redis", - "role":"replica" - } - }, - "spec":{ - "containers":[ - { - "name":"redis-replica", - "image":"registry.k8s.io/redis-slave:v2", - "ports":[ - { - "name":"redis-server", - "containerPort":6379 - } - ] - } - ] - } - } - } -} diff --git a/guestbook-go/redis-replica-controller.yaml b/guestbook-go/redis-replica-controller.yaml new file mode 100644 index 00000000..96238c21 --- /dev/null +++ b/guestbook-go/redis-replica-controller.yaml @@ -0,0 +1,24 @@ +kind: ReplicationController +apiVersion: v1 +metadata: + name: redis-replica + labels: + app: redis + role: replica +spec: + replicas: 2 + selector: + app: redis + role: replica + template: + metadata: + labels: + app: redis + role: replica + spec: + containers: + - name: redis-replica + image: k8s.gcr.io/redis-slave:v2 + ports: + - name: redis-server + containerPort: 6379 diff --git a/guestbook-go/redis-replica-service.json b/guestbook-go/redis-replica-service.json deleted file mode 100644 index 80a0c44c..00000000 --- a/guestbook-go/redis-replica-service.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "kind":"Service", - "apiVersion":"v1", - "metadata":{ - "name":"redis-replica", - "labels":{ - "app":"redis", - "role":"replica" - } - }, - "spec":{ - "ports": [ - { - "port":6379, - "targetPort":"redis-server" - } - ], - "selector":{ - "app":"redis", - "role":"replica" - } - } -} diff --git a/guestbook-go/redis-replica-service.yaml b/guestbook-go/redis-replica-service.yaml new file mode 100644 index 00000000..191db0b2 --- /dev/null +++ b/guestbook-go/redis-replica-service.yaml @@ -0,0 +1,14 @@ +kind: Service +apiVersion: v1 +metadata: + name: redis-replica + labels: + app: redis + role: replica +spec: + ports: + - port: 6379 + targetPort: redis-server + selector: + app: redis + role: replica