CLOVER🍀

That was when it all began.

OKD/Minishiftで、一時的にPodを作って動かしたり、Podをデバッグしたい

これは、なにをしたくて書いたもの?

これまで、OKD/MinishiftでDeploymentConfigを使ってデプロイしたPodに対してアクセスしたりしたことはあったのですが、
Pod単体を作ったりしたことはなかったので、確認に、と。

デバッグ用途で使ったりしますよね。

環境

今回の環境は、こちら。

$ minishift version
minishift v1.32.0+009893b


$ oc version
oc v3.11.0+0cbc58b
kubernetes v1.11.0+d4cacc0
features: Basic-Auth GSSAPI Kerberos SPNEGO

Server https://192.168.42.32:8443
kubernetes v1.11.0+d4cacc0

oc run

oc runコマンドを使えば、掲題のことは実現できるようです。

Developer CLI Operations / run

kubectl Usage Conventions - Kubernetes

どのようなリソースで立ち上げるかは、「--generator」オプションで指定します。kubectl runで実行する場合はデフォルトでPodに
なるようですが、OKDの場合はDeploymentConfigになります。

WildFlyのDockerイメージで試してみましょう。

最小の指定は、こちら。

## oc run NAME
$ oc run wildfly --image=jboss/wildfly
deploymentconfig.apps.openshift.io/wildfly created

ここで「wildfly」として指定しているのは、DeploymentConfigの名前です。「--image」で、Dockerイメージを指定します。

確認。

$ oc get all
NAME                  READY     STATUS    RESTARTS   AGE
pod/wildfly-1-zlj9r   1/1       Running   0          1m

NAME                              DESIRED   CURRENT   READY     AGE
replicationcontroller/wildfly-1   1         1         1         1m

NAME                                         REVISION   DESIRED   CURRENT   TRIGGERED BY
deploymentconfig.apps.openshift.io/wildfly   1          1         1         config

タグをちゃんと指定。

$ oc run wildfly --image=jboss/wildfly:15.0.0.Final

で、これをPodにする場合には「--generator」を使います。

$ oc run wildfly --image=jboss/wildfly:15.0.0.Final --generator=run-pod/v1

Podに入る場合。

$ oc exec -it wildfly bash
bash-4.2$ ls -l
total 0
drwxrwxr-x. 1 jboss root 24 Nov 30 23:03 wildfly
bash-4.2$ ls -l wildfly 
total 508
-rw-rw-r--. 1 jboss root  26530 Nov 30 23:03 LICENSE.txt
-rw-rw-r--. 1 jboss root   2221 Nov 30 23:03 README.txt
drwxrwxr-x. 3 jboss root     27 Nov 30 23:03 appclient
drwxrwxr-x. 3 jboss root   1113 Feb 13 13:53 bin
-rw-rw-r--. 1 jboss root   2451 Nov 30 23:03 copyright.txt
drwxrwxr-x. 6 jboss root     67 Nov 30 23:03 docs
drwxrwxr-x. 4 jboss root     38 Nov 30 23:03 domain
-rw-rw-r--. 1 jboss root 479999 Nov 30 23:03 jboss-modules.jar
drwxrwxr-x. 3 jboss root     20 Nov 30 23:03 modules
drwxrwxr-x. 1 jboss root     61 Feb 28 12:18 standalone
drwxrwxr-x. 2 jboss root    216 Feb 13 13:53 welcome-content

ターミナルを使うなら「-it」。

$ oc run wildfly --image=jboss/wildfly:15.0.0.Final --generator=run-pod/v1 -it

終了と同時にPodを削除するなら、「--rm」。

$ oc run wildfly --image=jboss/wildfly:15.0.0.Final --generator=run-pod/v1 -it --rm

まあ、こういう使い方をするのは、WildFlyとかにはならないでしょうけれど。Ubuntu Linuxのイメージを使った場合。

$ oc run ubuntu --image=ubuntu:18.04 --generator=run-pod/v1 -it --rm
If you don't see a command prompt, try pressing enter.
I have no name!@ubuntu:/$ ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
I have no name!@ubuntu:/$ exit
exit
Session ended, resume using 'oc attach ubuntu -c ubuntu -i -t' command when the pod is running
pod "ubuntu" deleted

デフォルトのコマンドを上書きするには、「--command --」のあとに実行するコマンドを入力します。

