ユーザ用ツール

サイト用ツール


サイドバー

このページの翻訳:



最近の更新



Tag Cloud

06_virtualization:05_container:13_kubernetes_create_nginx

13 Kubernetes Nginxを立ててみる

1.nginx pod

3つのnginxを立ててみる

nginx.yaml

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

create

kubectl create -f nginx.yaml
# kubectl get deployments.apps -o wide
NAME    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
nginx   3/3     3            3           24s   nginx        nginx:latest   app=nginx-app

# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-59bc74c488-669ng   1/1     Running   0          9m34s
nginx-59bc74c488-csj8x   1/1     Running   0          9m34s
nginx-59bc74c488-dc2zq   1/1     Running   0          9m34s

2.確認用にNginxの表示変更

Podを確認

# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS      AGE   IP            NODE       NOMINATED NODE   READINESS GATES
nginx-6c57499ddb-pjql2   1/1     Running   1 (17m ago)   55m   10.224.1.15   worker01   <none>           <none>
nginx-6c57499ddb-tfghq   1/1     Running   1 (17m ago)   55m   10.224.1.14   worker01   <none>           <none>
nginx-6c57499ddb-zg88c   1/1     Running   1 (17m ago)   55m   10.224.1.17   worker01   <none>           <none>

Podそれぞれ表示を変える

kubectl exec -it  nginx-6c57499ddb-pjql2 -- bash
echo pod1 | tee /usr/share/nginx/html/index.html
exit

kubectl exec -it  nginx-6c57499ddb-tfghq -- bash
echo pod2 | tee /usr/share/nginx/html/index.html
exit

kubectl exec -it  nginx-6c57499ddb-zg88c -- bash
echo pod3 | tee /usr/share/nginx/html/index.html
exit

3.workerノードで確認

root@worker01:~# curl 10.224.1.15
pod1
root@worker01:~# curl 10.224.1.14
pod2
root@worker01:~# curl 10.224.1.17
pod3

4.cluster IP 作成

cluster ipの8080にアクセスが来ると、nginx-appへ渡す

clusterIp.yaml

apiVersion: v1
kind: Service
metadata:
  name: cluster-ip
spec:
  type: ClusterIP
  ports:
    - name: cluster-port
      port: 8080
      targetPort: 80
  selector:
    app: nginx-app
# kubectl create -f clusterIp.yaml
# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
cluster-ip   ClusterIP   10.105.186.186   <none>        8080/TCP   10m
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP    85m

5.再度workerノードで確認

cluster ipの8080にアクセスすると、podどれかにアクセスする

root@worker01:~# curl 10.105.186.186:8080
pod3
root@worker01:~# curl 10.105.186.186:8080
pod2
root@worker01:~# curl 10.105.186.186:8080
pod1
root@worker01:~# curl 10.105.186.186:8080
pod2
root@worker01:~# curl 10.105.186.186:8080
pod1
root@worker01:~# curl 10.105.186.186:8080
pod2
root@worker01:~# curl 10.105.186.186:8080
pod1
root@worker01:~# curl 10.105.186.186:8080
pod2
root@worker01:~# curl 10.105.186.186:8080
pod1

6. ExternalIP用意

externalIp.yaml

apiVersion: v1
kind: Service
metadata:
  name: external-ip
spec:
  type: ClusterIP
  externalIPs:
    - 172.16.0.159
  ports:
    - name: cluster-port
      port: 8080
      targetPort: 80
  selector:
    app: nginx-app
# kubectl create -f externalIp.yaml
# kubectl get svc external-ip 
NAME          TYPE        CLUSTER-IP       EXTERNAL-IP    PORT(S)    AGE
external-ip   ClusterIP   10.101.128.182   172.16.0.159   8080/TCP   83s

7.次は外部からアクセス可能

root@jump:~# curl 172.16.0.159:8080
pod3
root@jump:~# curl 172.16.0.159:8080
pod1
root@jump:~# curl 172.16.0.159:8080
pod1
root@jump:~# curl 172.16.0.159:8080
pod3
root@jump:~# curl 172.16.0.159:8080
pod2
06_virtualization/05_container/13_kubernetes_create_nginx.txt · 最終更新: 2022/04/15 19:39 by matsui