factorio

by logansaso

Helm chartv1.3.1App 2.0.8Verified publisher

A Helm chart for running factorio in kubernetes

About this chart

factorio-server-charts

factorio-server-charts - A Helm chart for running factorio in kubernetes

Introduction

This chart deploys Factorio on a Kubernetes cluster using the Helm package manager.

Prerequisites

  • An open NodePort or a UDP capable LoadBalancer if playing publicly.
  • Networking knowledge to route the traffic to the appropriate port.

Background

This is something of a passion project for both learning kubernetes and because kubernetes is awesome. Because of this, I have only confirm this working on version 1.1.0 of Factorio, but I am sure it will work just fine on old versions as long as the schema doesn't change to much on the Secrets settings.

However, with the way this is implemented if you want to make it backwards compatible, you can update the appropriate sections in the values.yaml file to have the items added or removed to the json files that are required by factorio. Server-settings, map-gen-settings, etc.

I did run into one issue on my setup, and I feel like it will likely come up again if people follow along. The Factorio server is defaulting to run on port 30000 instead of 34197 because the default nginx port range for ingress does not have 34197 within the normal port range. This shows the line of code that specifically is causing the issue inside kubernetes :) https://github.com/kubernetes/kubernetes/blob/59876df736c41093363f4c198aeec05e29c9c902/cmd/kube-apiserver/app/server.go#L197

Releases

Releases are published using the official helm release action in github. https://github.com/helm/chart-releaser-action

Quick Start

  • Create a values.yaml file
  • Set the following options
rcon:
  external: false

nodeSelector:
  kubernetes.io/hostname: "<name of your node>" # this makes it easier for firewall rules and makes sure things dont change

# making sure the game doesnt auto update on redeploy
# https://hub.docker.com/r/factoriotools/factorio/
image:
  tag: "<set me to the appropriate docker tag>"


persistence:
  enabled: true

factorioServer:
  save_name: "<set me to your name>"

#needed if playing online
admin_list: [ ] # put your admins based on factorio names
# - "john_doe"
# - "jane_doe"

white_list: [ ] # put the people you want to play with you based on factorio names
# - "john_doe"
# - "jane_doe"

server_settings:
  name: Factorio-2022-01-kubernetes
  tags:
    - modded
  visibility:
    public: true
  require_user_verification: true

account:
  # Existing secret containing a valid factorio.com username and either a password or a token
  # Example:
  #
  # apiVersion: v1
  # kind: Secret
  # metadata:
  #   name: account-secret
  # data:
  #   username: <username in base64>
  #   password: <password in base64>
  #
  # accountSecret: account-secret
  accountSecret: ''
  # Your factorio.com login credentials. Required for games with visibility public
  username: ''
  password: ''
  # Authentication token. May be used instead of password above.
  token: ''

serverPassword:
  # password required to log into the factorio server
  game_password: ''
  # Existing Secret containing a `game_password` data entry
  passwordSecret: ''

Importing a save file

:warning: Importing a save file will OVERWRITE THE SERVER SAVEFILE with the name specified in factorioServer.save_name. Import with caution!

Importing by URL

To import your save file from a URL, set import_save.source_url to a direct-download link for your savegame. By default, the file will be downloaded and imported only once.

If, on pod intialization, you wish to re-import the file every time the contents of the savegame change, set import_save.reimport_on_change to true. :warning: If the savegame at the source url changes, this will overwrite the server save with that file. Use with caution!

If you wish to reimport the save file every time the pod reinitializes, regardless of changes, set import_save.reimport_every_time to true. This could be useful for demos or testing. :warning: This will overwrite the server savegame every time the pod reinitializes. Use with caution!

Manual Import

To import an existing save file, start/restart the pod at least once. This will create the import folder structure.

Now, copy the savegame you wish to import to the /factorio/save-importer/import/<existing_savegame_name>.zip on the running pod using whatever mechanism you prefer. To do this with kubectl:

