加入公众号一起发现美好的技术
1、创建deployment模板
1.1、以tomcat为例
kubectl create deployment tomcat --image=tomcat:8 -o yaml --dry-run=client >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
NAME READY STATUS RESTARTS AGE
mtomcat-7d6b5577b9-rwt6z 1/1 Running 0 39m
2、创建svc模板
kubectl expose deployment tomcat --port=80 --target-port=8080 --type=NodePort -o yaml --dry-run=client >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
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mtomcat NodePort 10.1.218.47 <none> 80:22856/TCP 39m
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