一、案例
1.1 自主式创建service关联上方的pod
资源名称 | my-nginx-kkk |
---|---|
命名空间 | my-kkk |
容器镜像 | nginx:1.21 |
容器端口 | 80 |
标签 | njzb:my-kkk |
1.1.1 创建一个demo文件夹
1.1.2 创建并获取模版文件
1.1.3 查看服务并编写yaml文件
1.1.4 编写yaml文件并部署,查看服务是否运行成功
apiVersion: v1
kind: Namespace
metadata:name: my-kkk
---
apiVersion: v1
kind: Pod
metadata:labels:njzb: my-kkkname: my-nginx-kkknamespace: my-kkk
spec:containers:- image: nginx:1.21name: my-nginx-kkkports:- containerPort: 80restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:labels:njzb: my-kkkname: my-nginx-kkknamespace: my-kkk
spec:ports:- nodePort: 31111 ###节点端口号port: 80 ###服务暴露端口protocol: TCP ###通信协议targetPort: 80 ###要转发到的容器端口selector:njzb: my-kkk ###该服务所选择的后端podtype: NodePort
修改yaml文件
apiVersion: v1
kind: Namespace
metadata:name: my-kkk
---
apiVersion: v1
kind: Pod
metadata:labels:njzb: my-kkkname: my-nginx-kkknamespace: my-kkk
spec:containers:- image: nginx:1.21name: my-nginx-kkkports:- containerPort: 80restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:labels:njzb: my-kkkname: my-nginx-kkknamespace: my-kkk
spec:ports:- nodePort: 31111 ###节点端口号port: 80 ###服务暴露端口protocol: TCP ###通信协议targetPort: 80 ###要转发到的容器端口selector:njzb: my-kkk ###该服务所选择的后端podtype: NodePort
查看所有内容,进入到pod内,输入内容,退出后检测是否有内容
1.2 部署redis
1.2.1 编写redis配置文件
apiVersion: apps/v1
kind: Deployment
metadata:name: redis-dpmlabels:app: redis
spec: ###资源所需参数replicas: 1selector:matchLabels:app: redis ###只会与标签app:redis的pod关联template: ###业务模板,如果有多个副本,所有副本属性会按照模板进行匹配metadata:labels:app: redisspec: ###资源所需参数containers:- name: redisimage: redis:latestports:- containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:name: redis-dbmlabels:app: redis
spec:selector:app: redisports:- nodePort: 32379port: 6379protocol: TCPtargetPort: 6379type: NodePort
1.2.2 运行redis配置文件
1.2.3 查看是否有服务和pod端口
1.3 部署myapp
1.3.1 编写myapp配置文件
apiVersion: apps/v1
kind: Deployment
metadata:name: myapp-kknamespace: defaultlabels:app: myapp
spec:replicas: 1selector:matchLabels:app: myapptemplate:metadata:labels:app: myappspec:containers:- name: myappimage: soscscs/myapp:v1ports:- containerPort: 80
1.3.2 运行服务并查看default下的pod是否运行成功
1.4 部署mysql
1.4.1 编写mysql配置文件
apiVersion: v1
kind: Namespace
metadata:name: mysql-server
---
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: mysql-01name: mysql-01namespace: mysql-server
spec: replicas: 1 selector: matchLabels: app: mysql-01 template: metadata: labels: app: mysql-01spec: containers:- image: mysql:latest name: mysql ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: "123123" volumeMounts:- name: mysql-datamountPath: /var/lib/mysqlvolumes:- name: mysql-dataemptyDir: {}
---
apiVersion: v1
kind: Service
metadata:name: mysql-servicenamespace: mysql-serverlabels:app: mysql-01
spec:type: NodePortports:- port: 3306targetPort: 3306selector:app: mysql-01