App Deployment With Kubernetes

DEPLOYING WITH KUBERNETES

Serikiayodele
3 min readFeb 7, 2023

Prerequisites

A little intro…

The format for the Kubernetes configuration files is yaml and the configuration basically has 3 parts:

  • Metadata: where the information of the component exists.
  • Specification: where the configuration you want to apply to the component is specified, the attributes of the specification will be specific to the kind of component you’re creating.
  • Status: automatically generated and added by Kubernetes, it gets the status information from the etcd, which is the cluster brain.

In the specification part of the deployment, we have the templates which has it’s own metadata and specification, it’s basically a configuration file inside a configuration file, and this configuration applies to a pod.

Connection is established using labels and selectors, metadata contains the labels and specification contains the selector, the deployment is connected to the pod by telling the deployment to match all the labels with the app value. The deployment label is also used by the service selector which with makes the connection between the service and the deployment or it’s pods.

Service has a port where the service itself is accessible at, the service also needs to know what port to forward the request and on what port that pod is listening and that’s the targetport, this targetport should match the containerport

Prepare Deployment files

Step 1- Mongo

Here are files for the mongo deployment, service and secret.

#mongo.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
labels:
apps: mongodb
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password

---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017

I’ve saved the environmental variables as secrets cause we don’t want everyone having access to those.

# mongo-secret.yaml

apiVersion: v1
kind: Secret
metadata:
name: mongodb-secret
type: Opaque
data:
mongo-root-username: <encrypted-username>
mongo-root-password: <encrypted-password>

Step 2- Mongo-Express

Here are files for the mongo-express deployment, service and config-map.

#mongo-express.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: mongo-express
labels:
app: mongo-express
spec:
replicas: 1
selector:
matchLabels:
app: mongo-express
template:
metadata:
labels:
app: mongo-express
spec:
containers:
- name: mongo-express
image: mongo-express
ports:
- containerPort: 8081
env:
- name: ME_CONFIG_MONGODB_ADMINUSERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: ME_CONFIG_MONGODB_ADMINPASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
- name: ME_CONFIG_MONGODB_SERVER
valueFrom:
configMapKeyRef:
name: mongodb-configmap
key: database_url

---
apiVersion: v1
kind: Service
metadata:
name: mongo-express-service
spec:
selector:
app: mongo-express
type: LoadBalancer
ports:
- protocol: TCP
port: 8081
targetPort: 8081
nodePort: 30000
  • ConfigMaps: ConfigMaps are used to store non-sensitive configuration data, such as configuration files, command-line flags, feature toggles, etc. They are stored unencrypted and can be accessed by a pod as environment variables or mounted as files.
# mongo-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: mongodb-configmap
data:
database_url: mongodb-service

Step 3- My-App

Here are files for the app’s deployment and service.

#my-app.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-deployment
labels:
app: my-app
spec:
replicas: 1
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-app
image: seriki/my-app:1.7
ports:
- containerPort: 3000

---
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 3000
targetPort: 3000

Step 4- Kustomization

I have a kustomization file, which allows you to manage multiple resources as a single unit.

# kustomization.yaml

resources:
- secrets/mongo-secret.yaml
- configmap/mongo-configmap.yaml
- deployments/mongo.yaml
- deployments/mongo-express.yaml
- deployments/my-app.yaml

Now, we just need to run `kubectl apply -k .` from the older holding the kustomization file

resources created

And, you should be able to access your app and mongo from their ports.

see also: App Deployment With Gitlab-CI, see also: App Deployment With Docker

--

--