kubernetes 常用鏡像倉庫
daocloud的docker鏡像庫:
daocloud.io/library
docker-hub的k8s鏡像庫:
mirrorgooglecontainers
aliyun的k8s鏡像庫:
registry.cn-hangzhou.aliyuncs.com/google-containers
aliyun的docker鏡像庫web頁面:
https://cr.console.aliyun.com/cn-hangzhou/images
google的鏡像庫web頁面:
https://console.cloud.google.com/gcr/images/google-containers?project=google-containers
控制器
1.ReplicaSet
ReplicaSets 可以獨立使用,但今天它主要被Deployments 用作協調 Pod 創建、刪除和更新的機制。 當您使用 Deployment 時,您不必擔心還要管理它們創建的 ReplicaSet。Deployment 會擁有並管理它們的 ReplicaSet。
案例:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# modify replicas according to your case
replicas: 3
selector:
matchLabels:
tier: frontend
matchExpressions:
- {key: tier, operator: In, values: [frontend]}
template:
metadata:
labels:
app: guestbook
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google_samples/gb-frontend:v3
resources:
requests:
cpu: 100m
memory: 100Mi
env:
- name: GET_HOSTS_FROM
value: env
# If your cluster config does not include a dns service, then to
# instead access environment variables to find service host
# info, comment out the 'value: dns' line above, and uncomment the
# line below.
# value: env
ports:
- containerPort: 80
[root@k8s-master ~]# kubectl describe rs/frontend
Name: frontend
Namespace: default
Selector: tier=frontend,tier in (frontend)
Labels: app=guestbook
tier=frontend
Annotations: <none>
Replicas: 3 current / 3 desired
Pods Status: 2 Running / 1 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=guestbook
tier=frontend
Containers:
php-redis:
Image: gcr.io/google_samples/gb-frontend:v3
Port: 80/TCP
Host Port: 0/TCP
Requests:
cpu: 100m
memory: 100Mi
Environment:
GET_HOSTS_FROM: env
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 72s replicaset-controller Created pod: frontend-npwrq
Normal SuccessfulCreate 71s replicaset-controller Created pod: frontend-mtsdh
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
frontend-9si5l 1/1 Running 0 1m
frontend-dnjpy 1/1 Running 0 1m
frontend-qhloh 1/1 Running 0 1m
2.ReplicationController
- 當 pods 數量過多時,ReplicationController 會終止多餘的 pods
- 當 pods 數量太少時,ReplicationController 將會啓動新的 pods
- 與手動創建的 pod 不同,由 ReplicationController 創建的 pods 在失敗、被刪除或被終止時會被自動替換
案例:
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
[root@k8s-master ~]# kubectl apply -f nginx.yaml
[root@k8s-master ~]# kubectl describe pods nginx
Name: nginx
Namespace: default
Selector: app=nginx
Labels: app=nginx
Annotations: <none>
Replicas: 3 current / 3 desired
Pods Status: 0 Running / 3 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx
Containers:
nginx:
Image: nginx
Port: 80/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
FirstSeen LastSeen Count From SubobjectPath Type Reason Message
--------- -------- ----- ---- ------------- ---- ------ -------
20s 20s 1 {replication-controller } Normal SuccessfulCreate Created pod: nginx-qrm3m
20s 20s 1 {replication-controller } Normal SuccessfulCreate Created pod: nginx-3ntk0
20s 20s 1 {replication-controller } Normal SuccessfulCreate Created pod: nginx-4ok8v
此時,創建了三個 pod,但是還沒有運行,可能正在拉取鏡像。 稍後,相同的命令可能顯示:
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
刪除一個 ReplicationController 以及它的 Pod:
- 要刪除一個 ReplicationController 以及它的 Pod,使用 kubectl delete
只刪除 ReplicationController:
- 使用 kubectl ,爲 kubectl delete 指定 --cascade=false 選項
- 你可以刪除一個 ReplicationController 而不影響它的任何 pod
注意:
ReplicaSet 是下一代 ReplicationController ,支持新的基於集合的標籤選擇器。 它主要被 Deployment 用來作爲一種編排 pod 創建、刪除及更新的機制。 請注意,我們推薦使用 Deployment 而不是直接使用 ReplicaSet,除非您需要自定義更新編排或根本不需要更新。
3.StatefulSets
- StatefulSet 是用來管理有狀態應用的工作負載 API 對象。
- StatefulSet 用來管理 Deployment 和擴展一組 Pod,並且能爲這些 Pod 提供序號和唯一性保證。
- 和 Deployment 相同的是,StatefulSet 管理了基於相同容器定義的一組 Pod。但和 Deployment不同的是,StatefulSet 爲它們的每個 Pod 維護了一個固定的 ID。這些 Pod是基於相同的聲明來創建的,但是不能相互替換:無論怎麼調度,每個 Pod 都有一個永久不變的 ID。
特點:
- 穩定的、唯一的網絡標識符。
- 穩定的、持久的存儲。
- 有序的、優雅的部署和縮放。
- 有序的、自動的滾動更新。
缺點:
- 給定 Pod 的存儲必須由 PersistentVolume 驅動 基於所請求的 storage class
來提供,或者由管理員預先提供。 - 刪除和/或收縮 StatefulSet 並不會刪除它關聯的存儲卷。這樣做是爲了保證數據安全,它通常比自動清除 StatefulSet
所有相關的資源更有價值。 - StatefulSet 當前需要 headless 服務 來負責 Pod 的網絡標識。您需要負責創建此服務。
- 當刪除 StatefulSets 時,StatefulSet 不提供任何終止 Pod 的保證。爲了實現StatefulSet 中的Pod 可以有序和優雅的終止,可以在刪除之前將 StatefulSet 縮放爲 0。
- 在默認 Pod 管理策略(OrderedReady) 時使用 滾動更新,可能進入需要 人工干預 才能修復的損壞狀態。
案例:
- 名爲 nginx 的無頭服務用來控制網絡域。
- 名爲 web 的 StatefulSet 有一個 Spec,它表明將在單個 Pod 中啓動 nginx 容器的 3 個副本。
- volumeClaimTemplates 將通過 PersistentVolumes 驅動提供的 PersistentVolume
來提供穩定的存儲。
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # has to match .spec.template.metadata.labels
serviceName: "nginx"
replicas: 3 # by default is 1
template:
metadata:
labels:
app: nginx # has to match .spec.selector.matchLabels
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: k8s.gcr.io/nginx-slim:0.8
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "my-storage-class"
resources:
requests:
storage: 1Gi
4.DaemonSet
- DaemonSet 確保全部(或者某些)節點上運行一個 Pod 的副本。當有節點加入集羣時, 也會爲他們新增一個 Pod,當有節點從集羣移除時,這些 Pod 也會被回收。刪除 DaemonSet 將會刪除它創建的所有 Pod。
特點:
-
運行集羣存儲 daemon,例如在每個節點上運行 glusterd、ceph。
-
在每個節點上運行日誌收集 daemon,例如fluentd、logstash。
-
在每個節點上運行監控 daemon,例如 Prometheus Node Exporter、Sysdig
代理、collectd、Dynatrace OneAgent、AppDynamics 代理、Datadog 代理、New Relic代理,Ganglia gmond 或 Instana 代理。 -
一個簡單的用法是在所有的節點上都啓動一個 DaemonSet,將被作爲每種類型的 daemon 使用。
-
一個稍微複雜的用法是單獨對每種 daemon 類型使用多個 DaemonSet,但具有不同的標誌,和/或對不同硬件類型具有不同的內存、CPU要求。
案例:
- 下面的 daemonset.yaml 文件描述了一個運行 fluentd-elasticsearch Docker 鏡像的
DaemonSet:
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: fluentd-elasticsearch
namespace: kube-system
labels:
k8s-app: fluentd-logging
spec:
selector:
matchLabels:
name: fluentd-elasticsearch
template:
metadata:
labels:
name: fluentd-elasticsearch
spec:
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
containers:
- name: fluentd-elasticsearch
image: quay.io/fluentd_elasticsearch/fluentd:v2.5.2
resources:
limits:
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
volumeMounts:
- name: varlog
mountPath: /var/log
- name: varlibdockercontainers
mountPath: /var/lib/docker/containers
readOnly: true
terminationGracePeriodSeconds: 30
volumes:
- name: varlog
hostPath:
path: /var/log
- name: varlibdockercontainers
hostPath:
path: /var/lib/docker/containers
- 基於 YAML 文件創建 DaemonSet
[root@k8s-master ~]# kubectl apply -f elasticsearch.yaml
5.Deployments
- Deployment控制器將實際狀態以受控的速率更改爲所需的狀態。您可以定義部署以創建新的副本集,或刪除現有部署並在新部署中採用其所有資源。
案例:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.4
ports:
- containerPort: 80