iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 14
0
DevOps

Cloud Native 開發維運一條龍系列 第 14

Day 14. Kubernetes Network 基本操作

  • 分享至 

  • xImage
  •  

今天要來做的練習是關於 k8s 的網路操作

observe how service connect to deployment

$ kubectl expose deployment/nginx --port=80

observe traffic

(on master node)$ kubectl describe po $(kubectl get po | grep nginx | awk -F' ' '{print $1}') | grep Node:

open another terminal

(on worker node)$ sudo tcpdump -i tunl0
(on master node)$ curl $(kubectl get svc | grep nginx | awk -F' ' '{print $3}') 

Nodeport

$ kubectl delete svc nginx
$ kubectl expose deployment/nginx --port=80 --type=NodePort
$ kubectl get svc | grep nginx

(curl again)

Ingress

first delete all resources created

$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml
$ kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/service-nodeport.yaml

modify /etc/hosts (important)

$ echo '${WORKER1_IP} foo.bar.com' | sudo tee -a /etc/hosts 
$ echo '${WORKER2_IP} foo.bar.com' | sudo tee -a /etc/hosts 

cat.yml

kind: Pod
apiVersion: v1
metadata:
  name: cat-app
  labels:
    app: cat
spec:
  containers:
    - name: cat-app
      image: hashicorp/http-echo
      args:
        - "-text=cat"

---

kind: Service
apiVersion: v1
metadata:
  name: cat-service
spec:
  selector:
    app: cat
  ports:
    - port: 5678

dog.yml

kind: Pod
apiVersion: v1
metadata:
  name: dog-app
  labels:
    app: dog
spec:
  containers:
    - name: dog-app
      image: hashicorp/http-echo
      args:
        - "-text=dog"

---

kind: Service
apiVersion: v1
metadata:
  name: dog-service
spec:
  selector:
    app: dog
  ports:
    - port: 5678

ingress.yml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: default-ingress
  namespace: default
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
        - path: /cat
          backend:
            serviceName: cat-service
            servicePort: 5678
        - path: /dog
          backend:
            serviceName: dog-service
            servicePort: 5678

上一篇
Day 13. Kubernetes POD 基本操作
下一篇
Day 15. Kubernetes Volume 基本操作
系列文
Cloud Native 開發維運一條龍18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言