Skip to content

Commit 0b8e95a

Browse files
committed
feature(Controller): Add first examples
1 parent 9f2d0da commit 0b8e95a

File tree

6 files changed

+206
-0
lines changed

6 files changed

+206
-0
lines changed

advanced/Controller/README.adoc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Controller
2+
3+
4+
```
5+
# Start minikube and enable the ingress addon
6+
minikube start
7+
minikube addons enable ingress
8+
9+
# Create a configmap holding the controller shell script:
10+
kubectl create configmap expose-controller-script --from-file=./expose-controller.sh
11+
12+
# Create the controller object:
13+
kubectl create -f expose-controller.yml
14+
15+
# Create a sample web application with an 'expose' annotation:
16+
kubectl create -f web-app.yml
17+
18+
# Check that an ingress has been created
19+
kubectl get ingress web-app -o yaml
20+
21+
# Open Ingress route
22+
open http://$(minikube ip)/mdn
23+
```
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM alpine
2+
RUN apk add --update \
3+
curl \
4+
jq \
5+
&& rm -rf /var/cache/apk/*
6+
ENTRYPOINT ["curl"]
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/bin/bash
2+
3+
# Call it with the namespace to monitor
4+
namespace=${1:-default}
5+
6+
# API URL setup. Requires an ambassador API proxy running side-by-side
7+
base=http://localhost:8001
8+
ns=namespaces/$namespace
9+
k8s_service_url=$base/api/v1/$ns/services
10+
k8s_ingress_url=$base/apis/extensions/v1beta1/$ns/ingresses
11+
12+
# Watch the K8s API on events on service objects
13+
echo "::: Starting to wait for events"
14+
curl -N -s -k $k8s_service_url?watch=true | while read event
15+
do
16+
# Event type
17+
type=$(echo $event | jq -r '.type')
18+
# Annotation "expose" on service
19+
expose=$(echo $event | jq -r '.object.metadata.annotations.expose?')
20+
# Firt service port
21+
port=$(echo $event | jq -r '.object.spec.ports[0]?.port')
22+
# Service
23+
service=$(echo $event | jq -r .object.metadata.name)
24+
25+
echo "::: $type -- $service [$port] expose = $expose"
26+
27+
# If a new service has been added and when its labeled with "expose", then
28+
# create an ingress objecgt for it
29+
if [ $type = "ADDED" ] && [ $expose != 'null' ]; then
30+
# Check for Ingress with the same name
31+
http_code=$(curl -s -o /dev/null -w "%{http_code}" $k8s_ingress_url/$service)
32+
if [ $http_code != 200 ]; then
33+
echo "::: Creating Ingress backend for service '$service'"
34+
cat - << EOT | curl -s -H "Content-Type: application/json" -X "POST" -d @- $k8s_ingress_url
35+
{
36+
"apiVersion": "extensions/v1beta1",
37+
"kind": "Ingress",
38+
"metadata": {
39+
"name": "$service",
40+
"namespace": "$namespace"
41+
},
42+
"spec": {
43+
"rules": [{
44+
"http": {
45+
"paths": [{
46+
"backend": {
47+
"serviceName": "$service",
48+
"servicePort": $port
49+
},
50+
"path": "$expose"
51+
}]
52+
}
53+
}]
54+
}
55+
}
56+
EOT
57+
echo
58+
else
59+
echo "::: Ingress '$service' already exists. Skipping ..."
60+
fi
61+
fi
62+
done
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Example Deployment using a config map as input for a template
2+
# which is processed from an init-container
3+
---
4+
apiVersion: extensions/v1beta1
5+
kind: Deployment
6+
metadata:
7+
labels:
8+
project: k8spatterns
9+
pattern: Controller
10+
name: expose-controller
11+
spec:
12+
replicas: 1
13+
template:
14+
metadata:
15+
labels:
16+
project: k8spatterns
17+
pattern: Controller
18+
spec:
19+
containers:
20+
- name: kubeapi-proxy
21+
image: rhuss/kubeapi-proxy
22+
- name: expose-controller
23+
image: rhuss/curl-jq
24+
command:
25+
- "sh"
26+
- "/expose-script/expose-controller.sh"
27+
volumeMounts:
28+
- mountPath: "/expose-script"
29+
name: expose-controller-script
30+
volumes:
31+
- name: expose-controller-script
32+
configMap:
33+
name: expose-controller-script
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM alpine
2+
ENV KUBECTL_VERSION=v1.8.0
3+
RUN apk update \
4+
&& apk add curl \
5+
&& curl -L -O https://dl.k8s.io/${KUBECTL_VERSION}/kubernetes-client-linux-amd64.tar.gz \
6+
&& tar zvxf kubernetes-client-linux-amd64.tar.gz kubernetes/client/bin/kubectl \
7+
&& mv kubernetes/client/bin/kubectl / \
8+
&& apk del curl \
9+
&& rm -rf kubernetes \
10+
&& rm -f kubernetes-client-linux-amd64.tar.gz \
11+
&& rm -rf /var/cache/apk/*
12+
ENTRYPOINT [ \
13+
"/bin/ash", "-c", \
14+
"/kubectl proxy \
15+
--server https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT \
16+
--certificate-authority=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt \
17+
--token=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) \
18+
--accept-paths='^.*' \
19+
"]

advanced/Controller/web-app.yml

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Example for a HTTP server with a git pulling sidecar
2+
---
3+
apiVersion: v1
4+
kind: List
5+
items:
6+
- apiVersion: v1
7+
kind: Pod
8+
metadata:
9+
name: web-app
10+
labels:
11+
project: k8spatterns
12+
pattern: Controller
13+
app: web-app
14+
spec:
15+
containers:
16+
# Main container is a stock httpd serving from /var/www/html
17+
- name: app
18+
image: centos/httpd
19+
ports:
20+
- containerPort: 80
21+
volumeMounts:
22+
- mountPath: /var/www/html/mdn
23+
name: git
24+
# Sidecar poll every 10 minutes a given repository with git
25+
- name: poll
26+
image: axeclbr/git
27+
volumeMounts:
28+
- mountPath: /var/lib/data
29+
name: git
30+
env:
31+
- name: GIT_REPO
32+
value: https://github.com/mdn/beginner-html-site-scripted
33+
command:
34+
- "sh"
35+
- "-c"
36+
- "git clone $(GIT_REPO) . && watch -n 600 git pull"
37+
workingDir: /var/lib/data
38+
volumes:
39+
# The shared directory for holding the files
40+
- emptyDir: {}
41+
name: git
42+
# A service which opens a NodePort is added for your convenience
43+
# but is not necessarily required for this example:
44+
- apiVersion: v1
45+
kind: Service
46+
metadata:
47+
labels:
48+
project: k8spatterns
49+
pattern: Controller
50+
app: web-app
51+
annotations:
52+
# Annotation indicating that this path should be reachable as
53+
# ingress route and will be picked up by the ingress controller:
54+
expose: "/mdn"
55+
name: web-app
56+
spec:
57+
ports:
58+
- name: http
59+
port: 8080
60+
protocol: TCP
61+
targetPort: 80
62+
selector:
63+
app: web-app

0 commit comments

Comments
 (0)