Kubernetes部署SpringCloud(三) 使用 Ingress-nginx 暴露服务

之前部署的zuul以及basic-info-api 都仅仅在于flannel 网络内可以访问.

现在来使用Ingress-nginx 对外暴露服务

以下用到的一些docker镜像,是存在我私有仓库的,请自行替换

创建命名空间namespace.yaml 或者直接 kubectl create namespace ingress-nginx

apiVersion: v1
kind: Namespace
metadata:
  name: ingress-nginx

执行

kubectl apply -f namespace.yaml

创建 default-backend.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: default-http-backend
  labels:
    app: default-http-backend
  namespace: ingress-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: default-http-backend
  template:
    metadata:
      labels:
        app: default-http-backend
    spec:
      terminationGracePeriodSeconds: 60
      containers:
      - name: default-http-backend
        # Any image is permissible as long as:
        # 1. It serves a 404 page at /
        # 2. It serves 200 on a /healthz endpoint
        image: 192.168.91.137:5000/defaultbackend:1.4
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          timeoutSeconds: 5
        ports:
        - containerPort: 8080
        resources:
          limits:
            cpu: 10m
            memory: 20Mi
          requests:
            cpu: 10m
            memory: 20Mi
---

apiVersion: v1
kind: Service
metadata:
  name: default-http-backend
  namespace: ingress-nginx
  labels:
    app: default-http-backend
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: default-http-backend

执行

kubectl create -f default-backend.yaml

创建without-rbac.yaml

apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
  name: nginx-ingress-controller
  namespace: ingress-nginx 
spec:
#  replicas: 1
  selector:
    matchLabels:
      app: ingress-nginx
  template:
    metadata:
      labels:
        app: ingress-nginx
      annotations:
        prometheus.io/port: '10254'
        prometheus.io/scrape: 'true' 
    spec:
      terminationGracePeriodSeconds: 60
      hostNetwork: true
      containers:
      - name: nginx-ingress-controller
        image: 192.168.91.137:5000/nginx-ingress-controller:0.11.0
        ports:
        - name: http
          containerPort: 80
          hostPort: 80
        - name: https
          containerPort: 443
          hostPort: 443
        livenessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          initialDelaySeconds: 10
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        readinessProbe:
          failureThreshold: 3
          httpGet:
            path: /healthz
            port: 10254
            scheme: HTTP
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 1
        env:
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: KUBERNETES_MASTER
            value: http://192.168.91.144:8080
        args:
          - /nginx-ingress-controller
          - --default-backend-service=$(POD_NAMESPACE)/default-http-backend
          - --configmap=$(POD_NAMESPACE)/nginx-configuration
          - --apiserver-host=http://192.168.91.144:8080

执行

kbuectl create -f without-rbac.yaml

创建 zuul-ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: zuul-ingress
  namespace: ms
spec:
  rules:
  - host: ms
    http:
      paths:
      - path: /
        backend:
          serviceName: zuul
          servicePort: 8200

执行

kubectl create -f zuul-ingress.yaml

修改host 添加 虚拟域名 ms

测试 ms直接指向 zuul 服务,然后转发到basic-info-api服务的 getHost

Kubernetes部署SpringCloud(三) 使用 Ingress-nginx 暴露服务_第1张图片

 

转载于:https://www.cnblogs.com/sweetchildomine/p/8847734.html

你可能感兴趣的:(java,运维)