$ kubectl apply -f deploy.yaml
deployment.apps "nginx" created
$ kubectl get deploy
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 13s
$ kubectl get po
NAME READY STATUS RESTARTS AGE
nginx-7db75b8b78-4wfgd 1/1 Running 0 16s
nginx-7db75b8b78-dfc5t 1/1 Running 0 16s
nginx-7db75b8b78-pmk4p 1/1 Running 0 16s
$ kubectl apply -f svc.yaml
service "nginx" created
Serviceを取得するにはserviceと指定するか、短縮形のsvcを指定する。
1
2
3
4
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15m
nginx ClusterIP 10.107.90.221 <none> 80/TCP 1m
また、allを指定すると
Deployment
ReplicaSet
Pod
Service
を取得できる。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ kubectl get all
NAME READY STATUS RESTARTS AGE
nginx-7db75b8b78-4wfgd 1/1 Running 0 5m
nginx-7db75b8b78-dfc5t 1/1 Running 0 5m
nginx-7db75b8b78-pmk4p 1/1 Running 0 5m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16m
nginx ClusterIP 10.107.90.221 <none> 80/TCP 2m
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 5m
NAME DESIRED CURRENT READY AGE
nginx-7db75b8b78 3 3 3 5m
/ # wget -O - $NGINX_SERVICE_HOST
Connecting to 10.107.90.221 (10.107.90.221:80)
<!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>
- 100% |********************************************| 612 0:00:00 ETA
/ # wget -O - nginx
Connecting to nginx (10.107.90.221:80)
<!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>
- 100% |********************************************| 612 0:00:00 ETA
Service名でアクセスすることができた。
minikubeでのNodePortへのアクセス
先程確認したが、Serviceを取得してNodePortのポート番号が取得できていた。
1
2
3
$ kubectl get svc/nginx-np
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-np NodePort 10.102.99.100 <none> 80:32109/TCP 34m
このポート番号でアクセスすればnginxの画面が表示できる。minikubeでは minikube service というコマンドにService名を渡すとNodePortで公開されているServiceのURLを組み立てて表示してくれる。
1
2
$ minikube service nginx-np --url
http://192.168.99.100:32109
$ kubectl apply -f svc-headless.yaml
service "nginx-headless" created
$ kubectl get svc/nginx-headless
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-headless ClusterIP None <none> <none> 11s
Cluster IPがNoneで作成されていることがわかる。
このHeadless Serviceの使い方はDNSを引いてみるとわかる。
1
2
3
4
5
6
7
8
9
10
11
12
$ kubectl run alpine -it --rm --image alpine -- ash
If you don't see a command prompt, try pressing enter.
/ # nslookup nginx-headless
Server: 10.96.0.10
Address: 10.96.0.10#53
Name: nginx-headless.default.svc.cluster.local
Address: 172.17.0.3
Name: nginx-headless.default.svc.cluster.local
Address: 172.17.0.2
Name: nginx-headless.default.svc.cluster.local
Address: 172.17.0.6