LoginSignup
3
1

More than 3 years have passed since last update.

Oracle Container Engine for Kubernetes(OKE)にIngressを設定する

Last updated at Posted at 2020-12-24

 はじめに

この記事はOracle Cloud Infrastructure Advent Calendar 2020 23日目の記事です。

Oracle Container Engine for Kubernetes(以下略OKE)へIngressを設定してみたので、手順をまとめます。

準備作業(OKEの作成・クライアント環境の準備)

以下を参考にOKEクラスターを作成し、Cloud ShellからOKEクラスターへアクセスできることを確認します。

以下の手順の1~4を実施
https://oracle-japan.github.io/paasdocs/documents/containers/common/#_7

Cloud Shellでkubectl get nodeを実行し、ノードの情報が取得できればOK

xxxxxxx@cloudshell:~ (ap-seoul-1)$ kubectl get node
NAME        STATUS   ROLES   AGE   VERSION
10.0.10.2   Ready    node    26h   v1.18.10

TLSシークレットの作成

ingressに設定する自己証明書を作成します。Cloud Shell環境で以下のコマンドを実行します。

$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginxsvc/O=nginxsvc"
$ kubectl create secret tls tls-secret --key tls.key --cert tls.crt

Ingress Controllerの作成

OKEはingressにネイティブに対応していないため、ingress-nginxを利用する必要があります。以下のコマンドを実行しingress-nginxをOKEに作成します。

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml

ingressリソースを作成します。
以下のファイルをcloud-generic.yamlの名前で作成します。

cloud-generic.yaml
kind: Service
apiVersion: v1
metadata:
  name: ingress-nginx
  namespace: ingress-nginx
  labels:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
spec:
  type: LoadBalancer
  selector:
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/part-of: ingress-nginx
  ports:
    - name: http
      port: 80
      targetPort: http
    - name: https
      port: 443
      targetPort: https

次に以下のコマンドを実行して、ingress-nginxイングレス・コントローラ・サービスを作成します。

kubectl apply -f cloud-generic.yaml

Ingress、サンプルアプリケーションの適用

以下のファイルをhello-world-ingress.yamlの名前で作成します。

hello-world-ingress.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: docker-hello-world
  labels:
    app: docker-hello-world
spec:
  selector:
    matchLabels:
      app: docker-hello-world
  replicas: 3
  template:
    metadata:
      labels:
        app: docker-hello-world
    spec:
      containers:
      - name: docker-hello-world
        image: scottsbaldwin/docker-hello-world:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: docker-hello-world-svc
spec:
  selector:
    app: docker-hello-world
  ports:
    - port: 8088
      targetPort: 80
  type: ClusterIP

次に以下のコマンドで作成します。

kubectl create -f hello-world-ingress.yaml

イングレス・リソースの作成

以下のファイルをingress.yamlの名前で作成します。

ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: hello-world-ing
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  tls:
  - secretName: tls-secret
  rules:
  - http:
      paths:
      - backend:
          serviceName: docker-hello-world-svc
          servicePort: 8088

以下のコマンドでingressを作成します。

kubectl create -f ingress.yaml

サンプルアプリへ接続テスト

以下のコマンドを実行し、ロードバランサーのIPを取得します。

xxxxxxxx_n@cloudshell:~ (ap-seoul-1)$ kubectl get svc -n ingress-nginx
NAME            TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)                      AGE
ingress-nginx   LoadBalancer   10.96.226.152   193.123.246.212   80:32177/TCP,443:30362/TCP   5m54s

表示されたEXTERNAL-IPにcurlコマンドを実行して応答を確認します。

xxxxxxxx_n@cloudshell:~ (ap-seoul-1)$ curl -k https://193.123.246.212
<h1>Hello webhook world from: docker-hello-world-665df7fd49-fftl8</h1>

参考
https://docs.cloud.oracle.com/ja-jp/iaas/Content/ContEng/Tasks/contengsettingupingresscontroller.htm

3
1
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
3
1