一、编辑yaml文件###

[root@K8s-Master Tools]# cat hello-world-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: hello-world
spec:
nodeSelector:
type: node1
containers:
- name: hello
image: "ubuntu:14.04"
command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]

二、创建并查看运行状态###

[root@K8s-Master Tools]# kubectl create -f hello-world-pod.yaml
pod/hello-world created
[root@K8s-Master Tools]# kubectl get pod hello-world
NAME READY STATUS RESTARTS AGE
hello-world 1/1 Running 0 8m2s
[root@K8s-Master Tools]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default hello-world 1/1 Running 0 3s
kube-system coredns-bccdc95cf-plst5 1/1 Running 13 36d
kube-system coredns-bccdc95cf-vkqzr 1/1 Running 13 36d
kube-system etcd-k8s-master 1/1 Running 15 36d
kube-system kube-apiserver-k8s-master 1/1 Running 15 36d
kube-system kube-controller-manager-k8s-master 1/1 Running 15 36d
kube-system kube-flannel-ds-amd64-4g6qc 1/1 Running 6 36d
kube-system kube-flannel-ds-amd64-ccj8v 1/1 Running 14 36d
kube-system kube-flannel-ds-amd64-fhrfj 1/1 Running 8 36d
kube-system kube-proxy-jp6mp 1/1 Running 13 36d
kube-system kube-proxy-rkbcx 1/1 Running 6 36d
kube-system kube-proxy-rmkqm 1/1 Running 8 36d
kube-system kube-scheduler-k8s-master 1/1 Running 15 36d
kube-system kubernetes-dashboard-6db4897b74-lqfl6 1/1 Running 1 24h



注意点:将pod分配到指定的节点

将pod调度到指定节点有2种方式:

方式一:强制约束Pod调度到指定Node节点上

Pod.spec.nodeName

方式二:通过label-selector机制选择节点

Pod.spec.nodeSelector





以下使用第二种方式实现pod调度到指定节点。

①为Node添加label

kubectl label node k8s-node1 type=node1
kubectl get nodes --show-labels
[root@K8s-Master Tools]# kubectl get nodes --show-labels
NAME STATUS ROLES AGE VERSION LABELS
k8s-master Ready master 37d v1.15.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master=,type=master
k8s-node1 Ready <none> 37d v1.15.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node1,kubernetes.io/os=linux,type=node1
k8s-node2 Ready <none> 37d v1.15.3 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-node2,kubernetes.io/os=linux,type=node2

②将pod分配到带有指定label的node

Pod操作:

删除Pod

kubectl delete pod hello

更新Pod

kubctl replace hello-world.yaml

三、可能遇到的问题###

Error:Back-off restarting failed container

