Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Daily Reboot Cronjob (K8s) #206

Merged
merged 22 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions k8s/cronjob-reboot/cm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: restart-deployment-configmap
namespace: palworld
NotaSF marked this conversation as resolved.
Show resolved Hide resolved
data:
restart-deployment.sh: |
#!/bin/bash
cont=$(kubectl -n palworld get pods -o=jsonpath='{.items[?(@.metadata.labels.app=="palworld-server")].metadata.name}')
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Saving_Server_Data..."
kubectl exec -n palworld -i pod/$cont rcon-cli save
sleep 30
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Backing_up_Server_Data..."
kubectl exec -n palworld -i pod/$cont backup
sleep 30
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Backup_Complete!_Rebooting_in_30_seconds..."
sleep 15
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_15_seconds..."
sleep 5
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_10_seconds..."
sleep 1
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_9_seconds..."
sleep 1
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_8_seconds..."
sleep 1
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_7_seconds..."
sleep 1
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_6_seconds..."
sleep 1
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_5_seconds..."
sleep 1
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_4_seconds..."
sleep 1
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_3_seconds..."
sleep 1
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_2_seconds..."
sleep 1
kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Rebooting_in_1_seconds..."
sleep 1
kubectl exec -n palworld -i pod/$cont rcon-cli "Shutdown 1"
sleep 30
kubectl -n palworld rollout restart deployment/palworld-server
NotaSF marked this conversation as resolved.
Show resolved Hide resolved

35 changes: 35 additions & 0 deletions k8s/cronjob-reboot/cronjob.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: batch/v1
kind: CronJob
metadata:
name: restart-deployment
namespace: palworld
NotaSF marked this conversation as resolved.
Show resolved Hide resolved
spec:
concurrencyPolicy: Forbid
schedule: '30 9 * * *'
jobTemplate:
spec:
backoffLimit: 1
activeDeadlineSeconds: 600
template:
spec:
serviceAccountName: restart-deploy
restartPolicy: Never
containers:
- name: kubectl
image: bitnami/kubectl
command:
- /bin/sh
- -c
- /restart-script/restart-deployment.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you even need a CM with the shell script? what about inline?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good point, I'm not sure actually. Pardon my ignorance, I'm a little new to this - when you say inline, do you mean taking the contents of the shell script and making it a one-liner instead of using a CM and mounting it as a volume?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi sorry for the confusion. What i mean is instead of having a configmap mounted you could also put that script in that command as an arg like

command:
  - /bin/sh
  - -c
args:
  - |
    cont=$(kubectl -n palworld get pods -o=jsonpath='{.items[?(@.metadata.labels.app=="palworld-server")].metadata.name}')
    kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Saving_Server_Data..."
    kubectl exec -n palworld -i pod/$cont rcon-cli save
    sleep 30
    kubectl exec -n palworld -i pod/$cont rcon-cli "Broadcast Backing_up_Server_Data..."
    kubectl exec -n palworld -i pod/$cont backup
 ...

volumeMounts:
- name: restart-script
mountPath: "/restart-script"
readOnly: true
volumes:
- name: restart-script
configMap:
name: restart-deployment-configmap
defaultMode: 0777
items:
- key: "restart-deployment.sh"
path: "restart-deployment.sh"
12 changes: 12 additions & 0 deletions k8s/cronjob-reboot/role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: restart-deployment
namespace: palworld
NotaSF marked this conversation as resolved.
Show resolved Hide resolved
rules:
- apiGroups: ["apps", "extensions"]
resources: ["deployments", "pods"]
verbs: ["get", "patch", "list", "watch"]
- apiGroups: [""]
resources: ["pods/exec", "pods"]
verbs: ["get", "list", "create"]
13 changes: 13 additions & 0 deletions k8s/cronjob-reboot/rolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: restart-deployment
namespace: palworld
NotaSF marked this conversation as resolved.
Show resolved Hide resolved
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: restart-deployment
subjects:
- kind: ServiceAccount
name: restart-deploy
namespace: palworld
5 changes: 5 additions & 0 deletions k8s/cronjob-reboot/serviceaccount.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: restart-deploy
namespace: palworld
NotaSF marked this conversation as resolved.
Show resolved Hide resolved