はじめに
前回、Kubernetesクラスタを構築することが出来ました。
こちらの環境を使用して、コンテナをデプロイしていきましょう。
Kubernetesの公式にチュートリアルが載っています。
Kubernetesクラスタ環境を持っていない場合は、ブラウザ上で操作できるkatacodaが用意されているので、これを使用するのも良いと思います。
https://kubernetes.io/docs/tutorials/kubernetes-basics/
Kubernetesのオブジェクトについて
Kubernetesは、コンテナを管理するために、いくつかのオブジェクトという概念が存在しています
Pod : コンテナを管理するための単位。Pod内に1以上のコンテナが含まれている
Replicaset : Podの冗長性を担保するための単位。Podが2個以上存在する事、といったルールを指定することで、それを守るように自動的に稼働してくれる
Deployment : ReplicaSetを管理するための単位。
Service : Podへのアクセス方法を定義する論理的な概念
詳細は以下URLを参照
Pod, Replicaset, Deploymentについて
https://qiita.com/tkusumi/items/01cd18c59b742eebdc6a#deployment
Serviceについて
https://qiita.com/MahoTakara/items/d18d8f9b36416353066c
Deploymentを作成
DockerHubにある Nginx イメージを使用して、Deploymentを作成します。
kubectl run nginx-test01 --image=nginx --port=80
memo kubernetes-bootcampの起動
kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl run nginx-test01 --image=nginx --port=80
deployment "nginx-test01" created
確認
Deploymentが作成されています
[root@sugi-kubernetes19-master01 ~]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-test01 1 1 1 0 23s
確認
初めは creating のステータスですが
一定時間後、podsがReadyとなります
[root@sugi-kubernetes19-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-test01-75b4dbd59d-bk7wd 1/1 Running 0 46s
podsの稼働しているNodeを確認します
[root@sugi-kubernetes19-master01 ~]# kubectl describe pods nginx-test01
Name: nginx-test01-75b4dbd59d-bk7wd
Namespace: default
Node: sugi-kubernetes19-node02.localdomain/192.168.120.224
Start Time: Sat, 17 Mar 2018 22:49:08 +0900
Labels: pod-template-hash=3160868158
run=nginx-test01
Annotations: <none>
Status: Running
IP: 10.244.2.2
Controlled By: ReplicaSet/nginx-test01-75b4dbd59d
Containers:
nginx-test01:
Container ID: docker://77497d5f597af7764bd68624957b34577a525b5fca5f084ee1d7f1c6c0a1e16f
Image: nginx
Image ID: docker-pullable://docker.io/nginx@sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6
Port: 80/TCP
State: Running
Started: Sat, 17 Mar 2018 22:49:46 +0900
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-wfq8g (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-wfq8g:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-wfq8g
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 1m default-scheduler Successfully assigned nginx-test01-75b4dbd59d-bk7wd to sugi-kubernetes19-node02.localdomain
Normal SuccessfulMountVolume 1m kubelet, sugi-kubernetes19-node02.localdomain MountVolume.SetUp succeeded for volume "default-token-wfq8g"
Normal Pulling 1m kubelet, sugi-kubernetes19-node02.localdomain pulling image "nginx"
Normal Pulled 40s kubelet, sugi-kubernetes19-node02.localdomain Successfully pulled image "nginx"
Normal Created 40s kubelet, sugi-kubernetes19-node02.localdomain Created container
Normal Started 40s kubelet, sugi-kubernetes19-node02.localdomain Started container
node02上で直接 docker ps を実行してみます
docker.io/nginx が動作しています。
それ以外にも、様々な管理用のコンテナが動いています。
あら?「registry.access.redhat.com/rhel7/pod-infrastructure:latest」イメージのコンテナが動いています
pause コンテナと呼ばれているものは、何物か不明
https://access.redhat.com/documentation/ja-jp/red_hat_enterprise_linux_atomic_host/7/html/getting_started_with_containers/creating_a_kubernetes_cluster_to_run_docker_formatted_container_images
[root@sugi-kubernetes19-node02 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
77497d5f597a docker.io/nginx@sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6 "nginx -g 'daemon ..." About a minute ago Up About a minute k8s_nginx-test01_nginx-test01-75b4dbd59d-bk7wd_default_f716bdd2-29e9-11e8-9843-0050569817ee_0
7808687f3c3b gcr.io/google_containers/pause-amd64:3.0 "/pause" 2 minutes ago Up About a minute k8s_POD_nginx-test01-75b4dbd59d-bk7wd_default_f716bdd2-29e9-11e8-9843-0050569817ee_0
ed67ec5eb244 quay.io/coreos/flannel@sha256:056cf57fd3bbe7264c0be1a3b34ec2e289b33e51c70f332f4e88aa83970ad891 "/opt/bin/flanneld..." 34 minutes ago Up 34 minutes k8s_kube-flannel_kube-flannel-ds-tvvbj_kube-system_419e6c68-29e5-11e8-9843-0050569817ee_1
62f9d1c12a1c gcr.io/google_containers/kube-proxy-amd64@sha256:424a9dfc295f26f9d1e8070836d6fa08c83f22d86e592dfccddc847a85b1ef20 "/usr/local/bin/ku..." 34 minutes ago Up 34 minutes k8s_kube-proxy_kube-proxy-gnnvw_kube-system_419f820e-29e5-11e8-9843-0050569817ee_0
4dab920df62e gcr.io/google_containers/pause-amd64:3.0 "/pause" 35 minutes ago Up 35 minutes k8s_POD_kube-proxy-gnnvw_kube-system_419f820e-29e5-11e8-9843-0050569817ee_0
c14fa76c1f1a gcr.io/google_containers/pause-amd64:3.0 "/pause" 35 minutes ago Up 35 minutes k8s_POD_kube-flannel-ds-tvvbj_kube-system_419e6c68-29e5-11e8-9843-0050569817ee_0
Podに接続 内部から接続
Deploymentを作成し、Podが生成されただけでは、Podは内部NWに接続されており、公開されていない状態です。
動作確認やDebugのために、内部NWに接続されているPodに接続するために、kube proxy コマンドを使用して、プロキシー接続することが出来ます
kubectl proxy
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl proxy
Starting to serve on 127.0.0.1:8001
Proxyに接続するには、以下のURLに接続すると指定したPodと通信することが出来ます
http://localhost:8001/api/v1/proxy/namespaces/default/pods/$POD_NAME/
podの名前を以下コマンドで取得します
[root@sugi-kubernetes-master01 ~]# kubectl describe pods nginx-test01 | grep ^Name:
Name: nginx-test01-75b4dbd59d-bk7wd
curlでpodで稼働しているnginxにhttp接続します
curl http://localhost:8001/api/v1/proxy/namespaces/default/pods/nginx-test01-75b4dbd59d-bk7wd/
実行例
[root@sugi-kubernetes19-master01 ~]# curl http://localhost:8001/api/v1/proxy/namespaces/default/pods/nginx-test01-75b4dbd59d-bk7wd/
<!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>
Pod内のコンテナは、通常、STDOUにアプリケーションログを出力します。
lubectl logs コマンドでログを確認することが出来ます
kubectl logs nginx-test01-75b4dbd59d-bk7wd
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl logs nginx-test01-75b4dbd59d-bk7wd
10.244.0.0 - - [17/Mar/2018:14:07:37 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "127.0.0.1, 192.168.120.220"x
kubectl exec コマンドを使用することで、Pod内に直接入ってコマンドを実行することができます。
このために exec コマンドで各種環境パラメータを確認することが出来ます
kubectl exec nginx-test01-75b4dbd59d-bk7wd env
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl exec nginx-test01-75b4dbd59d-bk7wd env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=nginx-test01-75b4dbd59d-bk7wd
KUBERNETES_PORT_443_TCP_PROTO=tcp
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1
KUBERNETES_SERVICE_HOST=10.96.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
KUBERNETES_PORT=tcp://10.96.0.1:443
KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443
NGINX_VERSION=1.13.9-1~stretch
NJS_VERSION=1.13.9.0.1.15-1~stretch
HOME=/root
Podでbashを実行して、表示します
kubectl exec -it nginx-test01-75b4dbd59d-bk7wd bash
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl exec -it nginx-test01-75b4dbd59d-bk7wd bash
root@nginx-test01-75b4dbd59d-bk7wd:/#
root@nginx-test01-75b4dbd59d-bk7wd:/# pwd
/
root@nginx-test01-75b4dbd59d-bk7wd:/#
root@nginx-test01-75b4dbd59d-bk7wd:/# ls -la
total 8
drwxr-xr-x 1 root root 28 Mar 17 13:49 .
drwxr-xr-x 1 root root 28 Mar 17 13:49 ..
-rwxr-xr-x 1 root root 0 Mar 17 13:49 .dockerenv
drwxr-xr-x 2 root root 4096 Mar 12 00:00 bin
drwxr-xr-x 2 root root 6 Feb 23 23:23 boot
drwxr-xr-x 5 root root 360 Mar 17 13:49 dev
drwxr-xr-x 1 root root 66 Mar 17 13:49 etc
drwxr-xr-x 2 root root 6 Feb 23 23:23 home
drwxr-xr-x 1 root root 45 Mar 12 00:00 lib
drwxr-xr-x 2 root root 34 Mar 12 00:00 lib64
drwxr-xr-x 2 root root 6 Mar 12 00:00 media
drwxr-xr-x 2 root root 6 Mar 12 00:00 mnt
drwxr-xr-x 2 root root 6 Mar 12 00:00 opt
dr-xr-xr-x 185 root root 0 Mar 17 13:49 proc
drwx------ 2 root root 37 Mar 12 00:00 root
drwxr-xr-x 1 root root 38 Mar 17 13:49 run
drwxr-xr-x 2 root root 4096 Mar 12 00:00 sbin
drwxr-xr-x 2 root root 6 Mar 12 00:00 srv
dr-xr-xr-x 13 root root 0 Mar 17 13:49 sys
drwxrwxrwt 1 root root 6 Mar 14 08:14 tmp
drwxr-xr-x 1 root root 66 Mar 12 00:00 usr
root@nginx-test01-75b4dbd59d-bk7wd:/# cat /usr/share/nginx/html/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のwelcomeページを書き換えてみます
bodyに deployment 名を付与してみました
cat <<'EOF' > /usr/share/nginx/html/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! from test01</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>
EOF
bashから抜けます (Dockerだとexitすると、コンテナ自体終了する場合がありましたが、Kubernetesだと関係ない?)
exit
kubectlのproxy経由で再度アクセスすると、先ほど書き換えた内容が反映されています
[root@sugi-kubernetes19-master01 ~]# curl http://localhost:8001/api/v1/proxy/namespaces/default/pods/nginx-test01-75b4dbd59d-bk7wd/
<!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! from test01</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>
Podに接続 外部から接続
Serviceという概念を確認します。
Serviceは主に以下の内容を提供する機能です(ServiceのTypeぶいさ)
- DeploymentでPodを作成しても、Kubernetesクラスタ外からはアクセス出来ません。
これを外部からアクセスを許可します - Node01サーバに障害が発生した際に、01で動作していたPodは02に移動します。
これにともなってPod側はIPアドレスが変更されてしまいますが、Service側で宛先をルーティングしてくれるので
外部の接続元は、アクセス先を変更する必要がなくなります - 複数のPodで負荷分散する際に、Serviceがロードバランスしてくれます (Typeによる)
ServiceのTypeは、現在以下の4種類が存在しています
-
ClusterIP (Default)
Kubernetesクラスタの内部公開用のサービス。クラスタ外からはアクセスが出来ない
どのpodにリクエストを振り分けるかは、Labelで設定 -
NodePort
クラスタ外部からアクセスする際に、このTypeを使用します。
作成時に、nodePortとよばれるポートが自動的に割り当てられ、すべてのnodeに対して、nodeのip:nodePortで該当podにアクセスできるようになる。(ClusterIPと同じく、podの行き先はlabelで設定)
nodePortは明示的に指定できるが、重複をさけるためk8sに任せた方がよい。
どのPortにアクセスしても、同一の結果が得られるが、どのPortが生きているかを判断する必要がある -
LoadBalancer
クラウドプロバイダーによるロードバランサーで、Podへのアクセスを公開します。
例)AWS, GCP, Azure, OpenStack 。 -
ExternalName
他のとは違い、podから外部サービスを参照したい際に使う
現在のServiceの一覧を確認します
Defaultでkubernetesという名前のClusterIPが作成されていました
[root@sugi-kubernetes19-master01 ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1h
以下のコマンドでDeploymentに対して、Service NodePort を作成します。
kubectl expose deployment/nginx-test01 --type="NodePort" --port 80
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl expose deployment/nginx-test01 --type="NodePort" --port 80
service "nginx-test01" exposed
Service一覧を確認します
nginx-test01 に対して、NodePortが作成されています。
[root@sugi-kubernetes19-master01 ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 15h
nginx-test01 NodePort 10.101.49.239 <none> 80:30112/TCP 3s
Serviceの詳細を確認します
[root@sugi-kubernetes19-master01 ~]# kubectl describe service/nginx-test01
Name: nginx-test01
Namespace: default
Labels: run=nginx-test01
Annotations: <none>
Selector: run=nginx-test01
Type: NodePort
IP: 10.101.49.239
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 30112/TCP
Endpoints: 10.244.2.2:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
アクセステスト
Kubernetesクラスタ外(192.168.120.252/24)からアクセス
NodePortを作成したので、全てのMaster/Nodeに対してアクセスすることが出来る
master01宛 : curl 192.168.120.220:30112
node01宛 : curl 192.168.120.223:30112
node02宛 : curl 192.168.120.224:30112
実行例
Master01宛
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30112
<!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! from test01</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>
[root@sugi-bastion-centos ~]#
node01宛
[root@sugi-bastion-centos ~]# curl 192.168.120.223:30112
<!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! from test01</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>
[root@sugi-bastion-centos ~]#
node02宛
[root@sugi-bastion-centos ~]# curl 192.168.120.224:30112
<!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! from test01</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>
[root@sugi-bastion-centos ~]#
Deploymentに、自動的にPodアクセスのためのLabelsが設定されました。
[root@sugi-kubernetes19-master01 ~]# kubectl describe deployment/nginx-test01
Name: nginx-test01
Namespace: default
CreationTimestamp: Sat, 17 Mar 2018 22:49:08 +0900
Labels: run=nginx-test01
Annotations: deployment.kubernetes.io/revision=1
Selector: run=nginx-test01
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: run=nginx-test01
Containers:
nginx-test01:
Image: nginx
Port: 80/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-test01-75b4dbd59d (1/1 replicas created)
Events: <none>
kubectlでget pods する際に、引数を渡すことで、指定したlabelsを持つpodをlistすることが出来ます
kubectl get pods -l run=nginx-test01
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl get pods -l run=nginx-test01
NAME READY STATUS RESTARTS AGE
nginx-test01-75b4dbd59d-bk7wd 1/1 Running 0 15h
同様にServiceも表示することができます
kubectl get services -l run=nginx-test01
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl get services -l run=nginx-test01
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-test01 NodePort 10.101.49.239 <none> 80:30112/TCP 27m
既に実行しているPodにLabelを追加することが出来ます
追加前のLabelsを確認します
[root@sugi-kubernetes19-master01 ~]# kubectl describe pods nginx-test01-75b4dbd59d-bk7wd
Name: nginx-test01-75b4dbd59d-bk7wd
Namespace: default
Node: sugi-kubernetes19-node02.localdomain/192.168.120.224
Start Time: Sat, 17 Mar 2018 22:49:08 +0900
Labels: pod-template-hash=3160868158
run=nginx-test01
Annotations: <none>
Status: Running
IP: 10.244.2.2
Controlled By: ReplicaSet/nginx-test01-75b4dbd59d
Containers:
nginx-test01:
Container ID: docker://77497d5f597af7764bd68624957b34577a525b5fca5f084ee1d7f1c6c0a1e16f
Image: nginx
Image ID: docker-pullable://docker.io/nginx@sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6
Port: 80/TCP
State: Running
Started: Sat, 17 Mar 2018 22:49:46 +0900
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-wfq8g (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-wfq8g:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-wfq8g
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>
app=vl というラベルを付与します
kubectl label pod nginx-test01-75b4dbd59d-bk7wd app=v1
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl label pod nginx-test01-75b4dbd59d-bk7wd app=v1
pod "nginx-test01-75b4dbd59d-bk7wd" labeled
付与されているか、確認します
Labelsに追加されています
[root@sugi-kubernetes19-master01 ~]# kubectl describe pods nginx-test01-75b4dbd59d-bk7wd
Name: nginx-test01-75b4dbd59d-bk7wd
Namespace: default
Node: sugi-kubernetes19-node02.localdomain/192.168.120.224
Start Time: Sat, 17 Mar 2018 22:49:08 +0900
Labels: app=v1
pod-template-hash=3160868158
run=nginx-test01
Annotations: <none>
Status: Running
IP: 10.244.2.2
Controlled By: ReplicaSet/nginx-test01-75b4dbd59d
Containers:
nginx-test01:
Container ID: docker://77497d5f597af7764bd68624957b34577a525b5fca5f084ee1d7f1c6c0a1e16f
Image: nginx
Image ID: docker-pullable://docker.io/nginx@sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6
Port: 80/TCP
State: Running
Started: Sat, 17 Mar 2018 22:49:46 +0900
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-wfq8g (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-wfq8g:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-wfq8g
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events: <none>
新たに付与したLabelを使用して、一覧表示することができます
kubectl get pods -l app=v1
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl get pods -l app=v1
NAME READY STATUS RESTARTS AGE
nginx-test01-75b4dbd59d-bk7wd 1/1 Running 0 15h
指定したlabelを使用しているserviceを削除することも出来ます
kubectl delete service -l run=nginx-test01
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl delete service -l run=nginx-test01
service "nginx-test01" deleted
削除されています
[root@sugi-kubernetes19-master01 ~]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16h
Clusterの外部から、NodePort宛にアクセスすると、アクセスすることが出来ません
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30112
curl: (7) Failed connect to 192.168.120.220:30112; Connection refused
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]# curl 192.168.120.223:30112
curl: (7) Failed connect to 192.168.120.223:30112; Connection refused
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]# curl 192.168.120.224:30112
curl: (7) Failed connect to 192.168.120.224:30112; Connection refused
クラスタ内部で直接確認し、アプリケーションが実行されていることを確認します
proxyを起動し、
[root@sugi-kubernetes19-master01 ~]# kubectl proxy
Starting to serve on 127.0.0.1:8001
curlでlocalhostアクセスをします
[root@sugi-kubernetes19-master01 ~]# curl http://localhost:8001/api/v1/proxy/namespaces/default/pods/nginx-test01-75b4dbd59d-bk7wd/
<!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! from test01</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>
スケールアウト
現在のDeploymentを確認します
[root@sugi-kubernetes19-master01 ~]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-test01 1 1 1 1 15h
次に、kubectl scale コマンドを使用して、replicasを4に更新します
kubectl scale deployments/nginx-test01 --replicas=4
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl scale deployments/nginx-test01 --replicas=4
deployment "nginx-test01" scaled
現在のDeploymentを確認
作成中です
[root@sugi-kubernetes19-master01 ~]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-test01 4 4 4 1 15h
全て準備完了になりました
[root@sugi-kubernetes19-master01 ~]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-test01 4 4 4 4 15h
Podsの一覧を確認します
-o wide を使用すると、配置先も表示されて便利です。
IPアドレスも異なるIPアドレスをそれぞれ保有しています
[root@sugi-kubernetes19-master01 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-test01-75b4dbd59d-bk7wd 1/1 Running 0 15h 10.244.2.2 sugi-kubernetes19-node02.localdomain
nginx-test01-75b4dbd59d-w9577 1/1 Running 0 50s 10.244.1.3 sugi-kubernetes19-node01.localdomain
nginx-test01-75b4dbd59d-wrxtb 1/1 Running 0 50s 10.244.2.3 sugi-kubernetes19-node02.localdomain
nginx-test01-75b4dbd59d-z5rvv 1/1 Running 0 50s 10.244.1.2 sugi-kubernetes19-node01.localdomain
MasterNodeから各種Podへ Flannel を介して、pingが出来ます
[root@sugi-kubernetes19-master01 ~]# ping 10.244.2.2
PING 10.244.2.2 (10.244.2.2) 56(84) bytes of data.
64 bytes from 10.244.2.2: icmp_seq=1 ttl=63 time=0.489 ms
64 bytes from 10.244.2.2: icmp_seq=2 ttl=63 time=0.386 ms
^C
--- 10.244.2.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.386/0.437/0.489/0.055 ms
[root@sugi-kubernetes19-master01 ~]# ping 10.244.1.3
PING 10.244.1.3 (10.244.1.3) 56(84) bytes of data.
64 bytes from 10.244.1.3: icmp_seq=1 ttl=63 time=0.694 ms
64 bytes from 10.244.1.3: icmp_seq=2 ttl=63 time=0.506 ms
^C
--- 10.244.1.3 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.506/0.600/0.694/0.094 ms
[root@sugi-kubernetes19-master01 ~]# ping 10.244.2.3
PING 10.244.2.3 (10.244.2.3) 56(84) bytes of data.
64 bytes from 10.244.2.3: icmp_seq=1 ttl=63 time=0.500 ms
64 bytes from 10.244.2.3: icmp_seq=2 ttl=63 time=0.522 ms
^C
--- 10.244.2.3 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.500/0.511/0.522/0.011 ms
[root@sugi-kubernetes19-master01 ~]# ping 10.244.1.2
PING 10.244.1.2 (10.244.1.2) 56(84) bytes of data.
64 bytes from 10.244.1.2: icmp_seq=1 ttl=63 time=0.397 ms
64 bytes from 10.244.1.2: icmp_seq=2 ttl=63 time=0.519 ms
^C
--- 10.244.1.2 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999
該当のNodeでdockerが動作しています
[root@sugi-kubernetes19-node01 ~]# docker ps | grep nginx
f88420cdb9ec docker.io/nginx@sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6 "nginx -g 'daemon ..." 2 minutes ago Up 2 minutes k8s_nginx-test01_nginx-test01-75b4dbd59d-z5rvv_default_a20e8d9c-2a6b-11e8-9843-0050569817ee_0
d09b7be09e60 docker.io/nginx@sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6 "nginx -g 'daemon ..." 2 minutes ago Up 2 minutes k8s_nginx-test01_nginx-test01-75b4dbd59d-w9577_default_a20c2eac-2a6b-11e8-9843-0050569817ee_0
47a57119ed0a gcr.io/google_containers/pause-amd64:3.0 "/pause" 2 minutes ago Up 2 minutes k8s_POD_nginx-test01-75b4dbd59d-z5rvv_default_a20e8d9c-2a6b-11e8-9843-0050569817ee_0
a2e9b6d5d048 gcr.io/google_containers/pause-amd64:3.0 "/pause" 2 minutes ago Up 2 minutes k8s_POD_nginx-test01-75b4dbd59d-w9577_default_a20c2eac-2a6b-11e8-9843-0050569817ee_0
[root@sugi-kubernetes19-node02 ~]# docker ps | grep nginx
5bb17b9cfed7 docker.io/nginx@sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6 "nginx -g 'daemon ..." 2 minutes ago Up 2 minutes k8s_nginx-test01_nginx-test01-75b4dbd59d-wrxtb_default_a20e6662-2a6b-11e8-9843-0050569817ee_0
ea8cd30caaa9 gcr.io/google_containers/pause-amd64:3.0 "/pause" 2 minutes ago Up 2 minutes k8s_POD_nginx-test01-75b4dbd59d-wrxtb_default_a20e6662-2a6b-11e8-9843-0050569817ee_0
77497d5f597a docker.io/nginx@sha256:f6e250eaa36af608af9ed1e4751f063f0ca0f5310b1a5d3ad9583047256f37f6 "nginx -g 'daemon ..." 15 hours ago Up 15 hours k8s_nginx-test01_nginx-test01-75b4dbd59d-bk7wd_default_f716bdd2-29e9-11e8-9843-0050569817ee_0
7808687f3c3b gcr.io/google_containers/pause-amd64:3.0 "/pause" 15 hours ago Up 15 hours k8s_POD_nginx-test01-75b4dbd59d-bk7wd_default_f716bdd2-29e9-11e8-9843-0050569817ee_0
deploymentの詳細を確認します
Replicas が 4となっていることを確認できます
[root@sugi-kubernetes19-master01 ~]# kubectl describe deployment nginx-test01
Name: nginx-test01
Namespace: default
CreationTimestamp: Sat, 17 Mar 2018 22:49:08 +0900
Labels: run=nginx-test01
Annotations: deployment.kubernetes.io/revision=1
Selector: run=nginx-test01
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: run=nginx-test01
Containers:
nginx-test01:
Image: nginx
Port: 80/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-test01-75b4dbd59d (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 6m deployment-controller Scaled up replica set nginx-test01-75b4dbd59d to 4
podも4個作成されています
[root@sugi-kubernetes19-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-test01-75b4dbd59d-bk7wd 1/1 Running 0 15h
nginx-test01-75b4dbd59d-w9577 1/1 Running 0 20m
nginx-test01-75b4dbd59d-wrxtb 1/1 Running 0 20m
nginx-test01-75b4dbd59d-z5rvv 1/1 Running 0 20m
どのpodが接続したか判別するために、各podのnginxのwelcomeページを編集します
bk7wd を対象
kubectl exec -it nginx-test01-75b4dbd59d-bk7wd bash
welcomeページ書き換え
cat <<'EOF' > /usr/share/nginx/html/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! from bk7wd</h1>
</body>
</html>
EOF
podから抜ける
exit
残りの3podも同様に編集する
w9577
kubectl exec -it nginx-test01-75b4dbd59d-w9577 bash
cat <<'EOF' > /usr/share/nginx/html/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! from w9577</h1>
</body>
</html>
EOF
exit
wrxtb
kubectl exec -it nginx-test01-75b4dbd59d-wrxtb bash
cat <<'EOF' > /usr/share/nginx/html/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! from wrxtb</h1>
</body>
</html>
EOF
exit
z5rvv
kubectl exec -it nginx-test01-75b4dbd59d-z5rvv bash
cat <<'EOF' > /usr/share/nginx/html/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! from z5rvv</h1>
</body>
</html>
EOF
exit
DeploymentにService NodePort を作成します
kubectl expose deployment/nginx-test01 --type="NodePort" --port 80
作成されたserviceを確認します
Portは30674を使用しています
[root@sugi-kubernetes19-master01 ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16h
nginx-test01 NodePort 10.103.139.147 <none> 80:30674/TCP 17s
クラスター外部からアクセスすると、NodePortでロードバランスされていることが確認できます
ロードバランスポリシーは、ランダム?
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<!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! from z5rvv</h1>
</body>
</html>
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<!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! from z5rvv</h1>
</body>
</html>
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<!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! from wrxtb</h1>
</body>
</html>
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<!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! from z5rvv</h1>
</body>
</html>
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]#
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<!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! from wrxtb</h1>
</body>
</html>
次にDeploymentのReplicaを4から2に変更します
変更前のpod一覧を確認します
[root@sugi-kubernetes19-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-test01-75b4dbd59d-bk7wd 1/1 Running 0 15h
nginx-test01-75b4dbd59d-w9577 1/1 Running 0 29m
nginx-test01-75b4dbd59d-wrxtb 1/1 Running 0 29m
nginx-test01-75b4dbd59d-z5rvv 1/1 Running 0 29m
repicaを減らして、スケールイン
kubectl scale deployments/nginx-test01 --replicas=2
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl scale deployments/nginx-test01 --replicas=2
deployment "nginx-test01" scaled
podが減っていることを確認出来ます
[root@sugi-kubernetes19-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-test01-75b4dbd59d-bk7wd 1/1 Running 0 15h
nginx-test01-75b4dbd59d-wrxtb 1/1 Running 0 31m
外部からNodePortへのアクセスも、2個に減っています
[root@sugi-bastion-centos ~]# curl 192.168.120.223:30674
<!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! from wrxtb</h1>
</body>
</html>
[root@sugi-bastion-centos ~]# curl 192.168.120.223:30674
<!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! from bk7wd</h1>
</body>
</html>
[root@sugi-bastion-centos ~]#
コンテナのローリングアップデート, ロールバック
ローリングアップデートをわかりやすくするために、Replicaを4に変更します
kubectl scale deployments/nginx-test01 --replicas=4
4へスケールアウトされています
[root@sugi-kubernetes19-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-test01-75b4dbd59d-bk7wd 1/1 Running 0 16h
nginx-test01-75b4dbd59d-tf7r4 1/1 Running 0 4m
nginx-test01-75b4dbd59d-vn29c 1/1 Running 0 4m
nginx-test01-75b4dbd59d-wrxtb 1/1 Running 0 45m
以下コマンドでコンテナのimageをアップデート出来ます。
kubectl set image deployments/nginx-test01 nginx-test01=httpd
実行例
[root@sugi-kubernetes19-master01 ~]# kubectl set image deployments/nginx-test01 nginx-test01=httpd
deployment "nginx-test01" image updated
podの状態
ローリングアップデートされています
[root@sugi-kubernetes19-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-test01-75b4dbd59d-bk7wd 1/1 Running 0 16h
nginx-test01-75b4dbd59d-vn29c 1/1 Running 0 13m
nginx-test01-75b4dbd59d-wrxtb 1/1 Running 0 53m
nginx-test01-946b57466-2rkms 0/1 ContainerCreating 0 1m
nginx-test01-946b57466-gvddq 0/1 ContainerCreating 0 1m
外部からアクセスすると、httpdイメージのindexが表示されています
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<html><body><h1>It works!</h1></body></html>
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<html><body><h1>It works!</h1></body></html>
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<html><body><h1>It works!</h1></body></html>
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<html><body><h1>It works!</h1></body></html>
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<html><body><h1>It works!</h1></body></html>
rolloutコマンドでステータスを確認します
successfullyと表示されると、アップデートが完了されていることを確認できます
[root@sugi-kubernetes19-master01 ~]# kubectl rollout status deployments/nginx-test01
deployment "nginx-test01" successfully rolled out
アップデートを取り消ししてみます
kubectl rollout undo deployments/nginx-test01
ロールバックされています (nginxイメージでコンテナを再作成するだけなので、コンテナ内で編集した内容は破棄される)
[root@sugi-kubernetes19-master01 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-test01-75b4dbd59d-2wrpj 0/1 ContainerCreating 0 4s
nginx-test01-75b4dbd59d-bs6jv 0/1 ContainerCreating 0 4s
nginx-test01-946b57466-2rkms 1/1 Running 0 6m
nginx-test01-946b57466-5f5vp 0/1 Terminating 0 5m
nginx-test01-946b57466-gvddq 1/1 Running 0 6m
nginx-test01-946b57466-mjszk 1/1 Running 0 5m
外部からNodePortへアクセスしてみると、nginxへ戻っています
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
<!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>
[root@sugi-bastion-centos ~]# curl 192.168.120.220:30674
参考URL
FlannelのVXLANバックエンドの仕組み
http://enakai00.hatenablog.com/entry/2015/04/02/173739
kubernetesチュートリアル
https://kubernetes.io/docs/tutorials/kubernetes-basics/
https://www.kaitoy.xyz/2017/10/11/goslings-on-kubernetes-cont/
https://qiita.com/tkusumi/items/01cd18c59b742eebdc6a#deployment
GKEのロードバランサーをDeepdive
https://qiita.com/apstndb/items/9d13230c666db80e74d0
Pod, Replicaset, Deploymentについて
https://qiita.com/tkusumi/items/01cd18c59b742eebdc6a#deployment
Serviceについて
https://qiita.com/MahoTakara/items/d18d8f9b36416353066c
Nginxについて
http://tigertaizo.hatenablog.com/entry/2015/10/18/172826