控制器:又称之为工作负载,分别包含以下类型控制器
[root@master ~]# vim nginx-deployment.yaml
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
kubectl create -f nginx-deployment.yaml
kubectl get pods,deploy,rs
查看控制器
kubectl edit deployment/nginx-deployment
查看历史版本
kubectl rollout history deployment/nginx-deployment
无状态
有状态
ps:k8s暴露方式主要就3种:ingress loadbalance(SLB/ALB K8S集群外的负载均衡器、Ng、harproxy、KONG、traefik等等) service
vim nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
labels:
app: nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
selector:
app: nginx
[root@master ~]# kubectl create -f nginx-service.yaml
service/nginx-svc created
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 5d7h
nginx-svc NodePort 10.105.83.81 80:30676/TCP 2s
在node节点操作,查看集群间通讯
因为Pod动态IP地址,所以常用于绑定DNS访问—来尽可能固定Pod的位置
[root@master demo]# vim headless.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
kubectl apply -f headless.yaml
kubectl get svc
再定义一个pod
[root@master demo]# vim dns-test.yaml
apiVersion: v1
kind: Pod
metadata:
name: dns-test
spec:
containers:
- name: busybox
image: busybox:1.28.4
args:
- /bin/sh
- -c
- sleep 36000
restartPolicy: Never
验证dns解析
kubectl create -f dns-test.yaml
kubectl get svc
解析kubernetes和nginx-svc名称
kubectl exec -it dns-test sh
创建StatefulSet.yaml文件
vim statefulSet.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: nginx-statefulset
namespace: default
spec:
serviceName: nginx
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
清理所有pod
kubectl delete -f .
kubectl create -f statefulSet.yaml
kubectl get pods,svc
解析pod的唯一域名和自身的ip
kubectl apply -f dns-test.yaml
kubectl exec -it dns-test sh
nslookup nginx-statefulset-0.nginx
nslookup nginx-statefulset-1.nginx
nslookup nginx-statefulset-2.nginx
总结
StatefulSet与Deployment区别:有身份的!
身份三要素:
[root@master demo]# vim daemonSet.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.4
ports:
- containerPort: 80
DaemonSet会在每个node节点都创建一个Pod
kubectl apply -f daemonSet.yaml
kubectl get pods
示例:
重试次数默认是6次,修改为4次,当遇到异常时Never状态会重启,所以要设定次数。
[root@master demo]# vim job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl:5.34.0
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
在node1节点下载perl镜像,因为镜像比较大所以提前下载好
docker pull perl
创建
查看到完成状态
结果输出到控制台
清除job资源
kubectl get job
kubectl delete -f job.yaml
示例:
[root@master demo]# vim cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: lcdb
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: lcdb
image: busybox
args:
- /bin/sh
- -c
- date; echo lcdb from the Kubernetes cluster
restartPolicy: OnFailure
kubectl apply -f cronjob.yaml
kubectl get pod
kubectl get cronjob
清除cronjob资源
kubectl delete -f cronjob.yaml