Ingress

【microK8s】

masterノードのIPアドレス

【multipass】

$ multipass list master

【LXD】

$ lxc list master

/etc/hostsにmasterノードIPアドレス登録

  • macrok8s.local
  • sub.macrok8s.local

マニフェスト作成

ubuntu@master:~$ mkdir ingress
ここにyamlファイルを作成する。

作成したYAMLファイルはこんな感じ。

ingress/service-1.yaml

# service-1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: service1
spec:
  replicas: 2
  selector:
    matchLabels:
      app: service1
  template:
    metadata:
      labels:
        app: service1
    spec:
      containers:
        - name: service1
          image: nginx:latest
          ports:
            - name: http
              containerPort: 80
          env:
            - name: MICROK8S_ENVIRONMENT
              value: Development
            - name: HELLOCONTAINERS_MESSAGE
              value: 'Hello from Service 1!'
      nodeSelector:
        kubernetes.io/os: linux
---
apiVersion: v1
kind: Service
metadata:
  name: service1
spec:
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: service1
  type: ClusterIP
  

ingress/service-2.yaml

# service-2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: service2
spec:
  replicas: 2
  selector:
    matchLabels:
      app: service2
  template:
    metadata:
      labels:
        app: service2
    spec:
      containers:
        - name: service2
          image: nginx:latest
          ports:
            - name: http
              containerPort: 80
          env:
            - name: MICROK8S_ENVIRONMENT
              value: Development
            - name: HELLOCONTAINERS_MESSAGE
              value: 'Hello from Service 2!'
      nodeSelector:
        kubernetes.io/os: linux
---
apiVersion: v1
kind: Service
metadata:
  name: service2
spec:
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: service2
  type: ClusterIP

ingress/ingress.yaml

# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress-microk8s
  labels:
    app: nginx-ingress-microk8s
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-forwarded-headers: "true"
spec:
  rules:
  - host: "microk8s.local"
    http:
      paths:
      - pathType: Prefix
        path: "/(.*)"
        backend:
          service:
            name: service1
            port:
              number: 80
  - host: "sub.microk8s.local"
    http:
      paths:
      - pathType: Prefix
        path: "/service2/(.*)"
        backend:
          service:
            name: service2
            port:
              number: 80
---
apiVersion: v1
kind: Service
metadata:
  name: ingress
  namespace: ingress
spec:
  selector:
    name: nginx-ingress-microk8s
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
    - name: https
      protocol: TCP
      port: 443
      targetPort: 443

apply

ubuntu@master:~$ kubectl apply -f ingress
タイトルとURLをコピーしました