参考文献: Kubernetesマイクロサービス開発の実践 (早川博 著)
https://kun432.hatenablog.com/entry/understanding-nginx-ingress-on-kubernetes
https://kubernetes.github.io/ingress-nginx/deploy/#bare-metal-clusters
kubectl explain ingress
kubectl api-resources
-- 1. Deployment作成
cat <<-'EOF' > deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deploy01
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
EOF
kubectl apply -f deploy.yaml
kubectl get deploy,po
kubectl delete -f deploy.yaml
-- 2. Service作成(NodePort)
cat <<-'EOF' > svc01.yaml
apiVersion: v1
kind: Service
metadata:
name: svc01
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 8080
targetPort: 80
nodePort: 31001
type: NodePort
EOF
kubectl apply -f svc01.yaml
kubectl get po -o wide
kubectl get svc
kubectl delete -f svc01.yaml
pod ipによる接続確認
curl http://172.16.160.203:80
curl http://172.16.160.204:80
→ 「Welcome to nginx!」
cluster ipによる接続確認
→ 「Welcome to nginx!」
nodeportによる接続確認
curl http://192.168.137.162:31001
→ 「Welcome to nginx!」
-- 3. Ingress-Nginx Controllerインストール
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.2/deploy/static/provider/baremetal/deploy.yaml
kubectl delete -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.8.2/deploy/static/provider/baremetal/deploy.yaml
kubectl get deploy --all-namespaces
kubectl get deploy ingress-nginx-controller -o yaml -n ingress-nginx
kubectl get po -o wide -n ingress-nginx
ingress pod ipによる接続確認
kubectl get svc -n ingress-nginx
kubectl get svc ingress-nginx-controller -o yaml -n ingress-nginx
ingress cluster ipによる接続確認
curl http://10.109.57.31:80
→ 404 Not Found
ingress nodeportによる接続確認
curl http://192.168.137.162:31350
→ 404 Not Found
kubectl edit service ingress-nginx-controller -n ingress-nginx
name: http
nodePort: 31350
↓
name: http
nodePort: 32001
-- 4. Ingress作成
cat <<-'EOF' > ing01.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ing01
annotations:
kubernetes.io/ingress.class: nginx
spec:
ingressClassName: nginx
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: svc01
port:
number: 8080
EOF
kubectl apply -f ing01.yaml
kubectl get ing
kubectl describe ingress ing01
kubectl delete -f ing01.yaml
curl -H 'host: example.com' http://192.168.137.162:32001
→ 「Welcome to nginx!」
curl http://192.168.137.162:32001
→ 404 Not Found