From a35d445fd1238e5d24501cd5febd2998c44502e9 Mon Sep 17 00:00:00 2001 From: Lachlan Evenson Date: Mon, 29 Jan 2024 21:19:02 -0800 Subject: [PATCH] Chapter 16 updates Signed-off-by: Lachlan Evenson --- 16-1-dns-service.yaml | 7 +++++ 16-10-mongo-simple.yaml | 25 ++++++++++++++++ 16-11-mongo-service.yaml | 11 +++++++ 16-12-mongo-configmap.yaml | 51 +++++++++++++++++++++++++++++++++ 16-13-mongo.yaml | 37 ++++++++++++++++++++++++ 16-2-external-ip-service.yaml | 7 +++++ 16-3-external-ip-endpoints.yaml | 9 ++++++ 16-4-nfs-volume.yaml | 14 +++++++++ 16-5-nfs-volume-claim.yaml | 13 +++++++++ 16-6-mysql-replicaset.yaml | 43 +++++++++++++++++++++++++++ 16-7-mysql-service.yaml | 10 +++++++ 16-8-storageclass.yaml | 9 ++++++ 16-9-dynamic-volume-claim.yaml | 12 ++++++++ 13 files changed, 248 insertions(+) create mode 100644 16-1-dns-service.yaml create mode 100644 16-10-mongo-simple.yaml create mode 100644 16-11-mongo-service.yaml create mode 100644 16-12-mongo-configmap.yaml create mode 100644 16-13-mongo.yaml create mode 100644 16-2-external-ip-service.yaml create mode 100644 16-3-external-ip-endpoints.yaml create mode 100644 16-4-nfs-volume.yaml create mode 100644 16-5-nfs-volume-claim.yaml create mode 100644 16-6-mysql-replicaset.yaml create mode 100644 16-7-mysql-service.yaml create mode 100644 16-8-storageclass.yaml create mode 100644 16-9-dynamic-volume-claim.yaml diff --git a/16-1-dns-service.yaml b/16-1-dns-service.yaml new file mode 100644 index 0000000..23a7cd0 --- /dev/null +++ b/16-1-dns-service.yaml @@ -0,0 +1,7 @@ +kind: Service +apiVersion: v1 +metadata: + name: external-database +spec: + type: ExternalName + externalName: database.company.com diff --git a/16-10-mongo-simple.yaml b/16-10-mongo-simple.yaml new file mode 100644 index 0000000..9ef719f --- /dev/null +++ b/16-10-mongo-simple.yaml @@ -0,0 +1,25 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: mongo +spec: + serviceName: "mongo" + replicas: 3 + selector: + matchLabels: + app: mongo + template: + metadata: + labels: + app: mongo + spec: + containers: + - name: mongodb + image: mongo:3.4.24 + command: + - mongod + - --replSet + - rs0 + ports: + - containerPort: 27017 + name: peer diff --git a/16-11-mongo-service.yaml b/16-11-mongo-service.yaml new file mode 100644 index 0000000..25a2cf4 --- /dev/null +++ b/16-11-mongo-service.yaml @@ -0,0 +1,11 @@ +apiVersion: v1 +kind: Service +metadata: + name: mongo +spec: + ports: + - port: 27017 + name: peer + clusterIP: None + selector: + app: mongo diff --git a/16-12-mongo-configmap.yaml b/16-12-mongo-configmap.yaml new file mode 100644 index 0000000..caa80bf --- /dev/null +++ b/16-12-mongo-configmap.yaml @@ -0,0 +1,51 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: mongo-init +data: + init.sh: | + #!/bin/bash + + # Need to wait for the readiness health check to pass so that the + # mongo names resolve. This is kind of wonky. + until ping -c 1 ${HOSTNAME}.mongo; do + echo "waiting for DNS (${HOSTNAME}.mongo)..." + sleep 2 + done + + until /usr/bin/mongo --eval 'printjson(db.serverStatus())'; do + echo "connecting to local mongo..." + sleep 2 + done + echo "connected to local." + + HOST=mongo-0.mongo:27017 + + until /usr/bin/mongo --host=${HOST} --eval 'printjson(db.serverStatus())'; do + echo "connecting to remote mongo..." + sleep 2 + done + echo "connected to remote." + + if [[ "${HOSTNAME}" != 'mongo-0' ]]; then + until /usr/bin/mongo --host=${HOST} --eval="printjson(rs.status())" \ + | grep -v "no replset config has been received"; do + echo "waiting for replication set initialization" + sleep 2 + done + echo "adding self to mongo-0" + /usr/bin/mongo --host=${HOST} \ + --eval="printjson(rs.add('${HOSTNAME}.mongo'))" + fi + + if [[ "${HOSTNAME}" == 'mongo-0' ]]; then + echo "initializing replica set" + /usr/bin/mongo --eval="printjson(rs.initiate(\ + {'_id': 'rs0', 'members': [{'_id': 0, \ + 'host': 'mongo-0.mongo:27017'}]}))" + fi + echo "initialized" + + while true; do + sleep 3600 + done diff --git a/16-13-mongo.yaml b/16-13-mongo.yaml new file mode 100644 index 0000000..75f21ed --- /dev/null +++ b/16-13-mongo.yaml @@ -0,0 +1,37 @@ +apiVersion: apps/v1 +kind: StatefulSet +metadata: + name: mongo +spec: + serviceName: "mongo" + replicas: 3 + selector: + matchLabels: + app: mongo + template: + metadata: + labels: + app: mongo + spec: + containers: + - name: mongodb + image: mongo:3.4.1 + command: + - mongod + - --replSet + - rs0 + ports: + - containerPort: 27017 + name: web + - name: init-mongo + image: mongo:3.4.1 + command: + - bash + - /config/init.sh + volumeMounts: + - name: config + mountPath: /config + volumes: + - name: config + configMap: + name: "mongo-init" diff --git a/16-2-external-ip-service.yaml b/16-2-external-ip-service.yaml new file mode 100644 index 0000000..eef45ee --- /dev/null +++ b/16-2-external-ip-service.yaml @@ -0,0 +1,7 @@ +kind: Service +apiVersion: v1 +metadata: + name: external-ip-database +spec: + ports: + - port: 3306 diff --git a/16-3-external-ip-endpoints.yaml b/16-3-external-ip-endpoints.yaml new file mode 100644 index 0000000..208f732 --- /dev/null +++ b/16-3-external-ip-endpoints.yaml @@ -0,0 +1,9 @@ +kind: Endpoints +apiVersion: v1 +metadata: + name: external-ip-database +subsets: + - addresses: + - ip: 192.168.0.1 + ports: + - port: 3306 diff --git a/16-4-nfs-volume.yaml b/16-4-nfs-volume.yaml new file mode 100644 index 0000000..f3ff6cb --- /dev/null +++ b/16-4-nfs-volume.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: PersistentVolume +metadata: + name: database + labels: + volume: my-volume +spec: + accessModes: + - ReadWriteMany + capacity: + storage: 1Gi + nfs: + server: 192.168.0.1 + path: "/exports" diff --git a/16-5-nfs-volume-claim.yaml b/16-5-nfs-volume-claim.yaml new file mode 100644 index 0000000..05e76af --- /dev/null +++ b/16-5-nfs-volume-claim.yaml @@ -0,0 +1,13 @@ +kind: PersistentVolumeClaim +apiVersion: v1 +metadata: + name: database +spec: + accessModes: + - ReadWriteMany + resources: + requests: + storage: 1Gi + selector: + matchLabels: + volume: my-volume diff --git a/16-6-mysql-replicaset.yaml b/16-6-mysql-replicaset.yaml new file mode 100644 index 0000000..041ef76 --- /dev/null +++ b/16-6-mysql-replicaset.yaml @@ -0,0 +1,43 @@ +apiVersion: apps/v1 +kind: ReplicaSet +metadata: + name: mysql + # labels so that we can bind a Service to this Pod + labels: + app: mysql +spec: + replicas: 1 + selector: + matchLabels: + app: mysql + template: + metadata: + labels: + app: mysql + spec: + containers: + - name: database + image: mysql + resources: + requests: + cpu: 1 + memory: 2Gi + env: + # Environment variables are not a best practice for security, + # but we're using them here for brevity in the example. + # See Chapter 11 for better options. + - name: MYSQL_ROOT_PASSWORD + value: some-password-here + livenessProbe: + tcpSocket: + port: 3306 + ports: + - containerPort: 3306 + volumeMounts: + - name: database + # /var/lib/mysql is where MySQL stores its databases + mountPath: "/var/lib/mysql" + volumes: + - name: database + persistentVolumeClaim: + claimName: database diff --git a/16-7-mysql-service.yaml b/16-7-mysql-service.yaml new file mode 100644 index 0000000..521cde2 --- /dev/null +++ b/16-7-mysql-service.yaml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: mysql +spec: + ports: + - port: 3306 + protocol: TCP + selector: + app: mysql diff --git a/16-8-storageclass.yaml b/16-8-storageclass.yaml new file mode 100644 index 0000000..09c93fd --- /dev/null +++ b/16-8-storageclass.yaml @@ -0,0 +1,9 @@ +apiVersion: storage.k8s.io/v1 +kind: StorageClass +metadata: + name: default + annotations: + storageclass.beta.kubernetes.io/is-default-class: "true" + labels: + kubernetes.io/cluster-service: "true" +provisioner: kubernetes.io/azure-disk diff --git a/16-9-dynamic-volume-claim.yaml b/16-9-dynamic-volume-claim.yaml new file mode 100644 index 0000000..ab1637c --- /dev/null +++ b/16-9-dynamic-volume-claim.yaml @@ -0,0 +1,12 @@ +kind: PersistentVolumeClaim +apiVersion: v1 +metadata: + name: my-claim + annotations: + volume.beta.kubernetes.io/storage-class: default +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 10Gi