#yaml文件之前博客有
kubectl create -f nginx-deployment.yaml
#Replicaset 是控制版本,副本数,回滚就是通过此来实现
kubectl get pods,deploy,rs
kubectl edit deployment/nginx-deployment
kubectl rollout history deployment/nginx-deployment
部署有状态应用
解决Pod独立生命周期,保持Pod启动顺序和唯一性
稳定,唯一的网络标识符,持久存储(例如:etcd配置文件,节点地址发生变化,将无法使用)
有序,优雅的部署和扩展、删除和终止(例如:mysql主从关系,先启动主,再启动从)
有序,滚动更新
应用场景:数据库
无状态:
1)deployment 认为所有的pod都是一样的
2)不用考虑顺序的要求
3)不用考虑在哪个node节点上运行
4)可以随意扩容和缩容
有状态
1)实例之间有差别,每个实例都有自己的独特性,元数据不同,例如etcd,zookeeper
2)实例之间不对等的关系,以及依靠外部存储的应用。
常规service和无头服务区别
1) service:一组Pod访问策略,提供cluster-IP群集之间通讯,还提供负载均衡和服务发现。
2)Headless service 无头服务,不需要cluster-IP,直接绑定具体的Pod的IP
#yaml文件之前博客有
kubectl create -f nginx-service.yaml
kubectl get svc
#在node节点上操作
systemctl restart flanneld.service
systemctl restart docker
#查看群集间通讯
curl 10.0.0.204
vim headless.yaml
kubectl create -f headless.yaml
kubectl get svc
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None #无群集内部通信地址
selector:
app: nginx
#复制coredns.yaml到master01的root家目录(Github下载)
kubectl create -f coredns.yaml
kubectl get pods -n kube-system
cd demo/
vim pod3.yaml
#验证dns解析
kubectl create -f pod3.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
#进入pod资源
kubectl exec -it dns-test sh
#创建完整资源,验证headless
vim sts.yaml
kubectl apply -f sts.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 sts.yaml
kubectl get pods
kubectl apply -f pod3.yaml
kubectl exec -it dns-test sh
#查看是否已经绑定到PodIP,ClusterIP实际上是负载均衡
nslookup nginx-statefulset-0.nginx
nslookup nginx-statefulset-1.nginx
nslookup nginx-statefulset-2.nginx
vim ds.yaml
kubectl apply -f ds.yaml
kubectl get pods -o wide
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
#示例中,重试次数默认是6次,修改为4次,当遇到异常时Never状态会重启,所以要设定次数。
#计算圆周率
vim job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
#在node节点上拉取perl镜像
docker pull perl
kubectl apply -f job.yaml
kubectl get pods
#在日志中查看结果
kubectl logs pi-6wtpp
vim cronjob.yaml
kubectl apply -f cronjob.yaml
kubectl get pods
kubectl logs hello-1590859620-l89h8
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure