如何创建服务

1、创建Deployment

#启动三个pod,运行httpd镜像,label是run:mcw-httpd,Seveice将会根据这个label挑选Pod
apiVersion: apps/v1

  1. [machangwei@mcwk8s-master ~]$ cat mcwHttpd.yml
  2. kind: Deployment
  3. metadata:
  4. name: mcw-httpd
  5. spec:
  6. replicas: 3
  7. selector:
  8. matchLabels:
  9. run: mcw-httpd
  10. template:
  11. metadata:
  12. labels:
  13. run: mcw-httpd
  14. spec:
  15. containers:
  16. - name: mcw-httpd
  17. image: httpd
  18. ports:
  19. - containerPort: 80
  20. [machangwei@mcwk8s-master ~]$ kubectl apply -f mcwHttpd.yml
  21. deployment.apps/mcw-httpd created
  22. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
  23. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  24. mcw-httpd-6fbf67d7d5-5rrkh 0/1 ContainerCreating 0 2m52s <none> mcwk8s-node1 <none> <none>
  25. mcw-httpd-6fbf67d7d5-bqq58 0/1 ImagePullBackOff 0 2m52s 10.244.0.78 mcwk8s-node2 <none> <none>
  26. mcw-httpd-6fbf67d7d5-j52ff 0/1 ImagePullBackOff 0 2m52s 10.244.0.70 mcwk8s-node1 <none> <none>
  27. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide #过一会后,查看Pod分配了各自的IP,容器在创建的时候ip是none
  28. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  29. mcw-httpd-6fbf67d7d5-5rrkh 1/1 Running 0 6m28s 10.244.0.71 mcwk8s-node1 <none> <none>
  30. mcw-httpd-6fbf67d7d5-bqq58 1/1 Running 0 6m28s 10.244.0.78 mcwk8s-node2 <none> <none>
  31. mcw-httpd-6fbf67d7d5-j52ff 1/1 Running 0 6m28s 10.244.0.70 mcwk8s-node1 <none> <none>
  32. [machangwei@mcwk8s-master ~]$ #这些ip只能被kubernates Cluster中的容器和节点访问

2、不通的情况,是不是就应该不通呢,答案是否。

  1. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. mcw-httpd-6fbf67d7d5-5rrkh 1/1 Running 0 16m 10.244.0.71 mcwk8s-node1 <none> <none>
  4. mcw-httpd-6fbf67d7d5-bqq58 1/1 Running 0 16m 10.244.0.78 mcwk8s-node2 <none> <none>
  5. mcw-httpd-6fbf67d7d5-j52ff 1/1 Running 0 16m 10.244.0.70 mcwk8s-node1 <none> <none>
  6. [machangwei@mcwk8s-master ~]$ curl 10.244.0.78 #主节点访问节点2上的pod的ip,连接超时
  7. curl: (7) Failed connect to 10.244.0.78:80; Connection timed out
  8.  
  9. 节点2上访问节点2上的pod ip是能访问的
  10. [root@mcwk8s-node2 ~]$ curl 10.244.0.78
  11. <html><body><h1>It works!</h1></body></html>
  12.  
  13. 怀疑是节点上flannel状态问题
  14. [machangwei@mcwk8s-master ~]$ kubectl get pod --all-namespaces|grep flannel
  15. kube-system kube-flannel-ds-cn4m9 0/1 Error 233 (5m26s ago) 2d11h
  16. kube-system kube-flannel-ds-hpgkz 1/1 Running 0 6d23h
  17. kube-system kube-flannel-ds-nnjvj 0/1 CrashLoopBackOff 271 (15s ago) 6d23h
  18.  
  19. 怀疑节点2上没添加-H的问题,然后添加重启docker daemon ,四个容器都重启了
  20. [root@mcwk8s-node2 ~]$ vim /usr/lib/systemd/system/docker.service
  21. [root@mcwk8s-node2 ~]$ grep -i execstart /usr/lib/systemd/system/docker.service
  22. ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0 --containerd=/run/containerd/containerd.sock
  23. [root@mcwk8s-node2 ~]$ systemctl daemon-reload
  24. [root@mcwk8s-node2 ~]$ systemctl restart docker
  25. [root@mcwk8s-node2 ~]$ docker ps
  26. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  27. 88de5020b420 registry.aliyuncs.com/google_containers/pause:3.6 "/pause" 1 second ago Up 1 second k8s_POD_kube-flannel-ds-cn4m9_kube-system_ef070440-6778-430f-92b9-a1c48b755d2b_1
  28. adf80a28c0be b46c42588d51 "/usr/local/bin/kube…" 2 seconds ago Up 1 second k8s_kube-proxy_kube-proxy-92g5c_kube-system_a69acf11-f51a-46d6-9472-d54b5383efef_1
  29. 46bceff879bd registry.aliyuncs.com/google_containers/pause:3.6 "/pause" 6 seconds ago Up 2 seconds k8s_POD_kube-proxy-92g5c_kube-system_a69acf11-f51a-46d6-9472-d54b5383efef_1
  30. a5cdf7f6ef3b registry.aliyuncs.com/google_containers/pause:3.6 "/pause" 6 seconds ago Up 2 seconds k8s_POD_mcw-httpd-6fbf67d7d5-bqq58_default_4ceffe1e-df14-47dd-82f0-83cb68555de7_1
  31.  
  32. 再次在主节点访问节点2上的pod ip ,还是无法访问,由于节点2pod重启了,所以ip被重新分配了一个
  33. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
  34. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  35. mcw-httpd-6fbf67d7d5-5rrkh 1/1 Running 0 30m 10.244.0.71 mcwk8s-node1 <none> <none>
  36. mcw-httpd-6fbf67d7d5-bqq58 1/1 Running 1 (95s ago) 30m 10.244.0.79 mcwk8s-node2 <none> <none>
  37. mcw-httpd-6fbf67d7d5-j52ff 1/1 Running 0 30m 10.244.0.70 mcwk8s-node1 <none> <none>
  38. [machangwei@mcwk8s-master ~]$
  39. [machangwei@mcwk8s-master ~]$
  40. [machangwei@mcwk8s-master ~]$ curl 10.244.0.79 #虽然重新分配了ip,但是还是无法访问
  41. curl: (7) Failed connect to 10.244.0.79:80; Connection timed out

3、创建Service

  1. [machangwei@mcwk8s-master ~]$ cat mcwHttpdService.yml
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: httpd-svc
  6. spec:
  7. selector:
  8. run: httpd
  9. ports:
  10. - protocol: TCP
  11. port: 8080
  12. targetPort: 80
  13. [machangwei@mcwk8s-master ~]$ kubectl apply -f mcwHttpdService.yml
  14. service/httpd-svc created
  15. [machangwei@mcwk8s-master ~]$ kubectl get service
  16. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  17. httpd-svc ClusterIP 10.99.77.45 <none> 8080/TCP 14s
  18. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d
  19. [machangwei@mcwk8s-master ~]$ curl 10.99.77.45:8080
  20. curl: (7) Failed connect to 10.99.77.45:8080; Connection refused

