春风十里不如你 —— Taozi - endpoints https://www.xiongan.host/index.php/tag/endpoints/ 【k8s】service服务和job服务 https://www.xiongan.host/index.php/archives/208/ 2023-05-17T13:35:50+08:00 Service服务发现使用Service使用Service实验使用的目录病创建后端的httpd-Dy[root@master servicefile]# vim httpd-dy.yaml kind: Deployment apiVersion: apps/v1 metadata: name: httpd spec: replicas: 3 selector:   matchLabels:     app: httpd template:   metadata:     labels:       app: httpd   spec:     containers:     - name: httpd       image: httpd       ports:       - containerPort: 80部署Deployment并查看信息创建httpd-service.yaml文件[root@master servicefile]# vim httpd-service.yaml创建service并查看该信息(下kubernetes服务是系统服务)测试服务可用性,通过curl命令查看服务是否正常[root@master servicefile]# curl 10.102.124.67:8080可以删除刚刚创建的服务创建httpd-expose.yaml,并部署[root@master servicefile]# vim httpd-expose.yaml kind: Service apiVersion: v1 metadata: name: httpd-svc spec: type: NodePort selector:   app: httpd ports: - protocol: TCP   port: 8080   targetPort: 80   nodePort: 30144使用跳板机浏览器登录,查看node节点ip:端口使用DNS创建client.yaml,创建一个客户端pod,测试DNS功能[root@master servicefile]# vim client.yaml kind: Pod apiVersion: v1 metadata: name: clientpod spec: containers:   - name: clientpod     image: busybox:1.28.3     args:     - /bin/sh     - -c     - sleep 30000创建并进入Pod命令行[root@master servicefile]# kubectl apply -f client.yaml使用nslookup命令查看服务域名,wget命令通过域名访问服务实训任务创建deployment1要求: 2 副本,镜像类型 httpd创建deployment2要求: 3副本,镜像类型 httpd创建 service1,service1 后端为 deployment1 和 deployment2 中所有 pod。创建 service2,service2 后端为 deployment1 中的第一个 pod 和 deployment2 中的第一个pod为dy的第一个pod和dy2的第一个pod打上标签tz=httpd01查看容器的详细信息查看端口信息,可以看到svc2的pod是要求所说的DeamonSet 与 Job使用DaemonSet创建一个Daemonset的yaml文件,并运行[root@master servicefile]# vim DS-nginx.yaml kind: DaemonSet apiVersion: apps/v1 metadata: name: nginx-daemonset spec: selector:   matchLabels:     app: nginx template:   metadata:     labels:       app: nginx   spec:     containers:     - name: nginx       image: nginx:1.7.9       ports:       - containerPort: 80查看daemonset的pod信息位置删除pod,查看daemonset的自动恢复功能查看到已经恢复好了使用Job创建Job的yaml文件[root@master servicefile]# vim pi-job.yaml kind: Job apiVersion: batch/v1 metadata: name: pi spec: template:   spec:     containers:     - name: pi       image: perl       command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]     restartPolicy: Never backoffLimit: 4创建Job并查看运行状态,他运行完毕后自动关闭了查看他的运行结果使用CronJob创建CronJob的yaml文件,设置每一分钟运行一次返回一次hello[root@master servicefile]# vim CJ-hello.yaml kind: CronJob apiVersion: batch/v1beta1 metadata: name: hello spec: schedule: "*/1 * * * *" jobTemplate:   spec:     template:       spec:         containers:         - name: hello           image: busybox           args:           - /bin/sh           - -c           - date; echo Hello from the Kubernets cluster-tz123         restartPolicy: OnFailure运行cronjob,查看运行情况查看pod的状态已经完成可以查看到运行cronjob后,每隔一分钟就会创建新的pod的,并输出信息实训任务创建一个 DaemonSet包含两个 pod镜像为 nginx创建一个job,用于输出helloworld创建一个 cronjob,在每日的 xx 小时 xx 点输出 helloworld。删除本次实验创建 DaemonSet,Job 和 CronJob。