Skip to content

Instantly share code, notes, and snippets.

@vbatts
Last active April 21, 2024 20:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vbatts/705c2542df39495319c8601c01b658e8 to your computer and use it in GitHub Desktop.
Save vbatts/705c2542df39495319c8601c01b658e8 to your computer and use it in GitHub Desktop.
flatcar+k3s cloud-init
#cloud-config
coreos:
units:
- name: install-opt-dir.service
content: |
[Unit]
Description=Install /opt directories
ConditionPathIsDirectory=!/opt/libexec
ConditionPathIsDirectory=!/opt/libexec.wd
[Service]
Type=oneshot
ExecStart=/usr/bin/mkdir -p /opt/libexec /opt/libexec.wd
[Install]
WantedBy=multi-user.target
- name: usr-libexec.mount
content: |
[Unit]
Description=Allow k8s CNI plugins to be installed
Before=local-fs.target
Requires=install-opt-dir.service
ConditionPathExists=/opt/libexec
ConditionPathExists=/opt/libexec.wd
[Mount]
Type=overlay
What=overlay
Where=/usr/libexec
Options=lowerdir=/usr/libexec,upperdir=/opt/libexec,workdir=/opt/libexec.wd
[Install]
WantedBy=local-fs.target
- name: install-k3s.service
command: start
content: |
[Unit]
Description=Install k3s
Requires=usr-libexec.mount
ConditionPathExists=!/etc/.k3s-installed
[Service]
Type=oneshot
Environment=INSTALL_K3S_BIN_DIR=/opt/bin INSTALL_K3S_SELINUX_WARN=true INSTALL_K3S_CHANNEL=latest
ExecStart=/usr/bin/mkdir -p /opt/bin
ExecStart=/usr/bin/curl -sfSL -o /tmp/k3s.sh https://get.k3s.io
ExecStart=/usr/bin/chmod +x /tmp/k3s.sh
ExecStart=/tmp/k3s.sh
ExecStart=/usr/bin/touch /etc/.k3s-installed
[Install]
WantedBy=multi-user.target
ssh_authorized_keys:
- <SSH_KEY>
hostname: flatcar-k3s-00
@frezbo
Copy link

frezbo commented Nov 16, 2020

https://gist.github.com/vbatts/705c2542df39495319c8601c01b658e8/#file-cloud-config-yaml-L14 missing an install target here. I'd have to add:

[Install]
        WantedBy=multi-user.target

for the install-opt-dir.service for the service to startup

@vbatts
Copy link
Author

vbatts commented Nov 16, 2020

@frezbo oh nice. Since it is a oneshot that is pulled in by dependency of usr-libexec.mount I didn't find that [Install] was needed. Did this config not work for you without that section?

@frezbo
Copy link

frezbo commented Nov 16, 2020

@vbatts I was using the ignition config and it failed to create the unit. If you want I can spin a vm and show the logs with [Install] section missing. This was with FlatCar Linux on DigitalOcean.

IgnitionConfig:

{
  "ignition": {
    "version": "2.1.0"
  },
  "systemd": {
    "units": [
      {
        "name": "install-opt-dir.service",
        "enabled": true,
        "contents": "[Unit]\n\tDescription=Install /opt directories\n\tConditionPathIsDirectory=!/opt/libexec\n\tConditionPathIsDirectory=!/opt/libexec.wd\n\n[Service]\n\tType=oneshot\n\tExecStart=/usr/bin/mkdir -p /opt/libexec /opt/libexec.wd\n\"
      }
    ]
  }
}

@vbatts
Copy link
Author

vbatts commented Dec 1, 2020

@frezbo 👍 no worries. And thanks for the ignition example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment