LoginSignup
2
2

More than 3 years have passed since last update.

k8sでnginxを起動してWelcome to nginxする

Last updated at Posted at 2020-04-25

ゴール

公式ドキュメントに記載されているNginxのマニフェストをデプロイして外部からアクセスできるようにする
スクリーンショット 2020-04-25 15.20.17.png

推奨環境

  • EC2
  • Kubeadm

作成するファイル一覧

work
├─ debug.yaml
├─ Dockerfile
├─ nginx-deployment.yaml
└─ nginx-service.yaml

debug用のPod作成

Dockerfile作成(Cent OS)

Dockerfile
FROM centos:7
# コンテナイメージ作成
docker build -t debug .

# イメージが作成されたか確認
docker images | grep debug

# コンテナ動作確認
docker run -it debug bash

Pod作成

マニフェストを作成する

debug.yaml
apiVersion: v1
kind: Pod
metadata:
  name: debug
  namespace: default
spec:
  containers:
    - name: debug
      image: debug
      imagePullPolicy: Never # ローカルに存在するコンテナイメージを取得する
      command:
        - "sh"
        - "-c"
      args:
        - |
          while true
          do
            sleep 5
          done

マニフェストをk8sにデプロイする

kubectl apply -f debug.yaml

Podの起動を確認する

kubectl get pod

NginxのDeployment作成(公式ドキュメントのもの)

nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

マニフェストをk8sにデプロイする

kubectl apply -f nginx-deployment.yaml

Depoymentsの確認

kubectl get deployments

Podの起動とIPアドレスを確認する

kubectl get pod -o wide

NAME                                READY   STATUS    RESTARTS   AGE     IP               NODE         NOMINATED NODE   READINESS GATES
debug                               1/1     Running   0          16m     192.168.29.97    k8s.master   <none>           <none>
nginx-deployment-6b474476c4-djqqv   1/1     Running   0          3m48s   192.168.29.98    k8s.master   <none>           <none>
nginx-deployment-6b474476c4-dww8l   1/1     Running   0          3m48s   192.168.29.99    k8s.master   <none>           <none>
nginx-deployment-6b474476c4-gjm29   1/1     Running   0          3m48s   192.168.29.100   k8s.master   <none>           <none>

debug用Podを使って疎通確認

# Podの中に入る
kubectl exec -it debug bash

# curlコマンド実行
curl 192.168.29.98
curl 192.168.29.99
curl 192.168.29.100

NginxのService作成(k8sクラスタ外からアクセスできるようにするためのもの)

公式ドキュメント参考

nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 30000
  type: NodePort

マニフェストをk8sにデプロイする

kubectl apply -f nginx-service.yaml

Serviceの確認

kubectl get svc

Serviceの詳細確認

kubectl describe service nginx-svc

Name:                     nginx-svc
Namespace:                default
Labels:                   <none>
Annotations:              Selector:  app=nginx
Type:                     NodePort
IP:                       10.96.67.76
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30000/TCP
Endpoints:                192.168.29.100:80,192.168.29.98:80,192.168.29.99:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

debug用Podを使って3つ動いているPodを自動的に割り当てるか確認

# Podの中に入る
kubectl exec -it debug bash

# curlコマンド実行
# curl <サービス名>
curl nginx-svc

クラスタ外部から疎通確認(DebugPodに入っていない状態)

# kubectl describe service nginx-svcで表示される「IP」の値
curl 10.96.67.76

ブラウザからも表示できるようにする

仮想マシン上のポート番号30000を開放する
AWSの場合、セキュリティグループのインバウンドのルールを編集してポートを開放する

ブラウザにアクセスする

http://<仮想マシンのIPアドレス>:30000

スクリーンショット 2020-04-25 15.20.17.png

2
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
2
2