1、创建deployment模板
1.1、以tomcat为例
kubectl create deployment tomcat --image=tomcat:8 -o yaml --dry-run >tomcat.yaml
1.2、查看创建的模板
[root@k8s-node1 ~]# cat tomcat.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
labels:
app: mtomcat
name: mtomcat
spec:
replicas: 1
selector:
matchLabels:
app: mtomcat
strategy: {}
template:
metadata:
labels:
app: mtomcat
spec:
containers:
- image: tomcat:8
name: mtomcat
resources: {}
1.3、创建tomcat
kubectl create -f tomcat.yaml
1.4、查看创建状态
[root@k8s-node1 ~]# kubectl get pod
NAMEREADYSTATUSRESTARTSAGE
mtomcat-7d6b5577b9-rwt6z1/1Running039m
2、创建svc模板
kubectl expose deployment tomcat --port=80 --target-port=8080 --type=NodePort -o yaml --dry-run >tomcat_svc.yaml
2.1、查看创建的svc模板
[root@k8s-node1 ~]# cat tomcat_svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
app: mtomcat
name: mtomcat
spec:
ports:
- port: 80#映射到宿主机端口
protocol: TCP
targetPort: 8080#容器端口
selector:
app: mtomcat
type: NodePort
2.2、创建svc
kubectl create -f tomcat_svc.yaml
2.3、查看创建状态
[root@k8s-node1 ~]# kubectl get svc
NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGE
mtomcatNodePort10.1.218.4780:22856/TCP39m
【使用kubectl命令创建yaml模板】3、检查是否可用
[root@k8s-node1 ~]# curl -I 192.168.29.176:22856
HTTP/1.1 200
Content-Type: text/html;
charset=UTF-8
Transfer-Encoding: chunked
Date: Thu, 28 Nov 2019 02:15:24 GMT