官方参考地址:https://nacos.io/zh-cn/docs/use-nacos-with-kubernetes.html
说明:
1、官网采用的nfs持久化部署
我将nacos持久化改成 emptyDir 临时目录挂载,同时又能满足自行调节nacos集群实例数。
2. emptyDir 临时目录挂载的nacos.yaml
cat > nacos.yaml << EOF
# nacos.yaml 可调整 集群副本数 扩容
---
apiVersion: v1
kind: Service
metadata:
name: nacos-headless
labels:
app: nacos
annotations:
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
type: NodePort
#clusterIP: None #如果不暴露端口需要开启这个
selector:
ports:
- port: 8848
name: server
targetPort: 8848
- port: 9848
name: client-rpc
targetPort: 9848
- port: 9849
name: raft-rpc
targetPort: 9849
## 兼容1.4.x版本的选举端口
- port: 7848
name: old-raft-rpc
targetPort: 7848
selector:
app: nacos
---
apiVersion: v1
kind: ConfigMap
metadata:
name: nacos-cm
data:
mysql.db.name: "nacos"
mysql.port: "3306"
mysql.user: "nacos"
mysql.password: "nacos"
mysql.host: "nacos"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nacos
spec:
serviceName: nacos-headless
replicas: 1
template:
metadata:
labels:
app: nacos
annotations:
pod.alpha.kubernetes.io/initialized: "true"
spec:
initContainers:
- name: peer-finder-plugin-install
image: nacos/nacos-peer-finder-plugin:1.1
imagePullPolicy: IfNotPresent
volumeMounts:
- mountPath: /home/nacos/plugins/peer-finder
name: data
subPath: peer-finder
containers:
- name: nacos
image: nacos/nacos-server:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8848
name: client-port
- containerPort: 9848
name: client-rpc
- containerPort: 9849
name: raft-rpc
- containerPort: 7848
name: old-raft-rpc
env:
- name: NACOS_REPLICAS
value: "3"
# 若是使用单实例部署 nacos ,需要设置: MODE=standalone
- name: MODE
value: standalone
- name: SERVICE_NAME
value: "nacos-headless"
- name: DOMAIN_NAME
value: "cluster.local"
- name: POD_NAMESPACE
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
- name: MYSQL_SERVICE_DB_NAME
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.db.name
- name: MYSQL_SERVICE_PORT
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.port
- name: MYSQL_SERVICE_USER
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.user
- name: MYSQL_SERVICE_HOST
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.host
- name: MYSQL_SERVICE_PASSWORD
valueFrom:
configMapKeyRef:
name: nacos-cm
key: mysql.password
- name: NACOS_SERVER_PORT
value: "8848"
- name: NACOS_APPLICATION_PORT
value: "8848"
- name: PREFER_HOST_MODE
value: "hostname"
volumeMounts:
- name: data
mountPath: /home/nacos/plugins/peer-finder
subPath: peer-finder
# - name: data
# mountPath: /home/nacos/data
# subPath: data
# - name: data
# mountPath: /home/nacos/logs
# subPath: logs
# 将 nacos 持久化改成 非持久化方式;官网使用的是 StorageClass 方式,需要依赖nfs等共享存储,以及共享存储插件实现
volumes:
- name: data
emptyDir: {}
selector:
matchLabels:
app: nacos
EOF
docker pull nacos/nacos-peer-finder-plugin:1.1
docker pull nacos/nacos-server:latest
kubectl apply -f nacos.yaml
3.登录验证
kubectl get pods,svc #查看暴露端口
root@k8s-master:~# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/nacos-0 1/1 Running 0 8m12s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 106d
service/nacos-headless NodePort 10.104.86.31 <none> 8848:30719/TCP,9848:31354/TCP,9849:31262/TCP,7848:32512/TCP 13m
root@k8s-master:~#
浏览器访问: IP:端口/nacos 我这里是http://49.0.241.2:30719/nacos/
账号和密码默认都是nacos