Creating a base for a development and production Deployment

Let's perform the following steps to create a base for a local Docker image registry Deployment that we will use later in this chapter:

  1. Create a directory named registry and another one underneath called base:
$ mkdir registry && mkdir registry/base
  1. Under registry/base, download the Deployment file named deployment-registry.yaml from the example repository:
$ cd registry/base/
$ wget https://raw.githubusercontent.com/k8sdevopscookbook/src/master/chapter2/kustomize/registry/base/deployment-registry.yaml
  1. Review the file to understand its structure. You will see that it is a Deployment manifest consisting of two containers named registry and registryui. You will see that the registry container has a volumeMount named registry-storage and this volume is provided by a persistent volume claim named registry-pvc:
$ cat deployment-registry.yaml
apiVersion: extensions/v1beta1
kind: Deployment
# actual file is longer, shortened to highlight important structure of the file only
- image: registry:2
#....#
- name: registry-storage
mountPath: /var/lib/registry
#....#
- name: registryui
image: hyper/docker-registry-web:latest
#....#
- name: registry-storage
persistentVolumeClaim:
claimName: registry-pvc

  1. Under the same registry/basedownload the service manifest file named service-registry.yaml from the example repository:
$ wget https://raw.githubusercontent.com/k8sdevopscookbook/src/master/chapter2/kustomize/registry/base/service-registry.yaml
  1. Review the file to understand its structure. You will see that it is a service manifest that exposes the service on each Node's IP at a static port; in this recipe, port 5000 for the registry service and port 80 for the registry-ui:
$ cat <<EOF > registry/base/service-registry.yaml
kind: Service
# actual file is longer, shortened to highlight important structure of the file only
type: NodePort
ports:
- name: registry
port: 5000
protocol: TCP
nodePort: 30120
- name: registry-ui
port: 80
protocol: TCP
nodePort: 30220
#....#
  1. Create a PersistentVolumeClaim manifest file named pvc-registry.yaml with the following content:
$ cat <<EOF > registry/base/pvc-registry.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: registry-pvc
labels:
app: kube-registry-pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10G
EOF

At this point, you can deploy the workload using all the resource files under the registry directory, by using kubectl apply -f registry/base. But every time you need to change a parameter in resources, such as app or label, you need to edit the files. The whole point of using Kustomize is to take advantage of reusing the files without modifying the source of the files.
  1. And finally, create the kustomization.yaml file. The following command will create the Kustomize resource content with the three separate manifest files we created previously:
$ cat <<EOF >./registry/base/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- deployment-registry.yaml
- service-registry.yaml
- pvc-registry.yaml
EOF
  1. Now, create two overlays to be used for development and production Deployments. The first one is for development:
$ mkdir registry/overlays && mkdir registry/overlays/dev 
$ cat <<EOF >./registry/overlays/dev/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ../../base
namePrefix: dev-
commonAnnotations:
note: Hello, I am development!
EOF
  1. And the second manifest will create the overlay for production:
$ mkdir registry/overlays/prod
$ cat <<EOF >./registry/overlays/prod/kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ../../base
namePrefix: prod-
commonAnnotations:
note: Hello, I am production!
EOF

  1. Check that dev and prod prefixes are injected into your Deployment. When you point to the prod folder, the annotation note will display "Hello, I am production!":
$ kubectl kustomize ./registry/overlays/prod/
# result shortened to highlight the annotation
metadata:
annotations:
note: Hello, I am production!
labels:
app: kube-registry-pv-claim
name: prod-registry-pvc
#...#
  1. When you point to the dev folder, the annotation note will display "Hello, I am development!":
$ kubectl kustomize ./dev/
... # removed
metadata:
annotations:
note: Hello, I am development!
labels:
app: kube-registry-pv-claim
name: dev-registry-pvc
... # removed
  1. Now, deploy the dev version of your application:
$ kubectl apply -k ./registry/overlays/dev

Similarly, you can inject labels, patch image versions, change the number of replicas, and deploy resources into a different namespace. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.60.249