1. 程式人生 > >控制pod在節點位置與其他類型的pod

控制pod在節點位置與其他類型的pod

bsp ever var dev failure field cloud ESS 多個

8. 控制pod位置

通過label標簽控制pod位置

kubectl label node node1 disktype=ssd #為節點node1做標簽

kubectl label node node1 disktype- #刪除標簽

kubectl get node --show-labels #查詢標簽

在描述pod規格裏面node加入標簽

技術分享圖片

9. deamonset

daemonset 特點為每個節點運行一個副本(特殊需求的,存儲日誌網絡等)

kubectl get daemonset --namespace=kube-system #查看daemonset

kube-flannel-ds和kube-proxy

分別在每個節點上運行(屬於系統組件)

kubectl get pod --namespace=kube-system -o wide #查看系統組件pod


技術分享圖片

10.查看flannel部署文件

cat kube-flannel.yml

---

kind: ClusterRole

apiVersion: rbac.authorization.k8s.io/v1beta1

metadata:

name: flannel

rules:

- apiGroups:

- ""

resources:

- pods

verbs:

- get

- apiGroups:

- ""

resources:

- nodes

verbs:

- list

- watch

- apiGroups:

- ""

resources:

- nodes/status

verbs:

- patch

---

kind: ClusterRoleBinding

apiVersion: rbac.authorization.k8s.io/v1beta1

metadata:

name: flannel

roleRef:

apiGroup: rbac.authorization.k8s.io

kind: ClusterRole

name: flannel

subjects:

- kind: ServiceAccount

name: flannel

namespace: kube-system

---

apiVersion: v1

kind: ServiceAccount

metadata:

name: flannel

namespace: kube-system

---

kind: ConfigMap

apiVersion: v1

metadata:

name: kube-flannel-cfg

namespace: kube-system

labels:

tier: node

app: flannel

data:

cni-conf.json: |

{

"name": "cbr0",

"type": "flannel",

"delegate": {

"isDefaultGateway": true

}

}

net-conf.json: |

{

"Network": "10.244.0.0/16",

"Backend": {

"Type": "vxlan"

}

}

---

apiVersion: extensions/v1beta1

kind: DaemonSet #指定類型

metadata:

name: kube-flannel-ds

namespace: kube-system #指定空間

labels:

tier: node

app: flannel

spec:

template:

metadata:

labels:

tier: node

app: flannel

spec:

hostNetwork: true #指定pod使用的網絡

nodeSelector: #指定節點標簽

beta.kubernetes.io/arch: amd64

tolerations:

- key: node-role.kubernetes.io/master

operator: Exists

effect: NoSchedule

serviceAccountName: flannel

initContainers:

- name: install-cni

image: quay.io/coreos/flannel:v0.9.1-amd64

command:

- cp

args:

- -f

- /etc/kube-flannel/cni-conf.json

- /etc/cni/net.d/10-flannel.conf

volumeMounts:

- name: cni

mountPath: /etc/cni/net.d

- name: flannel-cfg

mountPath: /etc/kube-flannel/

containers:

- name: kube-flannel

image: quay.io/coreos/flannel:v0.9.1-amd64

command: [ "/opt/bin/flanneld", "--ip-masq", "--kube-subnet-mgr" ]

securityContext:

privileged: true

env:

- name: POD_NAME

valueFrom:

fieldRef:

fieldPath: metadata.name

- name: POD_NAMESPACE

valueFrom:

fieldRef:

fieldPath: metadata.namespace

volumeMounts:

- name: run

mountPath: /run

- name: flannel-cfg

mountPath: /etc/kube-flannel/

volumes:

- name: run

hostPath:

path: /run

- name: cni

hostPath:

path: /etc/cni/net.d

- name: flannel-cfg

configMap:

name: kube-flannel-cfg



查看kube-proxy配置

查看命令行創建的配置

kubectl edit daemonset kube-proxy --namespace=kube-system 類型,名字和系統空間

kubectl edit deployment nginx

kubectl edit daemonset kube-proxy --namespace=kube-system

apiVersion: extensions/v1beta1

kind: DaemonSet

metadata:

creationTimestamp: 2018-07-27T06:21:19Z #時間

generation: 1

labels:

k8s-app: kube-proxy

name: kube-proxy

namespace: kube-system #指定的空間

resourceVersion: "355720"

selfLink: /apis/extensions/v1beta1/namespaces/kube-system/daemonsets/kube-proxy

uid: 464d59d8-9165-11e8-8aac-00155d3d4613

spec:

