5.4 KiB
MySql Example
This example takes some off-the-shelf k8s resources designed for MySQL, and customizes them to suit a production scenario.
In the production environment we want:
- MySQL resource names to be prefixed by 'prod-' to make them distinguishable.
- MySQL resources to have 'env: prod' labels so that we can use label selector to query these.
- MySQL to use persistent disk for storing data.
Download resources
Download deployment.yaml, service.yaml and
secret.yaml. These are plain k8s resources files one
could add to a k8s cluster to run MySql.
DEMO_HOME=$HOME/kinflate_demo/mysql
rm -rf $DEMO_HOME && mkdir -p $DEMO_HOME
cd $DEMO_HOME
# Get MySQL configs
for f in service secret deployment ; do \
wget https://raw.githubusercontent.com/kinflate/mysql/master/emptyDir/$f.yaml ; \
done
Initialize a manifest
A manifest is needed to group these resources together.
Create one:
mkdir -p $DEMO_HOME/prod
cd $DEMO_HOME/prod
kinflate init
You should now have a file called Kube-manifest.yaml:
cat $DEMO_HOME/prod/Kube-manifest.yaml
containing something like:
apiVersion: manifest.k8s.io/v1alpha1 kind: Manifest metadata: name: helloworld description: helloworld does useful stuff. namePrefix: some-prefix # Labels to add to all objects and selectors. # These labels would also be used to form the selector for apply --prune # Named differently than “labels” to avoid confusion with metadata for this object objectLabels: app: helloworld objectAnnotations: note: This is a example annotation resources: - deployment.yaml - service.yaml # There could also be configmaps in Base, which would make these overlays configmaps: [] # There could be secrets in Base, if just using a fork/rebase workflow secrets: [] recursive: true
Add resources
Add the resources to the manifest
cd $DEMO_HOME/prod
kinflate add resource ../secret.yaml
kinflate add resource ../service.yaml
kinflate add resource ../deployment.yaml
cat Kube-manifest.yaml
Kube-manifest.yaml's resources section should contain:
apiVersion: manifest.k8s.io/v1alpha1 .... resources: - ../secret.yaml - ../service.yaml - ../deployment.yaml
Now we are ready to apply our first customization.
NamePrefix Customization
Arrange for the MySQL resources to begin with prefix prod- (since they are meant for the production environment):
cd $DEMO_HOME/prod
kinflate set nameprefix 'prod-'
cat Kube-manifest.yaml
Kube-manifest.yaml should have updated value of namePrefix field:
apiVersion: manifest.k8s.io/v1alpha1 .... namePrefix: prod- objectAnnotations: note: This is a example annotation
This namePrefix directive adds prod- to all
resource names.
Run kinflate:
cd $DEMO_HOME/prod
kinflate inflate -f .
The output should contain:
apiVersion: v1 data: password: YWRtaW4= kind: Secret metadata: .... name: prod-mysql-pass-d2gtcm2t2k --- apiVersion: v1 kind: Service metadata: .... name: prod-mysql spec: .... --- apiVersion: apps/v1beta2 kind: Deployment metadata: .... name: prod-mysql spec: selector: ....
Label Customization
We want resources in production environment to have certain labels so that we can query them by label selector.
kinflate does not have set label command to add
label, but we can edit Kube-manifest.yaml file under
prod directory and add the production labels under
objectLabels fields as highlighted below.
cd $DEMO_HOME/prod
sed -i 's/app: helloworld/app: prod/' Kube-manifest.yaml
At this point, running kinflate inflate -f . will
generate MySQL configs with name-prefix 'prod-' and
labels env:prod.
Storage customization
Off the shelf MySQL uses emptyDir type volume, which
gets wiped away if the MySQL Pod is recreated, and that
is certainly not desirable for production
environment. So we want to use Persistent Disk in
production. Kinflate lets you apply patches to the
resources.
cd $DEMO_HOME/prod
# Create a patch for persistent-disk.yaml
cat <<'EOF' > persistent-disk.yaml
apiVersion: apps/v1beta2 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: mysql
spec:
template:
spec:
volumes:
- name: mysql-persistent-storage
emptyDir: null
gcePersistentDisk:
pdName: mysql-persistent-storage
EOF
# Edit the manifest file to add the above patch:
cat <<'EOF' >> $DEMO_HOME/prod/Kube-manifest.yaml
patches:
- persistent-disk.yaml
EOF
Lets break this down:
-
In the first step, we created a YAML file named
persistent-disk.yamlto patch the resource defined in deployment.yaml -
Then we added
persistent-disk.yamlto list ofpatchesinKube-manifest.yaml.kinflate inflatewill apply this patch to the deployment resource with the namemysqlas defined in the patch.
The output of the following command can now be applied
to the cluster (i.e. piped to kubectl apply) to
create the production environment.
kinflate inflate -f $DEMO_HOME/prod # | kubectl apply -f -