创建CoreDNS [root@k8smaster coredns]# ls coredns.yaml [root@k8smaster coredns]# kubectl create -f . serviceaccount "coredns" created clusterrole.rbac.authorization.k8s.io "system:coredns" created clusterrolebinding.rbac.authorization.k8s.io "system:coredns" created configmap "coredns" created deployment.extensions "coredns" created service "coredns" created

创建Dashboard [root@k8smaster addons]# cd dashboard/ [root@k8smaster dashboard]# ls admin-user-sa-rbac.yaml kubernetes-dashboard.yaml ui-admin-rbac.yaml ui-read-rbac.yaml [root@k8smaster dashboard]# kubectl create -f . serviceaccount "admin-user" created clusterrolebinding.rbac.authorization.k8s.io "admin-user" created secret "kubernetes-dashboard-certs" created serviceaccount "kubernetes-dashboard" created role.rbac.authorization.k8s.io "kubernetes-dashboard-minimal" created rolebinding.rbac.authorization.k8s.io "kubernetes-dashboard-minimal" created deployment.apps "kubernetes-dashboard" created service "kubernetes-dashboard" created clusterrole.rbac.authorization.k8s.io "ui-admin" created rolebinding.rbac.authorization.k8s.io "ui-admin-binding" created clusterrole.rbac.authorization.k8s.io "ui-read" created rolebinding.rbac.authorization.k8s.io "ui-read-binding" created

[root@k8smaster dashboard]# kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}') Name: admin-user-token-hxlzj Namespace: kube-system Labels: <none> Annotations: kubernetes.io/service-account.name=admin-user kubernetes.io/service-account.uid=515020bb-6ec5-11e8-bf01-000c29007455

Type: kubernetes.io/service-account-token

Data

ca.crt: 1359 bytes namespace: 11 bytes token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLWh4bHpqIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI1MTUwMjBiYi02ZWM1LTExZTgtYmYwMS0wMDBjMjkwMDc0NTUiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06YWRtaW4tdXNlciJ9.lqSmY5ZKhJ_s7czQs8Mif-a6pkSPU3OC3UzobE095HYlm3fn_l66bEIBV9-7BXdl9vyWch1g8Aryr0zyFrhjJxhK9t7eEzmLHP3qm3BjyTa4LlrKXxffkXpXiOSSlYxx4ZuExKO7rvvEtwEQUoru1eOGVRPJQwCfSMZsNQp27bBa6ocMgYJqOSqgd8iR76vq8RYKmOVGQRDbY_iVGZb_VGcFFGZexx8miPlgbk3zHjx9ENivIU9K9huoUsx505cpsCQWc-wz7_DTfKeOedmxh3e1ebUzDo9ww1pd-05sgN0GovuAEXtpoanGEVBaFPGoB3wb3b6ilyxY3FDHzFrTLA

查看状态 [root@k8smaster dashboard]# kubectl get pods -o wide -n kube-system NAME READY STATUS RESTARTS AGE IP NODE coredns-77c989547b-4dfnn 1/1 Running 5 11m 10.2.59.3 k8snode2 coredns-77c989547b-54s8d 1/1 Running 6 11m 10.2.57.3 k8snode1 kubernetes-dashboard-66c9d98865-txvx2 1/1 Running 2 1m 10.2.57.4 k8snode1

[root@k8smaster dashboard]# kubectl get services -o wide -n kube-system NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR coredns ClusterIP 10.1.0.2 <none> 53/UDP,53/TCP 12m k8s-app=coredns kubernetes-dashboard ClusterIP 10.1.72.67 <none> 443/TCP 10m k8s-app=kubernetes-dashboard [root@k8smaster dashboard]# kubectl cluster-info Kubernetes master is running at https://192.168.137.171:6443 CoreDNS is running at https://192.168.137.171:6443/api/v1/namespaces/kube-system/services/coredns:dns/proxy kubernetes-dashboard is running at https://192.168.137.171:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

[root@k8smaster ~]# kubectl cluster-info Kubernetes master is running at https://192.168.137.171:6443 CoreDNS is running at https://192.168.137.171:6443/api/v1/namespaces/kube-system/services/coredns:dns/proxy kubernetes-dashboard is running at https://192.168.137.171:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

[root@k8smaster ~]# kubectl get services -o wide -n kube-system NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR coredns ClusterIP 10.1.0.2 <none> 53/UDP,53/TCP 19m k8s-app=coredns kubernetes-dashboard ClusterIP 10.1.150.142 <none> 443/TCP 9m k8s-app=kubernetes-dashboard [root@k8smaster ~]# [root@k8smaster ~]# kubectl get pods -o wide -n kube-system NAME READY STATUS RESTARTS AGE IP NODE coredns-77c989547b-8tbcs 1/1 Running 0 7m 10.2.59.7 192.168.137.198 coredns-77c989547b-gzz2g 1/1 Running 0 7m 10.2.36.6 192.168.137.201 kubernetes-dashboard-66c9d98865-fk2lb 1/1 Running 0 8m 10.2.36.5 192.168.137.201