revisionHistoryLimit: 10

selector:

matchLabels:

k8s-app: kube-proxy

template:

metadata:

creationTimestamp: null

labels:

k8s-app: kube-proxy

spec:

containers:

- command:

- /usr/local/bin/kube-proxy

- --config=/var/lib/kube-proxy/config.conf

image: k8s.gcr.io/kube-proxy-amd64:v1.10.1

imagePullPolicy: IfNotPresent

name: kube-proxy

resources: {}

securityContext:

privileged: true

terminationMessagePath: /dev/termination-log

terminationMessagePolicy: File

volumeMounts:

- mountPath: /var/lib/kube-proxy

name: kube-proxy

- mountPath: /run/xtables.lock

name: xtables-lock

- mountPath: /lib/modules

name: lib-modules

readOnly: true

dnsPolicy: ClusterFirst

hostNetwork: true

restartPolicy: Always

schedulerName: default-scheduler

securityContext: {}

serviceAccount: kube-proxy

serviceAccountName: kube-proxy

terminationGracePeriodSeconds: 30

tolerations:

- effect: NoSchedule

key: node-role.kubernetes.io/master

- effect: NoSchedule

key: node.cloudprovider.kubernetes.io/uninitialized

value: "true"

volumes:

- configMap:

defaultMode: 420

name: kube-proxy

name: kube-proxy

- hostPath:

path: /run/xtables.lock

type: FileOrCreate

name: xtables-lock

- hostPath:

path: /lib/modules

type: ""

name: lib-modules

templateGeneration: 1

updateStrategy:

rollingUpdate:

maxUnavailable: 1

type: RollingUpdate

status: #daemonset運行時的狀態

currentNumberScheduled: 3

desiredNumberScheduled: 3

numberAvailable: 3

numberMisscheduled: 0

numberReady: 3

observedGeneration: 1

updatedNumberScheduled: 3



11.封裝自己的

cat node_ex.yml

apiVersion: extensions/v1beta1

kind: DaemonSet

metadata:

name: node-exporter-daemonset

spec:

template:

metadata:

labels:

app: prometheus

spec:

hostNetwork: true #使用host網絡

containers:

- name: node-exporter

image: prom/node-exporter

imagePullPolicy: IfNotPresent

command: #容器啟動命令

- /bin/node_exporter

- --path.procfs

- /host/proc

- --path.sysfs

- /host/sys

- --collector.filesystem.ignored-mount-points

- ^/(syslprocldevlhostlect)($|/)

volumeMounts: #掛載的位置

- name: proc

mountPath: /host/proc

- name: sys

mountPath: /host/sys

- name: root

mountPath: /rootfs

volumes: #掛在的目錄

- name: proc

hostPath:

path: /proc

- name: sys

hostPath:

path: /sys

- name: root

hostPath:

path: /



12.job類型

8. jobs

1.工作類容器(完成工作後退出)job

服務類容器類型deployment、daemonset、replicaset

工作類容器

cat job.yml

apiVersion: batch/v1

kind: Job

metadata:

name: job

spec:

template:

metadata:

name: myjob

spec:

containers:

- name: hello

image: busybox

command: ["echo","hello k8s job! "]

restartPolicy: Never

batch/v1當前job版本

kind指明類型

restartPolicy指定什麽情況需要重啟容器,對於job只能設置Never或者Onfailure,對於其他的比方應用可以設置always

查看job結果

kubectl get job

技術分享圖片

通過pod查看

kubectl get pod --show-all

技術分享圖片

執行失敗的情況

技術分享圖片

查看日誌

kubectl logs job-4jcbj

kubectl describe pod job-4jcbj

2.job並行性(多個提高執行效率)

可以通過parallelism實現

apiVersion: batch/v1

kind: Job

metadata:

name: job

spec:

completions: 6 #總數量

parallelism: 3 #每次並行數

template:

metadata:

name: myjob

spec:

containers:

- name: hello

image: busybox

command: ["inval","hello k10s job! "]

restartPolicy: OnFailure

技術分享圖片

3.定時job

cronjob定時計劃任務

cat job.yml

apiVersion: batch/v1beta1 #版本

kind: CronJob #定時計劃類型

metadata:

name: hello

spec:

schedule: "*/1 * * * *" #定時

jobTemplate: $job模版

spec:

template:

spec:

containers:

- name: hello

image: busybox

command: ["echo","hello k10s job! "]

restartPolicy: OnFailure

因節點原因(或其他原因),只能顯示出三個(一直在執行完成)


控制pod在節點位置與其他類型的pod