avatar

K8s 搭建 Nacos 高可用

将 Nacos 的凭据保存到 Secret

为 nacos 访问 mysql 的凭据 创建 secret。

1
2
3
4
5
6
7
8
9
10
11
apiVersion: v1
kind: Secret
metadata:
name: mysql-secret
namespace: tools
type: Opaque
data:
host: 127.0.0.1
port: 3306
user: temp_user
password: 123456

使用 ConfigMap 微调 Nacos 的配置

1
2
3
4
5
6
7
8
apiVersion: v1
kind: ConfigMap
metadata:
name: nacos
namespace: tools
data:
# 生产环境需要降低 Nacos 默认的 DEBUG 日志级别
nacos-logback.xml: "省略,请自行从 Nacos 的目录 /home/nacos/conf/nacos-logback.xml 拷贝下来修改"

使用 StatefulSet 创建 Nacos

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app.kubernetes.io/name: nacos
app.kubernetes.io/component: spring-cloud
name: nacos
namespace: tools
spec:
podManagementPolicy: OrderedReady
replicas: 3
selector:
matchLabels:
app.kubernetes.io/name: nacos
app.kubernetes.io/component: spring-cloud
template:
metadata:
labels:
app.kubernetes.io/name: nacos
app.kubernetes.io/component: spring-cloud
spec:
containers:
- env:
- name: TZ
value: Asia/Shanghai
- name: MODE
value: cluster
- name: SPRING_DATASOURCE_PLATFORM
value: mysql
- name: MYSQL_SERVICE_HOST
valueFrom:
secretKeyRef:
key: host
name: mysql-secret
optional: false
- name: MYSQL_SERVICE_DB_NAME
value: nacos
- name: MYSQL_SERVICE_PORT
valueFrom:
secretKeyRef:
key: port
name: mysql-secret
optional: false
- name: MYSQL_SERVICE_USER
valueFrom:
secretKeyRef:
key: user
name: mysql-secret
optional: false
- name: MYSQL_SERVICE_PASSWORD
valueFrom:
secretKeyRef:
key: password
name: mysql-secret
optional: false
- name: MYSQL_SERVICE_DB_PARAM
value: characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false
- name: JVM_XMS
value: 256m
- name: JVM_XMX
value: 512m
- name: JVM_XMN
value: 256m
- name: NACOS_SERVERS
value: nacos-0.nacos.tools:8848 nacos-1.nacos.tools:8848 nacos-2.nacos.tools:8848
- name: NACOS_SERVER_PORT
value: "8848"
image: nacos/nacos-server:v2.0.4
imagePullPolicy: IfNotPresent
name: nacos
resources:
limits:
cpu: 250m
memory: 1Gi
requests:
cpu: 250m
memory: 1Gi
securityContext:
privileged: false
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /home/nacos/conf/nacos-logback.xml
name: logs
subPath: nacos-logback.xml
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: qcloudregistrykey
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- configMap:
defaultMode: 420
name: nacos
name: logs
updateStrategy:
rollingUpdate:
partition: 0
type: RollingUpdate

将 Nacos 集群加入 nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
apiVersion: v1
kind: ConfigMap
metadata:
name: nginx
namespace: tools
data:
nacos.conf: |-
upstream nacos {
server nacos-0.nacos.tools:8848;
server nacos-1.nacos.tools:8848;
server nacos-2.nacos.tools:8848;
}
server {
listen 8848;
server_name localhost;
location / {
proxy_pass http://nacos;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
add_header X-Cache $upstream_cache_status;
add_header Cache-Control no-cache;
}
}
nginx.conf: |-
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
accept_mutex on;
multi_accept on;
use epoll;
worker_connections 1024;
}

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 500m;

sendfile on;
gzip on;
keepalive_timeout 30;
include /etc/nginx/conf.d/*.conf;
}

stream {
upstream nacos-grpc-9848 {
server nacos-0.nacos.tools:9848;
server nacos-1.nacos.tools:9848;
server nacos-2.nacos.tools:9848;
}

upstream nacos-grpc-9849 {
server nacos-0.nacos.tools:9849;
server nacos-1.nacos.tools:9849;
server nacos-2.nacos.tools:9849;
}

server {
listen 9848;
proxy_connect_timeout 300s;
proxy_timeout 300s;
proxy_pass nacos-grpc-9848;
}

server {
listen 9849;
proxy_connect_timeout 300s;
proxy_timeout 300s;
proxy_pass nacos-grpc-9849;
}
}

部署 Nginx 代理 Nacos 集群

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
app.kubernetes.io/name: nginx
app.kubernetes.io/component: load-balancer
name: nginx
namespace: tools
spec:
podManagementPolicy: OrderedReady
replicas: 1
selector:
matchLabels:
k8s-app: nginx
qcloud-app: nginx
template:
metadata:
labels:
app.kubernetes.io/name: nginx
app.kubernetes.io/component: load-balancer
spec:
containers:
- env:
- name: TZ
value: Asia/Shanghai
image: nginx:1.19.10
imagePullPolicy: IfNotPresent
name: nginx
resources:
limits:
cpu: 250m
memory: 512Mi
requests:
cpu: 250m
memory: 512Mi
securityContext:
privileged: false
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /etc/nginx/nginx.conf
name: nginx
subPath: nginx.conf
- mountPath: /var/log/nginx
name: logs
- mountPath: /etc/nginx/conf.d/nacos.conf
name: nginx-nacos
subPath: nacos.conf
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: qcloudregistrykey
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- configMap:
defaultMode: 420
items:
- key: nginx.conf
mode: 420
path: nginx.conf
name: nginx
name: nginx
- configMap:
defaultMode: 420
items:
- key: nacos.conf
mode: 420
path: nacos.conf
name: nginx
name: nginx-nacos
updateStrategy:
rollingUpdate:
partition: 0
type: RollingUpdate

验证 Nacos 集群状态

image.png

文章作者: 梦想歌
文章链接: https://mengxiangge.netlify.app/2022/01/01/K8s%20%E6%90%AD%E5%BB%BA%20Nacos%20%E9%AB%98%E5%8F%AF%E7%94%A8/
版权声明: 本博客所有文章除特别声明外,均采用 Apache 2.0 License 许可协议。转载请注明来自 梦想歌の网络日志
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论