2

3

1、创建应用一: [root@k8smaster ~]# kubectl run net-test --image=alpine --replicas=2 sleep 360000 deployment.apps "net-test" created [root@k8smaster ~]# [root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE net-test-5767cb94df-v8w6n 0/1 ContainerCreating 0 38s net-test-5767cb94df-z5xgm 0/1 ContainerCreating 0 37s [root@k8smaster ~]# [root@k8smaster ~]# kubectl get pods NAME READY STATUS RESTARTS AGE net-test-5767cb94df-v8w6n 1/1 Running 0 2m net-test-5767cb94df-z5xgm 1/1 Running 0 2m [root@k8smaster ~]# [root@k8smaster ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE net-test-5767cb94df-v8w6n 1/1 Running 0 2m 10.2.59.2 k8snode2 net-test-5767cb94df-z5xgm 1/1 Running 0 2m 10.2.57.2 k8snode1 [root@k8smaster ~]# ping 10.2.59.2 -c 4 PING 10.2.59.2 (10.2.59.2) 56(84) bytes of data. 64 bytes from 10.2.59.2: icmp_seq=1 ttl=63 time=1.01 ms 64 bytes from 10.2.59.2: icmp_seq=2 ttl=63 time=0.647 ms 64 bytes from 10.2.59.2: icmp_seq=3 ttl=63 time=1.34 ms 64 bytes from 10.2.59.2: icmp_seq=4 ttl=63 time=0.702 ms

--- 10.2.59.2 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3000ms rtt min/avg/max/mdev = 0.647/0.925/1.342/0.278 ms [root@k8smaster ~]# ping 10.2.57.2 -c 4 PING 10.2.57.2 (10.2.57.2) 56(84) bytes of data. 64 bytes from 10.2.57.2: icmp_seq=1 ttl=63 time=1.92 ms 64 bytes from 10.2.57.2: icmp_seq=2 ttl=63 time=0.415 ms 64 bytes from 10.2.57.2: icmp_seq=3 ttl=63 time=0.453 ms 64 bytes from 10.2.57.2: icmp_seq=4 ttl=63 time=0.483 ms

--- 10.2.57.2 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3000ms rtt min/avg/max/mdev = 0.415/0.820/1.929/0.640 ms

2、创建Nginx [root@k8smaster ~]# kubectl run nginx --image=nginx --replicas=2 deployment.apps "nginx" created

[root@k8smaster ~]# kubectl expose deployment nginx --type=NodePort --name=nginx --port=80 service "nginx" exposed

[root@k8smaster ~]# kubectl get services -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 7h <none> nginx NodePort 10.1.133.181 <none> 80:32109/TCP 6s run=nginx [root@k8smaster ~]# [root@k8smaster ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE nginx-65899c769f-nwxw4 1/1 Running 0 4m 10.2.59.6 192.168.137.198 nginx-65899c769f-sxq7m 1/1 Running 0 4m 10.2.36.3 192.168.137.201

Nginx访问成功!

[root@k8snode1 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.137.201:32109 rr -> 10.2.36.3:80 Masq 1 1 0
-> 10.2.59.6:80 Masq 1 1 0
TCP 192.168.137.215:32109 rr -> 10.2.36.3:80 Masq 1 0 0
-> 10.2.59.6:80 Masq 1 0 0
TCP 10.1.0.1:443 rr persistent 10800 -> 192.168.137.171:6443 Masq 1 0 0
TCP 10.1.0.2:53 rr -> 10.2.59.4:53 Masq 1 0 0
TCP 10.1.72.67:443 rr TCP 10.1.133.181:80 rr -> 10.2.36.3:80 Masq 1 0 0
-> 10.2.59.6:80 Masq 1 0 0
TCP 10.2.36.0:32109 rr -> 10.2.36.3:80 Masq 1 0 0
-> 10.2.59.6:80 Masq 1 0 0
TCP 10.2.36.1:32109 rr -> 10.2.36.3:80 Masq 1 0 0
-> 10.2.59.6:80 Masq 1 0 0
TCP 127.0.0.1:32109 rr -> 10.2.36.3:80 Masq 1 0 0
-> 10.2.59.6:80 Masq 1 0 0
UDP 10.1.0.2:53 rr -> 10.2.59.4:53 Masq 1 0 0