Wednesday, February 1, 2023

Deploy App to K8s Cluster

 

Deploy the DeliApp Image to Cluster


In the previous lab  we Dockerize our app for continuous integration which is the App we worked on so we will go further to demonstrate and deploy that same app into our Kubernetes cluster. 

Before we start the lab you need an Extension that will make your job easier for you. 


YAML Autogeneration using Kubernetes Extention

One of the easiest ways to create Kubernetes YAML is using the visual studio kubernetes extension.

Install the Kubernetes VS code extension, and it will help develop k8s manifests for most kubernetes objects. It also supports deploying apps to local and remote k8s clusters.

All you have to do is, start typing the Object name and it will automatically populate the options for you. Then, based on your selection, it will autogenerate the basic YAML structure for you as shown n the following image.




This extension supports YAML generation of Pods, Deployment, Statefulset, Replicationset, Persistent Volumes (PV), Persistent Volume Claims (PVC), etc.


let's try to deploy Application on the cluster using deployment and service yaml file 
The command to create deployment and service 


deployment.yml and copy and paste the below 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: appname
  labels:
    app: appname
spec:
  replicas: 1
  selector:
    matchLabels:
      app: appname
  template:
    metadata:
      labels:
        app: appname
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/arch
                operator: In
                values:
                - amd64
                - arm64
      containers:
      - name: appname
        image: username/appname
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 80



Create service file by below command

Service.yml

apiVersion: v1
kind: Service
metadata:
  name: appname
  labels:
    app: appname
spec:
  ports:
  - port: 80
    nodePort: 32000
    protocol: TCP
  selector:
    app: appname
  type: NodePort




copy the public cluster ip and with the port being expose in the SG(32000) and paste in the browser




 You can change type from NodePort to LoadBalancer by below service file

apiVersion: v1
kind: Service
metadata:
  name: appname
  labels:
    app: appname
spec:
  ports:
  - port: 80
    nodePort: 32000
    protocol: TCP
  selector:
    app: appname
  type: LoadBalancer





Create a namespace to deploy your application into with this command, kubectl create namespace (namespace name)

To create this app. all you have to do is Cd into the folder containing the files and run


kubectl create -f deploy.yml -n (namespace created earlier) and do the same for service.yml

No comments:

Post a Comment

Jenkins Scripted Pipeline - Create Jenkins Pipeline for Automating Builds, Code quality checks, Deployments to Tomcat - How to build, deploy WARs using Jenkins Pipeline - Build pipelines integrate with github, Sonarqube, Slack, JaCoCo, Nexus, Tomcat

  Jenkins Scripted Pipeline - Create Jenkins Pipeline for Automating Builds, Code quality checks, Deployments to Tomcat - How to build, depl...