微服务学习实战笔记 4.4-系统部署篇-k8s部署Consul集群

前面我们演练了在宿主机和docker中部署Consul,今天尝试在K8s中部署Consul集群。

一、部署Consul集群

在master节点执行如下命令:

1、创建Service

vi consul-service.yaml

  内容如下:

apiVersion: v1
kind: Service
metadata:  
   name: consul  
   labels:    
     name: consul
spec:  
   type: ClusterIP  
#   clusterIP: None
   ports:    
    - name: http      
      port: 8500      
      targetPort: 8500    
    - name: https      
      port: 8443
      targetPort: 8443
    - name: rpc
      port: 8400
      targetPort: 8400    
    - name: serflan-tcp      
      protocol: "TCP"      
      port: 8301      
      targetPort: 8301    
    - name: serflan-udp      
      protocol: "UDP"      
      port: 8301      
      targetPort: 8301    
    - name: serfwan-tcp      
      protocol: "TCP"      
      port: 8302      
      targetPort: 8302    
    - name: serfwan-udp      
      protocol: "UDP"      
      port: 8302      
      targetPort: 8302
    - name: server      
      port: 8300      
      targetPort: 8300    
    - name: consuldns      
      port: 8600      
      targetPort: 8600  
   selector:    
    app: consul

         保存后执行创建

kubectl create -f consul-service.yaml

  

2、在所有Node节点创建目录,用于存储Consul数据

mkdir -p /home/data

 

3、创建StatefulSetl类型Consul pod控制器

vi consul-statefulset.yaml

  内容如下:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: consul
spec:
  selector:
    matchLabels:
      app: consul
      component: server
  serviceName: consul
  podManagementPolicy: "Parallel"
  replicas: 3
  template: 
    metadata:
      labels:
        app: consul
        component: server
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: app
                    operator: In
                    values:
                      - consul
              topologyKey: kubernetes.io/hostname
#      serviceAccountName: consul
      terminationGracePeriodSeconds: 10
      containers:
      - name: consul
        image: consul:latest
        args:
             - "agent"
             - "-server"
             - "-bootstrap-expect=3"
             - "-ui"
             - "-data-dir=/consul/data"
             - "-bind=0.0.0.0"
             - "-client=0.0.0.0"
             - "-advertise=$(PODIP)"
             - "-retry-join=consul-0.consul.$(NAMESPACE).svc.cluster.local"
             - "-retry-join=consul-1.consul.$(NAMESPACE).svc.cluster.local"
             - "-retry-join=consul-2.consul.$(NAMESPACE).svc.cluster.local"
             - "-domain=cluster.local"
             - "-disable-host-node-id"
        volumeMounts:
            - name: data
              mountPath: /consul/data
        env:
            - name: PODIP
              valueFrom:
                fieldRef:
                  fieldPath: status.podIP
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
        ports:
            - containerPort: 8500
              name: ui-port
            - containerPort: 8400
              name: alt-port
            - containerPort: 53
              name: udp-port
            - containerPort: 8443
              name: https-port
            - containerPort: 8080
              name: http-port
            - containerPort: 8301
              name: serflan
            - containerPort: 8302
              name: serfwan
            - containerPort: 8600
              name: consuldns
            - containerPort: 8300
              name: server
      volumes:
        - name: data
          hostPath:
            path: /home/data

      保存后执行创建

kubectl create -f consul-statefulset.yaml

 

二、通过Nginx Ingress暴露管理Web

前面我们部署了Nginx Ingress,今天我们就通过它暴露Consul的管理界面。

vi ingress-consul.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-consul
  namespace: default
  annotations: 
    kubernets.io/ingress.class: "nginx"
spec:
  rules:
  - host: consul.test.com
    http:
      paths:
      - path: 
        backend:
          serviceName: consul    #consul Service 名称
          servicePort: 8500      #web 界面暴露的端口
kubectl apply -f ingress-consul.yaml

  由于Nginx Ingress需要通过域名映射,二配置的域名consul.test.com不存在,部署完成后现在访问主机 hosts 文件中添加主机映射,我Nginx Ingress是部署在k8s-node01节点。

       故添加192.168.2.172  consul.test.com 。

 

posted @ 2020-08-27 15:44  燕马越空  阅读(1141)  评论(0编辑  收藏  举报