1.初始化mysql,可以创建一个cm

apiVersion: v1
data:
mysql.db.host: 10.xxx
mysql.db.name: xxxx
mysql.password: xxx
mysql.port: ‘1234’
mysql.user: xxxx
kind: ConfigMap
metadata:
name: nacos-cm
namespace: openxx
resourceVersion: ‘2047504’

2.创建StatefulSet,开放容器端口。
其中端口 7848、8848、9848、9849是必须要开放的端口,8848是web页面端口,7848是集群数据同步接口,9848、9849是nacos 2.0.0以上的版本必须开发的端口。
ports:
- containerPort: 8848
name: client-port
protocol: TCP
- containerPort: 9848
name: client-rpc
protocol: TCP
- containerPort: 9849
name: raft-rpc
protocol: TCP
- containerPort: 7848
name: asyn-raft
protocol: TCP
3.初始化容器initContainers用于加载轮询插件
peer-finder会轮询(1秒)指定的k8s service ,如果service下面的pod地址列表发生变化,则重新写入Cluster.conf文件,目的是适应k8s集群的动态扩缩容。
initContainers:
- image: >-
xxxx/xxxx/nacos-peer-finder-plugin:1.1
imagePullPolicy: Always
name: peer-finder-plugin-install
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /home/nacos/plugins/peer-finder
name: data
subPath: peer-finder
以下是完整的yml文件

apiVersion: apps/v1
kind: StatefulSet
metadata:
labels:
k8s.kuboard.cn/name: nacos
name: nacos
namespace: xxxx
resourceVersion: ‘2122248’
spec:
podManagementPolicy: OrderedReady
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
app: nacos
serviceName: nacos-headless
template:
metadata:
annotations:
kubectl.kubernetes.io/restartedAt: ‘2022-06-13T17:15:45+08:00’
pod.alpha.kubernetes.io/initialized: ‘true’
creationTimestamp: null
labels:
app: nacos
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- nacos
namespaces:
- openapi-dev
topologyKey: kubernetes.io/hostname
containers:
- env:
- name: MYSQL_SERVICE_DB_NAME
valueFrom:
configMapKeyRef:
key: mysql.db.name
name: nacos-cm
- name: MYSQL_SERVICE_HOST
valueFrom:
configMapKeyRef:
key: mysql.db.host
name: nacos-cm
- name: MYSQL_SERVICE_PORT
valueFrom:
configMapKeyRef:
key: mysql.port
name: nacos-cm
- name: MYSQL_SERVICE_USER
valueFrom:
configMapKeyRef:
key: mysql.user
name: nacos-cm
- name: MYSQL_SERVICE_PASSWORD
valueFrom:
configMapKeyRef:
key: mysql.password
name: nacos-cm
- name: NACOS_REPLICAS
value: ‘3’
- name: SERVICE_NAME
value: nacos-headless
- name: DOMAIN_NAME
value: cluster.local
- name: POD_NAMESPACE
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
- name: NACOS_SERVER_PORT
value: ‘8848’
- name: NACOS_APPLICATION_PORT
value: ‘8848’
- name: PREFER_HOST_MODE
value: hostname
- name: NACOS_SERVERS
value: >-
nacos-0.nacos-headless.xxxxsvc.cluster.local:8848
nacos-1.nacos-headless.xxxx.svc.cluster.local:8848
nacos-2.nacos-headless.xxx.svc.cluster.local:8848
- name: MODE
value: cluster
image: >-
xxxx/nacos-server:v2.1.0
imagePullPolicy: IfNotPresent
name: nacos-cluster
ports:
- containerPort: 8848
name: client-port
protocol: TCP
- containerPort: 9848
name: client-rpc
protocol: TCP
- containerPort: 9849
name: raft-rpc
protocol: TCP
- containerPort: 7848
name: asyn-raft
protocol: TCP
resources:
limits:
cpu: 500m
memory: 2Gi
requests:
cpu: 500m
memory: 2Gi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /home/nacos/data2
name: data
subPath: data
- mountPath: /home/nacos/plugins/peer-finder
name: data
subPath: peer-finder
- mountPath: /home/nacos/log
name: data
subPath: logs
dnsPolicy: ClusterFirst
initContainers:
- image: >-
xxxxx/nacos-peer-finder-plugin:1.1
imagePullPolicy: Always
name: peer-finder-plugin-install
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /home/nacos/plugins/peer-finder
name: data
subPath: peer-finder
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- name: data
nfs:
path: /k8s_nfs2/dev/openapi
server: 1222
updateStrategy:
rollingUpdate:
partition: 0
type: RollingUpdate

4.接下来是svc配置

apiVersion: v1
kind: Service
metadata:
annotations:
service.alpha.kubernetes.io/tolerate-unready-endpoints: ‘true’
labels:
app: nacos
name: nacos
namespace: openapi-dev
resourceVersion: ‘2121515’
spec:
clusterIP: 192.168.238.20
clusterIPs:
- 192.168.238.20
externalTrafficPolicy: Cluster
ports:
- name: nacos-http
nodePort: 31668
port: 8848
protocol: TCP
targetPort: 8848
- name: raft-rpc
nodePort: 30441
port: 9849
protocol: TCP
targetPort: 9849
- name: ztptmw
nodePort: 31094
port: 9848
protocol: TCP
targetPort: 9848
- name: nkbm76
nodePort: 32692
port: 7848
protocol: TCP
targetPort: 7848
selector:
app: nacos
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}

5.以下是headless配置,和ingress结合

apiVersion: v1
kind: Service
metadata:
annotations:
service.alpha.kubernetes.io/tolerate-unready-endpoints: ‘true’
labels:
app: nacos
name: nacos-headless
namespace: openapi-dev
resourceVersion: ‘2045263’
spec:
clusterIP: None
clusterIPs:
- None
ports:
- name: server
port: 8848
protocol: TCP
targetPort: 8848
- name: client-rpc
port: 9848
protocol: TCP
targetPort: 9848
- name: raft-rpc
port: 9849
protocol: TCP
targetPort: 9849
- name: old-raft-rpc
port: 7848
protocol: TCP
targetPort: 7848
selector:
app: nacos
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
6.ingress配置

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
labels:
k8s.kuboard.cn/name: nacos
name: nacos
namespace: sss
resourceVersion: ‘2104882’
spec:
rules:
- host: www.fff.com
http:
paths:
- backend:
service:
name: nacos-headless
port:
number: 8848
path: /
pathType: Prefix
status:
loadBalancer:
ingress:
- ip: 192.168.168.231

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