diff --git a/zookeeper/Dockerfile b/zookeeper/Dockerfile index ed551521aa841000b126656cc42291007426fd86..bc9ef4cc89eb48c442bf0a80f9817a9e2d82c341 100644 --- a/zookeeper/Dockerfile +++ b/zookeeper/Dockerfile @@ -1,84 +1,62 @@ -FROM registry-vpc.cn-hangzhou.aliyuncs.com/schbrain/jdk:8u172-1.0 - -ENV ZOO_CONF_DIR=/conf \ - ZOO_DATA_DIR=/data \ - ZOO_DATA_LOG_DIR=/datalog \ - ZOO_LOG_DIR=/logs \ - ZOO_TICK_TIME=2000 \ - ZOO_INIT_LIMIT=5 \ - ZOO_SYNC_LIMIT=2 \ - ZOO_AUTOPURGE_PURGEINTERVAL=0 \ - ZOO_AUTOPURGE_SNAPRETAINCOUNT=3 \ - ZOO_MAX_CLIENT_CNXNS=60 \ - ZOO_STANDALONE_ENABLED=true \ - ZOO_ADMINSERVER_ENABLED=true - -# Add a user with an explicit UID/GID and create necessary directories -RUN set -eux; \ - groupadd -r zookeeper --gid=1000; \ - useradd -r -g zookeeper --uid=1000 zookeeper; \ - mkdir -p "$ZOO_DATA_LOG_DIR" "$ZOO_DATA_DIR" "$ZOO_CONF_DIR" "$ZOO_LOG_DIR"; \ - chown zookeeper:zookeeper "$ZOO_DATA_LOG_DIR" "$ZOO_DATA_DIR" "$ZOO_CONF_DIR" "$ZOO_LOG_DIR" - -# Install required packges -RUN set -eux; \ - apt-get update; \ - DEBIAN_FRONTEND=noninteractive \ - apt-get install -y --no-install-recommends \ - ca-certificates \ - dirmngr \ - gosu \ - gnupg \ - netcat \ - wget; \ - rm -rf /var/lib/apt/lists/*; \ -# Verify that gosu binary works - gosu nobody true - -ARG GPG_KEY=BBE7232D7991050B54C8EA0ADC08637CA615D22C -ARG SHORT_DISTRO_NAME=zookeeper-3.6.0 -ARG DISTRO_NAME=apache-zookeeper-3.6.0-bin - -# Download Apache Zookeeper, verify its PGP signature, untar and clean up -RUN set -eux; \ - ddist() { \ - local f="$1"; shift; \ - local distFile="$1"; shift; \ - local success=; \ - local distUrl=; \ - for distUrl in \ - 'https://www.apache.org/dyn/closer.cgi?action=download&filename=' \ - https://www-us.apache.org/dist/ \ - https://www.apache.org/dist/ \ - https://archive.apache.org/dist/ \ - ; do \ - if wget -q -O "$f" "$distUrl$distFile" && [ -s "$f" ]; then \ - success=1; \ - break; \ - fi; \ - done; \ - [ -n "$success" ]; \ - }; \ - ddist "$DISTRO_NAME.tar.gz" "zookeeper/$SHORT_DISTRO_NAME/$DISTRO_NAME.tar.gz"; \ - ddist "$DISTRO_NAME.tar.gz.asc" "zookeeper/$SHORT_DISTRO_NAME/$DISTRO_NAME.tar.gz.asc"; \ - export GNUPGHOME="$(mktemp -d)"; \ - #gpg --keyserver ha.pool.sks-keyservers.net --recv-key "$GPG_KEY" || \ - #gpg --keyserver pgp.mit.edu --recv-keys "$GPG_KEY" || \ - #gpg --keyserver keyserver.pgp.com --recv-keys "$GPG_KEY"; \ - #gpg --batch --verify "$DISTRO_NAME.tar.gz.asc" "$DISTRO_NAME.tar.gz"; \ - tar -zxf "$DISTRO_NAME.tar.gz"; \ - mv "$DISTRO_NAME/conf/"* "$ZOO_CONF_DIR"; \ - rm -rf "$GNUPGHOME" "$DISTRO_NAME.tar.gz" "$DISTRO_NAME.tar.gz.asc"; \ - chown -R zookeeper:zookeeper "/$DISTRO_NAME" - -WORKDIR $DISTRO_NAME -VOLUME ["$ZOO_DATA_DIR", "$ZOO_DATA_LOG_DIR", "$ZOO_LOG_DIR"] - -EXPOSE 2181 2888 3888 8080 - -ENV PATH=$PATH:/$DISTRO_NAME/bin \ - ZOOCFGDIR=$ZOO_CONF_DIR - -COPY docker-entrypoint.sh / -ENTRYPOINT ["/docker-entrypoint.sh"] -CMD ["zkServer.sh", "start-foreground"] +FROM ubuntu:16.04 + +ENV ZK_USER=zookeeper \ + ZK_DATA_DIR=/var/lib/zookeeper/data \ + ZK_DATA_LOG_DIR=/var/lib/zookeeper/log \ + ZK_LOG_DIR=/var/log/zookeeper \ + JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 + +ARG GPG_KEY=C823E3E5B12AF29C67F81976F5CECB3CB5E9BD2D +ARG ZK_DIST=apache-zookeeper-3.6.0-bin + +COPY sources.list /etc/apt/sources.list + +ADD $ZK_DIST.tar.gz /opt/ + +RUN set -x \ + && apt-get update \ + #&& apt-get install -y openjdk-8-jre-headless wget netcat-openbsd \ + && apt-get install -y openjdk-8-jre-headless netcat-openbsd \ + #&& wget -q "http://www.apache.org/dist/zookeeper/$ZK_DIST/$ZK_DIST.tar.gz" \ + #&& wget -q "http://www.apache.org/dist/zookeeper/$ZK_DIST/$ZK_DIST.tar.gz.asc" \ + #&& export GNUPGHOME="$(mktemp -d)" \ + #&& gpg --keyserver ha.pool.sks-keyservers.net --recv-key "$GPG_KEY" \ + #&& gpg --batch --verify "$ZK_DIST.tar.gz.asc" "$ZK_DIST.tar.gz" \ + #&& tar -xzf "$ZK_DIST.tar.gz" -C /opt \ + #&& rm -r "$GNUPGHOME" "$ZK_DIST.tar.gz" "$ZK_DIST.tar.gz.asc" \ + && ln -s /opt/$ZK_DIST /opt/zookeeper \ + && rm -rf /opt/zookeeper/CHANGES.txt \ + /opt/zookeeper/README.txt \ + /opt/zookeeper/NOTICE.txt \ + /opt/zookeeper/CHANGES.txt \ + /opt/zookeeper/README_packaging.txt \ + /opt/zookeeper/build.xml \ + /opt/zookeeper/config \ + /opt/zookeeper/contrib \ + /opt/zookeeper/dist-maven \ + /opt/zookeeper/docs \ + /opt/zookeeper/ivy.xml \ + /opt/zookeeper/ivysettings.xml \ + /opt/zookeeper/recipes \ + /opt/zookeeper/src \ + /opt/zookeeper/$ZK_DIST.jar.asc \ + /opt/zookeeper/$ZK_DIST.jar.md5 \ + /opt/zookeeper/$ZK_DIST.jar.sha1 \ + #&& apt-get autoremove -y wget \ + && rm -rf /var/lib/apt/lists/* + +# Copy configuration generator script to bin +COPY zkGenConfig.sh zkOk.sh zkMetrics.sh /opt/zookeeper/bin/ + +# Create a user for the zookeeper process and configure file system ownership +# for necessary directories and symlink the distribution as a user executable +RUN set -x \ + && useradd $ZK_USER \ + && [ `id -u $ZK_USER` -eq 1000 ] \ + && [ `id -g $ZK_USER` -eq 1000 ] \ + && mkdir -p $ZK_DATA_DIR $ZK_DATA_LOG_DIR $ZK_LOG_DIR /usr/share/zookeeper /tmp/zookeeper /usr/etc/ \ + && chown -R "$ZK_USER:$ZK_USER" /opt/$ZK_DIST $ZK_DATA_DIR $ZK_LOG_DIR $ZK_DATA_LOG_DIR /tmp/zookeeper \ + && ln -s /opt/zookeeper/conf/ /usr/etc/zookeeper \ + && ln -s /opt/zookeeper/bin/* /usr/bin \ + && ln -s /opt/zookeeper/$ZK_DIST.jar /usr/share/zookeeper/ \ + && ln -s /opt/zookeeper/lib/* /usr/share/zookeeper diff --git a/zookeeper/LICENSE b/zookeeper/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..96176c0a23301e56420f2718016ee34688b402db --- /dev/null +++ b/zookeeper/LICENSE @@ -0,0 +1,13 @@ +Copyright 2014 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. \ No newline at end of file diff --git a/zookeeper/Makefile b/zookeeper/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..faaced06b3bd69ce5a45bebe7fd2fbb89cc9908d --- /dev/null +++ b/zookeeper/Makefile @@ -0,0 +1,13 @@ +VERSION=v3 +PROJECT_ID=google_samples +PROJECT=gcr.io/${PROJECT_ID} + +all: build + +build: + docker build --pull -t ${PROJECT}/k8szk:${VERSION} . + +push: build + gcloud docker -- push ${PROJECT}/k8szk:${VERSION} + +.PHONY: all build push diff --git a/zookeeper/OWNERS b/zookeeper/OWNERS new file mode 100644 index 0000000000000000000000000000000000000000..c2365845b1b60373bd9fe224f23f08da14adc6da --- /dev/null +++ b/zookeeper/OWNERS @@ -0,0 +1,7 @@ +approvers: +- bprashanth +- enisoc +- erictune +- foxish +- janetkuo +- kow3ns diff --git a/zookeeper/README.md b/zookeeper/README.md new file mode 100644 index 0000000000000000000000000000000000000000..3a10f2219fb5cd5c0ef881945781add0d8a12ad8 --- /dev/null +++ b/zookeeper/README.md @@ -0,0 +1,236 @@ +# Kubernetes ZooKeeper K8SZK +This project contains a Docker image meant to facilitate the deployment of +[Apache ZooKeeper](https://zookeeper.apache.org/) on [Kubernetes](http://kubernetes.io/) using +[StatefulSets](https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/). +## Limitations +1. Scaling is not currently supported. An ensemble's membership can not be updated in a safe way +in ZooKeeper 3.4.10 (The current stable release). +2. Observers are currently not supported. Contributions are welcome. +3. Persistent Volumes must be used. emptyDirs will likely result in a loss of data. + +## Docker Image +The docker image contained in this repository is comprised of a base Ubuntu 16.04 image using the latest +release of the OpenJDK JRE based on the 1.8 JVM (JDK 8u111) and the latest stable release of +ZooKeeper, 3.4.10. Ubuntu is a much larger image than BusyBox or Alpine, but these images contain +mucl or ulibc. This requires a custom version of OpenJDK to be built against a libc runtime other +than glibc. No vendor of the ZooKeeper software supplies or verifies the software against such a +JVM, and, while Alpine or BusyBox would provide smaller images, we have prioritized a well known +environment. + +The image is built such that the ZooKeeper process is designated to run as a non-root user. By default, +this user is zookeeper. The ZooKeeper package is installed into the /opt/zookeeper directory, all +configuration is sym linked into the /usr/etc/zookeeper/, and all executables are sym linked into +/usr/bin. The ZooKeeper data directories are contained in /var/lib/zookeeper. This is identical to +the RPM distribution that users should be familiar with. + +## Configuration + +### Headless Service +The ZooKeeper Stateful Set requires a Headless Service to control the network domain for the +ZooKeeper processes. An example configuration is provided below. + +```yaml +apiVersion: v1 +kind: Service +metadata: + name: zk-svc + labels: + app: zk-svc +spec: + ports: + - port: 2888 + name: server + - port: 3888 + name: leader-election + clusterIP: None + selector: + app: zk-svc +``` +Note that the Service contains two ports. The server port is used for followers to tail the leaders +even log, and the leader-election port is used by the ensemble to perform leader election. +### Stateful Set +The Stateful Set configuration must match the Headless Service, and it must provide the number of +replicas. In the example below we request a ZooKeeper ensemble of size 3. +**As weighted quorums are not supported, it is imperative that an odd number of replicas be chosen. +Moreover, the number of replicas should be either 1, 3, 5, or 7. Ensembles may be scaled to larger +membership for read fan out, but, as this will adversely impact write performance, careful thought +should be given to selecting a larger value.** +```yaml +apiVersion: apps/v1beta1 +kind: StatefulSet +metadata: + name: zk +spec: + serviceName: zk-svc + replicas: 3 +``` +### Container Configuration +The zkGenConfig.sh script will generate the ZooKeeper configuration (zoo.cfg), Log4J configuration +(log4j.properties), and JVM configuration (jvm.env). These will be written to the +/opt/zookeeper/conf directory with correct read permissions for the zookeeper user. These files are +generated from environment variables that are injected into the container as in the example, minimal +configuration below. +```yaml +containers: + - name: k8szk + imagePullPolicy: Always + image: gcr.io/google_samples/k8szk:v3 + ports: + - containerPort: 2181 + name: client + - containerPort: 2888 + name: server + - containerPort: 3888 + name: leader-election + env: + - name : ZK_ENSEMBLE + value: "zk-0;zk-1;zk-2" + - name: ZK_CLIENT_PORT + value: "2181" + - name: ZK_SERVER_PORT + value: "2888" + - name: ZK_ELECTION_PORT + value: "3888" +``` +#### Membership Configuration +|Variable|Type|Default|Description| +|:------:|:---:|:-----:|:---------| +|ZK_ENSEMBLE|string|N/A|A colon separated list of servers in the ensemble.| +This is a mandatory configuration variable that is used to configure the membership of the +ZooKeeper ensemble. It is also used to prevent data loss during accidental scale operations. The +set can be computed as follows. For all integers in the range [0,replicas), prepend the name of +service followed by a dash to the integer. So for the Stateful Set above, the name is zk and we have +3 replicas. for the set {0,1,2} we prepend zk- giving us zk-0;zk-1;zk-2. + +#### Network Configuration +|Variable|Type|Default|Description| +|:------:|:---:|:-----:|:--------| +|ZK_CLIENT_PORT|integer|2181|The port on which the server will accept client requests.| +|ZK_SERVER_PORT|integer|2888|The port on which the leader will send events to followers.| +|ZK_ELECTION_PORT|integer|3888|The port on which the ensemble performs leader election.| +|ZK_MAX_CLIENT_CNXNS|integer|60|The maximum number of concurrent client connections that a server in the ensemble will accept.| + +The ZK_CLIENT_PORT, ZK_ELECTION_PORT, and ZK_SERVERS_PORT must be set to the containerPorts +specified in the container configuration, and the ZK_SERVER_PORT and ZK_ELECTION_PORT +must match the Headless Service configuration. However, if the default values of +the environment variables are used for both the containerPorts and the Headless Service, the +environment variables may be omitted from the configuration. + +#### ZooKeeper Time Configuration +|Variable|Type|Default|Description| +|:------:|:---:|:-----:|:--------| +|ZK_TICK_TIME|integer|2000|The number of wall clock ms that corresponds to a Tick for the ensembles internal time.| +|ZK_INIT_LIMIT|integer|5|The number of Ticks that an ensemble member is allowed to perform leader election.| +|ZK_SYNC_LIMIT|integer|10|The number of Tick by which a follower may lag behind the ensembles leader.| + +#### ZooKeeper Session Configuration +|Variable|Type|Default|Description| +|:------:|:---:|:-----:|:--------| +|ZK_MIN_SESSION_TIMEOUT|integer|2 * ZK_TICK_TIME|The minimum session timeout that the ensemble will allow a client to request.| +|ZK_MAX_SESSION_TIMEOUT|integer|20 * ZK_TICK_TIME|The maximum session timeout that the ensemble will allow a client to request.| + +#### Data Retention Configuration +**ZooKeeper does not, by default, purge old transactions logs or snapshots. This can cause +the disk to become full.** If you have backup procedures and retention policies that rely on +external systems, the snapshots can be retrieved manually from the /var/lib/zookeeper/data directory, +and the logs can be retrieved manually from the /var/lib/zookeeper/log directory. +These will be stored on the persistent volume. The zkCleanup.sh script can be used to manually purge +outdated logs and snapshots. + +If you do not have an existing retention policy and backup procedure, and if you are comfortable with +an automatic procedure, you can use the environment variables below to enable and configure +automatic data purge policies. + +|Variable|Type|Default|Description| +|:------:|:---:|:-----:|:---------| +|ZK_SNAP_RETAIN_COUNT|integer|3|The number of snapshots that the ZooKeeper process will retain if ZK_PURGE_INTERVAL is set to a value greater than 0.| +|ZK_PURGE_INTERVAL|integer|0|The delay, in hours, between ZooKeeper log and snapshot cleanups.| + +#### JVM Configuration +Currently the only supported JVM configuration is the JVM heap size. Be sure that the heap size you +request does not cause the process to swap out. + +|Variable|Type|Default|Description| +|:------:|:---:|:-----:|:--------| +|ZK_HEAP_SIZE|integer|2|The JVM heap size in Gibibytes.| + +#### Log Level Configuration +|Variable|Type|Default|Description| +|:------:|:---:|:-----:|:--------| +|ZK_LOG_LEVEL|enum(TRACE,DEBUG,INFO,WARN,ERROR,FATAL)|INFO|The Log Level that for the ZooKeeper processes logger.| + +#### Liveness and Readiness +The zkOk.sh script can be used to check the liveness and readiness of ZooKeeper process. The example +below demonstrates how to configure liveness and readiness probes for the Pods in the Stateful Set. +```yaml + readinessProbe: + exec: + command: + - sh + - -c + - "zkOk.sh" + initialDelaySeconds: 15 + timeoutSeconds: 5 + livenessProbe: + exec: + command: + - sh + - -c + - "zkOk.sh" + initialDelaySeconds: 15 + timeoutSeconds: 5 +``` +#### Volume Mounts +volumeMounts for the container should be defined as below. +```yaml + volumeMounts: + - name: datadir + mountPath: /var/lib/zookeeper +``` +### Storage Configuration +Currently, the use of Persistent Volumes to provide durable, network attached storage is mandatory. +**If you use the provided image with emptyDirs, you will likely suffer a data loss.** The example +below demonstrates how to request a dynamically provisioned persistent volume of 20 GiB. +```yaml + volumeClaimTemplates: + - metadata: + name: datadir + annotations: + volume.alpha.kubernetes.io/storage-class: anything + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: 20Gi +``` + +### Logging +The Log Level configuration may be modified via the ZK_LOG_LEVEL environment variable as described +above. However, the location of the log output is not modifiable. The ZooKeeper process must be +run in the foreground, and the log information will be shipped to the stdout. This is considered +to be a best practice for containerized applications, and it allows users to make use of the +log rotation and retention infrastructure that already exists for K8s. + +### Metrics +The zkMetrics script can be used to retrieve metrics from the ZooKeeper process and print them to +stdout. A recurring Kubernetes job can be used to collect these metrics and provide them to a +collector. +```bash +bash$ kubectl exec zk-0 zkMetrics.sh +zk_version 3.4.9-1757313, built on 08/23/2016 06:50 GMT +zk_avg_latency 0 +zk_max_latency 0 +zk_min_latency 0 +zk_packets_received 21 +zk_packets_sent 20 +zk_num_alive_connections 1 +zk_outstanding_requests 0 +zk_server_state follower +zk_znode_count 4 +zk_watch_count 0 +zk_ephemerals_count 0 +zk_approximate_data_size 27 +zk_open_file_descriptor_count 39 +zk_max_file_descriptor_count 1048576 + +``` diff --git a/zookeeper/apache-zookeeper-3.6.0-bin.tar.gz b/zookeeper/apache-zookeeper-3.6.0-bin.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..50d43af6d86b4529420037539b26fa89a12d713b Binary files /dev/null and b/zookeeper/apache-zookeeper-3.6.0-bin.tar.gz differ diff --git a/zookeeper/docker-entrypoint.sh b/zookeeper/docker-entrypoint.sh deleted file mode 100755 index 70bedbfa4c856bd8f6e05483fb816279ac3f7ded..0000000000000000000000000000000000000000 --- a/zookeeper/docker-entrypoint.sh +++ /dev/null @@ -1,47 +0,0 @@ -#!/bin/bash - -set -e - -# Allow the container to be started with `--user` -if [[ "$1" = 'zkServer.sh' && "$(id -u)" = '0' ]]; then - chown -R zookeeper "$ZOO_DATA_DIR" "$ZOO_DATA_LOG_DIR" "$ZOO_LOG_DIR" "$ZOO_CONF_DIR" - exec gosu zookeeper "$0" "$@" -fi - -# Generate the config only if it doesn't exist -if [[ ! -f "$ZOO_CONF_DIR/zoo.cfg" ]]; then - CONFIG="$ZOO_CONF_DIR/zoo.cfg" - { - echo "dataDir=$ZOO_DATA_DIR" - echo "dataLogDir=$ZOO_DATA_LOG_DIR" - - echo "tickTime=$ZOO_TICK_TIME" - echo "initLimit=$ZOO_INIT_LIMIT" - echo "syncLimit=$ZOO_SYNC_LIMIT" - - echo "autopurge.snapRetainCount=$ZOO_AUTOPURGE_SNAPRETAINCOUNT" - echo "autopurge.purgeInterval=$ZOO_AUTOPURGE_PURGEINTERVAL" - echo "maxClientCnxns=$ZOO_MAX_CLIENT_CNXNS" - echo "standaloneEnabled=$ZOO_STANDALONE_ENABLED" - echo "admin.enableServer=$ZOO_ADMINSERVER_ENABLED" - } >> "$CONFIG" - if [[ -z $ZOO_SERVERS ]]; then - ZOO_SERVERS="server.1=localhost:2888:3888;2181" - fi - - for server in $ZOO_SERVERS; do - echo "$server" >> "$CONFIG" - done - - if [[ -n $ZOO_4LW_COMMANDS_WHITELIST ]]; then - echo "4lw.commands.whitelist=$ZOO_4LW_COMMANDS_WHITELIST" >> "$CONFIG" - fi - -fi - -# Write myid only if it doesn't exist -if [[ ! -f "$ZOO_DATA_DIR/myid" ]]; then - echo "${ZOO_MY_ID:-1}" > "$ZOO_DATA_DIR/myid" -fi - -exec "$@" diff --git a/zookeeper/sources.list b/zookeeper/sources.list new file mode 100644 index 0000000000000000000000000000000000000000..82bd9625c805eae98764c57e76f03a3bb323a390 --- /dev/null +++ b/zookeeper/sources.list @@ -0,0 +1,19 @@ +# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ bionic main restricted +deb-src http://archive.ubuntu.com/ubuntu bionic main restricted #Added by software-properties +deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted +deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted multiverse universe #Added by software-properties +deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted +deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted multiverse universe #Added by software-properties +deb http://mirrors.aliyun.com/ubuntu/ bionic universe +deb http://mirrors.aliyun.com/ubuntu/ bionic-updates universe +deb http://mirrors.aliyun.com/ubuntu/ bionic multiverse +deb http://mirrors.aliyun.com/ubuntu/ bionic-updates multiverse +deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse +deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse #Added by software-properties +deb http://archive.canonical.com/ubuntu bionic partner +deb-src http://archive.canonical.com/ubuntu bionic partner +deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted +deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted multiverse universe #Added by software-properties +deb http://mirrors.aliyun.com/ubuntu/ bionic-security universe +deb http://mirrors.aliyun.com/ubuntu/ bionic-security multiverse + diff --git a/zookeeper/zkGenConfig.sh b/zookeeper/zkGenConfig.sh new file mode 100755 index 0000000000000000000000000000000000000000..8ea53e48b957c8331c120035a19fdfd60ebfc9a1 --- /dev/null +++ b/zookeeper/zkGenConfig.sh @@ -0,0 +1,159 @@ +#!/usr/bin/env bash +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +ZK_USER=${ZK_USER:-"zookeeper"} +ZK_LOG_LEVEL=${ZK_LOG_LEVEL:-"INFO"} +ZK_DATA_DIR=${ZK_DATA_DIR:-"/var/lib/zookeeper/data"} +ZK_DATA_LOG_DIR=${ZK_DATA_LOG_DIR:-"/var/lib/zookeeper/log"} +ZK_LOG_DIR=${ZK_LOG_DIR:-"var/log/zookeeper"} +ZK_CONF_DIR=${ZK_CONF_DIR:-"/opt/zookeeper/conf"} +ZK_CLIENT_PORT=${ZK_CLIENT_PORT:-2181} +ZK_SERVER_PORT=${ZK_SERVER_PORT:-2888} +ZK_ELECTION_PORT=${ZK_ELECTION_PORT:-3888} +ZK_TICK_TIME=${ZK_TICK_TIME:-2000} +ZK_INIT_LIMIT=${ZK_INIT_LIMIT:-10} +ZK_SYNC_LIMIT=${ZK_SYNC_LIMIT:-5} +ZK_HEAP_SIZE=${ZK_HEAP_SIZE:-2G} +ZK_JVM_OPTS=${ZK_JVM_OPTS:-} +ZK_MAX_CLIENT_CNXNS=${ZK_MAX_CLIENT_CNXNS:-60} +ZK_MIN_SESSION_TIMEOUT=${ZK_MIN_SESSION_TIMEOUT:- $((ZK_TICK_TIME*2))} +ZK_MAX_SESSION_TIMEOUT=${ZK_MAX_SESSION_TIMEOUT:- $((ZK_TICK_TIME*20))} +ZK_SNAP_RETAIN_COUNT=${ZK_SNAP_RETAIN_COUNT:-3} +ZK_PURGE_INTERVAL=${ZK_PURGE_INTERVAL:-0} +ID_FILE="$ZK_DATA_DIR/myid" +ZK_CONFIG_FILE="$ZK_CONF_DIR/zoo.cfg" +LOGGER_PROPS_FILE="$ZK_CONF_DIR/log4j.properties" +JAVA_ENV_FILE="$ZK_CONF_DIR/java.env" +HOST=`hostname -s` +DOMAIN=`hostname -d` + +function print_servers() { + for (( i=1; i<=$ZK_REPLICAS; i++ )) + do + echo "server.$i=$NAME-$((i-1)).$DOMAIN:$ZK_SERVER_PORT:$ZK_ELECTION_PORT" + done +} + +function validate_env() { + echo "Validating environment" + + if [ -z $ZK_REPLICAS ]; then + echo "ZK_REPLICAS is a mandatory environment variable" + exit 1 + fi + + if [[ $HOST =~ (.*)-([0-9]+)$ ]]; then + NAME=${BASH_REMATCH[1]} + ORD=${BASH_REMATCH[2]} + else + echo "Failed to extract ordinal from hostname $HOST" + exit 1 + fi + + MY_ID=$((ORD+1)) + echo "ZK_REPLICAS=$ZK_REPLICAS" + echo "MY_ID=$MY_ID" + echo "ZK_LOG_LEVEL=$ZK_LOG_LEVEL" + echo "ZK_DATA_DIR=$ZK_DATA_DIR" + echo "ZK_DATA_LOG_DIR=$ZK_DATA_LOG_DIR" + echo "ZK_LOG_DIR=$ZK_LOG_DIR" + echo "ZK_CLIENT_PORT=$ZK_CLIENT_PORT" + echo "ZK_SERVER_PORT=$ZK_SERVER_PORT" + echo "ZK_ELECTION_PORT=$ZK_ELECTION_PORT" + echo "ZK_TICK_TIME=$ZK_TICK_TIME" + echo "ZK_INIT_LIMIT=$ZK_INIT_LIMIT" + echo "ZK_SYNC_LIMIT=$ZK_SYNC_LIMIT" + echo "ZK_MAX_CLIENT_CNXNS=$ZK_MAX_CLIENT_CNXNS" + echo "ZK_MIN_SESSION_TIMEOUT=$ZK_MIN_SESSION_TIMEOUT" + echo "ZK_MAX_SESSION_TIMEOUT=$ZK_MAX_SESSION_TIMEOUT" + echo "ZK_HEAP_SIZE=$ZK_HEAP_SIZE" + echo "ZK_SNAP_RETAIN_COUNT=$ZK_SNAP_RETAIN_COUNT" + echo "ZK_PURGE_INTERVAL=$ZK_PURGE_INTERVAL" + echo "ZK_JVM_OPTS=$ZK_JVM_OPTS" + echo "ENSEMBLE" + print_servers + echo "Environment validation successful" +} + +function create_config() { + rm -f $ZK_CONFIG_FILE + echo "Creating ZooKeeper configuration" + echo "#This file was autogenerated by k8szk DO NOT EDIT" >> $ZK_CONFIG_FILE + echo "clientPort=$ZK_CLIENT_PORT" >> $ZK_CONFIG_FILE + echo "dataDir=$ZK_DATA_DIR" >> $ZK_CONFIG_FILE + echo "dataLogDir=$ZK_DATA_LOG_DIR" >> $ZK_CONFIG_FILE + echo "tickTime=$ZK_TICK_TIME" >> $ZK_CONFIG_FILE + echo "initLimit=$ZK_INIT_LIMIT" >> $ZK_CONFIG_FILE + echo "syncLimit=$ZK_SYNC_LIMIT" >> $ZK_CONFIG_FILE + echo "maxClientCnxns=$ZK_MAX_CLIENT_CNXNS" >> $ZK_CONFIG_FILE + echo "minSessionTimeout=$ZK_MIN_SESSION_TIMEOUT" >> $ZK_CONFIG_FILE + echo "maxSessionTimeout=$ZK_MAX_SESSION_TIMEOUT" >> $ZK_CONFIG_FILE + echo "autopurge.snapRetainCount=$ZK_SNAP_RETAIN_COUNT" >> $ZK_CONFIG_FILE + echo "autopurge.purgeInterval=$ZK_PURGE_INTERVAL" >> $ZK_CONFIG_FILE + + if [ $ZK_REPLICAS -gt 1 ]; then + print_servers >> $ZK_CONFIG_FILE + fi + + echo "Wrote ZooKeeper configuration file to $ZK_CONFIG_FILE" +} + +function create_data_dirs() { + echo "Creating ZooKeeper data directories and setting permissions" + + if [ ! -d $ZK_DATA_DIR ]; then + mkdir -p $ZK_DATA_DIR + chown -R $ZK_USER:$ZK_USER $ZK_DATA_DIR + fi + + if [ ! -d $ZK_DATA_LOG_DIR ]; then + mkdir -p $ZK_DATA_LOG_DIR + chown -R $ZK_USER:$ZK_USER $ZK_DATA_LOG_DIR + fi + + if [ ! -d $ZK_LOG_DIR ]; then + mkdir -p $ZK_LOG_DIR + chown -R $ZK_USER:$ZK_USER $ZK_LOG_DIR + fi + + if [ ! -f $ID_FILE ]; then + echo $MY_ID >> $ID_FILE + fi + + echo "Created ZooKeeper data directories and set permissions in $ZK_DATA_DIR" +} + +function create_log_props () { + rm -f $LOGGER_PROPS_FILE + echo "Creating ZooKeeper log4j configuration" + echo "zookeeper.root.logger=CONSOLE" >> $LOGGER_PROPS_FILE + echo "zookeeper.console.threshold="$ZK_LOG_LEVEL >> $LOGGER_PROPS_FILE + echo "log4j.rootLogger=\${zookeeper.root.logger}" >> $LOGGER_PROPS_FILE + echo "log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender" >> $LOGGER_PROPS_FILE + echo "log4j.appender.CONSOLE.Threshold=\${zookeeper.console.threshold}" >> $LOGGER_PROPS_FILE + echo "log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout" >> $LOGGER_PROPS_FILE + echo "log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n" >> $LOGGER_PROPS_FILE + echo "Wrote log4j configuration to $LOGGER_PROPS_FILE" +} + +function create_java_env() { + rm -f $JAVA_ENV_FILE + echo "Creating JVM configuration file" + echo "ZOO_LOG_DIR=$ZK_LOG_DIR" >> $JAVA_ENV_FILE + echo "JVMFLAGS=\"-Xmx$ZK_HEAP_SIZE -Xms$ZK_HEAP_SIZE $ZK_JVM_OPTS\"" >> $JAVA_ENV_FILE + echo "Wrote JVM configuration to $JAVA_ENV_FILE" +} + +validate_env && create_config && create_log_props && create_data_dirs && create_java_env diff --git a/zookeeper/zkMetrics.sh b/zookeeper/zkMetrics.sh new file mode 100755 index 0000000000000000000000000000000000000000..2f21459ff0b7b14fe1bb12a1f0c0ca729c57bae9 --- /dev/null +++ b/zookeeper/zkMetrics.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +ZK_CLIENT_PORT=${ZK_CLIENT_PORT:-2181} +echo mntr | nc localhost $ZK_CLIENT_PORT >& 1 diff --git a/zookeeper/zkOk.sh b/zookeeper/zkOk.sh new file mode 100755 index 0000000000000000000000000000000000000000..dbe417682fcf44ab866d4d5a30991a9b5cdef29d --- /dev/null +++ b/zookeeper/zkOk.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Copyright 2016 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# zkOk.sh uses the ruok ZooKeeper four letter work to determine if the instance +# is health. The $? variable will be set to 0 if server responds that it is +# healthy, or 1 if the server fails to respond. + +ZK_CLIENT_PORT=${ZK_CLIENT_PORT:-2181} +OK=$(echo ruok | nc 127.0.0.1 $ZK_CLIENT_PORT) +if [ "$OK" == "imok" ]; then + exit 0 +else + exit 1 +fi \ No newline at end of file diff --git a/zookeeper/zookeeper.yaml b/zookeeper/zookeeper.yaml new file mode 100644 index 0000000000000000000000000000000000000000..ea870ce45ed50a7f77d6dae3555bd2ff33638fb5 --- /dev/null +++ b/zookeeper/zookeeper.yaml @@ -0,0 +1,151 @@ +--- +apiVersion: v1 +kind: Service +metadata: + name: zk-svc + labels: + app: zk-svc +spec: + ports: + - port: 2888 + name: server + - port: 3888 + name: leader-election + clusterIP: None + selector: + app: zk +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: zk-cm +data: + jvm.heap: "1G" + tick: "2000" + init: "10" + sync: "5" + client.cnxns: "60" + snap.retain: "3" + purge.interval: "0" +--- +apiVersion: policy/v1beta1 +kind: PodDisruptionBudget +metadata: + name: zk-pdb +spec: + selector: + matchLabels: + app: zk + minAvailable: 2 +--- +apiVersion: apps/v1beta1 +kind: StatefulSet +metadata: + name: zk +spec: + serviceName: zk-svc + replicas: 3 + template: + metadata: + labels: + app: zk + spec: + affinity: + podAntiAffinity: + requiredDuringSchedulingIgnoredDuringExecution: + - labelSelector: + matchExpressions: + - key: "app" + operator: In + values: + - zk + topologyKey: "kubernetes.io/hostname" + containers: + - name: k8szk + imagePullPolicy: Always + image: gcr.io/google_samples/k8szk:v3 + resources: + requests: + memory: "2Gi" + cpu: "500m" + ports: + - containerPort: 2181 + name: client + - containerPort: 2888 + name: server + - containerPort: 3888 + name: leader-election + env: + - name : ZK_REPLICAS + value: "3" + - name : ZK_HEAP_SIZE + valueFrom: + configMapKeyRef: + name: zk-cm + key: jvm.heap + - name : ZK_TICK_TIME + valueFrom: + configMapKeyRef: + name: zk-cm + key: tick + - name : ZK_INIT_LIMIT + valueFrom: + configMapKeyRef: + name: zk-cm + key: init + - name : ZK_SYNC_LIMIT + valueFrom: + configMapKeyRef: + name: zk-cm + key: tick + - name : ZK_MAX_CLIENT_CNXNS + valueFrom: + configMapKeyRef: + name: zk-cm + key: client.cnxns + - name: ZK_SNAP_RETAIN_COUNT + valueFrom: + configMapKeyRef: + name: zk-cm + key: snap.retain + - name: ZK_PURGE_INTERVAL + valueFrom: + configMapKeyRef: + name: zk-cm + key: purge.interval + - name: ZK_CLIENT_PORT + value: "2181" + - name: ZK_SERVER_PORT + value: "2888" + - name: ZK_ELECTION_PORT + value: "3888" + command: + - sh + - -c + - zkGenConfig.sh && zkServer.sh start-foreground + readinessProbe: + exec: + command: + - "zkOk.sh" + initialDelaySeconds: 10 + timeoutSeconds: 5 + livenessProbe: + exec: + command: + - "zkOk.sh" + initialDelaySeconds: 10 + timeoutSeconds: 5 + volumeMounts: + - name: datadir + mountPath: /var/lib/zookeeper + securityContext: + runAsUser: 1000 + fsGroup: 1000 + volumeClaimTemplates: + - metadata: + name: datadir + spec: + accessModes: [ "ReadWriteOnce" ] + resources: + requests: + storage: 10Gi