[root@K8s-Master Tools]# kubectl describe pod hello-world
Name: hello-world
Namespace: default
Priority: 0
Node: k8s-node1/10.0.2.15
Start Time: Fri, 18 Oct 2019 17:16:13 +1100
Labels: <none>
Annotations: <none>
Status: Running
IP: 192.168.1.15
Containers:
hello:
Container ID: docker://ae85d78070dc02a19fed619c6270732cd98bfc1d824262b2935bc2362d7b70e5
Image: ubuntu:14.04
Image ID: docker-pullable://ubuntu@sha256:2f7c79927b346e436cc14c92bd4e5bd778c3bd7037f35bc639ac1589a7acfa90
Port: <none>
Host Port: <none>
Command:
/bin/echo
Hello
world
State: Waiting
Reason: CrashLoopBackOff
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Fri, 18 Oct 2019 17:18:10 +1100
Finished: Fri, 18 Oct 2019 17:18:10 +1100
Ready: False
Restart Count: 4
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-hctt8 (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-hctt8:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-hctt8
Optional: false
QoS Class: BestEffort
Node-Selectors: type=node1
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m31s default-scheduler Successfully assigned default/hello-world to k8s-node1
Normal Pulling 2m30s kubelet, k8s-node1 Pulling image "ubuntu:14.04"
Normal Pulled 2m10s kubelet, k8s-node1 Successfully pulled image "ubuntu:14.04"
Normal Created 34s (x5 over 2m10s) kubelet, k8s-node1 Created container hello
Normal Started 34s (x5 over 2m9s) kubelet, k8s-node1 Started container hello
Normal Pulled 34s (x4 over 2m8s) kubelet, k8s-node1 Container image "ubuntu:14.04" already present on machine
Warning BackOff 34s (x9 over 2m7s) kubelet, k8s-node1 Back-off restarting failed container

解决方法:

As per Describe Pod command listing, your Container inside the Pod has been already completed with exit code 0, which states about successful completion without any errors/problems, but the life cycle for the Pod was very short. To keep Pod running continuously you must specify a task that will never finish.

apiVersion: v1
kind: Pod
metadata:
name: ubuntu
spec:
containers:
- name: ubuntu
image: ubuntu
command: [ "/bin/bash", "-ce", "tail -f /dev/null" ]

参考链接:

将pod分配到指定的节点.

Back-off restarting failed container.

【Kubernetes】创建Pod并分配到指定节点的更多相关文章

  1. [置顶] kubernetes创建资源yaml文件例子--pod

    kubernetes创建pod的yaml文件,参数说明 apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 ...

  2. pod(一):Kubernetes(k8s)创建pod的两种方式

    目录 一.系统环境 二.前言 三.pod 四.创建pod 4.1 环境介绍 4.2 使用命令行的方式创建pod 4.2.1 创建最简单的pod 4.2.2 创建pod,指定镜像下载策略 4.2.3 创 ...

  3. k8s创建pod流程

    kubernetes 创建Pod 的 工作流: step.1 kubectl 向 k8s api server 发起一个create pod 请求(即我们使用Kubectl敲一个create pod命 ...

  4. K8s创建pod yaml文件详解

    kubernetes创建pod的yaml文件,参数说明 apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 ...

  5. k8s创建pod和service的过程

    一.概念介绍 更详细的参见:https://www.kubernetes.org.cn/5335.html 1.K8s K8s 是一种用于在一组主机上运行和协同容器化应用程序的系统,提供应用部署.规划 ...

  6. Kubernetes集群向指定节点上创建容器

    如果需要限制Pod到指定的Node上运行,则可以给Node打标签并给Pod配置NodeSelector. 给节点添加标签 首先查看节点信息 [root@k8s-master ~]# kubectl g ...

  7. Docker Kubernetes 创建管理 Pod

    Docker Kubernetes 容器扩容与缩容 环境: 系统:Centos 7.4 x64 Docker版本:18.09.0 Kubernetes版本:v1.8 管理节点:192.168.1.79 ...

  8. kubernetes创建资源对象yaml文件例子--pod详解

    apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: ...

  9. Kubernetes对Pod调度指定Node以及Node的Taint 和 Toleration

    由于博客园不支持markdown,推荐以下url阅读: 原创url:https://blog.csdn.net/weixin_42495873/article/details/103364868 ## ...

随机推荐

  1. C# 获取上传文件的文件名和后缀名

    //获得要上传的文件 HttpPostedFile file = Request.Files[]; //获得到文件名 string fileName = System.IO.Path.GetFileN ...

  2. IMEI校验思路及代码

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. ssh 出错 Permission denied (publickey,password).

    将客户端的~/.ssh/know_hosts 文件删掉试试 ssh debug信息 ssh -vvv xxx@192.168.1.111

  4. MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数

    MySql计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数 计算两日期时间之间相差的天数,秒数,分钟数,周数,小时数,这里主要分享的是通过MySql内置的函数 TimeStampDiff() ...

  5. 关于IOS 微信浏览器 点击输入框自动放大问题

    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0& ...

  6. @NOI模拟2017.06.30 - T3@ Right

    目录 @description@ @solution@ @part - 1@ @part - 2@ @accepted code@ @details@ @description@ JOHNKRAM 和 ...

  7. H3C TCP连接的拆除

  8. 将 vue.js 获取的 html 文本转化为纯文本

    我存入数据表中的数据是使用 html  格式,获取数据是使用 vue 获取. 遇到了一个问题,就是界面上显示的数据是 html 格式的,但是我需要它显示纯文本. 怎么做呢?首先在  js  中写一个将 ...

  9. NIO 中文乱码问题的解决代码实现

    之前在网上查询了很多关于解决NIO中文乱码的问题,仁者见仁智者见智,不过就找到的几种方法实现都太繁琐了,稍微研究了下NIO源码,以下是我自己的一种实现,偷懒用最简单的代码去实现是我的习惯! Demo: ...

  10. 2018-8-10-WPF-调试-获得追踪输出

    title author date CreateTime categories WPF 调试 获得追踪输出 lindexi 2018-08-10 19:16:51 +0800 2018-05-16 1 ...