update create in k8s

pull/2919/head
Monet Lee 10 months ago
parent 2d5eb5197b
commit f3f0fd022e

@ -30,18 +30,6 @@ spec:
- containerPort: 9092 # PLAINTEXT
- containerPort: 9093 # CONTROLLER
- containerPort: 9094 # EXTERNAL
# command:
# - /bin/bash
# - "-c"
# - |
# # /opt/bitnami/scripts/kafka/run.sh & /opt/bitnami/kafka/create-topic.sh; wait
# # /opt/bitnami/scripts/kafka/entrypoint.sh && /opt/bitnami/scripts/kafka/setup.sh && /opt/bitnami/scripts/kafka/run.sh
# & while ! echo > /dev/tcp/localhost/9092; do
# echo "Waiting for Kafka to start..."
# sleep 5
# done
# /opt/bitnami/kafka/create-topic.sh
# tail -f /dev/null
env:
- name: TZ
value: "Asia/Shanghai"
@ -59,34 +47,18 @@ spec:
value: "CONTROLLER:PLAINTEXT,EXTERNAL:PLAINTEXT,PLAINTEXT:PLAINTEXT"
- name: KAFKA_CFG_CONTROLLER_LISTENER_NAMES
value: "CONTROLLER"
- name: KAFKA_NUM_PARTITIONS
value: "8"
- name: KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE
value: "true"
volumeMounts:
- name: kafka-data
mountPath: /bitnami/kafka
- name: kafka-scripts
mountPath: /opt/bitnami/kafka/create-topic.sh
subPath: create-topic.sh
- name: create-topics
image: bitnami/kafka:3.5.1
command:
- /bin/bash
- "-c"
- |
/opt/bitnami/kafka/create-topic.sh && \
tail -f /dev/null
volumeMounts:
- name: kafka-scripts
mountPath: /opt/bitnami/kafka/create-topic.sh
subPath: create-topic.sh
- name: kafka-data
mountPath: /bitnami/kafka
volumes:
- name: kafka-scripts
configMap:
name: openim-config
defaultMode: 0755
items:
- key: create-topic.sh
path: create-topic.sh
- name: kafka-data
persistentVolumeClaim:
claimName: kafka-pvc

