minikube配置ingress,pod,service示例
一、安装ingress插件:
minikube addons enable ingress
二、创建deploy的pod:
kubectl -- create deployment -n ttshop php72-xdebug --image=harbor.xxxx.com/lhzl-base/php:7.2-auto-fpm-nginx-xdebug-redis.241205 --replicas=2
三、创建svc:
svc-php72-xdebug.yaml:
apiVersion: v1
kind: Service
metadata:
labels:
app: php72-xdebug
name: php72-xdebug
namespace: ttshop
spec:
selector:
app: php72-xdebug
ports:
- port: 80
protocol: TCP
targetPort: 80
ing-php72-xdebug.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-php72-xdebug
namespace: ttshop
spec:
ingressClassName: nginx
rules:
- host: "samtest.demo.com"
http:
paths:
- pathType: Prefix
path: "/" # 前缀是/后的所有请求会被转发至service
backend:
service:
name: php72-xdebug # 转发至的service
port:
number: 80 # service端口
- host: "samtest2.demo.com" # 多个规则
http:
paths:
- pathType: Prefix
path: "/"
backend:
service:
name: php72-xdebug
port:
number: 80
创建上述yaml配置的service和ingress:
kubectl apply -f svc-php72-xdebug.yaml
kubectl apply -f ing-php72-xdebugg.yaml
四、查看kube集群资源情况:
ubuntu@minikube:~/yaml$ kubectl get pod,svc,ing -Aowide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ingress-nginx pod/ingress-nginx-admission-create-xg6m8 0/1 Completed 0 131m 10.244.0.25 minikube <none> <none>
ingress-nginx pod/ingress-nginx-admission-patch-blz9g 0/1 Completed 1 131m 10.244.0.26 minikube <none> <none>
ingress-nginx pod/ingress-nginx-controller-bc57996ff-s69zw 1/1 Running 0 131m 10.244.0.27 minikube <none> <none>
kube-system pod/coredns-6f6b679f8f-ll4dh 1/1 Running 2 (18h ago) 10d 10.244.0.15 minikube <none> <none>
kube-system pod/etcd-minikube 1/1 Running 2 (18h ago) 10d 192.168.49.2 minikube <none> <none>
kube-system pod/kube-apiserver-minikube 1/1 Running 2 (18h ago) 10d 192.168.49.2 minikube <none> <none>
kube-system pod/kube-controller-manager-minikube 1/1 Running 2 (18h ago) 10d 192.168.49.2 minikube <none> <none>
kube-system pod/kube-proxy-7gl2m 1/1 Running 2 (18h ago) 10d 192.168.49.2 minikube <none> <none>
kube-system pod/kube-scheduler-minikube 1/1 Running 2 (18h ago) 10d 192.168.49.2 minikube <none> <none>
kube-system pod/storage-provisioner 1/1 Running 5 (4h39m ago) 10d 192.168.49.2 minikube <none> <none>
ttshop pod/php72-xdebug-7c88476c99-gbwp7 1/1 Running 1 (18h ago) 20h 10.244.0.17 minikube <none> <none>
ttshop pod/php72-xdebug-7c88476c99-ps5gl 1/1 Running 1 (18h ago) 20h 10.244.0.16 minikube <none> <none>
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
default service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10d <none>
ingress-nginx service/ingress-nginx-controller NodePort 10.103.164.111 <none> 80:30559/TCP,443:30830/TCP 131m app.kubernetes.io/component=controller,app.kubernetes.io/instance=ingress-nginx,app.kubernetes.io/name=ingress-nginx
ingress-nginx service/ingress-nginx-controller-admission ClusterIP 10.105.128.253 <none> 443/TCP 131m app.kubernetes.io/component=controller,app.kubernetes.io/instance=ingress-nginx,app.kubernetes.io/name=ingress-nginx
kube-system service/kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 10d k8s-app=kube-dns
ttshop service/php72-xdebug ClusterIP 10.104.157.99 <none> 80/TCP 5m9s app=php72-xdebug
NAMESPACE NAME CLASS HOSTS ADDRESS PORTS AGE
ttshop ingress.networking.k8s.io/ingress-php72-xdebug nginx samtest.demo.com,samtest2.demo.com 192.168.49.2 80 11m
五、测试ingress访问服务器:
curl -H 'Host:samtest.demo.com' http://192.168.49.2/
六、命令行安装各种svc的方法:
内部svc:kubectl -- expose deploy -n ttshop php72-xdebug --port=80 --target-port=80
ClusterIP类型SVC:kubectl -- expose deploy php72-xdebug --port=80 --target_port=80 --type=ClusterIP -n ttshop
NodePort类型SVC:kubectl -- expose deploy php72-xdebug --port=80 --target_port=80 --type=NodePort -n ttshop
七、在我的试验环境上设置对外访问:
由于我是macos上安装multipass的虚拟机中跑的minikube-docker,所以若对外提供访问需进行如下的路由和iptables设置;
我的网络环境如下:
我的macosIP:192.168.110.21
虚拟机IP:192.168.64.3
minikube-docker容器IP:192.168.49.2
设置如下:
1、虚拟机设置:
1.1、$route 查看虚拟机路由表如下:
$route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default bogon 0.0.0.0 UG 100 0 0 ens3
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.49.0 0.0.0.0 255.255.255.0 U 0 0 0 br-61679548a7ad
192.168.64.0 0.0.0.0 255.255.255.0 U 100 0 0 ens3
bogon 0.0.0.0 255.255.255.255 UH 100 0 0 ens3
1.2、虚拟机开启内核IP转发:
编辑/etc/sysctl.conf文件,将net.ipv4.ip_forward = 1这一行的注释去掉(如果没有这一行,可以手动添加),然后运行sudo sysctl -p使设置生效。
对于基于 Red Hat 或 CentOS 的系统,可以运行sudo sysctl -w net.ipv4.ip_forward = 1命令来开启 IP 转发。
1.3、设置虚拟iptables:
sudo iptables -A FORWARD -i ens3 -o br-61679548a7ad -j ACCEPT
sudo iptables -A FORWARD -i br-61679548a7ad -o ens3 -m state --state ESTABLISHED,RELATED -j ACCEPT
2、在macos宿主机上设置路由:
sudo route -n add 192.168.49.0/24 192.168.64.3