云计算第四阶段: cloud二周目 07-08

cloud 07

一、k8s服务管理

创建服务

# 资源清单文件
[root@master ~]# kubectl create service clusterip websvc --tcp=80:80 --dry-run=client -o yaml
[root@master ~]# vim websvc.yaml
---
kind: Service
apiVersion: v1
metadata:name: websvc
spec:type: ClusterIPselector:app: webports:- protocol: TCPport: 80targetPort: 80[root@master ~]# kubectl apply -f websvc.yaml 
service/websvc created
[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)
kubernetes   ClusterIP   10.245.0.1      <none>        443/TCP
websvc       ClusterIP   10.245.5.18     <none>        80/TCP
解析域名
# 安装工具软件包
[root@master ~]# dnf install -y bind-utils
# 查看 DNS 服务地址
[root@master ~]# kubectl -n kube-system get service kube-dns
NAME       TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)
kube-dns   ClusterIP   10.245.0.10   <none>        53/UDP,53/TCP,9153/TCP
# 域名解析测试
[root@master ~]# host websvc.default.svc.cluster.local 10.245.0.10
Using domain server:
Name: 10.245.0.10
Address: 10.245.0.10#53
Aliases: websvc.default.svc.cluster.local has address 10.245.5.18

创建后端应用

[root@master ~]# vim web1.yaml 
---
kind: Pod
apiVersion: v1
metadata:name: web1labels:app: web   # 服务靠标签寻找后端
spec:containers:- name: apacheimage: myos:httpd[root@master ~]# kubectl apply -f web1.yaml
pod/web1 created
[root@master ~]# curl http://10.245.5.18
Welcome to The Apache.
负载均衡
[root@master ~]# sed 's,web1,web2,' web1.yaml |kubectl apply -f -
pod/web2 created
[root@master ~]# sed 's,web1,web3,' web1.yaml |kubectl apply -f -
pod/web3 created
[root@master ~]# curl -s http://10.245.5.18/info.php |grep php_host
php_host:       web1
[root@master ~]# curl -s http://10.245.5.18/info.php |grep php_host
php_host:       web2
[root@master ~]# curl -s http://10.245.5.18/info.php |grep php_host
php_host:       web3
固定 IP 服务
[root@master ~]# vim websvc.yaml 
---
kind: Service
apiVersion: v1
metadata:name: websvc
spec:type: ClusterIPclusterIP: 10.245.1.80    # 可以设置 ClusterIPselector:app: webports:- protocol: TCPport: 80targetPort: 80[root@master ~]# kubectl replace --force -f websvc.yaml 
service "websvc" deleted
service/websvc replaced
[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)
kubernetes   ClusterIP   10.245.0.1    <none>        443/TCP
websvc       ClusterIP   10.245.1.80   <none>        80/TCP

端口别名

[root@master ~]# vim websvc.yaml 
---
kind: Service
apiVersion: v1
metadata:name: websvc
spec:type: ClusterIPclusterIP: 10.245.1.80selector:app: webports:- protocol: TCPport: 80targetPort: myhttp    # 使用别名查找后端服务端口[root@master ~]# kubectl replace --force -f websvc.yaml 
service "websvc" deleted
service/websvc replaced[root@master ~]# kubectl delete pod --all
pod "web1" deleted
pod "web2" deleted
pod "web3" deleted[root@master ~]# vim web1.yaml 
---
kind: Pod
apiVersion: v1
metadata:name: web1labels:app: web
spec:containers:- name: apacheimage: myos:httpdports:               # 配置端口规范- name: myhttp       # 端口别名protocol: TCP      # 协议containerPort: 80  # 端口号[root@master ~]# kubectl apply -f web1.yaml
pod/web1 created
[root@master ~]# curl http://10.245.1.80
Welcome to The Apache.

服务排错

---
kind: Service
apiVersion: v1
metadata:name: web123
spec:type: ClusterIPclusterIP: 192.168.1.88selector:app: apacheports:- protocol: TCPport: 80targetPort: web

nodePort


对外发布服务

[root@master ~]# vim mysvc.yaml
---
kind: Service
apiVersion: v1
metadata:name: mysvc
spec:type: NodePort            # 服务类型selector:app: webports:- protocol: TCPport: 80nodePort: 30080         # 映射端口号targetPort: 80[root@master ~]# kubectl apply -f mysvc.yaml 
service/mysvc configured
[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)
kubernetes   ClusterIP   10.245.0.1    <none>        443/TCP
websvc       ClusterIP   10.245.1.80   <none>        80/TCP
mysvc        NodePort    10.245.3.88   <none>        80:30080/TCP[root@master ~]# curl http://node-0001:30080
Welcome to The Apache.
[root@master ~]# curl http://node-0002:30080
Welcome to The Apache.
[root@master ~]# curl http://node-0003:30080
Welcome to The Apache.
[root@master ~]# curl http://node-0004:30080
Welcome to The Apache.
[root@master ~]# curl http://node-0005:30080
Welcome to The Apache.

二、lngress 安装与策略配置

安装控制器

