Kubernetes

Deploying and running a Devprime microservice on Kubernetes

To publish a container image to kubernetes, you need to have a public image in docker hub or in repositories such as Azure Container Registry (ACR), Amazon Elastic Container Registry, Google Container Registry, Oracle Cloud Infrastructure Container Registry, and others.

We recommend a read in the docker documentation that demonstrates how to build a docker image and publish to a repository.

Creating a “deployment.yml” deployment file
The standard procedure in kubernetes is to create a deployment file as “deployment.yml” and include the content below by modifying the image configuration. The “env” parameters that represent the environment variables.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
apiVersion : apps/v1
kind: Deployment
metadata:
  name: ms-order
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ms-order
  template:
    metadata:
      labels:
        app: ms-order
    spec:
      containers:
        - name: ms-order
          image: ms-order:latest
          imagePullPolicy: Always
          readinessProbe:  # Disponivel para receber trafego
            httpGet:
              path: /healthcheck #/healthz  # Endpoint que a sonda de prontidão deve verificar
              port: 80  # Porta em que o contêiner está escutando
            initialDelaySeconds: 30  # Tempo de espera inicial antes de iniciar as verificações
            periodSeconds: 10  # Frequência de verificação da prontidão
            timeoutSeconds: 1
            successThreshold: 1
            failureThreshold: 3
          livenessProbe:  # Disponivel para receber trafego
            httpGet:
              path: /healthcheck #/healthz  # Endpoint que a sonda de prontidão deve verificar
              port: 80  # Porta em que o contêiner está escutando
            initialDelaySeconds: 40  # Tempo de espera inicial antes de iniciar as verificações
            periodSeconds: 10 # Frequência de verificação da prontidão
            timeoutSeconds: 1 # How long to wait for a response
            successThreshold: 1 # Criterio para considerar sucesso
            failureThreshold: 3 # Number of failures to tolerate before restarting
            terminationGracePeriodSeconds: 90 # Override pod-level termination
          ports:
          - containerPort: 80
          env:
           - name: Devprime_app
             value: "license=Put your license|||debug=false|||debugstate=false|||debugstream=false|||debugweb=false|||showenviromentvariables=false|||tenancy=[enable=false,type=Shared,cache=State2,gateway=https://localhost:5003]"
           - name: Devprime_observability
             value: "enable=true|||saveinfile=false|||hidedetails=false|||hidedatetime=false|||showhttperrors=400"
           - name: Devprime_web
             value: "url=http://*:80|||enable=true|||enableswagger=true|||postsuccess=201|||postfailure=500|||getsuccess=200|||getfailure=500|||patchsuccess=200|||patchfailure=500|||putsuccess=200|||putfailure=500|||deletesuccess=200|||deletefailure=500"
           - name: Devprime_stream1
             value: "alias=Stream1|||enable=true|||default=true|||streamtype=RabbitMQ|||hostname=rabbitmq.default.svc|||user=guest|||password=guest|||port=5672|||exchange=Devprime|||exchangetype=direct|||retry=3|||fallback=State1"
           - name: Devprime_state1
             value: "alias=State1|||dbtype=mongodb|||connection=mongodb://mongoadmin:LltF8Nx*yo@mongodb.default.svc:27017|||timeout=5|||retry=2|||dbname=ms-order|||isssl=true|||numberofattempts=4|||durationofbreak=45"
           - name: Devprime_Custom
             value: "stream.orderevents=OrderEvents"
           - name: Devprime_Security
             value: ""
           - name: Devprime_Services
             value: "enable=true|||retry=3|||circuitbreak=45|||timeout=10|||connections=[clientid=your client id,clientsecret=your client secret,granttype=client_credentials,name=Services1,tokenuri=your token uri]"        

          resources:
            requests:
             memory: "75Mi"
             cpu: "1m"
            limits:
             memory: "130Mi"
             cpu: "150m"

After creating the file, just run apply.
kubectl apply -f .\deployment.yml

Then just list the pods
kubectl get pods

NAME                        READY   STATUS    RESTARTS   AGE
ms-order-848cc9c6bf-sxkhr   1/1     Running   0          10m

Creating a “service.yml” service file
We’ll also create a service to get a public ip for that pod. Create the
“service.yml.”

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
apiVersion: v1
kind: Service
metadata:  
  name: ms-order
spec:
  selector:    
    app: ms-order
  type: LoadBalancer
  ports:  
  - name: http
    port: 80
    targetPort: 80

Run apply to create the service on kubernetes
kubectl apply -f .\service.yml

Then all you have to do is consult and get the ip address.

NAME         TYPE           CLUSTER-IP     EXTERNAL-IP    PORT(S)        AGE
ms-order     LoadBalancer   10.0.146.208   20.85.248.63   80:30549/TCP   35m

Now all you have to do is open your app on the IP address of the EXTERNAL-IP.

Exporting the configurations to Kubernetes
Devprime offers a command to export the initial settings from the Deployment and Servicces file. When running in the project folder, it does all the initial steps.
dp export kubernetes

Devprime Kubernetes Microservices

Considerations
We’ve demonstrated how simple it is to publish a Devprime microservices to Kubernetes.

  • Utilize a DevOps process and services such as AKS, GKE, EKS, OKE
  • Use vault services for credentials.
Last modified April 11, 2024 (cc33f7e6)