Building an Environment for Python App with Kubernetes

To start building an environment for your Python app with Kubernetes, first log in to the DevZero dashboard. You should see an option in the Environments drawer to create a new environment (also referred to as a "template")

Create a new EnvironmentCreate a new Environment

Environment details #

  1. Provide some basic information about our environment. Add a Name (unique) and a Description.
  2. Select a Base Configuration from the dropdown list, which determines what type of machine your environments will be launched on.
  3. The last step (optional but highly encouraged): Pick a Color!
  4. Click the "Environment Details" heading to close the section and open the template editor.
New Environment detailsNew Environment details

**Note: You can safely ignore the Min Machines and Max Machines fields for now; we will use our global defaults to ensure your environments are provisioned as needed.

Creating the template #

To create a template in the DevZero dashboard for having pre-installed Kubernetes dependencies and other configurations.

Software Policies #

Software policies are a list of required package installations for the development environment.

  • Package installations that require apt-get, such as: `sudo apt-get install cmake`, can be added under software policy.
softwarepolicy:
- packagename: awscli 
- packagename: build-essential 
- packagename: cmake 
- packagename: vim-nox 
- packagename: python3-dev
- packagename: ripgrep

Repository Policies #

  • Repo policies are a list of repositories the user needs to be cloned in the development environment.

Below is an example public repository with a hello world python server.

repopolicy:
- path: /home/devzero/projects
  repourl: https://github.com/devzero-inc/devrel-python

Script Policies #

  • Script policies can have elaborate installations like Brew, Docker, executing a script from a cloned repo, etc.
  • Example script policy to install Docker
scriptpolicy:
- script: | # Install NPM yo
    apt-get update -y
    apt-get upgrade -y

    curl -fsSL https://deb.nodesource.com/setup_lts.x | bash -
    apt-get install -y nodejs
    npm install --global yarn

    # docker install instructions from: https://docs.docker.com/engine/install/ubuntu
    apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
    apt-get update -y
    apt-get install docker-ce docker-ce-cli containerd.io -y
    usermod -aG docker devzero

    # install docker compose- see: https://docs.docker.com/compose/install/
    curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
  • Example script policy to install brew
- script: | # install homebrew for linux
    NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> /home/devzero/.profile
    eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
  runas: devzero

Here, runas is the configuration allowing a user to run the script policy as either the "devzero" user or the "root" user. The root user is the default if runas is not specified.

  • Example script policy to install awscli, eksctl, and kubectl
- script: | # install awscli, eksctl and kubectl
    cd /home/devzero && curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    cd /home/devzero && unzip awscliv2.zip
    cd /home/devzero && sudo ./aws/install
    curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
    sudo mv /tmp/eksctl /usr/local/bin
    cd /home/devzero && curl -LO https://dl.k8s.io/release/v1.25.0/bin/linux/amd64/kubectl
    cd /home/devzero && sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
  • Example script policy for setting up "aws configure" and updating kubeconfig. The script will require to create AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, REGION and CLUSTER_NAME environment variables. (Refer to Template Environment Variable section below)
- script: | # configure aws cli
    aws --profile default configure set aws_access_key_id "$AWS_ACCESS_KEY_ID"
    aws --profile default configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
    aws eks --region $REGION update-kubeconfig --name $CLUSTER_NAME
  runas: devzero

Network Policies #

  • Use network policy to open ports of the development environment (box).
  • For example, to open port 6000 on the development box to expose the hello-world Python server, the network policy will look like the below:
networkpolicy:
  ports:
  - port: 6000
    protocol: http

Template Environment Variable #

  • To set up template-specific environment variables, click the square icon at the top of the template editing section.
Doc Image
  • Add new env variables by clicking Create New Variable.
Template Environment VariablesTemplate Environment Variables

The template environment variables can be accessed in the template editor using a variable name with $, for example, $REGION.

User Environment Variable #

  • Along with template environment variables, you can also set up User Environment variables, click the gear icon, ⚙, in the upper right and navigate to User Environment Variables or follow this Link.

Example of adding user environment variable

Adding Environment VariablesAdding Environment Variables

Run Python Server #

  • Now that you are connected to the cluster, we can create a hello-world service in the cluster.
  • The example Python repo has a simple service, pod, and deployment created using the deployment.yaml file. Once deployed, the Python server prints Hello from Python!
  • You can also run the deployment.yaml file in the script policy and forward the port so we can test the Python server on the local browser
  • Example of script policy
- script: | #run python server in cluster 
    cd /home/devzero/projects/devrel-python
    /usr/local/bin/kubectl apply -f ./kubernetes/deployment.yaml -n $NAMESPACE
  runas: devzero
  • If you want to test the Python server, you can either connect to the development machine and exec into the pod OR port-forward the Python server to port 6000 (indicated in the network policy above)
- script: | # port forward hello-python pod to 6000, to access via share-link
    /usr/local/bin/kubectl port-forward $(kubectl get pods -n $NAMESPACE --selector=app=hello-python -o jsonpath='{.items[0].metadata.name}') -n $NAMESPACE 6000:5000 >/dev/null 2>&1 &
  runas: devzero

You can create a share link to share a service running on port 6000 over the internet with your colleague.

  • Save the template by clicking on Save Template.
Saving templateSaving template
  • After template creation, click on Launch to create a new development environment (box)
Doc Image
  • On clicking Launch, a new environment will start building. This will take a few minutes to set up the template in a new development machine will take a few minutes.
Environment buildingEnvironment building
  • Once the environment is ready, the Python server should run on port 6000, as defined in the script policy.
  • Once the environment is built, click on the three dots and select Share.
Share-linkShare-link
  • Select port 6000 from the dropdown and click Create Link.
Create Share linkCreate Share link
  • Copy the Share Link and paste it into the browser. This will hit the Python server base route and print Hello from Python! in the browser.
Doc Image

PreviousBuild your First Environment
NextShare Your Environment