LoginSignup
2
0

More than 1 year has passed since last update.

Docker + Kubernetesでコンテナを操作しよう

Posted at

環境構築

以下の記事で作成した環境を使用します
PCに仮想化環境を構築しよう

コンテナの操作

手順

  • Podを作成(Pod内のコンテナはcentosとnginxのイメージを使用)
  • Pod内のコンテナに入りシェル実行
  • Podから出る

Podのマニフェストファイルを作成する

仮想マシンのrootディレクトリ配下にstudyフォルダを作成し、studyフォルダに以下のマニフェストファイルを作成

pods.yaml
apiVersion: v1
kind: Pod
metadata:
  name: debug
  namespace: default
  labels:
    env: study
spec:
  containers:
  - name: debug
    image: centos:7
    command:
      - "sh"
      - "-c"
    args:
    - |
      while true
      do
        sleep ${DELAY}
      done
    env:
    - name: "DELAY"
      value: "5"

---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: default
  labels:
    env: study
spec:
  containers:
  - name: nignx
    image: nginx:1.17.2-alpine

image.png

リソースの作成

VSCodeのターミナルから以下のコマンドを実行

  • studyに移動
[root@localhost ~]# cd study
[root@localhost study]# 
  • リソース作成
[root@localhost study]# kubectl apply -f pods.yml
pod/debug created
pod/nginx created
  • 確認
[root@localhost study]# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
debug   1/1     Running   0          47s
nginx   1/1     Running   0          47s

コンテナに入る

  • PodのIPアドレスを確認
[root@localhost study]# kubectl get pod -o wide
NAME    READY   STATUS    RESTARTS   AGE    IP           NODE                    NOMINATED NODE   READINESS GATES
debug   1/1     Running   0          3m8s   172.17.0.6   localhost.localdomain   <none>           <none>
nginx   1/1     Running   0          3m8s   172.17.0.5   localhost.localdomain   <none>           <none>

debugコンテナに入る

[root@localhost study]# kubectl exec -it debug sh
sh-4.2# 

debugコンテナからnginxコンテナにアクセス

sh-4.2# curl http://172.17.0.5/
<!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>

リソースの削除

  • コンテナから出る
sh-4.2# exit
exit
[root@localhost study]# 
  • リソースの削除
[root@localhost study]# kubectl delete -f pods.yml
pod "debug" deleted
pod "nginx" deleted
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0