nacos是阿里开源的一款动态服务发现、服务配置管理、服务及流量管理的应用。如果大家也在k8s里面用nacos请给我留言

公司一整套环境都是跑在容器平台上的,新接入nacos,容器化如下,在写这篇博客的时候阿里的nacos官方文档还没有k8s的教程

整体结构:

mysql做数据库,nginx代理后端nacos集群,clent端直接访问nginx,返回nacos集群的配置信息

1.nginx.yml

---
kind: ConfigMap
apiVersion: v1
metadata:
  name: nginx-nacos
  labels:
   version: "1.0"
data:
##################################### nginx #####################################
  nginx.conf: |-
    server {
      listen       80;
      charset utf-8;
      sendfile on;
      location / {
        rewrite '^/([a-z]+)/([a-z]+)$' /midenv/$1-$2.html;
       }    
      location /midenv {
        root /usr/share/nginx/html;
      }
    }
---

# ---
# kind: ConfigMap
# apiVersion: v1
# metadata:
#   name: nginx-html
#   namespace: zf
#   labels:
#    version: "1.0"
# data:
# ##################################### nginx #####################################
#   nacos-serverlist.html: |-
#     nacos:8848
# ---


---
apiVersion: v1
kind: Service
metadata:
 name: nacos
 labels:
  version: "1.0"
spec:
 ports:
  - port: 80
    protocol: TCP
    targetPort: 80
 selector:
  name: nacos
  version: "1.0"
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nacos-ingress
spec:
  rules:
  - host: nacos.youdomain.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nacosb
          servicePort: 80
---
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: nacos
 labels:
  version: "1.0"
spec:
 replicas: 1
 selector:
  matchExpressions:
   - {
      key: name, operator: In, values: [nacos],
      key: version, operator: In, values: ["1.0"]
     }
 template:
  metadata:
   labels:
    name: nacos
    version: "1.0"
  spec: 
   volumes:
   - name: nginx-nacos
     configMap: 
      name: nginx-nacos
      items: 
        - key: nginx.conf
          path: nginx.conf
   # - name: aucpackage
   #   nfs:
   #     path: /data/template/html/aucpackage
   #     server: nfs server
   containers:
   - name: nacosn
     image: nginx
     imagePullPolicy: IfNotPresent
     livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 10
      periodSeconds: 30
     ports:
     - containerPort: 80
       protocol: TCP
     volumeMounts:
      - mountPath: /etc/nginx/conf.d/
        name: nginx-nacos
     # volumeMounts:
     #  - mountPath: /usr/share/nginx/html/midenv/
     #    name: nginx-html

2.mysql.yml

---
apiVersion: v1
kind: Service
metadata:
 name: mysql-youdomain-service
 labels:
  version: "1.0"
spec:
 type: NodePort
 ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
 selector:
  name: mysql-chezhibao
  version: "1.0"

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata: 
 name: mysql-youdomain
 labels: 
  name: mysql-youdomain
  version: "1.0"
spec: 
 replicas: 1
 strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
 template: 
  metadata:
   labels: 
    name: mysql-youdomain
    version: "1.0"
  spec:
   volumes: 
   - name: sqldata
     nfs:
       path: /mysql/youdomain/data
       server: youNFS Service address
###这里放mysql的逻辑备份文件,注意文件格式是.sql的
   - name: sqldump
     nfs:
       path: /mysql/youdomain/docker-entrypoint-initdb.d/
       server: youNFS Service address
###这里是挂载mysql的配置文件
   - name: conf
     nfs:
       path: /mysql/conf
       server: youNFS Service address
   containers: 
   - name: msql-youdomain
     image: mysql:5.7
     imagePullPolicy: IfNotPresent
     resources:
      requests:
        memory: 1Gi
     ports: 
     - containerPort: 3306
       protocol: TCP
     env: 
     - name: MYSQL_ROOT_PASSWORD
       value: "password"
     # - name: --lower-case-table-names
     #   value: "1"
     volumeMounts:
      - name: sqldata 
        mountPath: /var/lib/mysql/
      - name: sqldump
        mountPath: /docker-entrypoint-initdb.d/
      - name: conf
        mountPath: /etc/mysql/conf.d

3.nacos.yml

---
apiVersion: v1
kind: Service
metadata:
 name: nacosserver
 labels:
  version: "1.0"
spec:
 type: NodePort
 ports:
  - name: sver
    protocol: TCP
    port: 8848
    targetPort: 8848
 selector:
  name: nacosserver
  version: "1.0"

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: nacosserver
 labels:
  version: "1.0"
spec:
 replicas: 1
 strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
 template:
  metadata:
   labels:
    name: nacosserver
    version: "1.0"
  spec: 
   volumes: 
   containers: 
   - name: nacosserver
     image: nacos0.3
     imagePullPolicy: IfNotPresent
     resources:
      requests:
        memory: 1Gi
     ports:
     - containerPort: 8848
       name: web
       protocol: TCP

4.nacos的dockerfile

# Version 0.1
# Command format: ONBUILD INSTRUCTION
# Description: nacos dockerfile
# author: john chance 
# https://github.com/zhuchance

# 基于centos7 ssh镜像

FROM centos-ssh-root

# 创建java home目录
RUN mkdir -p /usr/lib/java/jdk1.8.0_151 \
    && mkdir -p /opt

COPY nacos /opt/nacos
COPY profile /etc

# 将JDK复制到镜像中
COPY jdk1.8.0_151 /usr/lib/java/jdk1.8.0_151

# 配置JAVA 环境变量
ENV JAVA_HOME /usr/lib/java/jdk1.8.0_151
ENV PATH ${PATH}:${JAVA_HOME}/bin:${JAVA_HOME}/jre/bin
ENV CLASSPATH ${CLASSPATH}:${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib


# 设置编码
ENV LANG en_US.UTF-8    
ENV LANGUAGE en_US:en    
ENV LC_ALL en_US.UTF-8

# 映射端口
EXPOSE 8848 8848

# 启动tomcat
CMD ["/bin/bash","/opt/nacos/bin/startup.sh"]

 

 

Logo

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

更多推荐