15Stepで習得 Dockerから入るKubernetes その3

マニフェストファイル

今回はマニフェストファイルを作成してアプリケーションを記述し、実行する

マニフェストファイルの作成

Podのマニフェストファイル(nginxのサンプル)を作成

1
2
3
4
5
6
7
8
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:latest

マニフェストファイルの記述方法は
https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/
にあるがよくわからない
kindPodにすると、Podのマニフェストファイルとなる。

マニフェストファイルの適用

以下のコマンドで適用する

1
$ kubectl apply -f 作成したマニフェストファイル

実際に作成したファイルはkubernetes/nginx-pod.ymlなので以下を実行する

1
2
3
4
5
$ kubectl apply -f kubernetes/nginx-pod.yml
pod/nginx created
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 7s

クラスタネットワーク上のPodのIPアドレスを表示するには、-o wide オプションを追加する

1
2
3
$ kubectl get pod nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx 1/1 Running 0 52s 172.17.0.4 minikube <none> <none>

表示されているIPアドレスに80番でアクセスしてみると、ERR_SOCKET_NOT_CONNECTEDが返ってくる
クラスタネットワーク上のPodにクラスタネットワーク外からアクセスするためにはServiceを利用する必要がある
Serviceはクラスタネットワークのロードバランサ的な存在
今回はServiceなしで動作確認するために、クラスタネットワーク内にPodを作成してテストする

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
$ kubectl run busybox --image=busybox --restart=Never --rm -it sh
If you don't see a command prompt, try pressing enter.

/ # ifconfig
eth0 Link encap:Ethernet HWaddr 02:42:AC:11:00:05
inet addr:172.17.0.5 Bcast:172.17.255.255 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

/ # wget http://172.17.0.4
Connecting to 172.17.0.4 (172.17.0.4:80)
saving to 'index.html'
index.html 100% |********************************| 612 0:00:00 ETA
'index.html' saved
/ # more index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
/ #

nginxが動作していることが確認できた

次はDeploymentの作成をマニフェストファイルで行う