@ -15,12 +15,35 @@ spec:
spec:
containers:
- name: mongo
image: mongo:6.0.2
image: mongo:7.0
command:
[
"/bin/bash",
"-c",
"docker-entrypoint.sh mongod --wiredTigerCacheSizeGB 1 --auth & sleep 15; /scripts/mongo-init.sh; wait",
'
docker-entrypoint.sh mongod --wiredTigerCacheSizeGB $wiredTigerCacheSizeGB --auth &
until mongosh -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --authenticationDatabase admin --eval "db.runCommand({ ping: 1 })" &>/dev/null; do
echo "Waiting for MongoDB to start..."
sleep 1
done &&
mongosh -u $MONGO_INITDB_ROOT_USERNAME -p $MONGO_INITDB_ROOT_PASSWORD --authenticationDatabase admin --eval "
db = db.getSiblingDB(\"$MONGO_INITDB_DATABASE\");
if (!db.getUser(\"$MONGO_OPENIM_USERNAME\")) {
db.createUser({
user: \"$MONGO_OPENIM_USERNAME\",
pwd: \"$MONGO_OPENIM_PASSWORD\",
roles: [{role: \"readWrite\", db: \"$MONGO_INITDB_DATABASE\"}]
});
print(\"User created successfully: \");
print(\"Username: $MONGO_OPENIM_USERNAME\");
print(\"Password: $MONGO_OPENIM_PASSWORD\");
print(\"Database: $MONGO_INITDB_DATABASE\");
} else {
print(\"User already exists in database: $MONGO_INITDB_DATABASE, Username: $MONGO_OPENIM_USERNAME\");
}
" &&
tail -f /dev/null
'
]
ports:
- containerPort: 27017
@ -53,18 +76,8 @@ spec:
volumeMounts:
- name: mongo-storage
mountPath: /data/db
- name: script-volume
mountPath: /scripts
# subPath: mongo-init.sh
volumes:
- name: script-volume
configMap:
name: openim-config
items:
- key: mongo-init.sh
path: mongo-init.sh
mode: 0755
- name: mongo-storage
persistentVolumeClaim:
claimName: mongo-pvc

@ -811,112 +811,6 @@ data:
labels:
namespace: default
mongo-init.sh: |
echo "before running mongosh..."
mongosh <<EOF
var maxRetries = 300;
var connected = false;
var rootUsername = '$MONGO_INITDB_ROOT_USERNAME';
var rootPassword = '$MONGO_INITDB_ROOT_PASSWORD';
var dbName = '$MONGO_INITDB_DATABASE';
var openimUsername = '$MONGO_OPENIM_USERNAME';
var openimPassword = '$MONGO_OPENIM_PASSWORD';
print('Starting MongoDB connection script...');
print('Root username: ' + rootUsername);
print('Target database: ' + dbName);
print('OpenIM user to create: ' + openimUsername);
while (!connected && maxRetries > 0) {
try {
print('Attempting to connect to MongoDB. Remaining retries: ' + maxRetries);
db = connect('mongodb://127.0.0.1:27017/admin');
var authResult = db.auth(rootUsername, rootPassword);
if (authResult) {
print('Authentication successful for root user: ' + rootUsername);
connected = true;
} else {
print('Authentication failed for root user: ' + rootUsername + ' with password: ' + rootPassword);
quit(1);
}
} catch (e) {
maxRetries--;
print('Connection failed due to error: ' + e);
print('Retrying... Remaining attempts: ' + maxRetries);
sleep(1000); // Sleep for 1 second
}
}
if (connected) {
print('Connected to MongoDB successfully. Proceeding to create user...');
db = db.getSiblingDB(dbName);
var createUserResult = db.createUser({
user: openimUsername,
pwd: openimPassword,
roles: [{
role: 'readWrite',
db: dbName
}]
});
if (createUserResult.ok == 1) {
print('User creation successful. User: ' + openimUsername + ', Database: ' + dbName);
} else {
print('User creation failed for user: ' + openimUsername + ' in database: ' + dbName);
quit(1);
}
} else {
print('Failed to connect to MongoDB after 300 retries.');
quit(1);
}
print('Script execution completed.');
EOF
echo "mongosh completed..."
create-topic.sh: |
#!/usr/bin/env bash
# Wait for Kafka to be ready
KAFKA_SERVER=kafka-service:9092
MAX_ATTEMPTS=300
attempt_num=1
echo "Waiting for Kafka to be ready..."
until /opt/bitnami/kafka/bin/kafka-topics.sh --list --bootstrap-server $KAFKA_SERVER; do
echo "Attempt $attempt_num of $MAX_ATTEMPTS: Kafka not ready yet..."
if [ $attempt_num -eq $MAX_ATTEMPTS ]; then
echo "Kafka not ready after $MAX_ATTEMPTS attempts, exiting"
exit 1
fi
attempt_num=$((attempt_num+1))
sleep 1
done
echo "Kafka is ready. Creating topics..."
topics=("toRedis" "toMongo" "toPush" "toOfflinePush")
partitions=8
replicationFactor=1
for topic in "${topics[@]}"; do
if /opt/bitnami/kafka/bin/kafka-topics.sh --create \
--bootstrap-server $KAFKA_SERVER \
--replication-factor $replicationFactor \
--partitions $partitions \
--topic $topic
then
echo "Topic $topic created."
else
echo "Failed to create topic $topic."
fi
done
echo "All topics created."
webhooks.yml: |
url: http://127.0.0.1:10006/callbackExample
beforeSendSingleMsg:

Loading…
Cancel
Save