CentOS Stream 9
Sponsored Link

Kubernetes : Pod デプロイ2023/10/19

 

Pod 作成等の基本操作です。

当例では以下のように 4 台のノードを使用してクラスターを構成しています。

+----------------------+   +----------------------+
|   [ mgr.srv.world ]  |   |   [ dlp.srv.world ]  |
|     Manager Node     |   |     Control Plane    |
+-----------+----------+   +-----------+----------+
        eth0|10.0.0.25             eth0|10.0.0.30
            |                          |
------------+--------------------------+-----------
            |                          |
        eth0|10.0.0.51             eth0|10.0.0.52
+-----------+----------+   +-----------+----------+
| [ node01.srv.world ] |   | [ node02.srv.world ] |
|     Worker Node#1    |   |     Worker Node#2    |
+----------------------+   +----------------------+

 

Manager ノード または クラスター管理ユーザーを設定したクライアントホストで実行します。

[1] Pod の作成/削除等の操作です。
# [test-nginx] pod を起動する

[root@mgr ~]#
kubectl create deployment test-nginx --image=nginx

deployment.apps/test-nginx created
[root@mgr ~]#
kubectl get pods

NAME                         READY   STATUS    RESTARTS   AGE
test-nginx-f5c579c7d-w2hvb   1/1     Running   0          13s

# [test-nginx] pod の環境変数を表示する

[root@mgr ~]#
kubectl exec test-nginx-f5c579c7d-w2hvb -- env

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
TERM=xterm
HOSTNAME=test-nginx-f5c579c7d-w2hvb
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
KUBERNETES_PORT_443_TCP_PROTO=tcp
NGINX_VERSION=1.25.2
NJS_VERSION=0.8.0
PKG_RELEASE=1~bookworm
HOME=/root

# [test-nginx] pod にシェルアクセス

[root@mgr ~]#
kubectl exec -it test-nginx-f5c579c7d-w2hvb -- bash
root@test-nginx-f5c579c7d-w2hvb:/#
hostname

test-nginx-f5c579c7d-w2hvb
root@test-nginx-f5c579c7d-w2hvb:/#
exit
# [test-nginx] pod のログを表示する

[root@mgr ~]#
kubectl logs test-nginx-f5c579c7d-7vm8z

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/10/19 04:08:14 [notice] 1#1: using the "epoll" event method
2023/10/19 04:08:14 [notice] 1#1: nginx/1.25.2
2023/10/19 04:08:14 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
2023/10/19 04:08:14 [notice] 1#1: OS: Linux 5.14.0-373.el9.x86_64
2023/10/19 04:08:14 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/10/19 04:08:14 [notice] 1#1: start worker processes
.....
.....

# pod をスケールアウトする

[root@mgr ~]#
kubectl scale deployment test-nginx --replicas=3

deployment.extensions/test-nginx scaled
[root@mgr ~]#
kubectl get pods -o wide

NAME                         READY   STATUS    RESTARTS   AGE     IP                NODE               NOMINATED NODE   READINESS GATES
test-nginx-f5c579c7d-kt5jv   1/1     Running   0          15s     192.168.40.193    node01.srv.world   <none>           <none>
test-nginx-f5c579c7d-q5c6d   1/1     Running   0          15s     192.168.241.130   node02.srv.world   <none>           <none>
test-nginx-f5c579c7d-w2hvb   1/1     Running   0          2m17s   192.168.241.129   node02.srv.world   <none>           <none>

# サービスを設定する

[root@mgr ~]#
kubectl expose deployment test-nginx --type="NodePort" --port 80

service "test-nginx" exposed
[root@mgr ~]#
kubectl get services test-nginx

NAME         TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
test-nginx   NodePort   10.107.255.191   <none>        80:30505/TCP   5s

# ポートフォワードしてアクセス確認

[root@mgr ~]#
kubectl port-forward service/test-nginx --address 127.0.0.1 8082:80 &

[1] 2930
[root@mgr ~]#
curl localhost:8082

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
.....
.....
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

# サービスを削除する

[root@mgr ~]#
kubectl delete services test-nginx

service "test-nginx" deleted
# Pod を削除する

[root@mgr ~]#
kubectl delete deployment test-nginx

deployment.extensions "test-nginx" deleted
関連コンテンツ