Centos7 k8s 基础单元pod

一、pod基本

pod是k8s的基本操作单元,也是应用运行载体。

1、创建配置文件

[ ~]# vim k8s_pod.yml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  labels: 
    app: web
spec:
  containers:
    - name: nginx
      image: 192.168.125.130:5000/nginx:1.19
      ports:
        - containerPort: 80

2、创建容器

[ ~]# kubectl create -f k8s_pod.yml

3、查看容器

[ ~]# kubectl get pods
NAME      READY     STATUS              RESTARTS   AGE
nginx     0/1       ContainerCreating   0          2m[ ~]# kubectl describe pod nginx                               查看容器详情

4、网上下载pod-infrastructure.tar.gz包,并导入镜像,并打标签,推入私有仓库

[ opt]# docker load -i pod-infrastructure.tar.gz
[-master opt]# docker tag registry.access.redhat.com/rhel7/pod-infrastructure:latest 192.168.125.130:5000/pod-infrastructure:latest
[-master ~]# docker push 192.168.125.130:5000/pod-infrastructure:latest                                            推入私有仓库,记得先关闭selinux
The push refers to a repository [192.168.125.130:5000/pod-infrastructure]
d7e0f7eb92d7: Pushed 
279bfd6c7049: Pushed 
f5bd5357a1de: Pushed 
latest: digest: sha256:167c23c5a50d6070946dbc4747826215ce5a76d85b6616f591c9b1f21c377aa5 size: 948[ ~]# kubectl describe pod nginx                                                                            查看容器详情

5、修改node节点配置文件

[ ~]# vim /etc/kubernetes/kubelet

Centos7 k8s 基础单元pod

 地址改为私有镜像地址

6、重启服务

[ ~]# systemctl restart kubelet.service
[-node2 ~]# systemctl restart kubelet
[-master ~]# systemctl restart kube-apiserver.service

7、查看nginx容器状态

[ ~]# kubectl get pod
NAME      READY     STATUS    RESTARTS   AGE
nginx     1/1       Running   0          1h

   8、查看容器提供服务

[ ~]# kubectl get pod -o wide
NAME      READY     STATUS    RESTARTS   AGE       IP            NODE
nginx     1/1       Running   0          1h        172.16.64.2   k8s-node2
[-master ~]# curl -I 172.16.64.2
HTTP/1.1 200 OK
Server: nginx/1.19.0
Date: Wed, 10 Jun 2020 12:56:35 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 26 May 2020 15:00:20 GMT
Connection: keep-alive
ETag: "5ecd2f04-264"
Accept-Ranges: bytes

[-master ~]#

9、node节点查看容器

Centos7 k8s 基础单元pod

 一共起了两个容器,一个是nginx基础容器,只提供服务,k8s上的负载均衡和自动发现等高级功能靠另一个pod容器实现。两个容器共用一个IP地址,k8s官方最多支持4个容器共用一个IP地址。

Centos7 k8s 基础单元pod

nginx基础容器网络类型为container类型,与pod容器共用一个IP地址

[ ~]# docker inspect ba8ef90c1298

Centos7 k8s 基础单元pod

 pod容器是有网络的

   10、pod基本命令

[ ~]#  kubectl create -f k8s_pod.yml                      创建
[-master ~]#  kubectl delete pod nginx                           删除
[-master ~]# kubectl get pod nginx                               查询
[-master ~]# kubectl describe pod nginx                          查询
[-master ~]# kubectl replace /path/to/k8s_pod.yml                更新