kubernetes初始化容器顺序启动
程序员文章站
2022-07-13 22:10:24
...
最近工作中需要保证容器启动之前PostgreSQL先启动,然后Apollo再启动,不然会出现配置加载错误的问题。Kubernetes的初始化容器就能够满足这种场景。
InitContainers能够按照资源文件中初始化容器的顺序依次启动
apiVersion: apps/v1
kind: Deployment
metadata:
name: openailab-discovery-service
namespace: oas-uat
labels:
name: openailab-discovery-service
spec:
replicas: 1
selector:
matchLabels:
name: openailab-discovery-service
template:
metadata:
labels:
name: openailab-discovery-service
spec:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: env
operator: In
values:
- dev
containers:
- name: openailab-discovery-service
image: 192.168.3.202:8088/oascloud/openailab-discovery-service:latest
env:
- name: APOLLO_APP_ID
value: 'openailab-discovery-service'
- name: APOLLO_EUREKA_URI
value: 'http://10.98.100.95:8082/'
- name: APOLLO_NAMESPACE
value: 'application'
volumeMounts:
- name: openailab-discovery-service
mountPath: /usr/local/logs
command: ['sh','-c','java -server -Xms512m -Xmx1024m -Djava.io.tmpdir=/var/tmp -Duser.timezone=Asia/Shanghai -jar openailab-discovery-service-*.jar --server.port=8080 --spring.profiles.active=remote']
ports:
- containerPort: 8080
resources:
requests:
memory: 512Mi
cpu: 250m
limits:
memory: 1024Mi
cpu: 500m
initContainers:
- name: init-mysql
image: busybox
command: ['sh', '-c']
args:
- echo Prepare to postgresql initialize..;
until nc -w 1 -z 10.98.70.86 5432;
do echo Waiting for postgresql initialization to complete..; sleep 10; done;
echo Postgresql Initialization successful!;
- name: init-apollo
image: busybox
command: ['sh', '-c']
args:
- echo Prepare to apollo initialize..;
until nc -w 1 -z 10.98.100.95 8081;
do echo Waiting for eureka initialization to complete..; sleep 10; done;
echo Eureka is ready.;
until nc -w 1 -z 10.98.100.95 8091;
do echo Waiting for admin initialization to complete..; sleep 10; done;
echo Admin is ready.;
echo Apollo Initialization successful!;
volumes:
- name: openailab-discovery-service
hostPath:
path: /usr/local/logs
其中10.98.70.86是PostgreSQL容器集群IP(clusterIP),5432是PostgreSQL端口,10.98.100.95是Apollo的容器集群IP,8081、8091分别是Apollo的Eureka、Admin端口,以上配置方式就能够保证在PostgreSQL完全起来之后,再启动Apollo,待两个初始化容器都启动完成之后,最后启动我们的 openailab-discovery-service 容器就可以了。
上一篇: spark—键值对操作