[root@master ~]# cd plugins/ingress
[root@master ingress]# docker load -i ingress.tar.xz
[root@master ingress]# docker images|while read i t _;do[[ "${t}" == "TAG" ]] && continue[[ "${i}" =~ ^"harbor:443/".+ ]] && continuedocker tag ${i}:${t} harbor:443/plugins/${i##*/}:${t}docker push harbor:443/plugins/${i##*/}:${t}docker rmi ${i}:${t} harbor:443/plugins/${i##*/}:${t}
done
[root@master ingress]# sed -ri 's,^(\s*image: )(.*/)?(.+),\1harbor:443/plugins/\3,' deploy.yaml
443:    image: registry.k8s.io/ingress-nginx/controller:v1.9.6
546:    image: registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20231226-1a7112e06
599:    image: registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20231226-1a7112e06[root@master ingress]# kubectl apply -f deploy.yaml
[root@master ingress]# kubectl -n ingress-nginx get pods
NAME                                        READY   STATUS      RESTARTS
ingress-nginx-admission-create--1-lm52c     0/1     Completed   0
ingress-nginx-admission-patch--1-sj2lz      0/1     Completed   0
ingress-nginx-controller-5664857866-tql24   1/1     Running     0
验证后端服务
[root@master ~]# kubectl get pods,services 
NAME       READY   STATUS    RESTARTS   AGE
pod/web1   1/1     Running   0          35mNAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)
service/kubernetes   ClusterIP   10.245.0.1    <none>        443/TCP
service/websvc       ClusterIP   10.245.1.80   <none>        80/TCP
service/mysvc        NodePort    10.245.3.88   <none>        80:30080/TCP[root@master ~]# curl http://10.245.1.80
Welcome to The Apache.
对外发布服务
# 查询 ingress 控制器类名称
[root@master ~]# kubectl get ingressclasses.networking.k8s.io 
NAME    CONTROLLER             PARAMETERS   AGE
nginx   k8s.io/ingress-nginx   <none>       5m7s# 资源清单文件
[root@master ~]# kubectl create ingress mying --class=nginx --rule=nsd.tedu.cn/*=mysvc:80 --dry-run=client -o yaml
[root@master ~]# vim mying.yaml
---
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:name: mying
spec:ingressClassName: nginxrules:- host: nsd.tedu.cnhttp:paths:- path: /pathType: Prefixbackend:service:name: websvcport:number: 80[root@master ~]# kubectl apply -f mying.yaml 
ingress.networking.k8s.io/mying created
[root@master ~]# kubectl get ingress
NAME    CLASS   HOSTS         ADDRESS        PORTS
mying   nginx   nsd.tedu.cn   192.168.1.51   80
[root@master ~]# curl -H "Host: nsd.tedu.cn" http://192.168.1.51
Welcome to The Apache.


三、Dashboard 安装

#下面给大家介绍下新的k8s插件

web 管理插件

安装 Dashboard

[root@master ~]# cd plugins/dashboard
[root@master dashboard]# docker load -i dashboard.tar.xz
[root@master dashboard]# docker images|while read i t _;do[[ "${t}" == "TAG" ]] && continue[[ "${i}" =~ ^"harbor:443/".+ ]] && continuedocker tag ${i}:${t} harbor:443/plugins/${i##*/}:${t}docker push harbor:443/plugins/${i##*/}:${t}docker rmi ${i}:${t} harbor:443/plugins/${i##*/}:${t}
done
[root@master dashboard]# sed -ri 's,^(\s*image: )(.*/)?(.+),\1harbor:443/plugins/\3,' recommended.yaml
193:    image: kubernetesui/dashboard:v2.7.0
278:    image: kubernetesui/metrics-scraper:v1.0.8
[root@master dashboard]# kubectl apply -f recommended.yaml
[root@master dashboard]# kubectl -n kubernetes-dashboard get pods
NAME                                         READY   STATUS    RESTARTS
dashboard-metrics-scraper-66f6f56b59-b42ng   1/1     Running   0
kubernetes-dashboard-65ff57f4cf-lwtsk        1/1     Running   0

发布服务

# 查看服务状态
[root@master dashboard]# kubectl -n kubernetes-dashboard get service
NAME                        TYPE        CLUSTER-IP       PORT(S)
dashboard-metrics-scraper   ClusterIP   10.245.205.236   8000/TCP
kubernetes-dashboard        ClusterIP   10.245.215.40    443/TCP
# 获取服务资源对象文件
[root@master dashboard]# sed -n '30,45p' recommended.yaml >dashboard-svc.yaml
[root@master dashboard]# vim dashboard-svc.yaml
---
kind: Service
apiVersion: v1
metadata:labels:k8s-app: kubernetes-dashboardname: kubernetes-dashboardnamespace: kubernetes-dashboard
spec:type: NodePortports:- port: 443nodePort: 30443targetPort: 8443selector:k8s-app: kubernetes-dashboard[root@master dashboard]# kubectl apply -f dashboard-svc.yaml 
service/kubernetes-dashboard configured
[root@master dashboard]# kubectl -n kubernetes-dashboard get service
NAME                        TYPE        CLUSTER-IP       PORT(S)
dashboard-metrics-scraper   ClusterIP   10.245.205.236   8000/TCP
kubernetes-dashboard        NodePort    10.245.215.40    443:30443/TCP

  • #记得访问下仪表盘dashboard登录页面

