Skip to content

4. Writing the deployment scripts

Python Flask App Deployment Script

4.1. Create a Deployment

  • Create a deploy.yaml file. In terminal, run the following command:

    touch deploy.yaml
    

  • Open the deploy.yaml file in your favourite code editor.

    • Set the API version to v1 and the kind to Deployment.

      apiVersion: apps/v1
      kind: Deployment
      

    • Define the metadata for the deployment.

      metadata:
        name: sample-app
        labels:
          app: flask
      

    • Define the spec for the deployment. Add the container image name as a value for the image: key.

      spec:
        replicas: 1
        selector:
          matchLabels:
            app: flask
        template:
          metadata:
            labels:
              app: flask
          spec:
            containers:
            - name: sample-app
              image: <username>/sample-app:v1.0
              ports:
              - containerPort: 8080
      

  • Your deploy.yaml file should look something like this:

    deploy.yaml
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
    kind: Deployment
    metadata:
      name: sample-app
      labels:
        app: flask
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: flask
      template:
        metadata:
          labels:
            app: flask
        spec:
          containers:
          - name: sample-app
            image: <username>/sample-app:v1.0
            ports:
            - containerPort: 8080
    

  • Save the file.

4.2. Create a Service

  • Create a service.yaml file. In terminal, run the following command:

    touch service.yaml
    

  • Open the service.yaml file in your favourite code editor.

    • Set the API version to v1 and the kind to Service.

      apiVersion: v1
      kind: Service
      

    • Define the metadata for the deployment.

      metadata:
        name: my-flask-app-service
      

    • Define the spec for the deployment.

      spec:
        selector:
          app: flask
        ports:
        - protocol: TCP
          port: 8080
          targetPort: 8080
          nodePort: 32200
        type: NodePort
      

  • Your service.yaml file should look something like this:

    service.yaml
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    apiVersion: v1
    kind: Service
    metadata:
      name: my-flask-app-service
    spec:
      selector:
        app: flask
      ports:
      - protocol: TCP
        port: 8080
        targetPort: 8080
        nodePort: 32200
      type: NodePort
    

  • Save the file.

NodeJs Express App Deployment Script

4.1. Create a Deployment

  • Create a deploy.yaml file. In terminal, run the following command:

    touch deploy.yaml
    

  • Open the deploy.yaml file in your favourite code editor.

    • Set the API version to v1 and the kind to Deployment.

      apiVersion: apps/v1
      kind: Deployment
      

    • Define the metadata for the deployment.

      metadata:
        name: sample-app
        labels:
          app: express
      

    • Define the spec for the deployment. Add the container image name as a value for the image: key.

      spec:
        replicas: 1
        selector:
          matchLabels:
            app: express
        template:
          metadata:
            labels:
              app: express
          spec:
            containers:
            - name: sample-app
              image: <username>/sample-app:v1.0
              ports:
              - containerPort: 3000
      

  • Your deploy.yaml file should look something like this:

    deploy.yaml
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
    kind: Deployment
    metadata:
      name: sample-app
      labels:
        app: express
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: express
      template:
        metadata:
          labels:
            app: express
        spec:
          containers:
          - name: sample-app
            image: <username>/sample-app:v1.0
            ports:
            - containerPort: 3000
    

  • Save the file.

4.2. Create a Service

  • Create a service.yaml file. In terminal, run the following command:

    touch service.yaml
    

  • Open the service.yaml file in your favourite code editor.

    • Set the API version to v1 and the kind to Service.

      apiVersion: v1
      kind: Service
      

    • Define the metadata for the deployment.

      metadata:
        name: my-express-app-service
      

    • Define the spec for the deployment.

      spec:
        selector:
          app: express
        ports:
        - protocol: TCP
          port: 80
          targetPort: 3000
          nodePort: 32201
        type: NodePort
      

  • Your service.yaml file should look something like this:

    service.yaml
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    apiVersion: v1
    kind: Service
    metadata:
      name: my-express-app-service
    spec:
      selector:
        app: express
      ports:
      - protocol: TCP
        port: 80
        targetPort: 3000
        nodePort: 32201
      type: NodePort
    

  • Save the file.

Learning Materials

Back to top