ruk·si

☸️ Kubernetes
Multi-container Example

Updated at 2016-02-26 03:25

Google Container Registry (GCR) is a great service to host your private Docker images when hosting your Kubernetes cluster in Google Cloud Platform. Container engine seems to create a Docker registry automatically when you push images in.

1. Choose region: us.gcr.io / eu.gcr.io / asia.gcr.io
2. Decide image and tag: us.gcr.io/your-project-id/image-name:tag

gcloud docker push us.gcr.io/your-project-id/example-image:your-tag
gcloud docker pull us.gcr.io/your-project-id/example-image:your-tag

# You can find container clusters and registry at:
# https://console.cloud.google.com/kubernetes/list?project=your-project-id
# Define files below this section...

cd greetings
CGO_ENABLED=0 GOOS=linux go build -o greetings -a -tags netgo -ldflags '-w' .
docker build -t us.gcr.io/gobernetes/greetings:v1 .
docker run --rm -p 4000:4000 us.gcr.io/gobernetes/greetings:v1
# http://dockerhost:4000/
gcloud docker push us.gcr.io/gobernetes/greetings:v1

cd ../hello
CGO_ENABLED=0 GOOS=linux go build -o hello -a -tags netgo -ldflags '-w' .
docker build -t us.gcr.io/gobernetes/hello:v1 .
docker run --rm -p 3000:3000 us.gcr.io/gobernetes/hello:v1
# http://dockerhost:3000/
gcloud docker push us.gcr.io/gobernetes/hello:v1

cd ..
gcloud container clusters create production
kubectl create -f rc.yaml
kubectl get rc
kubectl get pods

kubectl create -f greetings-lb.yaml
kubectl create -f hello-lb.yaml
kubectl get service
kubectl describe services greetings-lb
kubectl describe services hello-lb
# Get "LoadBalancer Ingress" IP from both and you can access the containers.

# Releases are done by pushing new images, editing rc.yaml and submitting it.
kubectl rolling-update gobernetes-rc-v1 -f rc.yaml
kubectl rolling-update gobernetes-rc-v2 -f rc.yaml
kubectl rolling-update gobernetes-rc-v3 -f rc.yaml

# To tear down everything:
gcloud container clusters delete production

rc.yaml:

apiVersion: "v1"
kind: "ReplicationController"
metadata:
  name: "gobernetes-rc-v1"
  labels:
    name: "gobernetes-rc-v1"
spec:
  replicas: 3
  selector:
    name: "gobernetes"
    version: "v1"
  template:
    metadata:
      labels:
        name: "gobernetes"
        version: "v1"
    spec:
      containers:
        - name: "greetings"
          image: "us.gcr.io/gobernetes/greetings:v1"
          ports:
            - name: "http"
              containerPort: 4000
              protocol: "TCP"
        - name: "hello"
          image: "us.gcr.io/gobernetes/hello:v1"
          ports:
            - name: "http"
              containerPort: 3000
              protocol: "TCP"

greetings-lb.yaml:

apiVersion: "v1"
kind: "Service"
metadata:
  name: "greetings-lb"
  labels:
    name: "greetings-lb"
spec:
  type: "LoadBalancer"
  ports:
    - name: "http"
      port: 80
      targetPort: 4000
  selector:
    name: "gobernetes"

hello-lb.yaml:

apiVersion: "v1"
kind: "Service"
metadata:
  name: "hello-lb"
  labels:
    name: "hello-lb"
spec:
  type: "LoadBalancer"
  ports:
    - name: "http"
      port: 80
      targetPort: 3000
  selector:
    name: "gobernetes"

greetings/greetings.go:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe(":4000", nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Greetings from Go!")
}

hello/hello.go:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", handler)
    log.Fatal(http.ListenAndServe(":3000", nil))
}

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello from Go!")
}

greetings/Dockerfile:

FROM scratch
ADD greetings /
CMD ["/greetings"]

hello/Dockerfile:

FROM scratch
ADD hello /
CMD ["/hello"]

Source