四、RBAC 权限管理

服务账号与权限

创建服务账号

# 资源对象模板
[root@master ~]# kubectl -n kubernetes-dashboard create serviceaccount kube-admin --dry-run=client -o yaml
[root@master ~]# vim admin-user.yaml
---
kind: ServiceAccount
apiVersion: v1
metadata:name: kube-adminnamespace: kubernetes-dashboard[root@master ~]# kubectl apply -f admin-user.yaml 
serviceaccount/kube-admin created
[root@master ~]# kubectl -n kubernetes-dashboard get serviceaccounts 
NAME                   SECRETS   AGE
default                0         16m
kube-admin             0         11s
kubernetes-dashboard   0         16m

获取用户 token

[root@master ~]# kubectl -n kubernetes-dashboard create token kube-admin
<Base64 编码的令牌数据>

角色与鉴权

#类似网游DNF里面的角色管理,GM管理员和玩家的关系。

资源对象描述作用域
ServiceAccount服务账号,为 Pod 中运行的进程提供了一个身份单一名称空间
Role角色,包含一组代表相关权限的规则单一名称空间
ClusterRole角色,包含一组代表相关权限的规则全集群
RoleBinding将权限赋予用户,Role、ClusterRole 均可使用单一名称空间
ClusterRoleBinding将权限赋予用户,只可以使用 ClusterRole全集群

资源对象权限

createdeletedeletecollectiongetlistpatchupdatewatch
创建删除删除集合获取属性获取列表补丁更新监控
普通角色
[root@master ~]# kubectl cluster-info dump |grep authorization-mode"--authorization-mode=Node,RBAC",# 资源对象模板
[root@master ~]# kubectl -n default create role myrole --resource=pods --verb=get,list --dry-run=client -o yaml
[root@master ~]# kubectl -n default create rolebinding kube-admin-role --role=myrole --serviceaccount=kubernetes-dashboard:kube-admin --dry-run=client -o yaml
[root@master ~]# vim myrole.yaml 
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: myrolenamespace: default
rules:
- apiGroups:- ""resources:- podsverbs:- get- list---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: kube-admin-rolenamespace: default
roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: myrole
subjects:
- kind: ServiceAccountname: kube-adminnamespace: kubernetes-dashboard[root@master ~]# kubectl apply -f myrole.yaml 
role.rbac.authorization.k8s.io/myrole created
rolebinding.rbac.authorization.k8s.io/kube-admin-role created[root@master ~]# kubectl delete -f myrole.yaml 
role.rbac.authorization.k8s.io "myrole" deleted
rolebinding.rbac.authorization.k8s.io "kube-admin-role" deleted
集群管理员
[root@master ~]# kubectl get clusterrole
NAME                              CREATED AT
admin                             2022-06-24T08:11:17Z
cluster-admin                     2022-06-24T08:11:17Z
... ...# 资源对象模板
[root@master ~]# kubectl create clusterrolebinding kube-admin-role --clusterrole=cluster-admin --serviceaccount=kubernetes-dashboard:kube-admin --dry-run=client -o yaml
[root@master ~]# vim admin-user.yaml 
---
kind: ServiceAccount
apiVersion: v1
metadata:name: kube-adminnamespace: kubernetes-dashboard---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: kube-admin-role
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-admin
subjects:
- kind: ServiceAccountname: kube-adminnamespace: kubernetes-dashboard[root@master ~]# kubectl apply -f admin-user.yaml 
serviceaccount/kube-admin unchanged
clusterrolebinding.rbac.authorization.k8s.io/kube-admin-role created

cloud 08

#上一小节讲过K8S的有控制组件和计算组件。现在我们一起来深入研究K8S的控制组件。

一、Deployment

资源清单文件
[root@master ~]# kubectl create deployment myweb --image=myos:httpd --dry-run=client -o yaml
[root@master ~]# vim mydeploy.yaml
---
kind: Deployment          # 资源对象类型
apiVersion: apps/v1       # 版本
metadata:                 # 元数据name: mydeploy          # 名称
spec:                     # 详细定义replicas: 3             # 副本数量selector:               # 定义标签选择器matchLabels:          # 支持 matchExpressions 表达式语法app: deploy-httpd   # 通过标签来确定那个 Pod 由它来管理template:               # 定义用来创建 Pod 的模板,以下为 Pod 定义metadata:labels:app: deploy-httpdspec:containers:- name: apacheimage: myos:httpd
配置案例
# 创建控制器
[root@master ~]# kubectl apply -f mydeploy.yaml 
deployment.apps/mydeploy created[root@master ~]# kubectl get deployments
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
mydeploy   3/3     3            3           1s# 控制器自动创建 ReplicaSet
[root@master ~]# kubectl get replicasets 
NAME                  DESIRED   CURRENT   READY   AGE
mydeploy-76f96b85df   3         3         3       2s# 控制器自动创建 Pod
[root@master ~]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
mydeploy-76f96b85df-5gng9   1/1     Running   0          3s
mydeploy-76f96b85df-vsfrw   1/1     Running   0          3s
mydeploy-76f96b85df-z9x95   1/1     Running   0          3s# 集群自维护自治理
[root@master ~]# kubectl delete pod --all 
pod "mydeploy-76f96b85df-5gng9" deleted
pod "mydeploy-76f96b85df-vsfrw" deleted
pod "mydeploy-76f96b85df-z9x95" deleted# 删除后自动重新创建
[root@master ~]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
mydeploy-76f96b85df-7dvwh   1/1     Running   0          7s
mydeploy-76f96b85df-kpbz4   1/1     Running   0          7s
mydeploy-76f96b85df-kr2zq   1/1     Running   0          7s
集群服务

