A Journey from Dockerfile to Application Deployment on Kubernetes For Beginners

This blog shows examples to perform a basic application deployment on Kubernetes

Photo by Ian Taylor on Unsplash
FROM tomcat:9.0USER rootRUN rm -rf webappsRUN mv webapps.dist webappsCOPY demoapp.war /usr/local/tomcat/webapps/demoapp.war
docker build -t sampleapp:latest .
docker image ls
kind: NamespaceapiVersion: v1metadata: name: sample-ns
kind: PersistentVolumeapiVersion: v1metadata:  name: demoapp-logs  labels:    intent: logsspec:  storageClassName: “standard”  capacity:    storage: 500Mi  accessModes:   - ReadWriteMany  hostPath:   path: “/localPath/to/logs”
apiVersion: v1kind: PersistentVolumeClaimmetadata:  name: demoapp-pv-claim  namespace: sample-nsspec:  storageClassName: “standard”  selector:    matchExpressions:     - {key: intent, operator: In, values: [logs]}  accessModes:   - ReadWriteMany  resources:    requests:      storage: 500Mi
apiVersion: v1kind: Secretmetadata:  name: demoapp-secret  namespace: sample-nstype: Opaquedata:  DB_USER: YWRtaW4=  DB_PASS: cGFzc3dvcmQ=
apiVersion: v1kind: ConfigMapmetadata:  name: demoapp-cmap  namespace: sample-nsdata:  sample.properties: |-    key1=value1    key2=value2    key3=value3
apiVersion: v1kind: Servicemetadata:  name: demoapp-service  namespace: sample-ns  labels:    intent: service-for-demoappspec:  type: NodePort  ports:   - port: 8080     protocol: TCP     targetPort: 8080  selector:    app: demoapp— -apiVersion: v1kind: Servicemetadata:  name: demoapp-headless-service  namespace: sample-ns  labels:    intent: headless-service-for-demoappspec:  clusterIP: None  ports:  - name: http    port: 8080    protocol: TCP    targetPort: 8080  selector:   app: demoapp
apiVersion: apps/v1kind: StatefulSetmetadata:  name: demoapp-statefulset  namespace: sample-ns  labels:    app: demoappspec:  replicas: 2  selector:    matchLabels:     app: demoapp  serviceName: “demoapp-headless-service”  template:    metadata:     labels:       app: demoapp    spec:      volumes:       - name: tomcatlogs           persistentVolumeClaim:           claimName: “demoapp-pv-claim”       - name: configmap-volume         configMap:           name: “demoapp-cmap”      containers:       - name: demoapp         image: sampleapp:latest         imagePullPolicy: IfNotPresent         ports:          - name: http            containerPort: 8080            protocol: TCP         envFrom:         - secretRef:            name: “demoapp-secret”         volumeMounts:          - name: tomcatlogs            mountPath: /usr/local/tomcat/logs          - name: configmap-volume            mountPath: /opt/cmap
kubectl create -f namespace.yamlkubectl create -f configmap.yamlkubectl create -f pv.yamlkubectl create -f pvc.yamlkubectl create -f secret.yamlkubectl create -f sts.yamlkubectl create -f service.yaml
kubectl get svc | grep demoapp-service
kubectl delete -f sts.yamlkubectl delete -f configmap.yamlkubectl delete -f pvc.yamlkubectl delete -f pv.yamlkubectl delete -f secret.yamlkubectl delete -f service.yamlkubectl delete -f namespace.yaml



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store