Skip to content

Commit 85a7c5d

Browse files
PetSet examples
1 parent 85960b2 commit 85a7c5d

33 files changed

+1516
-0
lines changed

examples/pets/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# PetSet examples
2+
3+
These examples are tracked from the [Kubernetes contrib project @d6e4be](https://github.com/kubernetes/contrib/tree/d6e4be066cc076fbb91ff69691819e117711b30b/pets)

examples/pets/mysql/galera/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Mysql Galera
2+
3+
This example runs mysql galera through a petset.
4+
5+
## Bootstrap
6+
7+
Create the petset in this directory
8+
```
9+
$ kubectl create -f galera.yaml
10+
```
11+
12+
Once you have all 3 nodes in Running, you can run the "test.sh" script in this directory.
13+
This example requires manual intervention.
14+
Once you have all 3 nodes in Running, you can run the "test.sh" script in this directory.
15+
16+
## Caveats
17+
18+
Starting up all galera nodes at once leads to an issue where all the mysqls
19+
belive they're in the primary component because they don't see the others in
20+
the DNS. For the bootstrapping to work: mysql-0 needs to see itself, mysql-1
21+
needs to see itself and mysql-0, and so on, because the first node that sees
22+
a peer list of 1 will assume it's the leader.
23+
24+
## TODO
25+
26+
Expect better solutions for the following as petset matures.
27+
28+
* Failover
29+
* Scaling Up
30+
* Scaling Down
31+
* Image Upgrade
32+
* Maintenance
33+
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright 2016 The Kubernetes Authors All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# TODO: get rid of bash dependency and switch to plain busybox.
16+
# The tar in busybox also doesn't seem to understand compression.
17+
FROM debian:jessie
18+
MAINTAINER Prashanth.B <[email protected]>
19+
20+
RUN apt-get update && apt-get install -y wget
21+
22+
ADD on-start.sh /
23+
ADD my-galera.cnf /
24+
# See contrib/pets/peer-finder for details
25+
RUN wget -qO /peer-finder https://storage.googleapis.com/kubernetes-release/pets/peer-finder
26+
27+
ADD install.sh /
28+
RUN chmod -c 755 /install.sh /on-start.sh /peer-finder
29+
Entrypoint ["/install.sh"]
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2016 The Kubernetes Authors All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
all: push
16+
17+
TAG = 0.1
18+
PREFIX = gcr.io/google_containers/galera-install
19+
20+
container:
21+
docker build -t $(PREFIX):$(TAG) .
22+
23+
push: container
24+
gcloud docker push $(PREFIX):$(TAG)
25+
26+
clean:
27+
docker rmi $(PREFIX):$(TAG)
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#! /bin/bash
2+
3+
# Copyright 2016 The Kubernetes Authors All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# This volume is assumed to exist and is shared with parent of the init
18+
# container. It contains the mysq config.
19+
CONFIG_VOLUME="/etc/mysql"
20+
21+
# This volume is assumed to exist and is shared with the peer-finder
22+
# init container. It contains on-start/change configuration scripts.
23+
WORKDIR_VOLUME="/work-dir"
24+
25+
for i in "$@"
26+
do
27+
case $i in
28+
-c=*|--config=*)
29+
CONFIG_VOLUME="${i#*=}"
30+
shift
31+
;;
32+
-w=*|--work-dir=*)
33+
WORKDIR_VOLUME="${i#*=}"
34+
shift
35+
;;
36+
*)
37+
# unknown option
38+
;;
39+
esac
40+
done
41+
42+
echo installing config scripts into "${WORKDIR_VOLUME}"
43+
mkdir -p "${WORKDIR_VOLUME}"
44+
cp /on-start.sh "${WORKDIR_VOLUME}"/
45+
cp /peer-finder "${WORKDIR_VOLUME}"/
46+
47+
echo installing my-galera.cnf into "${CONFIG_VOLUME}"
48+
mkdir -p "${CONFIG_VOLUME}"
49+
chown -R mysql:mysql "${CONFIG_VOLUME}"
50+
cp /my-galera.cnf "${CONFIG_VOLUME}"/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[mysqld]
2+
user = mysql
3+
bind-address = 0.0.0.0
4+
wsrep_provider = /usr/lib/galera/libgalera_smm.so
5+
# TODO: is rsync the best option?
6+
wsrep_sst_method = rsync
7+
default_storage_engine = innodb
8+
binlog_format = row
9+
innodb_autoinc_lock_mode = 2
10+
innodb_flush_log_at_trx_commit = 0
11+
query_cache_size = 0
12+
query_cache_type = 0
13+
14+
# By default every node is standalone
15+
wsrep_cluster_address=gcomm://
16+
wsrep_cluster_name=galera
17+
wsrep_node_address=127.0.0.1
18+
19+
# TODO: Enable use privileges. This doesn't work
20+
# on mysql restart, for some reason after the SST
21+
# permissions are not setup correctly.
22+
skip-grant-tables
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#! /bin/bash
2+
3+
# Copyright 2016 The Kubernetes Authors All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# This script writes out a mysql galera config using a list of newline seperated
18+
# peer DNS names it accepts through stdin.
19+
20+
# /etc/mysql is assumed to be a shared volume so we can modify my.cnf as required
21+
# to keep the config up to date, without wrapping mysqld in a custom pid1.
22+
# The config location is intentionally not /etc/mysql/my.cnf because the
23+
# standard base image clobbers that location.
24+
CFG=/etc/mysql/my-galera.cnf
25+
26+
function join {
27+
local IFS="$1"; shift; echo "$*";
28+
}
29+
30+
HOSTNAME=$(hostname)
31+
# Parse out cluster name, formatted as: petset_name-index
32+
IFS='-' read -ra ADDR <<< "$(hostname)"
33+
CLUSTER_NAME="${ADDR[0]}"
34+
35+
while read -ra LINE; do
36+
if [[ "${LINE}" == *"${HOSTNAME}"* ]]; then
37+
MY_NAME=$LINE
38+
fi
39+
PEERS=("${PEERS[@]}" $LINE)
40+
done
41+
42+
if [ "${#PEERS[@]}" = 1 ]; then
43+
WSREP_CLUSTER_ADDRESS=""
44+
else
45+
WSREP_CLUSTER_ADDRESS=$(join , "${PEERS[@]}")
46+
fi
47+
sed -i -e "s|^wsrep_node_address=.*$|wsrep_node_address=${MY_NAME}|" ${CFG}
48+
sed -i -e "s|^wsrep_cluster_name=.*$|wsrep_cluster_name=${CLUSTER_NAME}|" ${CFG}
49+
sed -i -e "s|^wsrep_cluster_address=.*$|wsrep_cluster_address=gcomm://${WSREP_CLUSTER_ADDRESS}|" ${CFG}
50+
51+
# don't need a restart, we're just writing the conf in case there's an
52+
# unexpected restart on the node.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# A headless service to create DNS records
2+
apiVersion: v1
3+
kind: Service
4+
metadata:
5+
annotations:
6+
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
7+
name: galera
8+
labels:
9+
app: mysql
10+
spec:
11+
ports:
12+
- port: 3306
13+
name: mysql
14+
# *.galear.default.svc.cluster.local
15+
clusterIP: None
16+
selector:
17+
app: mysql
18+
---
19+
apiVersion: apps/v1alpha1
20+
kind: PetSet
21+
metadata:
22+
name: mysql
23+
spec:
24+
serviceName: "galera"
25+
replicas: 3
26+
template:
27+
metadata:
28+
labels:
29+
app: mysql
30+
annotations:
31+
pod.alpha.kubernetes.io/initialized: "true"
32+
pod.alpha.kubernetes.io/init-containers: '[
33+
{
34+
"name": "install",
35+
"image": "gcr.io/google_containers/galera-install:0.1",
36+
"imagePullPolicy": "Always",
37+
"args": ["--work-dir=/work-dir"],
38+
"volumeMounts": [
39+
{
40+
"name": "workdir",
41+
"mountPath": "/work-dir"
42+
},
43+
{
44+
"name": "config",
45+
"mountPath": "/etc/mysql"
46+
}
47+
]
48+
},
49+
{
50+
"name": "bootstrap",
51+
"image": "debian:jessie",
52+
"command": ["/work-dir/peer-finder"],
53+
"args": ["-on-start=\"/work-dir/on-start.sh\"", "-service=galera"],
54+
"env": [
55+
{
56+
"name": "POD_NAMESPACE",
57+
"valueFrom": {
58+
"fieldRef": {
59+
"apiVersion": "v1",
60+
"fieldPath": "metadata.namespace"
61+
}
62+
}
63+
}
64+
],
65+
"volumeMounts": [
66+
{
67+
"name": "workdir",
68+
"mountPath": "/work-dir"
69+
},
70+
{
71+
"name": "config",
72+
"mountPath": "/etc/mysql"
73+
}
74+
]
75+
}
76+
]'
77+
spec:
78+
containers:
79+
- name: mysql
80+
image: erkules/galera:basic
81+
ports:
82+
- containerPort: 3306
83+
name: mysql
84+
- containerPort: 4444
85+
name: sst
86+
- containerPort: 4567
87+
name: replication
88+
- containerPort: 4568
89+
name: ist
90+
args:
91+
- --defaults-file=/etc/mysql/my-galera.cnf
92+
- --user=root
93+
readinessProbe:
94+
exec:
95+
command:
96+
- sh
97+
- -c
98+
- "mysql -u root -e 'show databases;'"
99+
initialDelaySeconds: 15
100+
timeoutSeconds: 5
101+
volumeMounts:
102+
- name: datadir
103+
mountPath: /var/lib/
104+
- name: config
105+
mountPath: /etc/mysql
106+
volumes:
107+
- name: config
108+
emptyDir: {}
109+
- name: workdir
110+
emptyDir: {}
111+
volumeClaimTemplates:
112+
- metadata:
113+
name: datadir
114+
annotations:
115+
volume.alpha.kubernetes.io/storage-class: anything
116+
spec:
117+
accessModes: [ "ReadWriteOnce" ]
118+
resources:
119+
requests:
120+
storage: 10Gi

examples/pets/mysql/galera/test.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#! /bin/bash
2+
3+
# Copyright 2016 The Kubernetes Authors All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
kubectl exec mysql-0 -- mysql -u root -e "create database test;"
18+
kubectl exec mysql-1 -- mysql -u root -e "use test; create table pet (id int(10), name varchar(20));"
19+
kubectl exec mysql-1 -- mysql -u root -e "use test; insert into pet (id, name) values (1, \"galera\");"
20+
kubectl exec mysql-2 -- mysql -u root -e "use test; select * from pet;"
21+
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2016 The Kubernetes Authors All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# TODO: get rid of bash dependency and switch to plain busybox.
16+
# The tar in busybox also doesn't seem to understand compression.
17+
FROM debian:jessie
18+
MAINTAINER Prashanth.B <[email protected]>
19+
20+
RUN apt-get update && apt-get install -y mysql-client wget
21+
ADD mysql_healthz /mysql_healthz
22+
Entrypoint ["/mysql_healthz"]

0 commit comments

Comments
 (0)