# 创建集群服务
[root@master ~]# vim websvc.yaml
---
kind: Service
apiVersion: v1
metadata:name: websvc
spec:type: ClusterIPclusterIP: 10.245.1.80selector:app: deploy-httpdports:- protocol: TCPport: 80targetPort: 80[root@master ~]# kubectl replace --force -f websvc.yaml 
service/websvc replaced
[root@master ~]# curl -m 3 http://10.245.1.80
Welcome to The Apache.
集群扩缩容

#抽象来说,扩容就是在基础存储设备上,添加新的设备,然后挂载到新的设备上。达到扩容结果, 类似吃鸡游戏里的扩容弹夹。

而缩容就是为了达到更佳的运行效率,减少存储设备上的存储空间,达到缩容目的。古代的增兵减灶

# 集群扩容
[root@master ~]# kubectl scale deployment mydeploy --replicas 10
deployment.apps/mydeploy scaled[root@master ~]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
mydeploy-76f96b85df-kg27l   1/1     Running   0          6s
mydeploy-76f96b85df-q5fzb   1/1     Running   0          6s
mydeploy-76f96b85df-rxhp4   1/1     Running   0          6s
mydeploy-76f96b85df-szf69   1/1     Running   0          6s
mydeploy-76f96b85df-tp2xj   1/1     Running   0          6s
......# 集群缩容
[root@master ~]# kubectl scale deployment mydeploy --replicas=2
deployment.apps/mydeploy scaled[root@master ~]# kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
mydeploy-76f96b85df-7dvwh   1/1     Running   0          51s
mydeploy-76f96b85df-kr2zq   1/1     Running   0          51s
历史版本信息
# 查看历史版本
[root@master ~]# kubectl rollout history deployment mydeploy
deployment.apps/mydeploy 
REVISION  CHANGE-CAUSE
1         <none># 添加注释信息
[root@master ~]# kubectl annotate deployments mydeploy kubernetes.io/change-cause="httpd.v1"
deployment.apps/mydeploy annotated[root@master ~]# kubectl rollout history deployment mydeploy
deployment.apps/mydeploy 
REVISION  CHANGE-CAUSE
1         httpd.v1# 更新资源清单文件
[root@master ~]# vim mydeploy.yaml
# 在创建容器的镜像下面添加imagePullPolicy: Always[root@master ~]# kubectl apply -f mydeploy.yaml
deployment.apps/mydeploy patched# 更新版本信息
[root@master ~]# kubectl annotate deployments mydeploy kubernetes.io/change-cause="httpd.v2"
deployment.apps/mydeploy annotated[root@master ~]# kubectl rollout history deployment mydeploy
deployment.apps/mydeploy 
REVISION  CHANGE-CAUSE
1         httpd.v1
2         httpd.v2
滚动更新
# 修改镜像,滚动更新集群
[root@master ~]# kubectl set image deployment mydeploy apache=myos:nginx
deployment.apps/mydeploy image updated# 给新版本添加注释信息
[root@master ~]# kubectl annotate deployments mydeploy kubernetes.io/change-cause="nginx.v1"
deployment.apps/mydeploy annotated# 查看历史版本信息
[root@master ~]# kubectl rollout history deployment mydeploy
deployment.apps/mydeploy 
REVISION  CHANGE-CAUSE
1         httpd.v1
2         httpd.v2
3         nginx.v1# 访问验证服务
[root@master ~]# curl -m 3 http://10.245.1.80
Nginx is running !
版本回滚

#类似游戏里面的怀旧服,而这里的版本回滚是用于恢复数据

# 历史版本与回滚
[root@master ~]# kubectl rollout undo deployment mydeploy --to-revision 1
deployment.apps/mydeploy rolled back[root@master ~]# kubectl rollout history deployment mydeploy 
deployment.apps/mydeploy 
REVISION  CHANGE-CAUSE
2         httpd.v2
3         nginx.v1
4         httpd.v1[root@master ~]# curl -m 3 http://10.245.1.80
Welcome to The Apache.

清理资源对象
# 删除控制器时会自动回收自己创建的 Pod
[root@master ~]# kubectl delete deployments mydeploy
deployment.apps "mydeploy" deleted

二、DaemonSet

