随着RC的退出,Deployment作为Pod的控制器之一使用地越来越广泛,这篇文章以nginx服务为例,介绍一下使用的方式。
事前准备本文使用Kubernetes 1.17.2,可参看下文进行快速环境搭建:
- 单机版本或者集群版本环境搭建
[root@host131 ansible]# kubectl get node -o wide NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME 192.168.163.131 Ready2m25s v1.17.2 192.168.163.131CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 docker://19.3.5 [root@host131 ansible]#Deployment配置文件
使用如下yaml文件用于设定Deployment
[root@host131 nginx]# cat deployment.yml --- apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 1 selector: matchLabels: app: nginx-app template: metadata: labels: app: nginx-app spec: containers: - name: nginx-host image: nginx:1.17.8-alpine ports: - containerPort: 80 ... [root@host131 nginx]#
- 注意事项:matchLabels的内容需要与template中metadata的labels内容匹配,不过不用担心,在创建时会进行检查,比如将matchLabels改为app: nginx-app1,创建时会提示如下错误信息
[root@host131 nginx]# kubectl create -f deployment.yml The Deployment "nginx-deployment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"nginx-app1"}: `selector` does not match template `labels` [root@host131 nginx]#Service配置文件
使用如下Service配置文件
[root@host131 nginx]# cat service.yml --- apiVersion: v1 kind: Service metadata: name: nginx-service labels: app: nginx-service-app spec: type: NodePort ports: - port: 80 nodePort: 30000 selector: app: nginx-app ... [root@host131 nginx]#
- 注意事项:selector关联的需要是Deployment中template定义的label。
使用Deployment的配置文件生成Deployment
[root@host131 nginx]# kubectl create -f deployment.yml deployment.apps/nginx-deployment created [root@host131 nginx]#
结果确认
[root@host131 nginx]# kubectl get deployment -o wide |grep nginx nginx-deployment 1/1 1 1 34s nginx-host nginx:1.17.8-alpine app=nginx-app [root@host131 nginx]# [root@host131 nginx]# kubectl get pods -o wide |grep nginx nginx-deployment-7c5b8fc568-wkh9b 1/1 Running 0 51s 10.254.152.6 192.168.163.131[root@host131 nginx]# [root@host131 nginx]# [root@host131 nginx]# kubectl exec -it nginx-deployment-7c5b8fc568-wkh9b sh / # hostname nginx-deployment-7c5b8fc568-wkh9b / #生成Service
[root@host131 nginx]# kubectl create -f service.yml service/nginx-service created [root@host131 nginx]#
结果确认
[root@host131 nginx]# kubectl get service -o wide |grep nginx nginx-service NodePort 10.254.77.18280:30000/TCP 9s app=nginx-app [root@host131 nginx]#nginx使用:集群地址访问
在集群所在的集群中,可以使用集群地址+80端口进行访问
[root@host131 nginx]# curl http://10.254.77.182 <html> <head> <title>Welcome to nginx! width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }关注打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?