cat myapp-svc-headless.yaml
apiVersion: v1
kind: Service
metadata:
name: myapp-svc
namespace: default
spec:
selector:
app: myapp
release: canary
clusterIP: "None"
ports:
- port: 80
targetPort: 80
---
apiVersion: apps /v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 2
selector:
matchLabels:
app: myapp
release: canary
template:
metadata:
labels:
app: myapp
release: canary
spec:
containers:
- name: myapp
image: ikubernetes /myapp :v1
ports:
- name: http
containerPort: 80
# 创建pod时,用nodeSelector可实现精准分布
kubectl apply -f myapp-svc-headless.yaml
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443 /TCP 13d
myapp-svc ClusterIP None <none> 80 /TCP 29m
# 通过Ingress把myapp-svc发布出去
cat ingress-myapp.yaml
apiVersion: extensions /v1beta1
kind: Ingress
metadata:
name: ingress-myapp
namespace: default
annotations:
kubernetes.io /ingress .class: "nginx"
spec:
rules:
- host: myapp.lixiang.com
http:
paths:
- path:
backend:
serviceName: myapp-svc
servicePort: 80
namespace要和deployment和要发布的service处于同一个名称空间
annotations:说明我们要用到的ingress-controller是nginx,而不是Traefik、Envoy
host:表示访问这个域名,就会转发到后端myapp-deploy管理的pod上
kubectl apply -f ingress-myapp.yaml
kubectl get ingress
NAME HOSTS ADDRESS PORTS AGE
ingress-myapp myapp.lixiang.com 80 5m34s
# 进入交互式命令行
kubectl exec -n ingress-nginx -it nginx-ingress-controller-689498bc7c-sm972 -- /bin/sh
$ cat nginx.conf
## start server myapp.lixiang.com
server {
server_name myapp.lixiang.com ;
listen 80;
location / {
set $namespace "default" ;
set $ingress_name "ingress-myapp" ;
set $service_name "myapp-svc" ;
set $service_port "80" ;
set $location_path "/" ;
# ingress一经创建,就将信息注入到nginx-ingress-controller这个pod中,
# 个人感觉ingress像一个监视者、搬运工,nginx-ingress-controller起到反向代理的作用
# 添加一条hosts解析
curl myapp.lixiang.com:30080
Hello MyApp | Version: v1 | <a href= "hostname.html" >Pod Name< /a >
|