配置案例
[root@master ~]# cp -a mydeploy.yaml myds.yaml
[root@master ~]# vim myds.yaml
---
kind: DaemonSet         # 资源对象类型
apiVersion: apps/v1
metadata:name: myds            # 控制器名称
spec:# replicas: 2         # 删除副本参数selector:matchLabels:app: ds-httpd     # 修改标签防止冲突template:metadata:labels:app: ds-httpd   # 修改标签防止冲突spec:containers:- name: apacheimage: myos:httpdimagePullPolicy: Always[root@master ~]# kubectl apply -f myds.yaml 
daemonset.apps/myds created
[root@master ~]# kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   IP            NODE
myds-msrcx   1/1     Running   0          10.244.1.11   node-0001
myds-lwq8l   1/1     Running   0          10.244.2.17   node-0002
myds-4wt72   1/1     Running   0          10.244.3.14   node-0003
myds-6k82t   1/1     Running   0          10.244.4.15   node-0004
myds-9c6wc   1/1     Running   0          10.244.5.19   node-0005
清理资源对象
# 删除控制器
[root@master ~]# kubectl delete daemonsets myds
daemonset.apps "myds" deleted

三、Job、CronJob

 

 Job 控制器

# 资源文件模板
[root@master ~]# kubectl create job myjob --image=myos:8.5 --dry-run=client -o yaml -- sleep 3
[root@master ~]# vim myjob.yaml 
---
kind: Job
apiVersion: batch/v1
metadata:name: myjob
spec:template:  # 以下定义 Pod 模板metadata: {}spec:restartPolicy: OnFailurecontainers:- name: myjobimage: myos:8.5command: ["/bin/sh"]args:- -c- |sleep 3exit $((RANDOM%2))[root@master ~]# kubectl apply -f myjob.yaml 
job.batch/myjob created# 失败了会重启
[root@master ~]# kubectl get pods -l job-name=myjob -w
NAME             READY   STATUS      RESTARTS     AGE
myjob--1-lrtbk   1/1     Running     0            2s
myjob--1-lrtbk   0/1     Error       0            4s
myjob--1-lrtbk   1/1     Running     1 (1s ago)   5s
myjob--1-lrtbk   0/1     Completed   1            9s[root@master ~]# kubectl get jobs.batch 
NAME    COMPLETIONS   DURATION   AGE
myjob   1/1           8s         12s# 删除Job控制器
[root@master ~]# kubectl delete -f myjob.yaml 
job.batch "myjob" deleted

#pod控制器创建失败,任务会确保创建成功而重启,避免失败

Cronjob

#类似ansible中的crontab模块,可以定时执行某一任务
配置案例
# 资源对象模板
[root@master ~]# kubectl create cronjob mycj --image=myos:8.5 --schedule='* * * * *' --dry-run=client -o yaml -- sleep 3
[root@master ~]# vim mycj.yaml
---
kind: CronJob
apiVersion: batch/v1
metadata:name: mycj
spec:schedule: "* * * * *"jobTemplate:  # 以下定义 Job 模板metadata: {}spec:template:metadata: {}spec:restartPolicy: OnFailurecontainers:- name: myjobimage: myos:8.5command: ["/bin/sh"]args:- -c- |sleep 3exit $((RANDOM%2))[root@master ~]# kubectl apply -f mycj.yaml 
cronjob.batch/mycj created
[root@master ~]# kubectl get cronjobs 
NAME   SCHEDULE        SUSPEND   ACTIVE   LAST SCHEDULE   AGE
mycj   * * * * 1-5     False     0        <none>          4s# 按照时间周期,每分钟触发一个任务
[root@master ~]# kubectl get jobs -w
NAME                     READY   STATUS              RESTARTS
mycj-27808172--1-w6sbx   0/1     Pending             0
mycj-27808172--1-w6sbx   0/1     ContainerCreating   0
mycj-27808172--1-w6sbx   1/1     Running             0
mycj-27808172--1-w6sbx   0/1     Completed           1# 保留三次结果,多余的会被删除
[root@master ~]# kubectl get jobs 
NAME            COMPLETIONS   DURATION   AGE
mycj-27605367   1/1           31s        3m30s
mycj-27605368   1/1           31s        2m30s
mycj-27605369   1/1           31s        90s
mycj-27605370   0/1           30s        30s[root@master ~]# kubectl get jobs 
NAME            COMPLETIONS   DURATION   AGE
mycj-27605368   1/1           31s        2m33s
mycj-27605369   1/1           31s        93s
mycj-27605370   1/1           31s        33s# 删除CJ控制器
[root@master ~]# kubectl delete -f mycj.yaml 
cronjob.batch "mycj" deleted

四、StatefulSet