kubectl cp ./my_existing_savegame.zip <namespace>/<pod_name>:/factorio/save-importer/import

Restart the pod again to import your save file.

Installing mods

To Install mods, change mods.enabled to true, add any offical mods by their URL name under the mods.portal section, and any unofficial mods in the mods.unofficialsection.

mods:
  enabled: true
  # in order to use the mods portal you will need to specify the username and token in the server_settings.
  # name is determined by the url, it will be the last part of the url, not the title of the mod.
  portal:
    - Krastorio2
    - StorageTank2_Updated
    - early-robots
  # unofficial section is meant to just allow you to download and place folders into the mods folder.
  # we will not check version compatibility automatically with these downloads.
  # you can encounter an error if the file names dont match what the mod is expecting for example
  #Error Util.cpp:83: Failed to load mod "Squeak-Through": Filename of mod
  # /factorio/mods/Squeak-Through.zip doesn't match the expected Squeak Through_1.8.2.zip (case sensitive!)
  unofficial:
    - url: "https://github.com/Suprcheese/Squeak-Through/archive/refs/tags/1.8.2.zip"
      name: "Squeak Through_1.8.2.zip"

If the Factorio server doesn't start, check that the logs don't have an error with the mods. They are pretty verbose.

Space Exploration and other overhaul mods

It is possible that your mod may extend the default settings. As a result, your server might not start properly and instead throw an error.

While we expose all the default settings, we would not be able to determine what is needed if it is absent. To add new settings you would need to convert the desired json file into a yaml struct as we are mounting all configurations as a config map into the container.

the issue with Space Exploration, as an example, was that the autoplace_controls by default is an empty array, and thus we were not able to start the instance as there were no settings for them to evaluate. Additionally, they are also modifying some of the other settings. These changes implement the default recommended settings, available in the UI and were stored in a file at ...Factorio\mods\space-exploration\shared.lua. In the lua file, it has 2 sections, basic_settings and advanced_settings which correspond to settings in map-gen-settings.json and map-settings.json respecively.

At the time of writing, to make these changes you would need to add this to your values.yaml file to override their default values in the chart.

NOTE: These settings may change and it is important to check with the mod maintainer/community to check the recommended settings.

map_settings:
  pollution:
    enemy_attack_pollution_consumption_modifier: 0.5
  enemy_evolution:
    time_factor: 0.0000005
    destroy_factor: 0.0005
    pollution_factor: 0.00000025

map_gen_settings:
  water: 1.5
  starting_area: 2
  autoplace_controls: 
    hot:
      size: 0.5
    cold:
      size: 0.5
  property_expression_names:
    control-setting:moisture:bias: '0.05'
    control-setting:aux:bias: '-0.35'

More information about the debugging process for the space exploration mod can be read on Issue 24.

If you do run into any issues with mods, I will try to work with you on finding the right settings and document them as well.

Parameters

Kubernetes Parameters

NameDescriptionValue
replicaCountNumber of replicas to create (only 1 is supported)1
nodeSelectorNode labels for pod assignment{}
resources.requests.cpuThe requested cpu for the Factorio containers500m
resources.requests.memoryThe requested memory for the Factorio containers512Mi
resources.limitsIf you like to limit Factorio resources{}
strategy.typeStrategy used to replace old podsRecreate
tolerationsTolerations for pod assignment[]
affinityAffinity rules for pod assignment{}

File Ownership Parameters

NameDescriptionValue
puidUser ID that owns /factorio and that the game process runs as845
pgidGroup ID that owns /factorio and that the game process runs as845
podSecurityContextPod level security context, i.e. fsGroup{}
securityContextSecurity context for the Factorio container{runAsUser: 0}
initContainerSecurityContextSecurity context for the init containers; they chown the data dir so they should be root{runAsUser: 0}

Deploy factorio with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has factorio on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartfactorio is pinned at v1.3.1 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply