k8s pod部署到不同node_k8s部署nginx(Pod、Deployment、Service)
一、Podpod 是 kubernetes 中最小的编排单位,通常由一个容器组成 (有时候会有多个容器组成)nginx-pod.yaml将配置apply到k8skubectl apply -f nginx.yaml校验部署状态,此时 STATUS 为 Running 表明部署成功获取 Pod 部署的状态,特别是 IP , -o wide 列出IP/Node等更多信息kubectl get pods
一、Pod
pod 是 kubernetes 中最小的编排单位,通常由一个容器组成 (有时候会有多个容器组成)
nginx-pod.yaml

将配置apply到k8s
kubectl apply -f nginx.yaml

校验部署状态,此时 STATUS 为 Running 表明部署成功
获取 Pod 部署的状态,特别是 IP , -o wide 列出IP/Node等更多信息
kubectl get pods nginx -o wide

获取更加详细的信息
kubectl describe pod nginx

使用 kubectl exec 进入 Pod 的内部容器。如果 Pod 中有多个容器,使用 kubectl exec -c 指定容器
kubectl exec -it nginx sh
在 Pod 容器中执行命令,校验其中的 socket 情况以及 nginx 服务

netstat -tan
wget -q -O - localhost
二、Deployment
在 k8s 中编排应用可以更好地做弹性扩容,负载均衡。既然要均衡,一个 Pod 肯定不能均衡,自然要部署多个 Pod
docker-compose 可以简单地通过 docker-compose scale 来扩容,现在用k8s扩容
在k8s中管理 Pod 的称作 Controller,我们可以使用 Deployment 这种 Controller 来为 Pod 进行扩容,当然它还可以滚动升级,回滚,金丝雀等等关于部署的事情
我们编写一个 Deployment 的资源配置文件
- spec.template: 指定要部署的 Pod
- spec.replicas: 指定要部署的个数
- spec.selector: 定位需要管理的 Pod

我们使用 kubectl apply 部署生效后查看 Pod 以及 Deployment 状态
kubectl get pods -o wide -l 'app=nginx'

三、Service
Service 做服务发现 指定 Deployment 或者特定集合 Pod 的网络层抽象
- spec.selector: 指定如何选择 Pod
- spec.ports: 指定如何暴露端口

创建NodePort service时,用户可以指定范围为30000-32767的端口,对该端口的访问就能通过 kube-proxy 代理到service后端的pod中
我们使用 kubectl apply 部署生效后查看 Service 状态
kubectl get svc nginx-service -o wide

curl http://127.0.0.1:31871

截图中源码文件地址:https://gitee.com/pingfanrenbiji/study/blob/master/k8s/nginx/%E4%B8%80%E4%B8%AApod%E5%8D%95%E4%B8%AA%E5%AE%B9%E5%99%A8/
参考文章:https://juejin.im/post/5db8c2b46fb9a020256692dc
更多推荐
所有评论(0)