一、pod基本
pod是k8s的基本操作单元,也是应用运行载体。
1、创建配置文件
[root@k8s-master ~]# 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、创建容器
[root@k8s-master ~]# kubectl create -f k8s_pod.yml
3、查看容器
[root@k8s-master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE nginx 0/1 ContainerCreating 0 2m
[root@k8s-master ~]# kubectl describe pod nginx 查看容器详情
4、网上下载pod-infrastructure.tar.gz包,并导入镜像,并打标签,推入私有仓库
[root@k8s-master opt]# docker load -i pod-infrastructure.tar.gz [root@k8s-master opt]# docker tag registry.access.redhat.com/rhel7/pod-infrastructure:latest 192.168.125.130:5000/pod-infrastructure:latest [root@k8s-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
[root@k8s-master ~]# kubectl describe pod nginx 查看容器详情
5、修改node节点配置文件
[root@k8s-node1 ~]# vim /etc/kubernetes/kubelet
地址改为私有镜像地址
6、重启服务
[root@k8s-node1 ~]# systemctl restart kubelet.service [root@k8s-node2 ~]# systemctl restart kubelet [root@k8s-master ~]# systemctl restart kube-apiserver.service
7、查看nginx容器状态
[root@k8s-master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 1h
8、查看容器提供服务
[root@k8s-master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx 1/1 Running 0 1h 172.16.64.2 k8s-node2 [root@k8s-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 [root@k8s-master ~]#
9、node节点查看容器
一共起了两个容器,一个是nginx基础容器,只提供服务,k8s上的负载均衡和自动发现等高级功能靠另一个pod容器实现。两个容器共用一个IP地址,k8s官方最多支持4个容器共用一个IP地址。
nginx基础容器网络类型为container类型,与pod容器共用一个IP地址
[root@k8s-node2 ~]# docker inspect ba8ef90c1298
pod容器是有网络的
10、pod基本命令
[root@k8s-master ~]# kubectl create -f k8s_pod.yml 创建 [root@k8s-master ~]# kubectl delete pod nginx 删除 [root@k8s-master ~]# kubectl get pod nginx 查询 [root@k8s-master ~]# kubectl describe pod nginx 查询 [root@k8s-master ~]# kubectl replace /path/to/k8s_pod.yml 更新
起多个容器