# 查看默认命名空间的 pod $ kubectl get pods|pod|po No resources found in default namespace. # 等价于 $ kubectl get pods|pod|po -n default No resources found in default namespace.
# 代表使用的 api 版本 apiVersion:v1 # 代表创建的类型 kind:Pod # 用来书写对应元数据信息,比如指定 Pod 名称以及 namespace 命名空间,也可以自定义名字 metadata: name:nginx # 对 Pod 预期行为的规约,比如配置容器 spec: containers: -name:nginx image:nginx:1.19 ports: -containerPort:80
使用 kubectl create/apply -f 创建 pod
区别:
create 仅仅是不存在时创建,如果已经存在则报错!
apply 不存在创建,存在更新配置。推荐使用 apply!`
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ kubectl create -f nginx-pod.yml pod/nginx created $ kubectl get pods NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 9s $ kubectl create -f nginx-pod.yml Error from server (AlreadyExists): error when creating "nginx-pod.yml": pods "nginx" already exists
$ kubectl apply -f nginx-pod.yml pod/nginx created $ kubectl get pods NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 6s $ kubectl apply -f nginx-pod.yml pod/nginx unchanged
# 查看日志 (默认只查看第一个容器日志,这里是展示 nginx 日志) $ kubectl logs -f myapp-pod Defaulted container "nginx" out of: nginx, redis /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: 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 # 查看 pod 中指定容器的日志 $ kubectl logs -f myapp-pod -c 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: 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 $ kubectl logs -f myapp-pod -c redis(容器名称) 1:C 20 Jan 2026 13:40:23.358 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 20 Jan 2026 13:40:23.358 # Redis version=5.0.10, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 20 Jan 2026 13:40:23.358 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 1:M 20 Jan 2026 13:40:23.360 * Running mode=standalone, port=6379. 1:M 20 Jan 2026 13:40:23.360 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1:M 20 Jan 2026 13:40:23.360 # Server initialized 1:M 20 Jan 2026 13:40:23.360 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled. 1:M 20 Jan 2026 13:40:23.365 * Ready to accept connections