Headless 服务
[root@master ~]# cp websvc.yaml stssvc.yaml 
[root@master ~]# vim stssvc.yaml 
---
kind: Service
apiVersion: v1
metadata:name: stssvc          # 服务名称
spec:type: ClusterIPclusterIP: None       # 设置 IP 为 Noneselector:app: sts-httpd      # 设置 Pod 标签ports:- protocol: TCPport: 80targetPort: 80[root@master ~]# kubectl apply -f stssvc.yaml 
service/stssvc created[root@master ~]# kubectl get services stssvc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
stssvc       ClusterIP   None          <none>        80/TCP    51s
资源清单文件
[root@master ~]# cp -a mydeploy.yaml mysts.yaml
[root@master ~]# vim mysts.yaml
---
kind: StatefulSet       # 资源对象类型
apiVersion: apps/v1
metadata:name: mysts           # 控制器名称
spec:serviceName: stssvc   # 新增 headless 服务名称replicas: 3selector:matchLabels:app: sts-httpd    # 修改标签防止冲突template:metadata:labels:app: sts-httpd  # 修改标签防止冲突spec:containers:- name: apacheimage: myos:httpd
配置案例
# statefulset 主要解决了 Pod 创建顺序的问题
# statefulset 主要解决了访问指定 Pod 的问题
[root@master ~]# kubectl apply -f mysts.yaml 
statefulset.apps/mysts created[root@master ~]# kubectl get pods
NAME      READY   STATUS    RESTARTS   AGE
mysts-0   1/1     Running   0          3s
mysts-1   1/1     Running   0          2s
mysts-2   1/1     Running   0          1s# 所有 Pod IP 地址
[root@master ~]# host stssvc.default.svc.cluster.local 10.245.0.10
Using domain server:
Name: 10.245.0.10
Address: 10.245.0.10#53
Aliases: 
stssvc.default.svc.cluster.local has address 10.244.1.81
stssvc.default.svc.cluster.local has address 10.244.2.82
stssvc.default.svc.cluster.local has address 10.244.3.83# 单个 Pod IP 地址
[root@master ~]# host mysts-0.stssvc.default.svc.cluster.local 10.245.0.10
Using domain server:
Name: 10.245.0.10
Address: 10.245.0.10#53
Aliases: 
mysts-0.stssvc.default.svc.cluster.local has address 10.244.1.81# 删除sts控制器
[root@master ~]# kubectl delete -f mysts.yaml -f stssvc.yaml
statefulset.apps "mysts" deleted
service "stssvc" deleted
弹性云服务

五、HorizontalPodAutoscaler

 配置后端服务

# 为 Deploy 模板添加资源配额
[root@master ~]# cat mydeploy.yaml websvc.yaml >mycluster.yaml
[root@master ~]# vim mycluster.yaml 
---
kind: Deployment
apiVersion: apps/v1
metadata:name: mydeploy
spec:replicas: 1selector:matchLabels:app: deploy-httpdtemplate:metadata:labels:app: deploy-httpdspec:containers:- name: apacheimage: myos:httpdresources:           # 为该资源设置配额requests:          # HPA 控制器会根据配额使用情况伸缩集群cpu: 300m        # CPU 配额---
kind: Service
apiVersion: v1
metadata:name: websvc
spec:type: ClusterIPclusterIP: 10.245.1.80selector:app: deploy-httpdports:- protocol: TCPport: 80targetPort: 80[root@master ~]# kubectl replace --force -f mycluster.yaml
deployment.apps/mydeploy replaced
service/websvc replaced# 验证服务
[root@master ~]# kubectl top pods
NAME                       CPU(cores)   MEMORY(bytes)   
mydeploy-b4f9dc786-w4x2z   6m           18Mi            [root@master ~]# curl -s http://10.245.1.80/info.php
<pre>
Array
([REMOTE_ADDR] => 10.244.219.64[REQUEST_METHOD] => GET[HTTP_USER_AGENT] => curl/7.61.1[REQUEST_URI] => /info.php
)
php_host:   mydeploy-b4f9dc786-w4x2z
1229

HPA 控制器

[root@master ~]# vim myhpa.yaml 
---
kind: HorizontalPodAutoscaler
apiVersion: autoscaling/v2
metadata:name: myhpa
spec:behavior:scaleDown:stabilizationWindowSeconds: 60scaleTargetRef:kind: DeploymentapiVersion: apps/v1name: mydeployminReplicas: 1maxReplicas: 5metrics:- type: Resourceresource:name: cputarget:type: UtilizationaverageUtilization: 50[root@master ~]# kubectl apply -f myhpa.yaml 
horizontalpodautoscaler.autoscaling/myhpa created# 刚刚创建 unknown 是正常现象,最多等待 60s 就可以正常获取数据
[root@master ~]# kubectl get horizontalpodautoscalers
NAME    REFERENCE             TARGETS         MINPODS   MAXPODS   REPLICAS
myhpa   Deployment/mydeploy   <unknown>/50%   1         5         0[root@master ~]# kubectl get horizontalpodautoscalers
NAME    REFERENCE             TARGETS   MINPODS   MAXPODS   REPLICAS
myhpa   Deployment/mydeploy   0%/50%    1         5         3
配置案例

