Pod
- pod 是k8s的最小调度单位,可以包含多个容器
Pod 的 yml 示例
定义了pod里要运行什么容器
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx-container
image: nginx:1.29.0
运行Pod
kubectl apply -f nginx-pod.yml
查看全部Pods
kubectl get pods
使用命令映射Pod 端口
- 这里的 nginx-pod 是 yaml里面的 metadata.name
- 这个命令是
kubectl port-forward nginx-pod 4000:80
- 解释
命令将 nginx 默认的 80 端口映射到本机的 4000 端口,
打开浏览器或者 curl 来访问 http://127.0.0.1:4000 ,
查看是否成功访问 nginx 默认页面!
- 后台运行 port-forward
nohup kubectl port-forward nginx 4000:80 > /dev/null 2>&1 &
- 关闭 port-forward
ps aux | grep "kubectl port-forward"
# 找到对应的 PID,然后
kill <PID>
进入 Pod 内shell
kubectl exec -it 可以用来进入 Pod 内容器的 Shell。通过命令下面的命令来配置 nginx 的首页内容。
kubectl exec -it nginx -- /bin/bash
echo "hello kubernetes by nginx!" > /usr/share/nginx/html/index.html
kubectl port-forward nginx-pod 4000:80
查看pod 日志
kubectl logs --follow nginx
kubectl exec nginx -- ls
删除Pod
kubectl delete pod nginx
# pod "nginx" deleted
kubectl delete -f nginx-pod.yml
# pod "nginx" deleted