kubernetes创建pod
一、便携pod yml文件
$ vim k8s_pod.yml
apiVersion: v1 #定义k8s api的版本v1 kind: Pod #kind资源 Pod metadata: #属性,名字叫nginx,标签叫app : web(键值对) name: nginx labels: app: web spec: #详细 containers: #容器信息 - name: nginx #容器叫nginx image: nginx:latest #使用的镜像,这样是使用本地nginx镜像,当然可以使用私有仓库镜像*.*.*.*:5000/nginx:latest ports: #容器开放的端口 - containerPort: 80
二、镜像准备工作
下载镜像,并推送到私有镜像仓库
$ docker pull nginx$ docker tag nginx:latest *.*.*.*:5000/nginx:latest$ docker push *.*.*.*:5000/busybox:latest
三、创建pod
$ kubectl create -f k8s_pod.yml
如果报错,请修改 : $ vim /etc/kubernetes/apiserver 删除ServiceAccount字段 重启kubernetes服务 $ systemctl restart kube-apiserver.service
四、查询pod创建情况
$ kubectl get pod #一直处于创建,肯定不正常,正常应该是1/1
NAME READY STATUS RESTARTS AGE
nginx 0/1 ContainerCreating 0 4m
五、发现错误
kubectl describe pod nginx
发现如下错误:
六、解决
修改kubernetes配置,改成私仓地址
1: 下载 官方的rpm $ wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm 2: 导入 $ rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm | cpio -iv --to-stdout ./etc/rhsm/ca/redhat-uep.pem | tee /etc/rhsm/ca/redhat-uep.pem 3:安装完成后,我们把这个镜像 pull下来 ,镜像有点大,下载会比较慢 $ docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest 4: 打tag 把 pod-infrastructure:latest 传到我们的私有仓库, $ docker tag registry.access.redhat.com/rhel7/pod-infrastructure:latest *.*.*.*:5000/pod-infrastructure:latest $ docker push *.*.*.*:5000/pod-infrastructure:latest 5:如下操作,在所有节点node-1、Node-2上面操作 修改k8s配置,把红帽官网的下载地址,改成我们的私有仓库的镜像地址 $ vim /etc/kubernetes/kubelet
# /etc/kubernetes/kubelet
# pod infrastructure container # 将 KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest" # 替换成下方的内容 KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=*.*.*.*:5000/pod-infrastructure:latest"
七、重启kubelet
systemctl restart kubelet.service
八、重启Pod
在有yaml文件的情况下可以直接使用 kubectl replace --force -f xxx.yaml 来强制替换Pod的API对象,从而达到重启的目的。
kubectl replace --force -f k8s_pod.yaml
九、查看Pod状态
$ kubectldescribe pod nginx Name: nginx Namespace: default Node: k8s-node-2/*.*.*.* Start Time: Wed, 19 Jan 2022 14:42:51 +0800 Labels: app=web Status: Running IP: 10.0.4.3 Controllers: <none> Containers: nginx: Container ID: docker://a7416c09733f7d2eab1a62a6fcc12239d68c6de48386015b48c20 Image: nginx:latest Image ID: docker-pullable://docker.io/nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a1 Port: 80/TCP State: Running Started: Wed, 19 Jan 2022 14:42:59 +0800 Ready: True Restart Count: 0 Volume Mounts: <none> Environment Variables: <none> Conditions: Type Status Initialized True Ready True PodScheduled True No volumes. QoS Class: BestEffort Tolerations: <none> Events: FirstSeen LastSeen Count From SubObjectPath Type Reason Message --------- -------- ----- ---- ------------- -------- ------ ------- 18s 18s 1 {default-scheduler } Normal Scheduled Successfully assigned nginx to k8s-node-2 17s 17s 1 {kubelet k8s-node-2} spec.containers{nginx} Normal Pulling pulling image "nginx:latest" 18s 10s 2 {kubelet k8s-node-2} Warning MissingClusterDNS kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to DNSDefault policy. 10s 10s 1 {kubelet k8s-node-2} spec.containers{nginx} Normal Pulled Successfully pulled image "nginx:latest" 10s 10s 1 {kubelet k8s-node-2} spec.containers{nginx} Normal Created Created container with docker id a7416c09733f; Security:[seccomp=unconfined] 10s 10s 1 {kubelet k8s-node-2} spec.containers{nginx} Normal Started Started container with docker id a7416c09733f
$ kubectl get pod NAME READY STATUS RESTARTS AGE nginx 1/1 Running 0 36m
$ kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx 1/1 Running 0 36m 10.0.4.3 k8s-node-2
结束!