# 终端 1 访问提高负载
[root@master ~]# while sleep 1;do curl -s "http://10.245.1.80/info.php?id=100000" -o /dev/null; done &
# 终端 2 监控 HPA 变化
[root@master ~]# kubectl get hpa -w
NAME    REFERENCE             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
myhpa   Deployment/mydeploy   0%/50%    1         5         1          1m
myhpa   Deployment/mydeploy   31%/50%   1         5         1          2m
myhpa   Deployment/mydeploy   70%/50%   1         5         1          2m15s
myhpa   Deployment/mydeploy   72%/50%   1         5         2          2m30s
myhpa   Deployment/mydeploy   36%/50%   1         5         2          2m45s
myhpa   Deployment/mydeploy   55%/50%   1         5         2          3m
myhpa   Deployment/mydeploy   58%/50%   1         5         3          3m15s
myhpa   Deployment/mydeploy   39%/50%   1         5         3          3m30s
... ...
myhpa   Deployment/mydeploy   66%/50%   1         5         4          5m
myhpa   Deployment/mydeploy   68%/50%   1         5         5          5m15s
myhpa   Deployment/mydeploy   55%/50%   1         5         5          5m30s
myhpa   Deployment/mydeploy   58%/50%   1         5         5          5m45s
myhpa   Deployment/mydeploy   62%/50%   1         5         5          6m# 如果 60s 内平均负载小于标准值,就会自动缩减集群规模
[root@master ~]# kubectl get hpa -w
NAME    REFERENCE             TARGETS   MINPODS   MAXPODS   REPLICAS   AGE
myhpa   Deployment/mydeploy   52%/50%   1         5         5          13m
myhpa   Deployment/mydeploy   44%/50%   1         5         5          13m15s
myhpa   Deployment/mydeploy   38%/50%   1         5         5          13m30s
myhpa   Deployment/mydeploy   35%/50%   1         5         5          13m45s
myhpa   Deployment/mydeploy   28%/50%   1         5         5          14m
... ...
myhpa   Deployment/mydeploy   8%/50%    1         5         5          18m30s
myhpa   Deployment/mydeploy   9%/50%    1         5         4          18m45s
myhpa   Deployment/mydeploy   9%/50%    1         5         4          19m
myhpa   Deployment/mydeploy   12%/50%   1         5         3          19m15s
myhpa   Deployment/mydeploy   15%/50%   1         5         3          19m30s
myhpa   Deployment/mydeploy   18%/50%   1         5         2          19m45s
myhpa   Deployment/mydeploy   33%/50%   1         5         1          20m

课后总结:

#我们本节学的好多控制器,都有差异和区别,可以按照类似以下的提示词,来对AI提问,得到更加符合工作使用环境的回答。


至此云计算cloud二周目内容更新完毕!

大家有想练习的,可以去华为云、阿里云等云平台,创建帐号,使用30天免费体验版云产品

熟悉相关云产品的使用与配置,里面也有一些项目的免费体验课,可以照着案例学基本项目架构

下一阶段,将回重回网络阶段,深入了解云计算与云原生领域的网络架构知识.

                        

下个阶段见!!!   

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/56040.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ChatTTS在Windows电脑的本地部署与远程生成音频详细实战指南

文章目录 前言1. 下载运行ChatTTS模型2. 安装Cpolar工具3. 实现公网访问4. 配置ChatTTS固定公网地址 前言 本篇文章主要介绍如何快速地在Windows系统电脑中本地部署ChatTTS开源文本转语音项目&#xff0c;并且我们还可以结合Cpolar内网穿透工具创建公网地址&#xff0c;随时随…

react里实现左右拉伸实战

封装组件&#xff1a; 我自己写的一个简单的组件&#xff0c;可能有bug。不想自己写&#xff0c;建议用第三方库实现。 新建一个resizeBox.tsx文件写上代码如下&#xff1a; import React, { ReactNode, useState, useEffect, useRef } from react; import styles from &quo…

【中危】Oracle TNS Listener SID 可以被猜测

一、漏洞详情 Oracle 打补丁后&#xff0c;复测出一处中危漏洞&#xff1a;Oracle TNS Listener SID 可以被猜测。 可以通过暴力猜测的方法探测出Oracle TNS Listener SID&#xff0c;探测出的SID可以用于进一步探测Oracle 数据库的口令。 建议解决办法&#xff1a; 1. 不应该使…

【某农业大学计算机网络实验报告】实验四 路由信息协议RIP

实验目的&#xff1a; 1&#xff0e;深入了解RIP协议的特点和配置方法&#xff1a;通过此次实验&#xff0c;掌握RIP协议作为一种动态路由协议的基本工作原理&#xff0c;了解其距离向量算法的核心概念&#xff0c;以及如何在网络设备上配置RIP协议&#xff1b; 2.验证RIP协议…

基于微信小程序的电影交流平台

作者&#xff1a;计算机学姐 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、JSP、ElementUI、Python、小程序等&#xff0c;“文末源码”。 专栏推荐&#xff1a;前后端分离项目源码、SpringBoot项目源码、Vue项目源码、SSM项目源码、微信小程序源码 精品专栏&#xff1a;…

【Next.js 项目实战系列】02-创建 Issue

原文链接 CSDN 的排版/样式可能有问题&#xff0c;去我的博客查看原文系列吧&#xff0c;觉得有用的话&#xff0c;给我的库点个star&#xff0c;关注一下吧 上一篇【Next.js 项目实战系列】01-创建项目 创建 Issue 配置 MySQL 与 Prisma​ 在数据库中可以找到相关内容&…

Java项目-基于Springboot的招生管理系统项目(源码+说明).zip

作者&#xff1a;计算机学长阿伟 开发技术&#xff1a;SpringBoot、SSM、Vue、MySQL、ElementUI等&#xff0c;“文末源码”。 开发运行环境 开发语言&#xff1a;Java数据库&#xff1a;MySQL技术&#xff1a;SpringBoot、Vue、Mybaits Plus、ELementUI工具&#xff1a;IDEA/…

智联云采 SRM2.0 testService SQL注入漏洞复现

