Moving k8s support scripts out of code folder (#216)

* Adding pod setup to kubernetes startup scripts

* Adding template description to k8s README.md

* Fix typo.

* Moving kubernetes setup scripts out of go folder

* Fixing script error

* Adding messages to k8s scripts for clarity
This commit is contained in:
Michael Richmond 2016-08-16 09:12:52 -07:00 committed by GitHub
parent 79fd268e9c
commit 9aaeef6376
11 changed files with 105 additions and 69 deletions

View file

@ -36,7 +36,7 @@ before_script:
- ./etcd-v2.3.1-linux-amd64/etcd & - ./etcd-v2.3.1-linux-amd64/etcd &
# If docker is available, pull the kubernetes hyperkube image down and launch kubernetes. # If docker is available, pull the kubernetes hyperkube image down and launch kubernetes.
- if which docker &>/dev/null ; then docker pull gcr.io/google_containers/hyperkube-amd64:v1.2.4 ; docker ps -a ; fi - if which docker &>/dev/null ; then docker pull gcr.io/google_containers/hyperkube-amd64:v1.2.4 ; docker ps -a ; fi
- if which docker &>/dev/null ; then ./middleware/kubernetes/test/00_run_k8s.sh && ./middleware/kubernetes/test/10_setup_kubectl.sh && ./middleware/kubernetes/test/20_setup_k8s_services.sh ; docker ps -a ; fi - if which docker &>/dev/null ; then ./contrib/kubernetes/testscripts/start_k8s_with_services.sh ; docker ps -a ; fi
# Get golang dependencies, and build coredns binary # Get golang dependencies, and build coredns binary
- go get -v -d - go get -v -d
- go get github.com/coreos/go-etcd/etcd - go get github.com/coreos/go-etcd/etcd
@ -45,4 +45,4 @@ before_script:
script: script:
- go test -tags etcd -race -bench=. ./... - go test -tags etcd -race -bench=. ./...
# Run kubernetes integration tests only if kubectl is available. i.e. If kubernetes was launched # Run kubernetes integration tests only if kubectl is available. i.e. If kubernetes was launched
- ./middleware/kubernetes/test/kubectl version && go test -tags k8s -race -bench=. -run 'TestK8sIntegration' ./test - ./contrib/kubernetes/testscripts/kubectl version && go test -tags k8s -race -bench=. -run 'TestK8sIntegration' ./test

View file

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
set -e
# Based on instructions at: http://kubernetes.io/docs/getting-started-guides/docker/ # Based on instructions at: http://kubernetes.io/docs/getting-started-guides/docker/
#K8S_VERSION=$(curl -sS https://storage.googleapis.com/kubernetes-release/release/latest.txt) #K8S_VERSION=$(curl -sS https://storage.googleapis.com/kubernetes-release/release/latest.txt)
@ -19,6 +21,8 @@ else
DNS_ARGUMENTS="" DNS_ARGUMENTS=""
fi fi
echo "Starting kubernetes..."
docker run -d \ docker run -d \
--volume=/:/rootfs:ro \ --volume=/:/rootfs:ro \
--volume=/sys:/sys:ro \ --volume=/sys:/sys:ro \

View file

@ -1,9 +1,14 @@
#!/bin/bash #!/bin/bash
set -e
PWD=`pwd` PWD=`pwd`
BASEDIR=`readlink -e $(dirname ${0})` BASEDIR=`readlink -e $(dirname ${0})`
cd ${BASEDIR} cd ${BASEDIR}
echo "Setting up kubectl..."
if [ ! -e kubectl ]; then if [ ! -e kubectl ]; then
curl -O http://storage.googleapis.com/kubernetes-release/release/v1.2.4/bin/linux/amd64/kubectl curl -O http://storage.googleapis.com/kubernetes-release/release/v1.2.4/bin/linux/amd64/kubectl
chmod u+x kubectl chmod u+x kubectl

View file

@ -39,7 +39,6 @@ create_namespaces() {
# run_and_expose_service <servicename> <namespace> <image> <port> # run_and_expose_service <servicename> <namespace> <image> <port>
run_and_expose_service() { run_and_expose_service() {
if [ "${#}" != "4" ]; then if [ "${#}" != "4" ]; then
return -1 return -1
fi fi
@ -49,7 +48,7 @@ run_and_expose_service() {
image="${3}" image="${3}"
port="${4}" port="${4}"
echo " starting service '${service}' in namespace '${namespace}" echo " starting service '${service}' in namespace '${namespace}'"
${KUBECTL} get deployment --namespace=${namespace} --no-headers 2>/dev/null | grep -q ${service} ${KUBECTL} get deployment --namespace=${namespace} --no-headers 2>/dev/null | grep -q ${service}
if [ "${?}" != "0" ]; then if [ "${?}" != "0" ]; then
@ -67,6 +66,29 @@ run_and_expose_service() {
} }
#run_and_expose_rc nginx-controller nginx-rc.yml poddemo 80
run_and_expose_rc() {
if [ "${#}" != "4" ]; then
return -1
fi
rc_name="${1}"
rc_file="${2}"
namespace="${3}"
port="${4}"
echo " starting replication controller '${rc_name}' from '${rc_file}' in namespace '${namespace}'"
${KUBECTL} get rc --namespace=${namespace} --no-headers 2>/dev/null | grep -q ${rc_name}
if [ "${?}" != "0" ]; then
${KUBECTL} expose -f ${rc_file} --namespace=${namespace} --port=${port}
else
echo "warn: rc '${rc_name}' already running in namespace '${namespace}'"
fi
}
echo "Starting sample kubernetes services..."
wait_until_k8s_ready wait_until_k8s_ready
NAMESPACES="demo poddemo test" NAMESPACES="demo poddemo test"
@ -84,4 +106,14 @@ echo ""
echo "Services exposed:" echo "Services exposed:"
${KUBECTL} get services --all-namespaces ${KUBECTL} get services --all-namespaces
echo ""
echo "Starting replicationcontrollers:"
run_and_expose_rc nginx-controller nginx-rc.yml poddemo 80
echo ""
echo "ReplicationControllers exposed:"
${KUBECTL} get rc --all-namespaces
cd ${PWD} cd ${PWD}

View file

@ -6,9 +6,17 @@ Requirements:
The scripts in this directory startup kubernetes with docker as the container runtime. The scripts in this directory startup kubernetes with docker as the container runtime.
After starting kubernetes, a couple of kubernetes services are started to allow automatic After starting kubernetes, a couple of kubernetes services are started to allow automatic
testing of CoreDNS with kubernetes. testing of CoreDNS with kubernetes. The kubernetes integration tests in `test/kubernetes_test.go` depend on having some sample services running. The scripts in this folder
automate the launch of kubernetes and the creation of the expected sample services.
To use, run the scripts as: To start up kubernetes and launch some sample services,
run the script `start_k8s_with_services.sh`.
~~~
$ ./start_k8s_with_services.sh
~~~
Alternatively, the individual scripts may be run independently as needed:
~~~ ~~~
$ ./00_run_k8s.sh && ./10_setup_kubectl.sh && ./20_setup_k8s_services.sh $ ./00_run_k8s.sh && ./10_setup_kubectl.sh && ./20_setup_k8s_services.sh

View file

@ -0,0 +1,19 @@
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx-controller
namespace: poddemo
spec:
replicas: 2
selector:
role: load-balancer
template:
metadata:
labels:
role: load-balancer
spec:
containers:
- name: nginx
image: coreos/nginx
ports:
- containerPort: 80

View file

@ -0,0 +1,12 @@
#!/bin/bash
PWD=`pwd`
BASEDIR=`readlink -e $(dirname ${0})`
cd ${BASEDIR}
./00_run_k8s.sh && \
./10_setup_kubectl.sh && \
./20_setup_k8s_services.sh
cd ${PWD}

View file

@ -3,7 +3,8 @@
`kubernetes` enables reading zone data from a kubernetes cluster. Record names `kubernetes` enables reading zone data from a kubernetes cluster. Record names
are constructed as "myservice.mynamespace.coredns.local" where: are constructed as "myservice.mynamespace.coredns.local" where:
* "myservice" is the name of the k8s service (this may include multiple DNS labels, such as "c1.myservice"), * "myservice" is the name of the k8s service (this may include multiple DNS labels,
such as "c1.myservice"),
* "mynamespace" is the k8s namespace for the service, and * "mynamespace" is the k8s namespace for the service, and
* "coredns.local" is the zone configured for `kubernetes`. * "coredns.local" is the zone configured for `kubernetes`.
@ -50,7 +51,7 @@ This is the default kubernetes setup, with everything specified in full:
# API documentation: http://kubernetes.io/docs/user-guide/labels/ # API documentation: http://kubernetes.io/docs/user-guide/labels/
# Example selector below only exposes objects tagged as # Example selector below only exposes objects tagged as
# "application=nginx" in the staging or qa environments. # "application=nginx" in the staging or qa environments.
#labels environment in (staging, qa),application=nginx labels environment in (staging, qa),application=nginx
} }
# Perform DNS response caching for the coredns.local zone # Perform DNS response caching for the coredns.local zone
# Cache timeout is provided by the integer in seconds # Cache timeout is provided by the integer in seconds
@ -66,46 +67,20 @@ Defaults:
is required. The label selector syntax is described in the kubernetes API documentation at: is required. The label selector syntax is described in the kubernetes API documentation at:
http://kubernetes.io/docs/user-guide/labels/ http://kubernetes.io/docs/user-guide/labels/
### Template syntax
Record name templates can be constructed using the symbolic elements:
| template symbol | description |
| `{service}` | Kubernetes object/service name. |
| `{namespace}` | The kubernetes namespace. |
| `{type}` | The type of the kubernetes object. Supports values 'svc' and 'pod'. |
| `{zone}` | The zone configured for the kubernetes middleware. |
### Basic Setup ### Basic Setup
#### Launch Kubernetes #### Launch Kubernetes
Kubernetes is launched using the commands in the following `run_k8s.sh` script: Kubernetes is launched using the commands in the `contrib/kubernetes/testscripts/00_run_k8s.sh` script.
~~~
#!/bin/bash
# Based on instructions at: http://kubernetes.io/docs/getting-started-guides/docker/
#K8S_VERSION=$(curl -sS https://storage.googleapis.com/kubernetes-release/release/latest.txt)
K8S_VERSION="v1.2.4"
ARCH="amd64"
export K8S_VERSION
export ARCH
#DNS_ARGUMENTS="--cluster-dns=10.0.0.10 --cluster-domain=cluster.local"
DNS_ARGUMENTS=""
docker run -d \
--volume=/:/rootfs:ro \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:rw \
--volume=/var/lib/kubelet/:/var/lib/kubelet:rw \
--volume=/var/run:/var/run:rw \
--net=host \
--pid=host \
--privileged \
gcr.io/google_containers/hyperkube-${ARCH}:${K8S_VERSION} \
/hyperkube kubelet \
--containerized \
--hostname-override=127.0.0.1 \
--api-servers=http://localhost:8080 \
--config=/etc/kubernetes/manifests \
${DNS_ARGUMENTS} \
--allow-privileged --v=2
~~~
#### Configure kubectl and test #### Configure kubectl and test
@ -115,31 +90,8 @@ The kubernetes control client can be downloaded from the generic URL:
For example, the kubectl client for Linux can be downloaded using the command: For example, the kubectl client for Linux can be downloaded using the command:
`curl -sSL "http://storage.googleapis.com/kubernetes-release/release/v1.2.4/bin/linux/amd64/kubectl"` `curl -sSL "http://storage.googleapis.com/kubernetes-release/release/v1.2.4/bin/linux/amd64/kubectl"`
The following `setup_kubectl.sh` script can be stored in the same directory as The `contrib/kubernetes/testscripts/10_setup_kubectl.sh` script can be stored in the same directory as
kubectl to setup kubectl to setup kubectl to communicate with kubernetes running on the localhost.
kubectl to communicate with kubernetes running on the localhost:
~~~
#!/bin/bash
BASEDIR=`readlink -e $(dirname ${0})`
${BASEDIR}/kubectl config set-cluster test-doc --server=http://localhost:8080
${BASEDIR}/kubectl config set-context test-doc --cluster=test-doc
${BASEDIR}/kubectl config use-context test-doc
alias kubctl="${BASEDIR}/kubectl"
~~~
Verify that kubectl is working by querying for the kubernetes namespaces:
~~~
$ ./kubectl get namespaces
NAME STATUS AGE
default Active 8d
test Active 7d
~~~
#### Launch a kubernetes service and expose the service #### Launch a kubernetes service and expose the service
@ -158,6 +110,10 @@ $ ./kubectl expose deployment mynginx --namespace=demo --port=80
$ ./kubectl get service --namespace=demo $ ./kubectl get service --namespace=demo
~~~ ~~~
The script `contrib/kubernetes/testscripts/20_setup_k8s_services.sh` creates a couple of sample namespaces
with services running in those namespaces. The automated kubernetes integration tests in
`test/kubernetes_test.go` depend on these services and namespaces to exist in kubernetes.
#### Launch CoreDNS #### Launch CoreDNS

View file

@ -56,7 +56,7 @@ func (g *Kubernetes) StartKubeCache() error {
return err return err
} }
if g.LabelSelector == nil { if g.LabelSelector == nil {
log.Printf("[INFO] Kubernetes middleware configured without a label selector. No label-based filtering will be operformed.") log.Printf("[INFO] Kubernetes middleware configured without a label selector. No label-based filtering will be performed.")
} else { } else {
var selector labels.Selector var selector labels.Selector
selector, err = unversionedapi.LabelSelectorAsSelector(g.LabelSelector) selector, err = unversionedapi.LabelSelectorAsSelector(g.LabelSelector)