Skip to content

Instantly share code, notes, and snippets.

@nanderson94
Last active May 7, 2023 16:15
Show Gist options
  • Save nanderson94/e6a020d54dc888444ad3d28f51165aa2 to your computer and use it in GitHub Desktop.
Save nanderson94/e6a020d54dc888444ad3d28f51165aa2 to your computer and use it in GitHub Desktop.

The launch template requests Spot instances with at least 1 vCPU and 1GiB of memory up to 4x that. This will often select the "best value" instances which are capable of running the work, but not wasteful.

The cloud-config is embedded within the launch template, base64 encoded, sets up K3s, and applies my manifest (my user-name is embedded, so you probably want to replace https://cdn.n0t.dev/public/aws-spot.yaml with something of your own). Additionally, it installs stern which is just a handy way to get logs out of the cluster.

You'll need to do the AWS basics such as creating a keypair and configuring your security group to allow SSH in order to access your instance. But once on it, you can:

Login to your instance:

chmod 0600 ~/your-aws-key.pem
ssh -i ~/your-aws-key.pem ubuntu@127.0.0.1

Scale your concurrency:

kubectl scale -n archiveteam --replicas 8 deployments/imgur

Restart your containers, which also pulls the updated image:

kubectl rollout -n archvieteam restart deployments/imgur

Tail your logs, where stern comes in handy:

stern --kubeconfig /etc/rancher/k3s/k3s.yaml -n archiveteam --tail 100 deployment/imgur
{
"LaunchTemplateId": "lt-014bb6479c5e7b2a8",
"LaunchTemplateName": "imgur-grab",
"VersionNumber": 3,
"CreateTime": "2023-05-07T04:43:28.000Z",
"CreatedBy": "arn:aws:iam::791336602737:root",
"DefaultVersion": true,
"LaunchTemplateData": {
"BlockDeviceMappings": [
{
"DeviceName": "/dev/sda1",
"Ebs": {
"Encrypted": false,
"DeleteOnTermination": true,
"SnapshotId": "snap-02eb3fc532839ce94",
"VolumeSize": 16,
"VolumeType": "gp2"
}
}
],
"ImageId": "ami-007855ac798b5175e",
"KeyName": "nic-aws",
"Monitoring": {
"Enabled": false
},
"Placement": {
"Tenancy": "default"
},
"InstanceInitiatedShutdownBehavior": "terminate",
"UserData": "I2Nsb3VkLWNvbmZpZwpwYWNrYWdlX3VwZGF0ZTogdHJ1ZQpwYWNrYWdlX3VwZ3JhZGU6IHRydWUKcnVuY21kOgotIGN1cmwgLXNmTCBodHRwczovL2dldC5rM3MuaW8gLW8gL3Vzci9sb2NhbC9iaW4vazNzX2luc3RhbGwuc2gKLSBzaCAvdXNyL2xvY2FsL2Jpbi9rM3NfaW5zdGFsbC5zaAotIC91c3IvbG9jYWwvYmluL2t1YmVjdGwgYXBwbHkgLWYgaHR0cHM6Ly9zMy5uaWMtYS5uZXQvcHVibGljL2F3cy1zcG90LnlhbWwKLSBta2RpciAtcCAvdG1wL3N0ZXJuCi0gY3VybCAtc2ZMIGh0dHBzOi8vZ2l0aHViLmNvbS9zdGVybi9zdGVybi9yZWxlYXNlcy9kb3dubG9hZC92MS4yNS4wL3N0ZXJuXzEuMjUuMF9saW51eF9hbWQ2NC50YXIuZ3ogLW8gL3RtcC9zdGVybi9zdGVybl8xLjI1LjBfbGludXhfYW1kNjQudGFyLmd6Ci0gdGFyIHh2ZiAvdG1wL3N0ZXJuL3N0ZXJuXzEuMjUuMF9saW51eF9hbWQ2NC50YXIuZ3ogLUMgL3RtcC9zdGVybgotIG12IC90bXAvc3Rlcm4vc3Rlcm4gL3Vzci9sb2NhbC9iaW4vCi0gbWtkaXIgLXAgL2hvbWUvdWJ1bnR1Ly5rdWJlCi0gY3AgL2V0Yy9yYW5jaGVyL2szcy9rM3MueWFtbCAvaG9tZS91YnVudHUvLmt1YmUvY29uZmlnCi0gY2hvd24gdWJ1bnR1IC9ob21lL3VidW50dS8ua3ViZS9jb25maWcKLSBjaG1vZCAwNjQ0IC9ldGMvcmFuY2hlci9rM3MvazNzLnlhbWwKLSBjaG1vZCAwNjAwIC9ob21lL3VidW50dS8ua3ViZS9jb25maWc=",
"SecurityGroupIds": [
"sg-0b436f85d66d00e56"
],
"InstanceMarketOptions": {
"MarketType": "spot",
"SpotOptions": {
"MaxPrice": "0.05",
"InstanceInterruptionBehavior": "terminate"
}
},
"InstanceRequirements": {
"VCpuCount": {
"Min": 1,
"Max": 4
},
"MemoryMiB": {
"Min": 1024,
"Max": 4096
}
}
}
#cloud-config
package_update: true
package_upgrade: true
runcmd:
- curl -sfL https://get.k3s.io -o /usr/local/bin/k3s_install.sh
- sh /usr/local/bin/k3s_install.sh
- /usr/local/bin/kubectl apply -f https://cdn.n0t.dev/public/aws-spot.yaml
- mkdir -p /tmp/stern
- curl -sfL https://github.com/stern/stern/releases/download/v1.25.0/stern_1.25.0_linux_amd64.tar.gz -o /tmp/stern/stern_1.25.0_linux_amd64.tar.gz
- tar xvf /tmp/stern/stern_1.25.0_linux_amd64.tar.gz -C /tmp/stern
- mv /tmp/stern/stern /usr/local/bin/
- mkdir -p /home/ubuntu/.kube
- cp /etc/rancher/k3s/k3s.yaml /home/ubuntu/.kube/config
- chown ubuntu /home/ubuntu/.kube/config
- chmod 0644 /etc/rancher/k3s/k3s.yaml
- chmod 0600 /home/ubuntu/.kube/config
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment