feat(k8s): mvp

This commit is contained in:
Xiphoseer
2024-01-02 00:34:43 +01:00
parent 1941679d27
commit 166a283c70
16 changed files with 642 additions and 5 deletions

50
k8s/DLU.Dockerfile Normal file
View File

@@ -0,0 +1,50 @@
FROM gcc:12 AS build
RUN --mount=type=cache,id=build-apt-cache,target=/var/cache/apt \
apt update && \
apt install -y libssl-dev libcurl4-openssl-dev uncrustify git cmake && \
rm -rf /var/lib/apt/lists/*
WORKDIR /work
RUN git clone https://libwebsockets.org/repo/libwebsockets --depth 1 --branch v4.2-stable
RUN git clone https://github.com/yaml/libyaml --depth 1 --branch release/0.2.5
WORKDIR /work/libwebsockets/build
RUN cmake -DLWS_WITHOUT_TESTAPPS=ON -DLWS_WITHOUT_TEST_SERVER=ON -DLWS_WITHOUT_TEST_SERVER_EXTPOLL=ON \
-DLWS_WITHOUT_TEST_PING=ON -DLWS_WITHOUT_TEST_CLIENT=ON -DCMAKE_C_FLAGS="-fpic" -DCMAKE_INSTALL_PREFIX=/usr/local ..
RUN make install
WORKDIR /work/libyaml/build
RUN cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=ON ..
RUN make install
COPY ../thirdparty/kubernetes-client-c/kubernetes /work/kubernetes-client-c/
WORKDIR /work/kubernetes-client-c/build
RUN cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
RUN make install
FROM debian:12
RUN --mount=type=cache,id=build-apt-cache,target=/var/cache/apt \
apt update && \
apt install -y libssl3 libcurl4 && \
rm -rf /var/lib/apt/lists/*
WORKDIR /app
COPY build/mariadbcpp/src/mariadb_connector_cpp-build/libmariadb/libmariadb/libmariadb.so.3 /usr/local/lib
COPY --from=build /usr/local/lib/libkubernetes.so /usr/local/lib
COPY --from=build /usr/local/lib/libyaml.so /usr/local/lib
COPY --from=build /usr/local/lib/libwebsockets.so.18 /usr/local/lib
COPY --from=build /usr/local/lib/libwebsockets.so /usr/local/lib
COPY build/libmariadbcpp.so /usr/local/lib
RUN ldconfig
COPY build/MasterServerK8s /app/MasterServerK8s
COPY build/MasterServer /app/MasterServer
COPY build/AuthServer /app/AuthServer
COPY build/ChatServer /app/ChatServer
COPY build/WorldServer /app/WorldServer
COPY build/migrations /app/migrations
COPY build/navmeshes /app/navmeshes
ENTRYPOINT ["/app/MasterServer"]

26
k8s/README.md Normal file
View File

@@ -0,0 +1,26 @@
## Kubernetes Setup
Debian
```
minikube start --driver kvm2
minikube mount /path/to/client:/luclient
```
or
```
minikube start --mount --mount-options="ro" --mount-string="/path/to/client:/luclient"
```
Fish Shell
```
eval $(minikube -p minikube docker-env)
```
To Build
```
docker build -t ghcr.io/darkflameuniverse/darkflameserver -f ./k8s/DLU.Dockerfile .
```
Install
```
kubectl apply -k k8s
```

4
k8s/common.env Normal file
View File

@@ -0,0 +1,4 @@
EXTERNAL_IP=192.168.39.84
MYSQL_HOST=mariadb-service
MASTER_IP=master-service
DLU_CONFIG_DIR=/app/config

94
k8s/darkflame.yml Normal file
View File

@@ -0,0 +1,94 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: master-server
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-creator
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["create", "update", "patch", "get", "watch", "list", "delete"]
- apiGroups: [""]
resources: ["podtemplates"]
verbs: ["get", "watch", "list"]
- apiGroups: ["apps"]
resources: ["daemonsets", "deployments"]
verbs: ["create", "update", "patch", "get", "watch", "list", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: master-server-pod-creator
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: pod-creator
subjects:
- kind: ServiceAccount
name: master-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: master-server-deployment
labels:
app: master-server
spec:
replicas: 1
selector:
matchLabels:
app: master-server
template:
metadata:
labels:
app: master-server
spec:
serviceAccountName: master-server
volumes:
- name: game-config
configMap:
name: game-config
- name: luclient
hostPath:
path: /luclient
- name: res-server
hostPath:
path: /resServer
containers:
- name: master-server
image: ghcr.io/darkflameuniverse/darkflameserver
imagePullPolicy: IfNotPresent
command: ["/app/MasterServerK8s"]
ports:
- containerPort: 2000
protocol: UDP
name: master
envFrom:
- configMapRef:
name: env-common
- secretRef:
name: db-secret
prefix: MYSQL_
volumeMounts:
- mountPath: "/app/res"
name: luclient
- mountPath: "/app/resServer"
name: res-server
- mountPath: "/app/config"
name: game-config
---
apiVersion: v1
kind: Service
metadata:
name: master-service
spec:
selector:
app: master-server
ports:
- name: master
protocol: UDP
port: 2000
targetPort: master

17
k8s/kustomization.yml Normal file
View File

@@ -0,0 +1,17 @@
resources:
- mariadb.yml
- ../resources
- ../vanity
- pod-template.yml
- darkflame.yml
secretGenerator:
- name: db-secret
literals:
- DATABASE=darkflame
- USER=darkflame # for MariaDB
- USERNAME=darkflame # for DLU
- PASSWORD=gjN@5#TTE&H&Yees5Q3a%tTC
configMapGenerator:
- name: env-common
envs:
- common.env

74
k8s/mariadb.yml Normal file
View File

@@ -0,0 +1,74 @@
apiVersion: v1
kind: PersistentVolume
metadata:
name: pv0001
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
capacity:
storage: 5Gi
hostPath:
path: /data/pv0001/
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: db-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mariadb-deployment
labels:
app: mariadb
spec:
replicas: 1
selector:
matchLabels:
app: mariadb
template:
metadata:
labels:
app: mariadb
spec:
volumes:
- name: db-pv-storage
persistentVolumeClaim:
claimName: db-pv-claim
containers:
- name: mariadb
image: mariadb:11
ports:
- containerPort: 3306
name: tcp-db-svc
envFrom:
- secretRef:
name: db-secret
prefix: MARIADB_
env:
- name: MARIADB_RANDOM_ROOT_PASSWORD
value: "1"
volumeMounts:
- mountPath: "/var/lib/mysql"
name: db-pv-storage
---
apiVersion: v1
kind: Service
metadata:
name: mariadb-service
spec:
selector:
app: mariadb
ports:
- name: mariadb
protocol: TCP
port: 3306
targetPort: tcp-db-svc

50
k8s/pod-template.yml Normal file
View File

@@ -0,0 +1,50 @@
apiVersion: v1
kind: PodTemplate
metadata:
name: pod-template
template:
metadata:
labels:
app: darkflame-server
spec:
restartPolicy: OnFailure
volumes:
- name: game-config
configMap:
name: game-config
- name: vanity
configMap:
name: vanity
- name: luclient
hostPath:
path: /luclient
- name: res-server
hostPath:
path: /resServer
containers:
- name: darkflame-server
image: ghcr.io/darkflameuniverse/darkflameserver
imagePullPolicy: IfNotPresent
ports:
- containerPort: 3000
protocol: UDP
name: raknet
envFrom:
- configMapRef:
name: env-common
- secretRef:
name: db-secret
prefix: MYSQL_
volumeMounts:
- mountPath: "/app/res"
readOnly: true
name: luclient
- mountPath: "/app/resServer"
readOnly: true
name: res-server
- mountPath: "/app/config"
readOnly: true
name: game-config
- mountPath: "/app/vanity"
readOnly: true
name: vanity