一、案例
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-kkk
name: my-nginx-kkk
namespace: my-kkk
spec:
containers:
- image: nginx:1.21
name: my-nginx-kkk
ports:
- containerPort: 80
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
labels:
njzb: my-kkk
name: my-nginx-kkk
namespace: my-kkk
spec:
ports:
- nodePort: 31111 ###节点端口号
port: 80 ###服务暴露端口
protocol: TCP ###通信协议
targetPort: 80 ###要转发到的容器端口
selector:
njzb: my-kkk ###该服务所选择的后端pod
type: NodePort
修改yaml文件
apiVersion: v1
kind: Namespace
metadata:
name: my-kkk
---
apiVersion: v1
kind: Pod
metadata:
labels:
njzb: my-kkk
name: my-nginx-kkk
namespace: my-kkk
spec:
containers:
- image: nginx:1.21
name: my-nginx-kkk
ports:
- containerPort: 80
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
labels:
njzb: my-kkk
name: my-nginx-kkk
namespace: my-kkk
spec:
ports:
- nodePort: 31111 ###节点端口号
port: 80 ###服务暴露端口
protocol: TCP ###通信协议
targetPort: 80 ###要转发到的容器端口
selector:
njzb: my-kkk ###该服务所选择的后端pod
type: NodePort
查看所有内容,进入到pod内,输入内容,退出后检测是否有内容
1.2 部署redis
1.2.1 编写redis配置文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: redis-dpm
labels:
app: redis
spec: ###资源所需参数
replicas: 1
selector:
matchLabels:
app: redis ###只会与标签app:redis的pod关联
template: ###业务模板,如果有多个副本,所有副本属性会按照模板进行匹配
metadata:
labels:
app: redis
spec: ###资源所需参数
containers:
- name: redis
image: redis:latest
ports:
- containerPort: 6379
---
apiVersion: v1
kind: Service
metadata:
name: redis-dbm
labels:
app: redis
spec:
selector:
app: redis
ports:
- nodePort: 32379
port: 6379
protocol: TCP
targetPort: 6379
type: NodePort
1.2.2 运行redis配置文件
1.2.3 查看是否有服务和pod端口
1.3 部署myapp
1.3.1 编写myapp配置文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-kk
namespace: default
labels:
app: myapp
spec:
replicas: 1
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: soscscs/myapp:v1
ports:
- 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-01
name: mysql-01
namespace: mysql-server
spec:
replicas: 1
selector:
matchLabels:
app: mysql-01
template:
metadata:
labels:
app: mysql-01
spec:
containers:
- image: mysql:latest
name: mysql
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123123"
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: mysql-service
namespace: mysql-server
labels:
app: mysql-01
spec:
type: NodePort
ports:
- port: 3306
targetPort: 3306
selector:
app: mysql-01