dns访问Service

  1. [machangwei@mcwk8s-master ~]$ kubectl get deployment --namespace=kube-system
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. coredns 2/2 2 2 7d1h
  4. [machangwei@mcwk8s-master ~]$ kubectl get service -o wide
  5. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
  6. httpd-svc ClusterIP 10.99.77.45 <none> 8080/TCP 61m run=httpd
  7. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 7d1h <none>
  8. [machangwei@mcwk8s-master ~]$ kubectl run mcwbusybox --rm -ti --image=busybox /bin/bash
  9. pod "mcwbusybox" deleted
  10. error: timed out waiting for the condition
  11. [machangwei@mcwk8s-master ~]$ kubectl get pod
  12. NAME READY STATUS RESTARTS AGE
  13. mcw-httpd-6fbf67d7d5-5rrkh 1/1 Running 0 124m
  14. mcw-httpd-6fbf67d7d5-bqq58 1/1 Running 1 (95m ago) 124m
  15. mcw-httpd-6fbf67d7d5-j52ff 1/1 Running 0 124m
  16.  
  17. 只可以访问自己节点上的pod,不能跨主机同命名空间内的pod互相通信。不知道哪里的问题
  18. [machangwei@mcwk8s-master ~]$ kubectl run mcwcentos3 -it --image=centos /bin/bash
  19. If you don't see a command prompt, try pressing enter.
  20. [root@mcwcentos3 /]# curl 10.244.0.79
  21. curl: (7) Failed to connect to 10.244.0.79 port 80: Connection timed out
  22. [root@mcwcentos3 /]# curl 10.244.0.70
  23. <html><body><h1>It works!</h1></body></html>
  24. [root@mcwcentos3 /]# curl 10.244.0.71
  25. <html><body><h1>It works!</h1></body></html>
  26.  
  27. route add -host 10.244.0.0 dev flannel.1
  28. flannel.1
  29.  
  30. NMI watchdog: BUG: soft lockup - CPU#0 stuck for 22s
  31. 报错现象 系统或者网络占用过多CPU,造成内核软死锁(soft lockup)。Soft lockup名称解释:所谓,soft lockup就是说,这个bug没有让系统彻底死机,但是若干个进程(或...

fannel网络问题

网络是有问题的

  1. 两个节点上的fannel状态不对
  2. [machangwei@mcwk8s-master ~]$ kubectl get pod --all-namespaces -o wide|grep flannel
  3. kube-system kube-flannel-ds-cn4m9 0/1 CrashLoopBackOff 286 (3m50s ago) 3d10h 10.0.0.6 mcwk8s-node2 <none> <none>
  4. kube-system kube-flannel-ds-hpgkz 1/1 Running 1 (22h ago) 7d22h 10.0.0.4 mcwk8s-master <none> <none>
  5. kube-system kube-flannel-ds-nnjvj 0/1 CrashLoopBackOff 325 (3m21s ago) 7d22h 10.0.0.5 mcwk8s-node1 <none> <none>
  6. [machangwei@mcwk8s-master ~]
  7.  
  8. 节点上查看对应容器的日志错误信,未成功注册cidr
  9. [root@mcwk8s-node2 ~]$ docker ps -a|grep flannel
  10. 2252229253e3 e6ea68648f0c "/opt/bin/flanneld -…" 10 seconds ago Exited (1) 2 seconds ago k8s_kube-flannel_kube-flannel-ds-cn4m9_kube-system_ef070440-6778-430f-92b9-a1c48b755d2b_284
  11. [root@mcwk8s-node2 ~]$ docker logs 225
  12. E0120 13:40:49.244765 1 main.go:325] Error registering network: failed to acquire lease: node "mcwk8s-node2" pod cidr not assigned
  13. W0120 13:40:49.245111 1 reflector.go:424] github.com/flannel-io/flannel/subnet/kube/kube.go:379: watch of *v1.Node ended with: an error on the server ("unable to decode an event from the watch stream: context canceled") has prevented the request from succeeding
  14.  
  15. master初始化写进来的cidr
  16. [root@mcwk8s-master ~]$ grep cidr /etc/kubernetes/manifests/kube-controller-manager.yaml
  17. - --allocate-node-cidrs=true
  18. - --cluster-cidr=10.244.0.0/24
  19.  
  20. 部署fannel用的网络
  21. [machangwei@mcwk8s-master ~]$ grep -C 3 '"Network"' mm.yml
  22. }
  23. net-conf.json: |
  24. {
  25. "Network": "10.244.0.0/16",
  26. "Backend": {
  27. "Type": "vxlan"
  28. }
  29.  
  30. 修改网络
  31. [machangwei@mcwk8s-master ~]$ vim mm.yml
  32. [machangwei@mcwk8s-master ~]$ grep -C 3 '"Network"' mm.yml
  33. }
  34. net-conf.json: |
  35. {
  36. "Network": "10.244.0.0/24",
  37. "Backend": {
  38. "Type": "vxlan"
  39. }
  40. [machangwei@mcwk8s-master ~]

从头部署了k8s

  1. 修改网络后还是不行。直接从头部署k8s就好了,把flannel网络和初始化master都设置为10.244.0.0/16 网络
  2.  
  3. [machangwei@mcwk8s-master ~]$ kubectl get pod --all-namespaces
  4. NAMESPACE NAME READY STATUS RESTARTS AGE
  5. kube-system coredns-6d8c4cb4d-vctxx 1/1 Running 1 (5m57s ago) 33m
  6. kube-system coredns-6d8c4cb4d-xkv9h 1/1 Running 0 33m
  7. kube-system etcd-mcwk8s-master 1/1 Running 0 33m
  8. kube-system kube-apiserver-mcwk8s-master 1/1 Running 1 (4m42s ago) 33m
  9. kube-system kube-controller-manager-mcwk8s-master 1/1 Running 1 33m
  10. kube-system kube-flannel-ds-fvwgm 1/1 Running 0 22m
  11. kube-system kube-flannel-ds-l5fdg 1/1 Running 0 25m
  12. kube-system kube-flannel-ds-mzdcw 1/1 Running 0 21m
  13. kube-system kube-proxy-796l7 1/1 Running 0 21m
  14. kube-system kube-proxy-8wtxn 1/1 Running 0 22m
  15. kube-system kube-proxy-qr6b8 1/1 Running 0 33m
  16. kube-system kube-scheduler-mcwk8s-master 1/1 Running 1 33m
  17. [machangwei@mcwk8s-master ~]$
  18.  
  19. [machangwei@mcwk8s-master ~]$ kubectl get nodes
  20. NAME STATUS ROLES AGE VERSION
  21. mcwk8s-master Ready control-plane,master 35m v1.23.1
  22. mcwk8s-node1 Ready <none> 23m v1.23.1
  23. mcwk8s-node2 Ready <none> 22m v1.23.1

网络正常的三个节点,网卡和路由如下

  1. 跟之前相比,很明显的是多了路由了。之前应该是节点网络没好,所以路由也不全
  2.  
  3. 主节点
  4. [root@mcwk8s-master ~]$ ip a
  5. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
  6. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  7. inet 127.0.0.1/8 scope host lo
  8. valid_lft forever preferred_lft forever
  9. inet6 ::1/128 scope host
  10. valid_lft forever preferred_lft forever
  11. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  12. link/ether 00:0c:29:a2:3a:b7 brd ff:ff:ff:ff:ff:ff
  13. inet 10.0.0.4/24 brd 10.0.0.255 scope global ens33
  14. valid_lft forever preferred_lft forever
  15. inet6 fe80::6b7a:2214:bef3:5850/64 scope link
  16. valid_lft forever preferred_lft forever
  17. 3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
  18. link/ether 00:0c:29:a2:3a:c1 brd ff:ff:ff:ff:ff:ff
  19. 4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
  20. link/ether 02:42:af:00:ac:08 brd ff:ff:ff:ff:ff:ff
  21. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  22. valid_lft forever preferred_lft forever
  23. 5: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN
  24. link/ether ca:9d:fb:2c:b7:22 brd ff:ff:ff:ff:ff:ff
  25. inet 10.244.0.0/32 brd 10.244.0.0 scope global flannel.1
  26. valid_lft forever preferred_lft forever
  27. inet6 fe80::c89d:fbff:fe2c:b722/64 scope link
  28. valid_lft forever preferred_lft forever
  29. 6: cni0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP qlen 1000
  30. link/ether 3e:f7:13:d6:6b:5b brd ff:ff:ff:ff:ff:ff
  31. inet 10.244.0.1/24 brd 10.244.0.255 scope global cni0
  32. valid_lft forever preferred_lft forever
  33. inet6 fe80::3cf7:13ff:fed6:6b5b/64 scope link
  34. valid_lft forever preferred_lft forever
  35. 7: vethf9bacd46@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP
  36. link/ether f6:9d:2c:f7:ff:4e brd ff:ff:ff:ff:ff:ff link-netnsid 0
  37. inet6 fe80::f49d:2cff:fef7:ff4e/64 scope link
  38. valid_lft forever preferred_lft forever
  39. 8: veth59372586@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP
  40. link/ether 56:da:7b:cb:75:49 brd ff:ff:ff:ff:ff:ff link-netnsid 1
  41. inet6 fe80::54da:7bff:fecb:7549/64 scope link
  42. valid_lft forever preferred_lft forever
  43. [root@mcwk8s-master ~]$
  44.  
  45. node1
  46. [root@mcwk8s-node1 ~]$ route -n
  47. Kernel IP routing table
  48. Destination Gateway Genmask Flags Metric Ref Use Iface
  49. 0.0.0.0 10.0.0.253 0.0.0.0 UG 100 0 0 ens33
  50. 10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
  51. 10.244.0.0 10.244.0.0 255.255.255.0 UG 0 0 0 flannel.1
  52. 10.244.1.0 0.0.0.0 255.255.255.0 U 0 0 0 cni0
  53. 10.244.2.0 10.244.2.0 255.255.255.0 UG 0 0 0 flannel.1
  54. 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
  55. [root@mcwk8s-node1 ~]$ ip a
  56. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
  57. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  58. inet 127.0.0.1/8 scope host lo
  59. valid_lft forever preferred_lft forever
  60. inet6 ::1/128 scope host
  61. valid_lft forever preferred_lft forever
  62. 2: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
  63. link/ether 00:0c:29:0b:a3:15 brd ff:ff:ff:ff:ff:ff
  64. 3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  65. link/ether 00:0c:29:0b:a3:0b brd ff:ff:ff:ff:ff:ff
  66. inet 10.0.0.5/24 brd 10.0.0.255 scope global ens33
  67. valid_lft forever preferred_lft forever
  68. inet6 fe80::3516:c22b:d62:c43f/64 scope link
  69. valid_lft forever preferred_lft forever
  70. 4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
  71. link/ether 02:42:5e:08:56:63 brd ff:ff:ff:ff:ff:ff
  72. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  73. valid_lft forever preferred_lft forever
  74. 5: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN
  75. link/ether 4e:92:a2:b0:6b:5a brd ff:ff:ff:ff:ff:ff
  76. inet 10.244.1.0/32 brd 10.244.1.0 scope global flannel.1
  77. valid_lft forever preferred_lft forever
  78. inet6 fe80::4c92:a2ff:feb0:6b5a/64 scope link
  79. valid_lft forever preferred_lft forever
  80. 6: cni0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP qlen 1000
  81. link/ether e2:d3:b2:00:28:bf brd ff:ff:ff:ff:ff:ff
  82. inet 10.244.1.1/24 brd 10.244.1.255 scope global cni0
  83. valid_lft forever preferred_lft forever
  84. inet6 fe80::e0d3:b2ff:fe00:28bf/64 scope link
  85. valid_lft forever preferred_lft forever
  86. 7: vethafc53bc5@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP
  87. link/ether 32:51:41:e9:e1:68 brd ff:ff:ff:ff:ff:ff link-netnsid 0
  88. inet6 fe80::3051:41ff:fee9:e168/64 scope link
  89. valid_lft forever preferred_lft forever
  90. 8: veth8246ed1c@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP
  91. link/ether 12:67:28:44:fa:cd brd ff:ff:ff:ff:ff:ff link-netnsid 1
  92. inet6 fe80::1067:28ff:fe44:facd/64 scope link
  93. valid_lft forever preferred_lft forever
  94. [root@mcwk8s-node1 ~]$
  95.  
  96. node2
  97. [root@mcwk8s-node2 ~]$ route -n
  98. Kernel IP routing table
  99. Destination Gateway Genmask Flags Metric Ref Use Iface
  100. 0.0.0.0 10.0.0.253 0.0.0.0 UG 100 0 0 ens33
  101. 10.0.0.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
  102. 10.244.0.0 10.244.0.0 255.255.255.0 UG 0 0 0 flannel.1
  103. 10.244.1.0 10.244.1.0 255.255.255.0 UG 0 0 0 flannel.1
  104. 10.244.2.0 0.0.0.0 255.255.255.0 U 0 0 0 cni0
  105. 172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
  106. You have new mail in /var/spool/mail/root
  107. [root@mcwk8s-node2 ~]$ ip a
  108. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
  109. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  110. inet 127.0.0.1/8 scope host lo
  111. valid_lft forever preferred_lft forever
  112. inet6 ::1/128 scope host
  113. valid_lft forever preferred_lft forever
  114. 2: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
  115. link/ether 00:0c:29:eb:83:cd brd ff:ff:ff:ff:ff:ff
  116. 3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  117. link/ether 00:0c:29:eb:83:c3 brd ff:ff:ff:ff:ff:ff
  118. inet 10.0.0.6/24 brd 10.0.0.255 scope global ens33
  119. valid_lft forever preferred_lft forever
  120. inet6 fe80::fd02:359f:93a4:95af/64 scope link
  121. valid_lft forever preferred_lft forever
  122. 4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN
  123. link/ether 02:42:ff:3f:37:b0 brd ff:ff:ff:ff:ff:ff
  124. inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
  125. valid_lft forever preferred_lft forever
  126. 5: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN
  127. link/ether 56:16:44:85:ca:57 brd ff:ff:ff:ff:ff:ff
  128. inet 10.244.2.0/32 brd 10.244.2.0 scope global flannel.1
  129. valid_lft forever preferred_lft forever
  130. inet6 fe80::5416:44ff:fe85:ca57/64 scope link
  131. valid_lft forever preferred_lft forever
  132. 6: cni0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP qlen 1000
  133. link/ether 06:1f:10:ee:32:6f brd ff:ff:ff:ff:ff:ff
  134. inet 10.244.2.1/24 brd 10.244.2.255 scope global cni0
  135. valid_lft forever preferred_lft forever
  136. inet6 fe80::41f:10ff:feee:326f/64 scope link
  137. valid_lft forever preferred_lft forever
  138. 7: vethdf3032e1@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP
  139. link/ether 1e:50:bf:95:46:4d brd ff:ff:ff:ff:ff:ff link-netnsid 0
  140. inet6 fe80::1c50:bfff:fe95:464d/64 scope link
  141. valid_lft forever preferred_lft forever
  142. [root@mcwk8s-node2 ~]$

flannel正常之后,验证集群内pod跨主机访问

  1. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
  2. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  3. mcw-httpd-6fbf67d7d5-5qfrl 0/1 ContainerCreating 0 5m19s <none> mcwk8s-node1 <none> <none>
  4. mcw-httpd-6fbf67d7d5-98x8d 0/1 ContainerCreating 0 5m19s <none> mcwk8s-node2 <none> <none>
  5. mcw-httpd-6fbf67d7d5-bpbq4 0/1 ContainerCreating 0 5m19s <none> mcwk8s-node1 <none> <none>
  6. [machangwei@mcwk8s-master ~]$ kubectl describe pod mcw-httpd-6fbf67d7d5-5qfrl
  7. Events: #容器创建中的状态时间段,包括拉取镜像的过程
  8. Type Reason Age From Message
  9. ---- ------ ---- ---- -------
  10. Normal Scheduled 4m49s default-scheduler Successfully assigned default/mcw-httpd-6fbf67d7d5-5qfrl to mcwk8s-node1
  11. Normal Pulling 4m35s kubelet Pulling image "httpd"
  12.  
  13. mcwk8s-node1一直卡在拉取不下镜像,然后手动到节点1docker pull,拉取完镜像之后,pod马上就运行状态了
  14. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
  15. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  16. mcw-httpd-6fbf67d7d5-5qfrl 1/1 Running 0 21m 10.244.1.2 mcwk8s-node1 <none> <none>
  17. mcw-httpd-6fbf67d7d5-98x8d 1/1 Running 0 21m 10.244.2.2 mcwk8s-node2 <none> <none>
  18. mcw-httpd-6fbf67d7d5-bpbq4 1/1 Running 0 21m 10.244.1.3 mcwk8s-node1 <none> <none>
  19. [machangwei@mcwk8s-master ~]$
  20.  
  21. 查看网络,现在分配的pod上分配的ip,可以正常的在集群其它节点上进行访问了。也就是Pod分配了各自的ip,这些ip只能被Kuernetes Cluster 中的容器和节点访问。前面那句话已经得到验证
  22. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
  23. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  24. mcw-httpd-6fbf67d7d5-5qfrl 1/1 Running 0 21m 10.244.1.2 mcwk8s-node1 <none> <none>
  25. mcw-httpd-6fbf67d7d5-98x8d 1/1 Running 0 21m 10.244.2.2 mcwk8s-node2 <none> <none>
  26. mcw-httpd-6fbf67d7d5-bpbq4 1/1 Running 0 21m 10.244.1.3 mcwk8s-node1 <none> <none>
  27. [machangwei@mcwk8s-master ~]$ curl 10.244.1.2
  28. <html><body><h1>It works!</h1></body></html>
  29. [machangwei@mcwk8s-master ~]$ curl 10.244.1.3
  30. <html><body><h1>It works!</h1></body></html>
  31. [machangwei@mcwk8s-master ~]$ hostname
  32. mcwk8s-master
  33. [machangwei@mcwk8s-master ~]$

部署service

部署service,但是没有后端服务

  1. endpoint是空的,肯定不能通过服务ip加端口,去curl访问服务的响应数据
  2. [machangwei@mcwk8s-master ~]$ cat mcwHttpdService.yml
  3. apiVersion: v1
  4. kind: Service
  5. metadata:
  6. name: httpd-svc
  7. spec:
  8. selector:
  9. run: httpd
  10. ports:
  11. - protocol: TCP
  12. port: 8080
  13. targetPort: 80
  14. [machangwei@mcwk8s-master ~]$ kubectl apply -f mcwHttpdService.yml #创建Service
  15. service/httpd-svc created
  16. [machangwei@mcwk8s-master ~]$ kubectl get service #查看service。有服务名,集群ip,以及端口,
  17. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  18. httpd-svc ClusterIP 10.102.232.38 <none> 8080/TCP 11s
  19. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 140m
  20. [machangwei@mcwk8s-master ~]$ curl 10.102.232.38:8080 #通过curl集群ip冒号端口,无法访问,因为没有endpoint
  21. curl: (7) Failed connect to 10.102.232.38:8080; Connection refused
  22. [machangwei@mcwk8s-master ~]$ curl 10.244.1.2 #通过访问pod ip ,可以直接在节点上访问到集群中的pod的对应的80服务
  23. <html><body><h1>It works!</h1></body></html>
  24. [machangwei@mcwk8s-master ~]$
  25. [machangwei@mcwk8s-master ~]$ ping 10.244.1.2 #可以在节点上ping通集群中pod的ip。
  26. PING 10.244.1.2 (10.244.1.2) 56(84) bytes of data.
  27. 64 bytes from 10.244.1.2: icmp_seq=1 ttl=63 time=1.04 ms
  28. [machangwei@mcwk8s-master ~]$
  29. [machangwei@mcwk8s-master ~]$ kubectl get service
  30. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  31. httpd-svc ClusterIP 10.102.232.38 <none> 8080/TCP 75s
  32. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 141m
  33. [machangwei@mcwk8s-master ~]$ curl 10.102.232.38:8080
  34. curl: (7) Failed connect to 10.102.232.38:8080; Connection refused
  35. [machangwei@mcwk8s-master ~]$ kubectl describe service httpd-svc #查看服务,可以看到endponts为none
  36. Name: httpd-svc
  37. Namespace: default
  38. Labels: <none>
  39. Annotations: <none>
  40. Selector: run=httpd
  41. Type: ClusterIP
  42. IP Family Policy: SingleStack
  43. IP Families: IPv4
  44. IP: 10.102.232.38
  45. IPs: 10.102.232.38
  46. Port: <unset> 8080/TCP
  47. TargetPort: 80/TCP
  48. Endpoints: <none>
  49. Session Affinity: None
  50. Events: <none>
  51. [machangwei@mcwk8s-master ~]$

正常的有后端pod

  1. [machangwei@mcwk8s-master ~]$ cat mcwHttpdService.yml #修改前的yml文件,
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. name: httpd-svc
  6. spec:
  7. selector:
  8. run: httpd
  9. ports:
  10. - protocol: TCP
  11. port: 8080
  12. targetPort: 80
  13. [machangwei@mcwk8s-master ~]$ kubectl get pod --show-labels #查看服务对应的后端pod的标签,发现是run=mcw-httpd,
  14. NAME READY STATUS RESTARTS AGE LABELS
  15. mcw-httpd-6fbf67d7d5-5qfrl 1/1 Running 0 107m pod-template-hash=6fbf67d7d5,run=mcw-httpd
  16. mcw-httpd-6fbf67d7d5-98x8d 1/1 Running 0 107m pod-template-hash=6fbf67d7d5,run=mcw-httpd
  17. mcw-httpd-6fbf67d7d5-bpbq4 1/1 Running 0 107m pod-template-hash=6fbf67d7d5,run=mcw-httpd
  18. [machangwei@mcwk8s-master ~]$ kubectl delete -f mcwHttpdService.yml
  19. service "httpd-svc" deleted
  20. [machangwei@mcwk8s-master ~]$ vim mcwHttpdService.yml
  21. [machangwei@mcwk8s-master ~]$ cat mcwHttpdService.yml #修改选择器,纠正标签,指定service后端pod的标签是run: mcw-httpd
  22. apiVersion: v1
  23. kind: Service
  24. metadata:
  25. name: httpd-svc
  26. spec:
  27. selector:
  28. run: mcw-httpd
  29. ports:
  30. - protocol: TCP
  31. port: 8080
  32. targetPort: 80
  33. [machangwei@mcwk8s-master ~]$ kubectl apply -f mcwHttpdService.yml #重新部署服务
  34. service/httpd-svc created
  35. [machangwei@mcwk8s-master ~]$ kubectl get service #查看服务,集群已经换掉了
  36. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  37. httpd-svc ClusterIP 10.99.19.228 <none> 8080/TCP 7s
  38. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 146m
  39. [machangwei@mcwk8s-master ~]$ curl 10.99.19.228:8080 #这下可以正常curl 集群IP:端口,来访问服务了。
  40. <html><body><h1>It works!</h1></body></html>
  41. [machangwei@mcwk8s-master ~]$ kubectl describe service httpd-svc
  42. Name: httpd-svc
  43. Namespace: default
  44. Labels: <none>
  45. Annotations: <none>
  46. Selector: run=mcw-httpd
  47. Type: ClusterIP
  48. IP Family Policy: SingleStack
  49. IP Families: IPv4
  50. IP: 10.99.19.228
  51. IPs: 10.99.19.228
  52. Port: <unset> 8080/TCP #查看到现在的服务,后endpoint的值
  53. TargetPort: 80/TCP # endpoint的值,是代表有可以访问带有指定标签pod ip,指定pod目标端口的服务。
  54. Endpoints: 10.244.1.2:80,10.244.1.3:80,10.244.2.2:80
  55. Session Affinity: None #也就是访问集群ip:端口,应该是负载均衡路由到这带有服务选择对应标签的pod ip和目标端口的
  56. Events: <none> #集群ip是创建服务时分配的,而选择带有什么样标签的pod,以及pod对应服务的端口,即目标端口,是在yml里面已经设置好了
  57. [machangwei@mcwk8s-master ~]$

service文件介绍

  1. [machangwei@mcwk8s-master ~]$ cat mcwHttpdService.yml
  2. apiVersion: v1 #Service的apiVersion
  3. kind: Service #资源类型
  4. metadata:
  5. name: httpd-svc
  6. spec:
  7. selector: #指明挑选那些label为run: httpd的pod作为Service的后端
  8. run: httpd
  9. ports:
  10. - protocol: TCP
  11. port: 8080 #将Servicede 8080端口映射到Pod的80端口,使用TCP协议
  12. targetPort: 80

Cluster IP底层实现

  1. service相关的防火墙规则,集群ippod ip相关
  2. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
  3. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  4. mcw-httpd-6fbf67d7d5-5qfrl 1/1 Running 0 20h 10.244.1.2 mcwk8s-node1 <none> <none>
  5. mcw-httpd-6fbf67d7d5-98x8d 1/1 Running 0 20h 10.244.2.2 mcwk8s-node2 <none> <none>
  6. mcw-httpd-6fbf67d7d5-bpbq4 1/1 Running 0 20h 10.244.1.3 mcwk8s-node1 <none> <none>
  7. [machangwei@mcwk8s-master ~]$ kubectl get service
  8. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  9. httpd-svc ClusterIP 10.99.19.228 <none> 8080/TCP 18h
  10. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 20h
  11. [machangwei@mcwk8s-master ~]$
  12.  
  13. 查看当前主机防火墙
  14. [root@mcwk8s-master ~]$ iptables-save |grep httpd-svc
  15. -A KUBE-SEP-26GESA23ILIBJ6BG -s 10.244.1.2/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
  16. -A KUBE-SEP-26GESA23ILIBJ6BG -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 10.244.1.2:80
  17. -A KUBE-SEP-5MDWNIS6FGKOLKLF -s 10.244.1.3/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
  18. -A KUBE-SEP-5MDWNIS6FGKOLKLF -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 10.244.1.3:80
  19. -A KUBE-SEP-MZ7D7IEY543CBPN3 -s 10.244.2.2/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
  20. -A KUBE-SEP-MZ7D7IEY543CBPN3 -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 10.244.2.2:80
  21. -A KUBE-SERVICES -d 10.99.19.228/32 -p tcp -m comment --comment "default/httpd-svc cluster IP" -m tcp --dport 8080 -j KUBE-SVC-IYRDZZKXS5EOQ6Q6
  22. -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 ! -s 10.244.0.0/16 -d 10.99.19.228/32 -p tcp -m comment --comment "default/httpd-svc cluster IP" -m tcp --dport 8080 -j KUBE-MARK-MASQ
  23. -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -m statistic --mode random --probability 0.33333333349 -j KUBE-SEP-26GESA23ILIBJ6BG
  24. -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-5MDWNIS6FGKOLKLF
  25. -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -j KUBE-SEP-MZ7D7IEY543CBPN3
  26. [root@mcwk8s-master ~]$
  27.  
  28. 一:
  29. -A KUBE-SERVICES -d 10.99.19.228/32 -p tcp -m comment --comment "default/httpd-svc cluster IP" -m tcp --dport 8080 -j KUBE-SVC-IYRDZZKXS5EOQ6Q6
  30. -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 ! -s 10.244.0.0/16 -d 10.99.19.228/32 -p tcp -m comment --comment "default/httpd-svc cluster IP" -m tcp --dport 8080 -j KUBE-MARK-MASQ
  31. 1、其它源地址访问httpd-svc,则允许
  32. 2、如果Cluster内的pod(源地址来自10.244.0.0/16)要访问httpd-svc,则跳转到KUBE-SVC-IYRDZZKXS5EOQ6Q6
  33.  
  34. KUBE-SVC-IYRDZZKXS5EOQ6Q6规则之一如下:
  35. -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -m statistic --mode random --probability 0.33333333349 -j KUBE-SEP-26GESA23ILIBJ6BG
  36.  
  37. 二:
  38. -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -m statistic --mode random --probability 0.33333333349 -j KUBE-SEP-26GESA23ILIBJ6BG
  39. -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -m statistic --mode random --probability 0.50000000000 -j KUBE-SEP-5MDWNIS6FGKOLKLF
  40. -A KUBE-SVC-IYRDZZKXS5EOQ6Q6 -m comment --comment "default/httpd-svc" -j KUBE-SEP-MZ7D7IEY543CBPN3
  41.  
  42. 11/3概率跳转到规则KUBE-SEP-26GESA23ILIBJ6BG
  43. 21/3概率(剩下2/3的一半)跳转到规则KUBE-SEP-5MDWNIS6FGKOLKLF
  44. 31/3概率跳转到规则KUBE-SEP-MZ7D7IEY543CBPN3
  45.  
  46. 三:
  47. -A KUBE-SEP-26GESA23ILIBJ6BG -s 10.244.1.2/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
  48. -A KUBE-SEP-26GESA23ILIBJ6BG -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 10.244.1.2:80
  49. -A KUBE-SEP-5MDWNIS6FGKOLKLF -s 10.244.1.3/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
  50. -A KUBE-SEP-5MDWNIS6FGKOLKLF -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 10.244.1.3:80
  51. -A KUBE-SEP-MZ7D7IEY543CBPN3 -s 10.244.2.2/32 -m comment --comment "default/httpd-svc" -j KUBE-MARK-MASQ
  52. -A KUBE-SEP-MZ7D7IEY543CBPN3 -p tcp -m comment --comment "default/httpd-svc" -m tcp -j DNAT --to-destination 10.244.2.2:80
  53.  
  54. 将请求分别转发到后端的三个pod.iptables将访问Service的流量转发到后端pod,而且使用类型轮询的负载均衡策略。集群的每一个结点都配置了相同的iptables规则,这样就确保了整个集群都能通过Service的集群ip访问服务。

DNS访问Service

访问本身namespace中的

  1. [machangwei@mcwk8s-master ~]$ kubectl get deployment --namespace=kube-system #查看 dns组件
  2. NAME READY UP-TO-DATE AVAILABLE AGE
  3. coredns 2/2 2 2 21h
  4. [machangwei@mcwk8s-master ~]$
  5. [machangwei@mcwk8s-master ~]$ kubectl run mcwcentos --rm -ti --image=centos /bin/bash #创建pod并进入
  6. If you don't see a command prompt, try pressing enter.
  7. [root@mcwcentos /]# wget httpd-svc.default:8080 #在pod中通过<SERVICE_NAME>.<NAMESPACE_NAME>来访问Service
  8. bash: wget: command not found
  9. [root@mcwcentos /]# curl httpd-svc.default:8080
  10. <html><body><h1>It works!</h1></body></html>
  11. [root@mcwcentos /]#
  12. [root@mcwcentos /]# curl httpd-svc:8080 #因为pod和httpd-svc同属于default namespace,因此可以省略default命名空间,直接访问服务
  13. <html><body><h1>It works!</h1></body></html>
  14. [root@mcwcentos /]# yum -y install bind-utils #安装工具,以方便使用nslookup命令
  15. Failed to set locale, defaulting to C.UTF-8
  16. Last metadata expiration check: 0:02:36 ago on Fri Jan 21 12:45:19 2022.
  17. Dependencies resolved.
  18. [root@mcwcentos /]# ls /etc/yum.repos.d/ 查看cenos镜像中的repo文件
  19. CentOS-Linux-AppStream.repo CentOS-Linux-ContinuousRelease.repo CentOS-Linux-Devel.repo CentOS-Linux-FastTrack.repo CentOS-Linux-Media.repo CentOS-Linux-PowerTools.repo
  20. CentOS-Linux-BaseOS.repo CentOS-Linux-Debuginfo.repo CentOS-Linux-Extras.repo CentOS-Linux-HighAvailability.repo CentOS-Linux-Plus.repo CentOS-Linux-Sources.repo
  21. [root@mcwcentos /]# nslookup httpd-svc #用命令查看httpd-svc的DNS信息
  22. Server: 10.96.0.10
  23. Address: 10.96.0.10#53
  24.  
  25. Name: httpd-svc.default.svc.cluster.local #这个是DNS服务器,dns组件,这个是httpd的完整域名
  26. Address: 10.99.19.228 #可以看到这个是service的cluster ip
  27.  
  28. [root@mcwcentos /]#
  29.  
  30. [machangwei@mcwk8s-master ~]$ kubectl get service 集群ip
  31. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  32. httpd-svc ClusterIP 10.99.19.228 <none> 8080/TCP 19h
  33. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21h
  34. [machangwei@mcwk8s-master ~]$
  35.  
  36. 查看这个新创建的pod
  37. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide|grep mcwcentos
  38. mcwcentos 1/1 Running 0 8m8s 10.244.2.3 mcwk8s-node2 <none> <none>
  39.  
  40. 到pod对应节点上找到这个容器
  41. [root@mcwk8s-node2 ~]$ docker ps
  42. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  43. 0b11a9d9ac35 centos "/bin/bash" 8 minutes ago Up 8 minutes k8s_mcwcentos_mcwcentos_default_05aaed6d-57b0-4f6b-807d-1cd0e9c39ad9_0
  44.  
  45. 然后查看容器的日志,可以看到,记录了我进入容器后做的所有操作包括yum安装的过程
  46. [root@mcwk8s-node2 ~]$ docker logs 0b1|tail
  47. CentOS-Linux-AppStream.repo CentOS-Linux-ContinuousRelease.repo CentOS-Linux-Devel.repo CentOS-Linux-FastTrack.repo CentOS-Linux-Media.repo CentOS-Linux-PowerTools.repo
  48. CentOS-Linux-BaseOS.repo CentOS-Linux-Debuginfo.repo CentOS-Linux-Extras.repo CentOS-Linux-HighAvailability.repo CentOS-Linux-Plus.repo CentOS-Linux-Sources.repo
  49. [root@mcwcentos /]#
  50. [root@mcwcentos /]# nslookup httpd-svc
  51. Server: 10.96.0.10
  52. Address: 10.96.0.10#53
  53.  
  54. Name: httpd-svc.default.svc.cluster.local
  55. Address: 10.99.19.228
  56.  
  57. [root@mcwk8s-node2 ~]$
  58.  
  59. 主节点进入的pod中:
  60. [root@mcwcentos /]# ping httpd-svc
  61. PING httpd-svc.default.svc.cluster.local (10.99.19.228) 56(84) bytes of data.
  62. ^C
  63. --- httpd-svc.default.svc.cluster.local ping statistics ---
  64. 2 packets transmitted, 0 received, 100% packet loss, time 1001ms
  65.  
  66. [root@mcwcentos /]# curl httpd-svc
  67. curl: (7) Failed to connect to httpd-svc port 80: Connection timed out
  68. [root@mcwcentos /]# curl httpd-svc:8080
  69. <html><body><h1>It works!</h1></body></html>
  70. [root@mcwcentos /]# curl httpd-svc.default.svc.cluster.local:8080 #也可以curl完整域名,如果不加端口,不行,因为服务里显示用到这个端口了
  71. <html><body><h1>It works!</h1></body></html>
  72. [root@mcwcentos /]#

访问其它namespace中的service

  1. 查看已存在的namespcace
  2. [machangwei@mcwk8s-master ~]$ kubectl get namespace #这几个都是部署好集群时就已经创建好的namespace
  3. NAME STATUS AGE
  4. default Active 21h
  5. kube-node-lease Active 21h
  6. kube-public Active 21h
  7. kube-system Active 21h
  8. [machangwei@mcwk8s-master ~]$
  9.  
  10. 查看之前部署的文件
  11. [machangwei@mcwk8s-master ~]$ cat mcwHttpd.yml
  12. apiVersion: apps/v1
  13. kind: Deployment
  14. metadata:
  15. name: mcw-httpd
  16. spec:
  17. replicas: 3
  18. selector:
  19. matchLabels:
  20. run: mcw-httpd
  21. template:
  22. metadata:
  23. labels:
  24. run: mcw-httpd
  25. spec:
  26. containers:
  27. - name: mcw-httpd
  28. image: httpd
  29. ports:
  30. - containerPort: 80
  31. [machangwei@mcwk8s-master ~]$ cat mcwHttpdService.yml
  32. apiVersion: v1
  33. kind: Service
  34. metadata:
  35. name: httpd-svc
  36. spec:
  37. selector:
  38. run: mcw-httpd
  39. ports:
  40. - protocol: TCP
  41. port: 8080
  42. targetPort: 80
  43. [machangwei@mcwk8s-master ~]$ cat mcwhttpd2quanyml #查看上两个文件合并修改名称,标签等信息,添加指定的命名空间。多个资源用---来分割
  44. apiVersion: apps/v1
  45. kind: Deployment
  46. metadata:
  47. name: mcw-httpd2
  48. namespace: kube-public
  49. spec:
  50. replicas: 3
  51. selector:
  52. matchLabels:
  53. run: mcw-httpd2
  54. template:
  55. metadata:
  56. labels:
  57. run: mcw-httpd2
  58. spec:
  59. containers:
  60. - name: mcw-httpd2
  61. image: httpd
  62. ports:
  63. - containerPort: 80
  64. ---
  65. apiVersion: v1
  66. kind: Service
  67. metadata:
  68. name: httpd2-svc
  69. namespace: kube-public
  70. spec:
  71. selector:
  72. run: mcw-httpd2
  73. ports:
  74. - protocol: TCP
  75. port: 8080
  76. targetPort: 80
  77. [machangwei@mcwk8s-master ~]$
  78.  
  79. 查看mcwcentosip
  80. [root@mcwcentos /]# ip a
  81. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
  82. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  83. inet 127.0.0.1/8 scope host lo
  84. valid_lft forever preferred_lft forever
  85. 3: eth0@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default
  86. link/ether c6:c1:ce:94:49:24 brd ff:ff:ff:ff:ff:ff link-netnsid 0
  87. inet 10.244.2.3/24 brd 10.244.2.255 scope global eth0
  88. valid_lft forever preferred_lft forever
  89. [root@mcwcentos /]# hostname -i
  90. 10.244.2.3
  91. [root@mcwcentos /]#
  92.  
  93. [machangwei@mcwk8s-master ~]$ kubectl apply -f mcwhttpd2quanyml #部署httpd2服务
  94. deployment.apps/mcw-httpd2 created
  95. service/httpd2-svc created
  96. [machangwei@mcwk8s-master ~]$ kubectl get service --namespace=kube-public #查看服务2,需要指定命名空间
  97. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  98. httpd2-svc ClusterIP 10.101.134.243 <none> 8080/TCP 39s
  99. [machangwei@mcwk8s-master ~]$ kubectl get service #不指定命名空间,无法看到kube-public中的服务
  100. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  101. httpd-svc ClusterIP 10.99.19.228 <none> 8080/TCP 19h
  102. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21h
  103. [machangwei@mcwk8s-master ~]$
  104. [machangwei@mcwk8s-master ~]$ kubectl run mcwcentos2 --rm -ti --image=centos /bin/bash #运行第二个,mcwcentos2
  105. If you don't see a command prompt, try pressing enter.
  106. [root@mcwcentos2 /]# curl httpd2-svc:8080 #pod属于default命名空间,访问其它命名空间的服务,不指定命名空间,直接访问服务名加端口,是无法访问到
  107. curl: (6) Could not resolve host: httpd2-svc
  108. [root@mcwcentos2 /]# curl httpd2-svc.kube-public:8080 #加上服务名称.命名空间:服务端口,就可以实现在pod中跨命名空间访问服务
  109. <html><body><h1>It works!</h1></body></html>
  110. [root@mcwcentos2 /]# ping -c 2 10.244.2.3 #pod中能直接ping同一命名空间内的pod
  111. PING 10.244.2.3 (10.244.2.3) 56(84) bytes of data.
  112. 64 bytes from 10.244.2.3: icmp_seq=1 ttl=64 time=0.252 ms
  113. 64 bytes from 10.244.2.3: icmp_seq=2 ttl=64 time=0.072 ms
  114.  
  115. --- 10.244.2.3 ping statistics ---
  116. 2 packets transmitted, 2 received, 0% packet loss, time 1001ms
  117. rtt min/avg/max/mdev = 0.072/0.162/0.252/0.090 ms
  118. [root@mcwcentos2 /]#
  119. [root@mcwcentos2 /]# exit #当ctrl+d退出时,这个命令创建的pod就被删除掉了,get pod也看不见这个容器
  120. Session ended, resume using 'kubectl attach mcwcentos2 -c mcwcentos2 -i -t' command when the pod is running
  121. pod "mcwcentos2" deleted
  122. [machangwei@mcwk8s-master ~]$
  123.  
  124. 如下,默认只能看到default下的pod
  125. [machangwei@mcwk8s-master ~]$ kubectl get pod
  126. NAME READY STATUS RESTARTS AGE
  127. mcw-httpd-6fbf67d7d5-5qfrl 1/1 Running 0 21h
  128. mcw-httpd-6fbf67d7d5-98x8d 1/1 Running 0 21h
  129. mcw-httpd-6fbf67d7d5-bpbq4 1/1 Running 0 21h
  130. [machangwei@mcwk8s-master ~]$
  131. [machangwei@mcwk8s-master ~]$
  132. [machangwei@mcwk8s-master ~]$ kubectl get pod --all-namespaces
  133. NAMESPACE NAME READY STATUS RESTARTS AGE
  134. default mcw-httpd-6fbf67d7d5-5qfrl 1/1 Running 0 21h
  135. default mcw-httpd-6fbf67d7d5-98x8d 1/1 Running 0 21h
  136. default mcw-httpd-6fbf67d7d5-bpbq4 1/1 Running 0 21h
  137. kube-public mcw-httpd2-6b98bfbbbf-69jb5 1/1 Running 0 21m
  138. kube-public mcw-httpd2-6b98bfbbbf-qv7g9 1/1 Running 0 21m
  139. kube-public mcw-httpd2-6b98bfbbbf-ztddf 1/1 Running 0 21m
  140. kube-system coredns-6d8c4cb4d-vctxx 1/1 Running 1 (21h ago) 22h
  141. kube-system coredns-6d8c4cb4d-xkv9h 1/1 Running 0 22h

外网如何访问Service

1、集群ip加端口,可以提供集群内部访问服务。

  1. 这里是在三个节点上都通过curl集群ip:端口,正常访问。如果是pod中,不同命名空间的pod中访问,结果如何,以后验证,应该也是没有问题的。
  2. [machangwei@mcwk8s-master ~]$ kubectl get service
  3. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  4. httpd-svc NodePort 10.107.208.46 <none> 8080:30450/TCP 14m
  5. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
  6. [machangwei@mcwk8s-master ~]$ curl 10.107.208.46:8080
  7. <html><body><h1>It works!</h1></body></html>
  8.  
  9. [root@mcwk8s-node1 ~]$ curl 10.107.208.46:8080
  10. <html><body><h1>It works!</h1></body></html>
  11.  
  12. [root@mcwk8s-node2 ~]$ curl 10.107.208.46:8080
  13. <html><body><h1>It works!</h1></body></html>
  14. [root@mcwk8s-node2 ~]$

2、这里演示node port 方式将应用的service暴露给cluster外部。

  1. [machangwei@mcwk8s-master ~]$ ls #查看当前有的yml文件
  2. mcwhttpd2quanyml mcwHttpdService.yml mcwHttpd.yml mm.yml
  3. [machangwei@mcwk8s-master ~]$ kubectl get service #查看当前服务
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  5. httpd-svc ClusterIP 10.99.19.228 <none> 8080/TCP 19h
  6. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
  7. [machangwei@mcwk8s-master ~]$ kubectl get pod #查看当前的pod
  8. NAME READY STATUS RESTARTS AGE
  9. mcw-httpd-6fbf67d7d5-5qfrl 1/1 Running 0 21h
  10. mcw-httpd-6fbf67d7d5-98x8d 1/1 Running 0 21h
  11. mcw-httpd-6fbf67d7d5-bpbq4 1/1 Running 0 21h
  12. [machangwei@mcwk8s-master ~]$ kubectl delete -f mcwHttpdService.yml #把已有的服务删除掉,一会创建新的服务,还用以前的pod
  13. service "httpd-svc" deleted
  14. [machangwei@mcwk8s-master ~]$ kubectl get service #查看当前有的服务,之前的一个服务已经成功删除
  15. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  16. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
  17. [machangwei@mcwk8s-master ~]$ vim mcwHttpdService.yml #编辑服务yml
  18. [machangwei@mcwk8s-master ~]$ cat mcwHttpdService.yml #将服务yml增添键值对 type: NodePort。
  19. apiVersion: v1
  20. kind: Service
  21. metadata:
  22. name: httpd-svc
  23. spec:
  24. type: NodePort
  25. selector:
  26. run: mcw-httpd
  27. ports:
  28. - protocol: TCP
  29. port: 8080
  30. targetPort: 80
  31. [machangwei@mcwk8s-master ~]$ kubectl apply -f mcwHttpdService.yml #创建服务,好像不用删除,直接重新执行,会重新创建服务,有时间验证
  32. service/httpd-svc created
  33. [machangwei@mcwk8s-master ~]$ kubectl get service #查看新创建的服务,可以看到端口部分,多了一个端口,30450,
  34. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  35. httpd-svc NodePort 10.107.208.46 <none> 8080:30450/TCP 21s
  36. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 22h
  37. [machangwei@mcwk8s-master ~]$ netstat -an|grep 30450 #查看要暴露的端口,这个端口在所有节点(node)上都有,节点ip:这个端口,都能供外网访问,供集群外的访问
  38. tcp 0 0 0.0.0.0:30450 0.0.0.0:* LISTEN
  39. [machangwei@mcwk8s-master ~]$ curl 10.107.208.46:8080 #集群ip 端口访问服务
  40. <html><body><h1>It works!</h1></body></html>
  41. [machangwei@mcwk8s-master ~]$ curl 10.107.208.46:30450 #集群ip访问这个端口是不行的
  42. curl: (7) Failed connect to 10.107.208.46:30450; Connection timed out
  43. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide #查看pod的ip
  44. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  45. mcw-httpd-6fbf67d7d5-5qfrl 1/1 Running 0 21h 10.244.1.2 mcwk8s-node1 <none> <none>
  46. mcw-httpd-6fbf67d7d5-98x8d 1/1 Running 0 21h 10.244.2.2 mcwk8s-node2 <none> <none>
  47. mcw-httpd-6fbf67d7d5-bpbq4 1/1 Running 0 21h 10.244.1.3 mcwk8s-node1 <none> <none>
  48. [machangwei@mcwk8s-master ~]$ curl 10.244.1.2 #直接在节点上curl pod的ip,就能访问到服务
  49. <html><body><h1>It works!</h1></body></html>
  50. [machangwei@mcwk8s-master ~]$ curl 10.244.1.2:30450 #直接在节点上curl ip:要暴露的端口,是不行的
  51. curl: (7) Failed connect to 10.244.1.2:30450; Connection refused
  52. [machangwei@mcwk8s-master ~]$ hostname -i
  53. 10.0.0.4
  54. [machangwei@mcwk8s-master ~]$ curl 10.0.0.4:30450 #应该用curl 节点ip:要暴露的端口
  55. <html><body><h1>It works!</h1></body></html>
  56. [machangwei@mcwk8s-master ~]$ curl 10.0.0.5:30450 #节点ip:这个端口,还可以在外面浏览器上访问。
  57. <html><body><h1>It works!</h1></body></html> #只不过三个 节点ip加这个端口,外网访问这个服务的时候,
  58. [machangwei@mcwk8s-master ~]$ curl 10.0.0.6:30450 #是否后端实现了负载均衡,调度到三个后端pod上呢
  59. <html><body><h1>It works!</h1></body></html>
  60. [machangwei@mcwk8s-master ~]$
  61.  
  62. 主节点上找到服务的三个pod
  63. [machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
  64. NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
  65. mcw-httpd-6fbf67d7d5-5qfrl 1/1 Running 0 21h 10.244.1.2 mcwk8s-node1 <none> <none>
  66. mcw-httpd-6fbf67d7d5-98x8d 1/1 Running 0 21h 10.244.2.2 mcwk8s-node2 <none> <none>
  67. mcw-httpd-6fbf67d7d5-bpbq4 1/1 Running 0 21h 10.244.1.3 mcwk8s-node1 <none> <none>
  68.  
  69. 根据三个pod信息,去节点上找到这三个容器,将容器内容修改,添加上自己的pod ip
  70. [root@mcwk8s-node1 ~]$ netstat -an|grep 30450
  71. tcp 0 0 0.0.0.0:30450 0.0.0.0:* LISTEN
  72. [root@mcwk8s-node1 ~]$ docker ps |grep mcw-httpd-6fbf67d7d5-bpbq4
  73. c978d2770826 httpd "httpd-foreground" 22 hours ago Up 22 hours k8s_mcw-httpd_mcw-httpd-6fbf67d7d5-bpbq4_default_1380d70d-e2b1-4276-b80f-813bcd3bae10_0
  74. eddd4b542888 registry.aliyuncs.com/google_containers/pause:3.6 "/pause" 22 hours ago Up 22 hours k8s_POD_mcw-httpd-6fbf67d7d5-bpbq4_default_1380d70d-e2b1-4276-b80f-813bcd3bae10_0
  75. [root@mcwk8s-node1 ~]$ docker exec -it c978 bash
  76. root@mcw-httpd-6fbf67d7d5-bpbq4:/usr/local/apache2# ls
  77. bin build cgi-bin conf error htdocs icons include logs modules
  78. root@mcw-httpd-6fbf67d7d5-bpbq4:/usr/local/apache2# cd htdocs/
  79. root@mcw-httpd-6fbf67d7d5-bpbq4:/usr/local/apache2/htdocs# cat index.html
  80. <html><body><h1>It works!</h1></body></html>
  81. root@mcw-httpd-6fbf67d7d5-bpbq4:/usr/local/apache2/htdocs# echo '<html><body><h1>It works!</h1></body></html> 10.244.1.3'>index.html
  82. root@mcw-httpd-6fbf67d7d5-bpbq4:/usr/local/apache2/htdocs#
  83.  
  84. 在主节点上直接访问各个pod ip,查看都修改成功
  85. [machangwei@mcwk8s-master ~]$ curl 10.244.1.3
  86. <html><body><h1>It works!</h1></body></html> 10.244.1.3
  87. [machangwei@mcwk8s-master ~]$
  88.  
  89. 然后根据节点ip加要暴露出去的端口,进行访问。可以发现,在节点上无论访问哪个节点ip:端口,都能实现后端三个pod的负载均衡。也就是说实现了负载均衡的。访问任意一个结点ip:加要暴露的端口,都等于访问这个服务,而这个服务后端三个pod是实现负载均衡的。在浏览器上访问,也是能看出了的,
  90. [machangwei@mcwk8s-master ~]$ kubectl get service
  91. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  92. httpd-svc NodePort 10.107.208.46 <none> 8080:30450/TCP 46m
  93. kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h
  94. [machangwei@mcwk8s-master ~]$ hostname -i
  95. 10.0.0.4
  96. [machangwei@mcwk8s-master ~]$ curl 10.0.0.4:30450
  97. <html><body><h1>It works!</h1></body></html> 10.244.2.2
  98. [machangwei@mcwk8s-master ~]$ curl 10.0.0.4:30450
  99. <html><body><h1>It works!</h1></body></html> 10.244.1.2
  100. [machangwei@mcwk8s-master ~]$ curl 10.0.0.4:30450
  101. <html><body><h1>It works!</h1></body></html> 10.244.2.2
  102. [machangwei@mcwk8s-master ~]$ curl 10.0.0.4:30450
  103. <html><body><h1>It works!</h1></body></html> 10.244.2.2
  104. [machangwei@mcwk8s-master ~]$ curl 10.0.0.4:30450
  105. <html><body><h1>It works!</h1></body></html> 10.244.2.2
  106. [machangwei@mcwk8s-master ~]$ curl 10.0.0.4:30450
  107. <html><body><h1>It works!</h1></body></html> 10.244.1.2
  108. [machangwei@mcwk8s-master ~]$ curl 10.0.0.4:30450
  109. <html><body><h1>It works!</h1></body></html> 10.244.1.3
  110. [machangwei@mcwk8s-master ~]$ curl 10.0.0.4:30450
  111. <html><body><h1>It works!</h1></body></html> 10.244.2.2
  112.  
  113. [machangwei@mcwk8s-master ~]$ curl 10.0.0.5:30450
  114. <html><body><h1>It works!</h1></body></html> 10.244.1.2
  115. [machangwei@mcwk8s-master ~]$ curl 10.0.0.5:30450
  116. <html><body><h1>It works!</h1></body></html> 10.244.1.3

3、loadbalancer

以后添加

k8s通过Service访问Pod的更多相关文章

  1. linux运维、架构之路-K8s通过Service访问Pod

    一.通过Service访问Pod 每个Pod都有自己的IP地址,当Controller用新的Pod替换发生故障的Pod时,新Pod会分配到新的IP地址,例如:有一组Pod对外提供HTTP服务,它们的I ...

  2. k8s通过service访问pod(五)--技术流ken

    service 每个 Pod 都有自己的 IP 地址.当 controller 用新 Pod 替代发生故障的 Pod 时,新 Pod 会分配到新的 IP 地址.这样就产生了一个问题: 如果一组 Pod ...

  3. k8s通过service访问pod(五)

    service 每个 Pod 都有自己的 IP 地址.当 controller 用新 Pod 替代发生故障的 Pod 时,新 Pod 会分配到新的 IP 地址.这样就产生了一个问题: 如果一组 Pod ...

  4. 第六章 通过Service访问Pod(中)

    6.2 Cluster IP 底层实现 Cluster IP 是一个虚拟IP,是由K8s节点上的iptables规则管理的. 使用类似轮询的方法访问Pod. 6.3 DNS 访问Service 在Cl ...

  5. 第六章 通过Service访问Pod(上)

    不应该直接使用Pod的ID地址作为对外提供服务的接口,应为一旦Pod重启,IP地址就变化了,解决方案是使用Service. 6.1 创建Service K8s service从逻辑上代表了一组Pod, ...

  6. 通过 Service 访问 Pod - 每天5分钟玩转 Docker 容器技术(136)

    本节开始学习 Service.我们不应该期望 Kubernetes Pod 是健壮的,而是要假设 Pod 中的容器很可能因为各种原因发生故障而死掉.Deployment 等 controller 会通 ...

  7. 通过 Service 访问 Pod

    我们不应该期望 Kubernetes Pod 是健壮的,而是要假设 Pod 中的容器很可能因为各种原因发生故障而死掉.Deployment 等 controller 会通过动态创建和销毁 Pod 来保 ...

  8. 通过 Service 访问 Pod【转】

    本节开始学习 Service.我们不应该期望 Kubernetes Pod 是健壮的,而是要假设 Pod 中的容器很可能因为各种原因发生故障而死掉.Deployment 等 controller 会通 ...

  9. 第六章 通过Service访问Pod(下)

    6.4 外网如何访问service (1)ClusterIp: Service通过Cluster内部的IP对外提供服务,只有Cluster内的节点和Pod可以访问,这是默认的Service类型. (2 ...

随机推荐

  1. 如何把maven项目转为eclipse项目

    如何把maven项目转为eclipse项目,按照如下操作便可. 在cmd窗口, 载cmd窗口进入到maven项目所在目录下,输入如下命令: mvn eclipse:eclipse 这样便可.

  2. pdf2swf转换不成功该怎么解决啊,Process p=r.exec("D:/swf/pdf2swf.exe \""+pdfFile.getPath()+"\" -o \""+swfFile.getPath()+"\" -T 9");

    pdf2swf转换不成功该怎么解决啊,可以这样解决吧,请注意命令的用法啊:Process p=r.exec("D:/swf/pdf2swf.exe  \""+pdfFil ...

  3. 使用PostMan测试WebService接口教程

    一.操作步骤 1.设置URL 2.设置请求模式:Post 3.设置Header:添加 Content-Type ,值为 text/xml;charset=utf-8 4.设置Body:勾选raw 5. ...

  4. JAVA通过经纬度获取两点之间的距离

    private static double EARTH_RADIUS = 6378.137; private static double rad(double d) { return d * Math ...

  5. Cornfields(poj2019)

    Cornfields Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6798   Accepted: 3315 Descri ...

  6. Special Prime

    Special Prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. On the Optimization of Deep Networks: Implicit Acceleration by Overparameterization

    目录 引 主要内容 定理1 Claim 1 Claim 2 定理2 证明 定理1的证明 Claim 1 的证明 Kronecker product (克罗内克积) Theorem 2 的证明 代码 A ...

  8. [python]MergeTxt按列合并同一个文件下多个txt文件

    开发需求:应项目需要,要将记录成txt的实验数据进行按列合并(也即为不同文件上下合并),从而进行机器学习训练. 实验数据类似如此 模拟验证数据 1.txt *****1***** abcdefghij ...

  9. <学习opencv>图像变换

    拉伸.收缩.扭曲和旋转 统一调整大小 我们经常会遇到一些我们希望转换为其他尺寸的图像. 我们可能想要扩大或缩小图像; 这两项任务都是由同一个功能完成的. cv::resize() 该cv::resiz ...

  10. [opencv]白平衡算法中的灰度世界法,改善图像发红发蓝发绿的现象

    #include<iostream> #include <opencv2/opencv.hpp> #include <math.h> using namespace ...