1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Kubernetesでingressリソースを作って、パスごとにそれぞれ対応するPodへルーティングする設定をする

Posted at

情報

操作手順

Ingress用のアプリケーションを起動

sample-ingress-apps.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: sample-ingress-svc-1
spec:
  type: NodePort
  ports:
    - name: "http-port"
      protocol: "TCP"
      port: 8888
      targetPort: 80
  selector:
    ingress-app: sample1
---
apiVersion: v1
kind: Pod
metadata:
  name: sample-ingress-apps-1
  labels:
    ingress-app:  sample1
spec:
  containers:
    - name: nginx-container
      image:  nginx:1.12
      ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: sample-ingress-svc-2
spec:
  type: NodePort
  ports:
    - name: "http-port"
      protocol: "TCP"
      port: 8888
      targetPort: 80
  selector:
    ingress-app:  sample2
---
apiVersion: v1
kind: Pod
metadata:
  name: sample-ingress-apps-2
  labels:
    ingress-app:  sample2
spec:
  containers:
    - name: nginx-container
      image:  nginx:1.12
      ports:
        - containerPort:  80
---
apiVersion: v1
kind: Service
metadata:
  name: sample-ingress-default
spec:
  type: NodePort
  ports:
    - name: "http-port"
      protocol: "TCP"
      port: 8888
      targetPort: 80
  selector:
    ingress-app:  default
---
apiVersion: v1
kind: Pod
metadata:
  name: sample-ingress-default
  labels:
    ingress-app:  default
spec:
  containers:
    - name: nginx-container
      image:  nginx:1.12
      ports:
        - containerPort:  80

アプリケーションをデプロイ

kubectl apply -f sample-ingress-apps.yaml

出力結果

service/sample-ingress-svc-1 created
pod/sample-ingress-apps-1 created
service/sample-ingress-svc-2 created
pod/sample-ingress-apps-2 created
service/sample-ingress-default created
pod/sample-ingress-default created

各Podへのホスト名を返すindex.htmlを作成

1系

kubectl exec -it sample-ingress-apps-1 -- mkdir /usr/share/nginx/html/path1/
kubectl exec -it sample-ingress-apps-1 -- cp /etc/hostname /usr/share/nginx/html/path1/index.html

2系

kubectl exec -it sample-ingress-apps-2 -- mkdir /usr/share/nginx/html/path2/
kubectl exec -it sample-ingress-apps-2 -- cp /etc/hostname /usr/share/nginx/html/path2/index.html

3系

kubectl exec -it sample-ingress-default -- cp /etc/hostname /usr/share/nginx/html/index.html

Secretリソースを作成

自己証明書作成

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ~/tls.key -out ~/tls.crt -subj "/CN=sample.example.com"

secretの作成

kubectl create secret tls --save-config tls-sample --key ~/tls.key --cert ~/tls.crt

出力結果

secret/tls-sample created

Ingressリソース作成

annotationsのところはEKS用でIngressを使う場合につける識別子

sample-ingress.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: sample-ingress
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
spec:
  rules:
  - host: sample.example.com
    http:
      paths:
      - path: /path1/*
        backend:
          serviceName:  sample-ingress-svc-1
          servicePort:  8888
      - path: /path2/*
        backend:
          serviceName:  sample-ingress-svc-2
          servicePort:  8888
  backend:
    serviceName:  sample-ingress-default
    servicePort:  8888
  tls:
  - hosts:
    - sample.example.com
    secretName: tls-sample

デプロイ

kubectl apply -f sample-ingress.yaml

出力結果

ingress.extensions/sample-ingress created

動作確認

1系

curl  http://<ELBのDNS名>/path1/index.html -H "Host: sample.example.com"

出力結果

sample-ingress-apps-1

2系

curl  http://<ELBのDNS名>/path2/index.html -H "Host: sample.example.com"

出力結果

sample-ingress-apps-2

3系

curl  http://<ELBのDNS名>/index.html -H "Host: sample.example.com"

出力結果

sample-ingress-default
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?