51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Kubernetes的Pod操作

查询容器 {#%E6%9F%A5%E8%AF%A2%E5%AE%B9%E5%99%A8}

查看容器,不指定命名空间默认查询默认空间的容器

[root@k8s-master ~]# kubectl get pods
No resources found in default namespace.
[root@k8s-master ~]# 

加上-A显示所有的容器

[root@k8s-master ~]# kubectl get pods -A
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-6c89d944d5-7dsxq   1/1     Running   0          2d23h
kube-system   calico-node-bcnb6                          1/1     Running   0          2d22h
kube-system   calico-node-lbpd7                          1/1     Running   0          2d23h
kube-system   coredns-59c898cd69-jlrv8                   1/1     Running   0          2d23h
kube-system   coredns-59c898cd69-v2r7b                   1/1     Running   0          2d23h
kube-system   etcd-k8s-master                            1/1     Running   0          2d23h
kube-system   kube-apiserver-k8s-master                  1/1     Running   0          2d23h
kube-system   kube-controller-manager-k8s-master         1/1     Running   0          2d23h
kube-system   kube-proxy-cwdwf                           1/1     Running   0          2d23h
kube-system   kube-proxy-hrm5w                           1/1     Running   0          2d22h
kube-system   kube-scheduler-k8s-master                  1/1     Running   0          2d23h
kuboard       kuboard-agent-2-7998f5997b-t9mqp           1/1     Running   0          2d22h
kuboard       kuboard-agent-678fb9dd98-6hb6z             1/1     Running   0          2d22h
kuboard       kuboard-etcd-mqnmx                         1/1     Running   0          2d22h
kuboard       kuboard-v3-79797c7b84-qw2cr                1/1     Running   0          2d22h
[root@k8s-master ~]# 

通过-n查询指定命名空间下的容器

[root@k8s-master ~]# kubectl get pods -n kuboard
NAME                               READY   STATUS    RESTARTS   AGE
kuboard-agent-2-7998f5997b-t9mqp   1/1     Running   0          2d22h
kuboard-agent-678fb9dd98-6hb6z     1/1     Running   0          2d22h
kuboard-etcd-mqnmx                 1/1     Running   0          2d22h
kuboard-v3-79797c7b84-qw2cr        1/1     Running   0          2d22h

启动Pod {#%E5%90%AF%E5%8A%A8pod}

通过kubectl run Pod名 --image=镜像

kubectl run nginx --image=nginx

启动好后,此时是访问不了容器服务的,通过describe查看Pod信息

可以看到k8s分配的IP是10.100.64.193,这个IP范围是在我们安装时指定的。

[root@k8s-master ~]# kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         k8s-slave/192.168.170.90
Start Time:   Fri, 22 Mar 2024 02:51:13 -0400
Labels:       run=nginx
Annotations:  cni.projectcalico.org/podIP: 10.100.64.193/32
              cni.projectcalico.org/podIPs: 10.100.64.193/32
Status:       Running
IP:           10.100.64.193
IPs:
  IP:  10.100.64.193

请求一下这个地址,在服务器内部是可以访问到页面了

[root@k8s-master ~]# curl 10.100.64.193:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>`

查看容器日志通过kubectl logs -f Pod名 ,这是在默认空间下的,如果其它空间下通过-n 空间指定。

[root@k8s-master ~]# kubectl logs -f nginx
/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
2024/03/22 06:52:34 [notice] 1#1: using the "epoll" event method
2024/03/22 06:52:34 [notice] 1#1: nginx/1.25.4
2024/03/22 06:52:34 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
2024/03/22 06:52:34 [notice] 1#1: OS: Linux 3.10.0-1160.el7.x86_64
2024/03/22 06:52:34 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2024/03/22 06:52:34 [notice] 1#1: start worker processes
2024/03/22 06:52:34 [notice] 1#1: start worker process 28
2024/03/22 06:52:34 [notice] 1#1: start worker process 29
10.100.235.192 - - [22/Mar/2024:06:58:56 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.29.0" "-"

进入容器,和Docker语法都类似

kubectl exec -it Pod名 --bash

如果不是在默认空间,通过-n 指定,例如:kubectl exec -it Pod名 -n mytest --bash

[root@k8s-master ~]# kubectl exec -it nginx --bash
Error: unknown flag: --bash
See 'kubectl exec --help' for usage.
[root@k8s-master ~]# kubectl exec -it nginx -- bash
root@nginx:/# ls
bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@nginx:/# exit
exit
[root@k8s-master ~]# 

删除容器 {#%E5%88%A0%E9%99%A4%E5%AE%B9%E5%99%A8}

通过delete删除

[root@k8s-master ~]# kubectl delete pod nginx
pod "nginx" deleted

如果是在其他空间,加上-n指定空间,例如在mytest空间下

[root@k8s-master ~]# kubectl delete pod nginx -n mytest

通过yml方式启动Pod {#%E9%80%9A%E8%BF%87yml%E6%96%B9%E5%BC%8F%E5%90%AF%E5%8A%A8pod}

编写个yml文件,如Pod-nginx.yml

vi Pod-nginx.yml

设置放在mytest空间下的

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: mytest
spec:
  containers:
  - image: nginx:latest
    name:  nginx

启动,通过-f指定我们编写yml文件

kubectl apply -f Pod-nginx.yml

查看容器

[root@k8s-master ~]# kubectl get pod nginx -n mytest
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          11s

查询详细信息,IP是10.100.64.194

[root@k8s-master ~]# kubectl describe pod nginx -n mytest
Name:         nginx
Namespace:    mytest
Priority:     0
Node:         k8s-slave/192.168.170.90
Start Time:   Fri, 22 Mar 2024 03:14:53 -0400
Labels:       <none>
Annotations:  cni.projectcalico.org/podIP: 10.100.64.194/32
              cni.projectcalico.org/podIPs: 10.100.64.194/32
Status:       Running
IP:           10.100.64.194
IPs:
  IP:  10.100.64.194

访问没问题

[root@k8s-master ~]# curl 10.100.64.194
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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@k8s-master ~]# `

删除yml启动的容器 {#%E5%88%A0%E9%99%A4yml%E5%90%AF%E5%8A%A8%E7%9A%84%E5%AE%B9%E5%99%A8}

通过delete -f 指定启动yml文件删除

[root@k8s-master ~]# kubectl delete -f Pod-nginx.yml 
pod "nginx" deleted

赞(0)
未经允许不得转载:工具盒子 » Kubernetes的Pod操作