k8s搭建高可用spring-cloud-config配置中心集群
k8s搭建高可用配置中心查找镜像docker部署关闭认证方式部署开启认证方式部署docker-compose方式部署k8s方式部署使用configMap挂载配置挂载本地目录,让多个容器共享同一个配置目录测试应用加载配置中心配置启动查找镜像镜像地址: https://hub.docker.com/r/lhstack/config-serverdocker部署关闭认证方式部署docker run --
·
k8s搭建高可用配置中心
查找镜像
镜像地址: https://hub.docker.com/r/lhstack/config-server
docker部署
关闭认证方式部署
docker run --name config-server -p 7000:7000 -e EUREKA_SERVER_URL="http://admin:123456@10.43.87.61:8761/eureka" -e SECURITY_ENABLE=false -d lhstack/config-server
开启认证方式部署
# 停掉之前运行的容器
docker rm -f config-server
# 启动带认证方式的容器,使用默认用户名和密码
docker run --name config-server -p 7000:7000 -e EUREKA_SERVER_URL="http://admin:123456@10.43.87.61:8761/eureka" -v ${PWD}/config:/config-server/conf/config -d lhstack/config-server
这个时候就需要用户名和密码了
docker-compose方式部署
# 停掉启动的容器
docker rm -f config-server
cat > docker-compose.yml <<EOF
version: '3'
services:
config-server-1:
image: lhstack/config-server
container_name: config-server-1
ports:
- 7001:7000
environment:
EUREKA_SERVER_URL: "http://admin:123456@10.43.87.61:8761/eureka"
JAVA_OPTS: "-Xmx128m -Xms128m"
SECURITY_USERNAME: admin
SECURITY_PASSWORD: admin
volumes:
- ./config:/config-server/conf/config
deploy:
resources:
limits:
cpus: '1'
memory: '256M'
logging:
options:
max-file: '1'
max-size: '3k'
config-server-2:
image: lhstack/config-server
container_name: config-server-2
ports:
- 7002:7000
environment:
EUREKA_SERVER_URL: "http://admin:123456@10.43.87.61:8761/eureka"
JAVA_OPTS: "-Xmx128m -Xms128m"
SECURITY_USERNAME: admin
SECURITY_PASSWORD: admin
volumes:
- ./config:/config-server/conf/config
deploy:
resources:
limits:
cpus: '1'
memory: '256M'
logging:
options:
max-file: '1'
max-size: '3k'
EOF
docker-compose up -d
在config目录下面创建对应配置文件
k8s方式部署
使用configMap挂载配置
# 停掉启动的配置中心
docker-compose down
# 生成脚本
cat > config-server.yml<<EOF
apiVersion: v1
kind: Service
metadata:
name: config-server
spec:
type: ClusterIP
ports:
- name: http
port: 7000
selector:
app: config-server
---
apiVersion: v1
kind: ConfigMap
metadata:
name: config-server
data:
test-a.yml: |
server:
port: 8761
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: config-server
spec:
replicas: 2
selector:
matchLabels:
app: config-server
template:
metadata:
labels:
app: config-server
spec:
volumes:
- name: config
configMap:
name: config-server
containers:
- name: config-server
image: lhstack/config-server
imagePullPolicy: IfNotPresent
ports:
- containerPort: 7000
env:
- name: "JAVA_OPTS"
value: "-Xmx512m -Xms512m"
- name: EUREKA_SERVER_URL
value: "http://admin:123456@10.43.87.61:8761/eureka"
- name: SECURITY_USERNAME
value: "admin"
- name: SECURITY_PASSWORD
value: "123456"
volumeMounts:
- name: config
mountPath: /config-server/conf/config
resources:
requests:
cpu: 50m
memory: 64Mi
limits:
cpu: 100m
memory: 512Mi
EOF
kubectl apply -f config-server.yml
kubectl get pod
第一次安装会先拉取镜像
修改configMap中配置的内容,加上test-b.yml配置
等待几秒钟,再次访问配置中心
挂载本地目录方式
# 停掉之前启动的集群
kubectl delete -f config-server.yml
# 创建集群
cat > config-server.yml<<EOF
apiVersion: v1
kind: Service
metadata:
name: config-server
spec:
type: ClusterIP
ports:
- name: http
port: 7000
selector:
app: config-server
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: config-server
spec:
replicas: 2
selector:
matchLabels:
app: config-server
template:
metadata:
labels:
app: config-server
spec:
volumes:
- name: config
hostPath:
path: /home/springcloud/config-server/config #挂载配置所在目录
containers:
- name: config-server
image: lhstack/config-server
imagePullPolicy: IfNotPresent
ports:
- containerPort: 7000
env:
- name: "JAVA_OPTS"
value: "-Xmx512m -Xms512m"
- name: EUREKA_SERVER_URL
value: "http://admin:123456@10.43.87.61:8761/eureka"
- name: SECURITY_USERNAME
value: "admin"
- name: SECURITY_PASSWORD
value: "123456"
volumeMounts:
- name: config
mountPath: /config-server/conf/config
resources:
requests:
cpu: 50m
memory: 64Mi
limits:
cpu: 100m
memory: 512Mi
EOF
# 启动集群
kubectl apply -f config-server.yml
添加配置到挂载的目录里面
测试应用加载配置中心配置启动
更多推荐
已为社区贡献2条内容
所有评论(0)