$ oc run ubuntu --image=ubuntu:18.04 --generator=run-pod/v1 -it --rm --command -- ls -l
If you don't see a command prompt, try pressing enter.
Error attaching, falling back to logs: unable to upgrade connection: container ubuntu not found in pod ubuntu_myproject
total 0
drwxr-xr-x.   2 root root 1137 Feb  4 21:05 bin
drwxr-xr-x.   2 root root    6 Apr 24  2018 boot
drwxr-xr-x.   5 root root  380 Feb 28 12:25 dev
drwxr-xr-x.   1 root root   66 Feb 28 12:25 etc
drwxr-xr-x.   2 root root    6 Apr 24  2018 home
drwxr-xr-x.   8 root root   96 May 23  2017 lib
drwxr-xr-x.   2 root root   34 Feb  4 21:03 lib64
drwxr-xr-x.   2 root root    6 Feb  4 21:02 media
drwxr-xr-x.   2 root root    6 Feb  4 21:02 mnt
drwxr-xr-x.   2 root root    6 Feb  4 21:02 opt
dr-xr-xr-x. 192 root root    0 Feb 28 12:25 proc
drwx------.   2 root root   37 Feb  4 21:04 root
drwxr-xr-x.   1 root root   21 Feb 28 12:25 run
drwxr-xr-x.   1 root root   21 Feb  6 03:37 sbin
drwxr-xr-x.   2 root root    6 Feb  4 21:02 srv
dr-xr-xr-x.  13 root root    0 Feb 28 11:22 sys
drwxrwxrwt.   2 root root    6 Feb  4 21:05 tmp
drwxr-xr-x.   1 root root   18 Feb  4 21:02 usr
drwxr-xr-x.   1 root root   17 Feb  4 21:04 var
pod "ubuntu" deleted

「--」を付けない場合は、追加の引数になります。

「oc run --help」でいろいろ例が出てくるので、見てみるとよいでしょう。

$ oc run --help
Create and run a particular image, possibly replicated 

Creates a deployment config to manage the created container(s). You can choose to run in the foreground for an
interactive container execution.  You may pass 'run/v1' to --generator to create a replication controller instead of a
deployment config.

Usage:
  oc run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas] [--dry-run=bool]
[--overrides=inline-json] [--command] -- [COMMAND] [args...] [flags]

Examples:
  # Start a single instance of nginx.
  oc run nginx --image=nginx
  
  # Start a single instance of hazelcast and let the container expose port 5701 .
  oc run hazelcast --image=hazelcast --port=5701
  
  # Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster"
  # and "POD_NAMESPACE=default" in the container.
  oc run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"
  
  # Start a replicated instance of nginx.
  oc run nginx --image=nginx --replicas=5
  
  # Dry run. Print the corresponding API objects without creating them.
  oc run nginx --image=nginx --dry-run
  
  # Start a single instance of nginx, but overload the spec of the deployment config with
  # a partial set of values parsed from JSON.
  oc run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'
  
  # Start a pod of busybox and keep it in the foreground, don't restart it if it exits.
  oc run -i -t busybox --image=busybox --restart=Never
  
  # Start the nginx container using the default command, but use custom arguments (arg1 .. argN)
  # for that command.
  oc run nginx --image=nginx -- <arg1> <arg2> ... <argN>
  
  # Start the nginx container using a different command and custom arguments.
  oc run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>
  
  # Start the job to compute π to 2000 places and print it out.
  oc run pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'
  
  # Start the cron job to compute π to 2000 places and print it out every 5 minutes.
  oc run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'

Options:

〜省略〜

実行中のPodの中に入る

すでに実行中のDeploymentConfig、Podに対しては

$ oc run wildfly --image=jboss/wildfly
deploymentconfig.apps.openshift.io/wildfly created

oc rshでPod内に入れます。

$ oc rsh dc/wildfly
sh-4.2$ ls -l
total 0
drwxrwxr-x. 1 jboss root 24 Jan  5 19:10 wildfly

oc execでも可。

$ oc exec -it wildfly-1-4rlvh bash
bash-4.2$ 

こちらは、kubectlでも同じ。

Get a Shell to a Running Container - Kubernetes

既存のDeploymentConfigから、デバッグ起動する

また、既存のDeploymentConfigから、「oc debug」でコンテナ実行前の状態に入れます。

$ oc debug dc/wildfly
Defaulting container name to wildfly.
Use 'oc describe pod/wildfly-debug -n myproject' to see all of the containers in this pod.

Debugging with pod/wildfly-debug, original command: <image entrypoint>
Waiting for pod to start ...
If you don't see a command prompt, try pressing enter.
sh-4.2$