【永続化】Ingress

masterノードのIPアドレス

$ multipass list

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

  • macrok8s.local
  • sub.macrok8s.local

マニフェスト作成

ubuntu@master:~$ mkdir ingress-pv
ここに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
      volumes:
        - name: http
          persistentVolumeClaim:
            claimName: ingress-pv-claim
---
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
      volumes:
        - name: http
          persistentVolumeClaim:
            claimName: ingress-pv-claim
---
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

ingress-pv/ingress-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: ingress-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: ingress-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

apply

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