部署Pod服务
分别使用kubectl run和kubectl apply 部署nginx和tomcat服务
# 快速启动一个nginx服务
kubectl run my-nginx --image=nginx --port=80# 使用yaml创建tomcat服务
kubectl apply -f my-tomcat.yaml
my-tomcat.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: tomcat-deploymentlabels:app: tomcat
spec:replicas: 2selector:matchLabels:app: tomcattemplate:metadata:labels:app: tomcatspec:containers:- name: tomcat-containerimage: tomcat:latestports:- containerPort: 8080
- kubectl run:主要用于快速创建一个新的Pod,通常用于临时或一次性任务
- kubectl apply:跟进yaml文件中定义的资源进行创建或更新,适合管理长期存在的Kubernetes对象
执行成功后,可以看到Pod已经执行起来
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx 1/1 Running 1 (3h17m ago) 8h
tomcat-deployment-6cc9875b5-krskl 1/1 Running 1 (13m ago) 55m
tomcat-deployment-6cc9875b5-qkk9h 1/1 Running 0 55m
部署service服务
部署nginx service服务
# 使用`kubectl expose`命令基于已有的`my-nginx` Pod 快速生成 Service 的 YAML 文件
kubectl expose pod my-nginx --name=my-nginx-service --port=18080 --target-port=80 --type=NodePort --dry-run=client -o yaml > my-nginx-service.yaml[root@master ~]# kubectl apply -f my-nginx-service.yaml[root@master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 46h
my-nginx-service NodePort 10.109.146.167 <none> 18080:32192/TCP 6m46s
部署tomcat service服务
kubectl expose deployment tomcat-deployment --name=my-tomcat-service --port=18089 --target-port=8080 --type=NodePort --dry-run=client -o yaml > my-tomcat-service.yaml[root@master ~]# kubectl apply -f my-tomcat-service.yaml
service/my-tomcat-service created[root@master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d
my-nginx-service NodePort 10.109.146.167 <none> 18080:32192/TCP 141m
my-tomcat-service NodePort 10.99.171.11 <none> 18089:31875/TCP 15s
安装ingress controller服务
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.12.0-beta.0/deploy/static/provider/cloud/deploy.yaml# 通过节点的31520/30871 访问nginx controller的80/443端口
[root@master ~]# kubectl get service -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller LoadBalancer 10.109.66.157 <pending> 80:31520/TCP,443:30871/TCP 171m
ingress-nginx-controller-admission ClusterIP 10.110.31.148 <none> 443/TCP 171m
使用ingress转发服务
kubectl create ingress my-ingress-nginx --class=nginx --rule="mynginx.com/*=my-nginx-service:18080" --dry-run=client -o yaml > my-nginx-ingress.yamlkubectl create ingress my-ingress-tomcat --class=nginx --rule="mytomcat.com/*=my-tomcat-service:18089" --dry-run=client -o yaml > my-tomcat-ingress.yaml[root@master ~]# kubectl apply -f my-tomcat-ingress.yaml
ingress.networking.k8s.io/my-ingress-tomcat created[root@master ~]# kubectl apply -f my-nginx-ingress.yaml
ingress.networking.k8s.io/my-ingress-nginx created# 两个ingress资源已经被创建
[root@master ~]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
my-ingress-nginx nginx mynginx.com 80 9s
my-ingress-tomcat nginx mytomcat.com 80 6s
通过浏览器,ingress controller地址
配置hosts172.16.15.129 mynginx.com
172.16.15.129 mytomcat.com
访问nginx
http://mynginx.com:31520
访问tomcat
http://mytomcat.com:31520/index.jsp