当前位置:   article > 正文

kubernetes创建pod

kubernetes创建一个镜像,推送到镜像仓库,然后创建pod

一、便携pod yml文件

$ vim k8s_pod.yml
  1. apiVersion: v1 #定义k8s api的版本v1
  2. kind: Pod #kind资源 Pod
  3. metadata: #属性,名字叫nginx,标签叫app : web(键值对)
  4. name: nginx
  5. labels:
  6. app: web
  7. spec: #详细
  8. containers: #容器信息
  9. - name: nginx #容器叫nginx
  10. image: nginx:latest #使用的镜像,这样是使用本地nginx镜像,当然可以使用私有仓库镜像*.*.*.*:5000/nginx:latest
  11. ports: #容器开放的端口
  12. - containerPort: 80

二、镜像准备工作

下载镜像,并推送到私有镜像仓库

$ docker pull nginx$ docker tag nginx:latest *.*.*.*:5000/nginx:latest$ docker push *.*.*.*:5000/busybox:latest

三、创建pod

$ kubectl create -f k8s_pod.yml
  1. 如果报错,请修改 :
  2. $ vim /etc/kubernetes/apiserver
  3. 删除ServiceAccount字段
  4. 重启kubernetes服务
  5. $ systemctl restart kube-apiserver.service

四、查询pod创建情况

$ kubectl get pod #一直处于创建,肯定不正常,正常应该是1/1NAME READY STATUS RESTARTS AGEnginx 0/1 ContainerCreating 0 4m

 五、发现错误

kubectl describe pod nginx

发现如下错误:

六、解决

修改kubernetes配置,改成私仓地址

  1. 1: 下载 官方的rpm
  2. $ wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm
  3. 2: 导入
  4. $ 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
  5. 3:安装完成后,我们把这个镜像 pull下来 ,镜像有点大,下载会比较慢
  6. $ docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
  7. 4: 打tag 把 pod-infrastructure:latest 传到我们的私有仓库,
  8. $ docker tag registry.access.redhat.com/rhel7/pod-infrastructure:latest *.*.*.*:5000/pod-infrastructure:latest
  9. $ docker push *.*.*.*:5000/pod-infrastructure:latest
  10. 5:如下操作,在所有节点node-1、Node-2上面操作
  11. 修改k8s配置,把红帽官网的下载地址,改成我们的私有仓库的镜像地址
  12. $ vim /etc/kubernetes/kubelet
# /etc/kubernetes/kubelet
  1. # pod infrastructure container
  2. # 将
  3. KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"
  4. # 替换成下方的内容
  5. 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状态

  1. $ kubectldescribe pod nginx
  2. Name: nginx
  3. Namespace: default
  4. Node: k8s-node-2/*.*.*.*
  5. Start Time: Wed, 19 Jan 2022 14:42:51 +0800
  6. Labels: app=web
  7. Status: Running
  8. IP: 10.0.4.3
  9. Controllers: <none>
  10. Containers:
  11. nginx:
  12. Container ID: docker://a7416c09733f7d2eab1a62a6fcc12239d68c6de48386015b48c20
  13. Image: nginx:latest
  14. Image ID: docker-pullable://docker.io/nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a1
  15. Port: 80/TCP
  16. State: Running
  17. Started: Wed, 19 Jan 2022 14:42:59 +0800
  18. Ready: True
  19. Restart Count: 0
  20. Volume Mounts: <none>
  21. Environment Variables: <none>
  22. Conditions:
  23. Type Status
  24. Initialized True
  25. Ready True
  26. PodScheduled True
  27. No volumes.
  28. QoS Class: BestEffort
  29. Tolerations: <none>
  30. Events:
  31. FirstSeen LastSeen Count From SubObjectPath Type Reason Message
  32. --------- -------- ----- ---- ------------- -------- ------ -------
  33. 18s 18s 1 {default-scheduler } Normal Scheduled Successfully assigned nginx to k8s-node-2
  34. 17s 17s 1 {kubelet k8s-node-2} spec.containers{nginx} Normal Pulling pulling image "nginx:latest"
  35. 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.
  36. 10s 10s 1 {kubelet k8s-node-2} spec.containers{nginx} Normal Pulled Successfully pulled image "nginx:latest"
  37. 10s 10s 1 {kubelet k8s-node-2} spec.containers{nginx} Normal Created Created container with docker id a7416c09733f; Security:[seccomp=unconfined]
  38. 10s 10s 1 {kubelet k8s-node-2} spec.containers{nginx} Normal Started Started container with docker id a7416c09733f
  1. $ kubectl get pod
  2. NAME READY STATUS RESTARTS AGE
  3. nginx 1/1 Running 0 36m
  1. $ kubectl get pod -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE
  3. nginx 1/1 Running 0 36m 10.0.4.3 k8s-node-2

结束!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/162402
推荐阅读
相关标签
  

闽ICP备14008679号