使用Docker + Kubernetes来操作容器

环境建设

在下面的文章中使用创建的环境:
尝试在计算机上建立虚拟化环境。

容器的操作 de

步骤

    • Podを作成(Pod内のコンテナはcentosとnginxのイメージを使用)

 

    • Pod内のコンテナに入りシェル実行

 

    Podから出る

创建Pod的清单文件

在虚拟机的根目录下创建一个名为“study”的文件夹,并且在该文件夹中创建以下的清单文件。

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>

进入调试容器

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

从调试容器访问到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
bannerAds