0x01 产品简介 智联云采是一款针对企业供应链管理难题及智能化转型升级需求而设计的解决方案,针对企业供应链管理难题,及智能化转型升级需求,智联云采依托人工智能、物联网、大数据、云等技术,通过软硬件系统化方案,帮助企业实现供应商关系管理和采购线上化、移动化、智能…

求助,宠物空气净化器该怎么选?双十一有什么推荐购买的吗?

今晚就要付双十一尾款了&#xff0c;拖延症晚期的我还没做什么功课。本来不打算消费的&#xff0c;看了眼购物车&#xff0c;之前想买的宠物空气净化器降价了不少&#xff0c;不想错失这次优惠。 我家猫孩子之前不怎么掉毛的&#xff0c;连日常的梳毛我都经常偷懒&#xff0c;…

WordPress+Nginx 安装教程

WordPress 是一个开源的网站建设工具&#xff0c;可以用它来“快速”搭建个人博客&#xff0c;官网等等。它本身是用 php 开发的&#xff0c;本身部署不复杂&#xff0c;主要是需要一些配套的东西才能跑起来&#xff0c;网上的一些教程也是写的不清不楚&#xff0c;本文针对非 …

Centos7安装ZLMediaKit

https://github.com/ZLMediaKit/ZLMediaKit 一 获取代码 git clone https://gitee.com/xia-chu/ZLMediaKit cd ZLMediaKit git submodule update --init git submodule update --init 命令用于初始化和更新 Git 仓库中的子模块&#xff08;submodules&#xff09;。这个命令…

安全生产玩手机检测系统 玩手机识别系统 玩手机监测预警系统 使用 Python 和 OpenCV 库实现

在生产作业过程中&#xff0c;员工玩手机是一种极其危险的行为。它会分散员工的注意力&#xff0c;使其无法专注于工作任务。生产现场往往存在各种潜在的危险因素&#xff0c;如机械设备的运转、高空作业、化学品的使用等&#xff0c;一旦员工分心&#xff0c;就很容易忽视这些…

跨浏览器免费书签管理系统

随着互联网信息的爆炸式增长&#xff0c;如何有效管理我们日常浏览中发现的重要网页&#xff0c;成为了每个重度互联网用户的需求。一个跨平台的书签管理网站能够帮助用户在不同设备之间无缝同步和管理书签。本文将分享如何使用 Python 和 SQLite 构建一个简单、易于维护的跨平…

暖水毯/取暖毯语音识别控制芯片IC方案

暖水毯、取暖毯作为现代家居生活的温暖伴侣&#xff0c;其智能化升级已是大势所趋。在暖水毯与取暖毯中融入语音识别控制芯片IC方案&#xff0c;为用户的冬日取暖体验带来了革命性的变革。 一、暖水毯/取暖毯增加语音识别控制芯片方案&#xff0c;让产品能通过对话来调节&…

Ubuntu 24.04 系统上配置 Node.js 运行环境

本文我们重点介绍两种安装 Node.js 的方法。第一种方法使用 NVM (Node VersionManager)&#xff0c;这是安装和管理多个 Node.js 版本的最好和最快的方法。第二种方法使用官方包存储库在 Ubuntu 上安装 Node.js&#xff0c;一次只允许安装一个版本。 必备条件 A running Ubun…

qUtf8Printable()和qPrintable()

qUtf8Printable 函数是 Qt 框架中的一个实用函数&#xff0c;其主要作用是将 QString 类型的字符串安全地转换为 const char* 类型的 UTF-8 编码字符串。这个转换过程对于需要将 QString 与那些接受 const char* 参数的 C 风格字符串函数进行交互的场景特别有用。 qUtf8Printa…

3.Java入门笔记--基础语法

1.字面量 概念&#xff1a;计算机用来处理数据的&#xff0c;字面量就是告诉程序员数据在程序中的书写格式 常用数据&#xff1a;整数&#xff0c;小数直接写&#xff1b;字符单引号&#xff08;A&#xff09;且只能放一个字符&#xff1b;字符串双引号&#xff08;"Hel…

ROUGE:摘要自动评估软件包

算法解析 ROUGE&#xff08;Recall-Oriented Understudy for Gisting Evaluation&#xff09;是一组用于自动评估文本摘要质量的指标&#xff0c;主要通过比较机器生成的摘要与一个或多个参考摘要之间的重合程度来衡量。ROUGE 包括多个变体&#xff0c;其中最常用的有 ROUGE-N…

深度解析模型调优与正则化:L1、L2正则化及偏差-方差的权衡

&#x1f3af; 深度解析模型调优与正则化&#xff1a;L1、L2正则化及偏差-方差的权衡 &#x1f4d6; 目录 &#x1f31f; 模型调优的本质&#xff1a;偏差与方差的权衡&#x1f50e; 正则化的概念与作用&#x1f6e0; L1正则化&#xff08;Lasso回归&#xff09;详解与实现⚙…

linux 修改主机名和用户名颜色

编译 ~/.bashrc vim ~/.bashrc 如下格式 PS1\[\e[1;31m\]\h:\[\e[0;32m\]\w \[\e[1;34m\]\u\[\e[0m\]\$ PS1${debian_chroot:($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]\[\033[01;36m\]\h \[\033[01;33m\]\w \[\033[01;35m\]\$ \[\033[00m\] if [ -e /lib/terminfo…