K8S进阶实战
夏佳怡1、资源创建方式
2、Namespace
名称空间用来隔离资源
1 2
| kubectl create ns hello kubectl delete ns hello
|
1 2 3 4
| apiVersion: v1 kind: Namespace metadata: name: hello
|
3、Pod
运行中的一组容器,Pod是kubernetes中应用的最小单位.
一个pod中可以运行多个容器
一个物理机中可以有多个pod
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| kubectl run mynginx --image=nginx
kubectl get pod
kubectl describe pod Pod名字
kubectl delete pod Pod名字
kubectl logs Pod名字
kubectl get pod -owide
curl 192.168.169.136
|
1 2 3 4 5 6 7 8 9 10 11
| apiVersion: v1 kind: Pod metadata: labels: run: mynginx name: mynginx
spec: containers: - image: nginx name: mynginx
|
1 2 3 4 5 6 7 8 9 10 11 12
| apiVersion: v1 kind: Pod metadata: labels: run: myapp name: myapp spec: containers: - image: nginx name: nginx - image: tomcat:8.5.68 name: tomcat
|
4、Deployment
控制Pod,使Pod拥有多副本,自愈,扩缩容等能力
1 2 3 4 5
| kubectl run mynginx --image=nginx
kubectl create deployment mytomcat --image=tomcat:8.5.68
|
1、多副本
1
| kubectl create deployment my-dep --image=nginx --replicas=3
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| apiVersion: apps/v1 kind: Deployment metadata: labels: app: my-dep name: my-dep spec: replicas: 3 selector: matchLabels: app: my-dep template: metadata: labels: app: my-dep spec: containers: - image: nginx name: nginx
|
2、扩缩容
1
| kubectl scale --replicas=5 deployment/my-dep
|
1 2 3
| kubectl edit deployment my-dep
|
3、自愈&故障转移
4、滚动更新
1 2 3
| kubectl set image deployment/my-dep nginx=nginx:1.16.1 --record kubectl rollout status deployment/my-dep
|
5、版本回退
1 2 3 4 5 6 7 8 9 10 11 12
| kubectl rollout history deployment/my-dep
kubectl rollout history deployment/my-dep --revision=2
kubectl rollout undo deployment/my-dep
kubectl rollout undo deployment/my-dep --to-revision=2
|
更多:
除了Deployment,k8s还有 StatefulSet
、DaemonSet
、Job
等 类型资源。我们都称为 工作负载
。
有状态应用使用 StatefulSet
部署,无状态应用使用 Deployment
部署
https://kubernetes.io/zh/docs/concepts/workloads/controllers/
5、Service
将一组 Pods 公开为网络服务的抽象方法。
可以将功能相同的一组Pods抽象为一个service
1 2 3 4 5
| kubectl expose deployment my-dep --port=8000 --target-port=80
kubectl get pod -l app=my-dep
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVersion: v1 kind: Service metadata: labels: app: my-dep name: my-dep spec: selector: app: my-dep ports: - port: 8000 protocol: TCP targetPort: 80
|
1、ClusterIP
给service分配集群ip
1 2
| kubectl expose deployment my-dep --port=8000 --target-port=80 --type=ClusterIP
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| apiVersion: v1 kind: Service metadata: labels: app: my-dep name: my-dep spec: ports: - port: 8000 protocol: TCP targetPort: 80 selector: app: my-dep type: ClusterIP
|
2、NodePort
分配一个物理机的真实端口范围在 30000-32767 之间,但是无法负载均衡,所有的物理机的该端口都打开
1
| kubectl expose deployment my-dep --port=8000 --target-port=80 --type=NodePort
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| apiVersion: v1 kind: Service metadata: labels: app: my-dep name: my-dep spec: ports: - port: 8000 protocol: TCP targetPort: 80 selector: app: my-dep type: NodePort
|
6、Ingress
1、安装
1 2 3 4 5 6 7 8 9 10 11
| wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.47.0/deploy/static/provider/baremetal/deploy.yaml
vi deploy.yaml
registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/ingress-nginx-controller:v0.46.0
kubectl get pod,svc -n ingress-nginx
|
2、使用
官网地址:https://kubernetes.github.io/ingress-nginx/
就是nginx做的
测试环境
应用如下yaml,准备好测试环境
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| apiVersion: apps/v1 kind: Deployment metadata: name: hello-server spec: replicas: 2 selector: matchLabels: app: hello-server template: metadata: labels: app: hello-server spec: containers: - name: hello-server image: registry.cn-hangzhou.aliyuncs.com/lfy_k8s_images/hello-server ports: - containerPort: 9000 --- apiVersion: apps/v1 kind: Deployment metadata: labels: app: nginx-demo name: nginx-demo spec: replicas: 2 selector: matchLabels: app: nginx-demo template: metadata: labels: app: nginx-demo spec: containers: - image: nginx name: nginx --- apiVersion: v1 kind: Service metadata: labels: app: nginx-demo name: nginx-demo spec: selector: app: nginx-demo ports: - port: 8000 protocol: TCP targetPort: 80 --- apiVersion: v1 kind: Service metadata: labels: app: hello-server name: hello-server spec: selector: app: hello-server ports: - port: 8000 protocol: TCP targetPort: 9000
|
1、域名访问
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress-host-bar spec: ingressClassName: nginx rules: - host: "hello.atguigu.com" http: paths: - pathType: Prefix path: "/" backend: service: name: hello-server port: number: 8000 - host: "demo.atguigu.com" http: paths: - pathType: Prefix path: "/nginx" backend: service: name: nginx-demo port: number: 8000
|
2、路径重写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| apiVersion: networking.k8s.io/v1 kind: Ingress metadata: annotations: nginx.ingress.kubernetes.io/rewrite-target: /$2 name: ingress-host-bar spec: ingressClassName: nginx rules: - host: "hello.atguigu.com" http: paths: - pathType: Prefix path: "/" backend: service: name: hello-server port: number: 8000 - host: "demo.atguigu.com" http: paths: - pathType: Prefix path: "/nginx(/|$)(.*)" backend: service: name: nginx-demo port: number: 8000
|
3、流量限制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: ingress-limit-rate annotations: nginx.ingress.kubernetes.io/limit-rps: "1" spec: ingressClassName: nginx rules: - host: "haha.atguigu.com" http: paths: - pathType: Exact path: "/" backend: service: name: nginx-demo port: number: 8000
|