From 44ea045b4dbb92f25927803f4c59dc40c166aca8 Mon Sep 17 00:00:00 2001 From: aneez004 Date: Sun, 4 Sep 2022 16:35:27 +0530 Subject: [PATCH 1/9] Update postgres.yaml --- kustomize/postgres/postgres.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kustomize/postgres/postgres.yaml b/kustomize/postgres/postgres.yaml index e89bb6b6..de8ce2f0 100644 --- a/kustomize/postgres/postgres.yaml +++ b/kustomize/postgres/postgres.yaml @@ -1,7 +1,7 @@ apiVersion: postgres-operator.crunchydata.com/v1beta1 kind: PostgresCluster metadata: - name: hippo + name: postgres-moodle spec: image: registry.developers.crunchydata.com/crunchydata/crunchy-postgres:ubi8-14.5-0 postgresVersion: 14 From 711c67f646672f7d5a189d1259a61ae07139b0bc Mon Sep 17 00:00:00 2001 From: Anees Aboobacker Date: Mon, 5 Sep 2022 16:31:38 +0530 Subject: [PATCH 2/9] changes for lab7 --- client-setup.sh | 86 ++++++++++++ kustomize/postgres/kustomization.yaml | 2 +- kustomize/postgres/postgres.yaml | 1 + moodle/deployment.yaml | 181 ++++++++++++++++++++++++++ moodle/docker-compose.yml | 37 ++++++ moodle/moodel-mariadb-configmap.yaml | 47 +++++++ moodle/moodle-deploy.yaml | 65 +++++++++ 7 files changed, 418 insertions(+), 1 deletion(-) create mode 100644 client-setup.sh create mode 100644 moodle/deployment.yaml create mode 100644 moodle/docker-compose.yml create mode 100644 moodle/moodel-mariadb-configmap.yaml create mode 100644 moodle/moodle-deploy.yaml diff --git a/client-setup.sh b/client-setup.sh new file mode 100644 index 00000000..5801cf79 --- /dev/null +++ b/client-setup.sh @@ -0,0 +1,86 @@ +#!/bin/bash + +# Copyright 2020 - 2022 Crunchy Data Solutions, Inc. +# 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. + +# This script should be run after the operator has been deployed +PGO_OPERATOR_NAMESPACE="${PGO_OPERATOR_NAMESPACE:-postgres-operator}" +PGO_USER_ADMIN="${PGO_USER_ADMIN:-pgouser-admin}" +PGO_CLIENT_VERSION="${PGO_CLIENT_VERSION:-v4.7.7}" +PGO_CLIENT_URL="https://github.com/CrunchyData/postgres-operator/releases/download/${PGO_CLIENT_VERSION}" + +PGO_CMD="${PGO_CMD-kubectl}" + +# Checks operating system and determines which binary to download +UNAME_RESULT=$(uname) +if [[ "${UNAME_RESULT}" == "Linux" ]] +then + BIN_NAME="pgo" +elif [[ "${UNAME_RESULT}" == "Darwin" ]] +then + BIN_NAME="pgo-mac" +else + echo "${UNAME_RESULT} is not supported, valid operating systems are: Linux, Darwin" + echo "Exiting..." + exit 1 +fi + +# Creates the output directory for files +OUTPUT_DIR="${HOME}/.pgo/${PGO_OPERATOR_NAMESPACE}" +install -d -m a-rwx,u+rwx "${OUTPUT_DIR}" + +if [ -f "${OUTPUT_DIR}/pgo" ] +then + echo "pgo Client Binary detected at: ${OUTPUT_DIR}" + echo "Updating Binary..." +fi + +echo "Operating System found is ${UNAME_RESULT}..." +echo "Downloading ${BIN_NAME} version: ${PGO_CLIENT_VERSION}..." +curl -Lo "${OUTPUT_DIR}/pgo" "${PGO_CLIENT_URL}/${BIN_NAME}" +chmod +x "${OUTPUT_DIR}/pgo" + + +# Check that the pgouser-admin secret exists +if [ -z "$($PGO_CMD get secret -n ${PGO_OPERATOR_NAMESPACE} ${PGO_USER_ADMIN})" ] +then + echo "${PGO_USER_ADMIN} Secret not found in namespace: ${PGO_OPERATOR_NAMESPACE}" + echo "Please ensure that the PostgreSQL Operator has been installed." + echo "Exiting..." + exit 1 +fi + +# Check that the pgo.tls secret exists +if [ -z "$($PGO_CMD get secret -n ${PGO_OPERATOR_NAMESPACE} pgo.tls)" ] +then + echo "pgo.tls Secret not found in namespace: ${PGO_OPERATOR_NAMESPACE}" + echo "Please ensure that the PostgreSQL Operator has been installed." + echo "Exiting..." + exit 1 +fi + +# Restrict access to the target file before writing +kubectl_get_private() { touch "$1" && chmod a-rwx,u+rw "$1" && $PGO_CMD get > "$1" "${@:2}"; } + +# Use the pgouser-admin secret to generate pgouser file +kubectl_get_private "${OUTPUT_DIR}/pgouser" secret -n "${PGO_OPERATOR_NAMESPACE}" "${PGO_USER_ADMIN}" \ + -o 'go-template={{ .data.username | base64decode }}:{{ .data.password | base64decode }}' + +# Use the pgo.tls secret to generate the client cert files +kubectl_get_private "${OUTPUT_DIR}/client.crt" secret -n "${PGO_OPERATOR_NAMESPACE}" pgo.tls -o 'go-template={{ index .data "tls.crt" | base64decode }}' +kubectl_get_private "${OUTPUT_DIR}/client.key" secret -n "${PGO_OPERATOR_NAMESPACE}" pgo.tls -o 'go-template={{ index .data "tls.key" | base64decode }}' + +echo "pgo client files have been generated, please add the following to your bashrc" +echo "export PATH=${OUTPUT_DIR}:\$PATH" +echo "export PGOUSER=${OUTPUT_DIR}/pgouser" +echo "export PGO_CA_CERT=${OUTPUT_DIR}/client.crt" +echo "export PGO_CLIENT_CERT=${OUTPUT_DIR}/client.crt" +echo "export PGO_CLIENT_KEY=${OUTPUT_DIR}/client.key" diff --git a/kustomize/postgres/kustomization.yaml b/kustomize/postgres/kustomization.yaml index 249b4106..e1ce9015 100644 --- a/kustomize/postgres/kustomization.yaml +++ b/kustomize/postgres/kustomization.yaml @@ -1,4 +1,4 @@ -namespace: postgres-operator +namespace: moodle resources: - postgres.yaml diff --git a/kustomize/postgres/postgres.yaml b/kustomize/postgres/postgres.yaml index de8ce2f0..24a2fc32 100644 --- a/kustomize/postgres/postgres.yaml +++ b/kustomize/postgres/postgres.yaml @@ -7,6 +7,7 @@ spec: postgresVersion: 14 instances: - name: instance1 + replicas: 2 dataVolumeClaimSpec: accessModes: - "ReadWriteOnce" diff --git a/moodle/deployment.yaml b/moodle/deployment.yaml new file mode 100644 index 00000000..ec99d153 --- /dev/null +++ b/moodle/deployment.yaml @@ -0,0 +1,181 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + annotations: + autopilot.gke.io/resource-adjustment: '{"input":{"containers":[{"requests":{"cpu":"300m","memory":"512Mi"},"name":"moodle"}]},"output":{"containers":[{"limits":{"cpu":"500m","ephemeral-storage":"1Gi","memory":"512Mi"},"requests":{"cpu":"500m","ephemeral-storage":"1Gi","memory":"512Mi"},"name":"moodle"}]},"modified":true}' + deployment.kubernetes.io/revision: "1" + meta.helm.sh/release-name: moodle + meta.helm.sh/release-namespace: default + creationTimestamp: "2022-09-04T11:48:49Z" + generation: 1 + labels: + app.kubernetes.io/instance: moodle + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: moodle + helm.sh/chart: moodle-14.2.1 + name: moodle + namespace: default + resourceVersion: "1408408" + uid: bb3a2b5f-96c6-41e9-a1d4-668ce2522621 +spec: + progressDeadlineSeconds: 600 + replicas: 1 + revisionHistoryLimit: 10 + selector: + matchLabels: + app.kubernetes.io/instance: moodle + app.kubernetes.io/name: moodle + strategy: + rollingUpdate: + maxSurge: 25% + maxUnavailable: 25% + type: RollingUpdate + template: + metadata: + annotations: + prometheus.io/port: "9117" + prometheus.io/scrape: "true" + creationTimestamp: null + labels: + app.kubernetes.io/instance: moodle + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: moodle + helm.sh/chart: moodle-14.2.1 + spec: + affinity: + podAntiAffinity: + preferredDuringSchedulingIgnoredDuringExecution: + - podAffinityTerm: + labelSelector: + matchLabels: + app.kubernetes.io/instance: moodle + app.kubernetes.io/name: moodle + namespaces: + - default + topologyKey: kubernetes.io/hostname + weight: 1 + containers: + - env: + - name: BITNAMI_DEBUG + value: "false" + - name: ALLOW_EMPTY_PASSWORD + value: "yes" + - name: APACHE_HTTP_PORT_NUMBER + value: "8080" + - name: APACHE_HTTPS_PORT_NUMBER + value: "8443" + - name: MOODLE_DATABASE_TYPE + value: mariadb + - name: MOODLE_DATABASE_HOST + value: moodle-mariadb + - name: MOODLE_DATABASE_PORT_NUMBER + value: "3306" + - name: MOODLE_DATABASE_NAME + value: bitnami_moodle + - name: MOODLE_DATABASE_USER + value: bn_moodle + - name: MOODLE_DATABASE_PASSWORD + valueFrom: + secretKeyRef: + key: mariadb-password + name: moodle-mariadb + - name: MOODLE_SKIP_BOOTSTRAP + value: "no" + - name: MOODLE_USERNAME + value: user + - name: MOODLE_PASSWORD + valueFrom: + secretKeyRef: + key: moodle-password + name: moodle + - name: MOODLE_EMAIL + value: user@example.com + image: docker.io/bitnami/moodle:4.0.3-debian-11-r0 + imagePullPolicy: IfNotPresent + livenessProbe: + failureThreshold: 6 + httpGet: + path: /login/index.php + port: http + scheme: HTTP + initialDelaySeconds: 600 + periodSeconds: 10 + successThreshold: 1 + timeoutSeconds: 5 + name: moodle + ports: + - containerPort: 8080 + name: http + protocol: TCP + - containerPort: 8443 + name: https + protocol: TCP + readinessProbe: + failureThreshold: 6 + httpGet: + path: /login/index.php + port: http + scheme: HTTP + initialDelaySeconds: 30 + periodSeconds: 5 + successThreshold: 1 + timeoutSeconds: 3 + resources: + limits: + cpu: 500m + ephemeral-storage: 1Gi + memory: 512Mi + requests: + cpu: 500m + ephemeral-storage: 1Gi + memory: 512Mi + securityContext: + capabilities: + drop: + - NET_RAW + runAsNonRoot: true + runAsUser: 1001 + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + volumeMounts: + - mountPath: /bitnami/moodle + name: moodle-data + subPath: moodle + - mountPath: /bitnami/moodledata + name: moodle-data + subPath: moodledata + dnsPolicy: ClusterFirst + hostAliases: + - hostnames: + - status.localhost + ip: 127.0.0.1 + restartPolicy: Always + schedulerName: default-scheduler + securityContext: + fsGroup: 1001 + seccompProfile: + type: RuntimeDefault + terminationGracePeriodSeconds: 30 + volumes: + - name: moodle-data + persistentVolumeClaim: + claimName: moodle-moodle +status: + availableReplicas: 1 + conditions: + - lastTransitionTime: "2022-09-04T11:53:20Z" + lastUpdateTime: "2022-09-04T11:53:20Z" + message: Deployment has minimum availability. + reason: MinimumReplicasAvailable + status: "True" + type: Available + - lastTransitionTime: "2022-09-04T11:48:50Z" + lastUpdateTime: "2022-09-04T11:53:20Z" + message: ReplicaSet "moodle-98cbdbd5f" has successfully progressed. + reason: NewReplicaSetAvailable + status: "True" + type: Progressing + observedGeneration: 1 + readyReplicas: 1 + replicas: 1 + updatedReplicas: 1 diff --git a/moodle/docker-compose.yml b/moodle/docker-compose.yml new file mode 100644 index 00000000..acff83ee --- /dev/null +++ b/moodle/docker-compose.yml @@ -0,0 +1,37 @@ +version: '2' +services: + mariadb: + image: docker.io/bitnami/mariadb:10.6 + environment: + # ALLOW_EMPTY_PASSWORD is recommended only for development. + - ALLOW_EMPTY_PASSWORD=yes + - MARIADB_USER=bn_moodle + - MARIADB_DATABASE=bitnami_moodle + - MARIADB_CHARACTER_SET=utf8mb4 + - MARIADB_COLLATE=utf8mb4_unicode_ci + volumes: + - 'mariadb_data:/bitnami/mariadb' + moodle: + image: docker.io/bitnami/moodle:4 + ports: + - '80:8080' + - '443:8443' + environment: + - MOODLE_DATABASE_HOST=mariadb + - MOODLE_DATABASE_PORT_NUMBER=3306 + - MOODLE_DATABASE_USER=bn_moodle + - MOODLE_DATABASE_NAME=bitnami_moodle + # ALLOW_EMPTY_PASSWORD is recommended only for development. + - ALLOW_EMPTY_PASSWORD=yes + volumes: + - 'moodle_data:/bitnami/moodle' + - 'moodledata_data:/bitnami/moodledata' + depends_on: + - mariadb +volumes: + mariadb_data: + driver: local + moodle_data: + driver: local + moodledata_data: + driver: local diff --git a/moodle/moodel-mariadb-configmap.yaml b/moodle/moodel-mariadb-configmap.yaml new file mode 100644 index 00000000..2e208612 --- /dev/null +++ b/moodle/moodel-mariadb-configmap.yaml @@ -0,0 +1,47 @@ +apiVersion: v1 +data: + my.cnf: |- + [mysqld] + skip-name-resolve + explicit_defaults_for_timestamp + basedir=/opt/bitnami/mariadb + plugin_dir=/opt/bitnami/mariadb/plugin + port=3306 + socket=/opt/bitnami/mariadb/tmp/mysql.sock + tmpdir=/opt/bitnami/mariadb/tmp + max_allowed_packet=16M + bind-address=* + pid-file=/opt/bitnami/mariadb/tmp/mysqld.pid + log-error=/opt/bitnami/mariadb/logs/mysqld.log + character-set-server=UTF8 + collation-server=utf8_general_ci + slow_query_log=0 + slow_query_log_file=/opt/bitnami/mariadb/logs/mysqld.log + long_query_time=10.0 + + [client] + port=3306 + socket=/opt/bitnami/mariadb/tmp/mysql.sock + default-character-set=UTF8 + plugin_dir=/opt/bitnami/mariadb/plugin + + [manager] + port=3306 + socket=/opt/bitnami/mariadb/tmp/mysql.sock + pid-file=/opt/bitnami/mariadb/tmp/mysqld.pid +kind: ConfigMap +metadata: + annotations: + meta.helm.sh/release-name: moodle + meta.helm.sh/release-namespace: default + creationTimestamp: "2022-09-04T11:48:49Z" + labels: + app.kubernetes.io/component: primary + app.kubernetes.io/instance: moodle + app.kubernetes.io/managed-by: Helm + app.kubernetes.io/name: mariadb + helm.sh/chart: mariadb-11.2.1 + name: moodle-mariadb + namespace: default + resourceVersion: "1405668" + uid: 40b5d312-94cd-4aa3-b129-6db9882ed44f diff --git a/moodle/moodle-deploy.yaml b/moodle/moodle-deploy.yaml new file mode 100644 index 00000000..ee9d6d2f --- /dev/null +++ b/moodle/moodle-deploy.yaml @@ -0,0 +1,65 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + app: moodle-postgres + name: moodle-postgres + namespace: moodle +spec: + replicas: 1 + selector: + matchLabels: + app: moodle-postgres + strategy: {} + template: + metadata: + creationTimestamp: null + labels: + app: moodle-postgres + spec: + containers: + - image: bitnami/moodle + name: moodle + env: + - name: ALLOW_EMPTY_PASSWORD + value: "yes" + - name: MOODLE_USERNAME + value: "admin" + - name: MOODLE_PASSWORD + value: "moodle" + - name: MOODLE_DATABASE_TYPE + value: "pgsql" + - name: MOODLE_DATABASE_HOST + value: "postgres-moodle-primary" + - name: MOODLE_DATABASE_PORT_NUMBER + value: "5432" + - name: MOODLE_DATABASE_NAME + value: "postgres-moodle" + - name: MOODLE_DATABASE_USER + value: "postgres-moodle" + - name: MOODLE_DATABASE_PASSWORD + valueFrom: + secretKeyRef: + name: postgres-moodle-pguser-postgres-moodle + key: password +--- +apiVersion: v1 +kind: Service +metadata: + labels: + app: moodle-postgres + name: moodle-postgres + namespace: moodle +spec: + ports: + - name: port-1 + port: 80 + protocol: TCP + targetPort: 8080 + - name: port-2 + port: 443 + protocol: TCP + targetPort: 8443 + selector: + app: moodle-postgres + type: LoadBalancer From 74cea671553db06738790f067e3d347a32891ca3 Mon Sep 17 00:00:00 2001 From: aneez004 Date: Mon, 5 Sep 2022 16:38:01 +0530 Subject: [PATCH 3/9] Update postgres.yaml --- kustomize/postgres/postgres.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kustomize/postgres/postgres.yaml b/kustomize/postgres/postgres.yaml index 24a2fc32..99a0b6f7 100644 --- a/kustomize/postgres/postgres.yaml +++ b/kustomize/postgres/postgres.yaml @@ -7,7 +7,7 @@ spec: postgresVersion: 14 instances: - name: instance1 - replicas: 2 + replicas: 1 dataVolumeClaimSpec: accessModes: - "ReadWriteOnce" From 6b6e16d4d3bc279d51e321cdef1debd091871800 Mon Sep 17 00:00:00 2001 From: Anees Aboobacker Date: Tue, 6 Sep 2022 11:48:08 +0530 Subject: [PATCH 4/9] added mysql sts for lab6 --- mysql-sts.yaml | 168 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 168 insertions(+) create mode 100644 mysql-sts.yaml diff --git a/mysql-sts.yaml b/mysql-sts.yaml new file mode 100644 index 00000000..859f61d0 --- /dev/null +++ b/mysql-sts.yaml @@ -0,0 +1,168 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: mysql +spec: + selector: + matchLabels: + app: mysql + app.kubernetes.io/name: mysql + serviceName: mysql + replicas: 3 + template: + metadata: + labels: + app: mysql + app.kubernetes.io/name: mysql + spec: + initContainers: + - name: init-mysql + image: mysql:debian + command: + - bash + - "-c" + - | + set -ex + # Generate mysql server-id from pod ordinal index. + [[ `hostname` =~ -([0-9]+)$ ]] || exit 1 + ordinal=${BASH_REMATCH[1]} + echo [mysqld] > /mnt/conf.d/server-id.cnf + # Add an offset to avoid reserved server-id=0 value. + echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf + # Copy appropriate conf.d files from config-map to emptyDir. + if [[ $ordinal -eq 0 ]]; then + cp /mnt/config-map/primary.cnf /mnt/conf.d/ + else + cp /mnt/config-map/replica.cnf /mnt/conf.d/ + fi + volumeMounts: + - name: conf + mountPath: /mnt/conf.d + - name: config-map + mountPath: /mnt/config-map + - name: clone-mysql + image: gcr.io/google-samples/xtrabackup:1.0 + command: + - bash + - "-c" + - | + set -ex + # Skip the clone if data already exists. + [[ -d /var/lib/mysql/mysql ]] && exit 0 + # Skip the clone on primary (ordinal index 0). + [[ `hostname` =~ -([0-9]+)$ ]] || exit 1 + ordinal=${BASH_REMATCH[1]} + [[ $ordinal -eq 0 ]] && exit 0 + # Clone data from previous peer. + ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql + # Prepare the backup. + xtrabackup --prepare --target-dir=/var/lib/mysql + volumeMounts: + - name: data + mountPath: /var/lib/mysql + subPath: mysql + - name: conf + mountPath: /etc/mysql/conf.d + containers: + - name: mysql + image: mysql:debian + env: + - name: MYSQL_ALLOW_EMPTY_PASSWORD + value: "1" + ports: + - name: mysql + containerPort: 3306 + volumeMounts: + - name: data + mountPath: /var/lib/mysql + subPath: mysql + - name: conf + mountPath: /etc/mysql/conf.d + resources: + requests: + cpu: 500m + memory: 1Gi + livenessProbe: + exec: + command: ["mysqladmin", "ping"] + initialDelaySeconds: 30 + periodSeconds: 10 + timeoutSeconds: 5 + readinessProbe: + exec: + # Check we can execute queries over TCP (skip-networking is off). + command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"] + initialDelaySeconds: 5 + periodSeconds: 2 + timeoutSeconds: 1 + - name: xtrabackup + image: gcr.io/google-samples/xtrabackup:1.0 + ports: + - name: xtrabackup + containerPort: 3307 + command: + - bash + - "-c" + - | + set -ex + cd /var/lib/mysql + + # Determine binlog position of cloned data, if any. + if [[ -f xtrabackup_slave_info && "x$( change_master_to.sql.in + # Ignore xtrabackup_binlog_info in this case (it's useless). + rm -f xtrabackup_slave_info xtrabackup_binlog_info + elif [[ -f xtrabackup_binlog_info ]]; then + # We're cloning directly from primary. Parse binlog position. + [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1 + rm -f xtrabackup_binlog_info xtrabackup_slave_info + echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\ + MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in + fi + + # Check if we need to complete a clone by starting replication. + if [[ -f change_master_to.sql.in ]]; then + echo "Waiting for mysqld to be ready (accepting connections)" + until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done + + echo "Initializing replication from clone position" + mysql -h 127.0.0.1 \ + -e "$( Date: Tue, 6 Sep 2022 23:07:21 +0530 Subject: [PATCH 5/9] added lab6 --- lab6/mysql-configmap.yaml | 19 ++++ lab6/mysql-services.yaml | 32 +++++++ lab6/mysql-sts.yaml | 168 +++++++++++++++++++++++++++++++++ lab6/wordpress-deployment.yaml | 67 +++++++++++++ 4 files changed, 286 insertions(+) create mode 100644 lab6/mysql-configmap.yaml create mode 100644 lab6/mysql-services.yaml create mode 100644 lab6/mysql-sts.yaml create mode 100644 lab6/wordpress-deployment.yaml diff --git a/lab6/mysql-configmap.yaml b/lab6/mysql-configmap.yaml new file mode 100644 index 00000000..afcc599e --- /dev/null +++ b/lab6/mysql-configmap.yaml @@ -0,0 +1,19 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: mysql + labels: + app: mysql + app.kubernetes.io/name: mysql +data: + primary.cnf: | + # Apply this config only on the primary. + [mysqld] + log-bin + default_authentication_plugin=mysql_native_password + replica.cnf: | + # Apply this config only on replicas. + [mysqld] + super-read-only + #default_authentication_plugin=mysql_native_password + diff --git a/lab6/mysql-services.yaml b/lab6/mysql-services.yaml new file mode 100644 index 00000000..bc015066 --- /dev/null +++ b/lab6/mysql-services.yaml @@ -0,0 +1,32 @@ +# Headless service for stable DNS entries of StatefulSet members. +apiVersion: v1 +kind: Service +metadata: + name: mysql + labels: + app: mysql + app.kubernetes.io/name: mysql +spec: + ports: + - name: mysql + port: 3306 + clusterIP: None + selector: + app: mysql +--- +# Client service for connecting to any MySQL instance for reads. +# For writes, you must instead connect to the primary: mysql-0.mysql. +apiVersion: v1 +kind: Service +metadata: + name: mysql-read + labels: + app: mysql + app.kubernetes.io/name: mysql + readonly: "true" +spec: + ports: + - name: mysql + port: 3306 + selector: + app: mysql diff --git a/lab6/mysql-sts.yaml b/lab6/mysql-sts.yaml new file mode 100644 index 00000000..d7f8dfda --- /dev/null +++ b/lab6/mysql-sts.yaml @@ -0,0 +1,168 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: mysql +spec: + selector: + matchLabels: + app: mysql + app.kubernetes.io/name: mysql + serviceName: mysql + replicas: 3 + template: + metadata: + labels: + app: mysql + app.kubernetes.io/name: mysql + spec: + initContainers: + - name: init-mysql + image: mysql:5.7-debian + command: + - bash + - "-c" + - | + set -ex + # Generate mysql server-id from pod ordinal index. + [[ `hostname` =~ -([0-9]+)$ ]] || exit 1 + ordinal=${BASH_REMATCH[1]} + echo [mysqld] > /mnt/conf.d/server-id.cnf + # Add an offset to avoid reserved server-id=0 value. + echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf + # Copy appropriate conf.d files from config-map to emptyDir. + if [[ $ordinal -eq 0 ]]; then + cp /mnt/config-map/primary.cnf /mnt/conf.d/ + else + cp /mnt/config-map/replica.cnf /mnt/conf.d/ + fi + volumeMounts: + - name: conf + mountPath: /mnt/conf.d + - name: config-map + mountPath: /mnt/config-map + - name: clone-mysql + image: gcr.io/google-samples/xtrabackup:1.0 + command: + - bash + - "-c" + - | + set -ex + # Skip the clone if data already exists. + [[ -d /var/lib/mysql/mysql ]] && exit 0 + # Skip the clone on primary (ordinal index 0). + [[ `hostname` =~ -([0-9]+)$ ]] || exit 1 + ordinal=${BASH_REMATCH[1]} + [[ $ordinal -eq 0 ]] && exit 0 + # Clone data from previous peer. + ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql + # Prepare the backup. + xtrabackup --prepare --target-dir=/var/lib/mysql + volumeMounts: + - name: data + mountPath: /var/lib/mysql + subPath: mysql + - name: conf + mountPath: /etc/mysql/conf.d + containers: + - name: mysql + image: mysql:5.7-debian + env: + - name: MYSQL_ALLOW_EMPTY_PASSWORD + value: "1" + ports: + - name: mysql + containerPort: 3306 + volumeMounts: + - name: data + mountPath: /var/lib/mysql + subPath: mysql + - name: conf + mountPath: /etc/mysql/conf.d + resources: + requests: + cpu: 500m + memory: 1Gi + livenessProbe: + exec: + command: ["mysqladmin", "ping"] + initialDelaySeconds: 30 + periodSeconds: 10 + timeoutSeconds: 5 + readinessProbe: + exec: + # Check we can execute queries over TCP (skip-networking is off). + command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"] + initialDelaySeconds: 5 + periodSeconds: 2 + timeoutSeconds: 1 + - name: xtrabackup + image: gcr.io/google-samples/xtrabackup:1.0 + ports: + - name: xtrabackup + containerPort: 3307 + command: + - bash + - "-c" + - | + set -ex + cd /var/lib/mysql + + # Determine binlog position of cloned data, if any. + if [[ -f xtrabackup_slave_info && "x$( change_master_to.sql.in + # Ignore xtrabackup_binlog_info in this case (it's useless). + rm -f xtrabackup_slave_info xtrabackup_binlog_info + elif [[ -f xtrabackup_binlog_info ]]; then + # We're cloning directly from primary. Parse binlog position. + [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1 + rm -f xtrabackup_binlog_info xtrabackup_slave_info + echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\ + MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in + fi + + # Check if we need to complete a clone by starting replication. + if [[ -f change_master_to.sql.in ]]; then + echo "Waiting for mysqld to be ready (accepting connections)" + until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done + + echo "Initializing replication from clone position" + mysql -h 127.0.0.1 \ + -e "$( Date: Tue, 6 Sep 2022 23:21:09 +0530 Subject: [PATCH 6/9] changed mysqlsts --- lab6/mysql-sts.yaml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lab6/mysql-sts.yaml b/lab6/mysql-sts.yaml index d7f8dfda..07cda83c 100644 --- a/lab6/mysql-sts.yaml +++ b/lab6/mysql-sts.yaml @@ -68,7 +68,13 @@ spec: image: mysql:5.7-debian env: - name: MYSQL_ALLOW_EMPTY_PASSWORD - value: "1" + value: "1" + - name: MYSQL_DATABASE + value: "wordpress" + - name: MYSQL_USER + value: "wordpress" + - name: MYSQL_PASSWORD + value: "wordpress" ports: - name: mysql containerPort: 3306 From b3ba0055fb41bcec5c1a95ea16ed1da23ccc215f Mon Sep 17 00:00:00 2001 From: Anees Aboobacker Date: Tue, 6 Sep 2022 23:26:56 +0530 Subject: [PATCH 7/9] changed mysqlsts again b --- lab6/{mysql-sts.yaml => mysql-sts.yaml.bak} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lab6/{mysql-sts.yaml => mysql-sts.yaml.bak} (100%) diff --git a/lab6/mysql-sts.yaml b/lab6/mysql-sts.yaml.bak similarity index 100% rename from lab6/mysql-sts.yaml rename to lab6/mysql-sts.yaml.bak From 8fdb944689f5f88f189adee95c45cdf05a8a18c8 Mon Sep 17 00:00:00 2001 From: Anees Aboobacker Date: Tue, 6 Sep 2022 23:27:24 +0530 Subject: [PATCH 8/9] changed mysqlsts again and again --- lab6/{mysql-sts.yaml.bak => mysql-sts.yaml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename lab6/{mysql-sts.yaml.bak => mysql-sts.yaml} (100%) diff --git a/lab6/mysql-sts.yaml.bak b/lab6/mysql-sts.yaml similarity index 100% rename from lab6/mysql-sts.yaml.bak rename to lab6/mysql-sts.yaml From 87033393fa286179637baad07305aaf57c9c3439 Mon Sep 17 00:00:00 2001 From: aneez004 Date: Tue, 6 Sep 2022 23:30:20 +0530 Subject: [PATCH 9/9] Update mysql-sts.yaml --- lab6/mysql-sts.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lab6/mysql-sts.yaml b/lab6/mysql-sts.yaml index 07cda83c..76a83ed9 100644 --- a/lab6/mysql-sts.yaml +++ b/lab6/mysql-sts.yaml @@ -68,13 +68,13 @@ spec: image: mysql:5.7-debian env: - name: MYSQL_ALLOW_EMPTY_PASSWORD - value: "1" + value: "1" - name: MYSQL_DATABASE - value: "wordpress" + value: "wordpress" - name: MYSQL_USER - value: "wordpress" + value: "wordpress" - name: MYSQL_PASSWORD - value: "wordpress" + value: "wordpress" ports: - name: mysql containerPort: 3306