$ kubectl cluster-info Kubernetes master is running at https://10.245.1.2 Heapster is running at https://10.245.1.2/api/v1/proxy/namespaces/kube-system/services/heapster KubeDNS is running at https://10.245.1.2/api/v1/proxy/namespaces/kube-system/services/kube-dns kubernetes-dashboard is running at https://10.245.1.2/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard Grafana is running at https://10.245.1.2/api/v1/proxy/namespaces/kube-system/services/monitoring-grafana InfluxDB is running at https://10.245.1.2/api/v1/proxy/namespaces/kube-system/services/monitoring-influxdb
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
0x20 install network solution
Kubernetes itself doesn’t enforce NetworkPolicy. You’ll need to run a networking solution like Calico, Canal, etc as your network plugin to get NetworkPolicy features.
[root@kubernetes-master ~]# ETCD_ENDPOINTS=http://10.245.1.2:4379 ./calicoctl status calico-node container is running. Status: Up About a minute Running felix version 1.4.0
IPv4 BGP status IP: 10.245.1.2 AS Number: 64511 (inherited) +--------------|-------------------|-------|----------|-------------+ | Peer address | Peer type | State | Since | Info | +--------------|-------------------|-------|----------|-------------+ | 10.245.1.3 | node-to-node mesh | up | 08:22:15 | Established | +--------------|-------------------|-------|----------|-------------+
spec: hostNetwork:true containers: -name:calico-policy-controller # Make sure to pin this to your desired version. image:calico/kube-policy-controller env: # Configure the policy controller with the location of # your etcd cluster. -name:ETCD_ENDPOINTS value:"http://10.245.1.2:4379"
在开发机上通过上面下载的yaml文件创建controller.
1
$ kubectl create -f policy-controller.yaml
0x25 配置确认
在开发机器上确认一下 calico-policy-controller 的状态是否正常。
1 2 3 4 5 6 7 8
$ kubectl get pod --namespace=kube-system NAME READY STATUS RESTARTS AGE calico-policy-controller-0i4so 1/1 Running 1 23h heapster-v1.2.0-2582472167-91sw6 4/4 Running 24 4d kube-dns-v19-dfhx9 3/3 Running 41 4d kube-proxy-kubernetes-node-1 1/1 Running 7 4d kubernetes-dashboard-v1.4.0-gydiz 1/1 Running 22 4d monitoring-influxdb-grafana-v4-auln8 2/2 Running 15 4d
并确认 master 和 node 上 calico-node 容器是否正常。
0x30 test network policy
在开发机上面创建 namespace 名为 test 且在对其配置每 pod 隔离,并在其中创建名为 nginx 的 deployment 和 service,
1 2 3 4
kubectl create ns test # 创建 test 命名空间 kubectl annotate ns test "net.beta.kubernetes.io/network-policy={\"ingress\": {\"isolation\": \"DefaultDeny\"}}" --overwrite # 每 pod 隔离 kubectl --namespace=test run nginx --image=nginx # 创建 deployment kubectl --namespace=test expose deployment nginx --port=80 # 创建 service
kubectl --namespace=test get svc,pod NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/nginx 10.247.114.187 <none> 80/TCP 3h NAME READY STATUS RESTARTS AGE po/nginx-701339712-wcim9 1/1 Running 0 3h
$ kubectl annotate ns test"net.beta.kubernetes.io/network-policy={\"ingress\": {\"isolation\": \"DefaultDeny\"}}" --overwrite namespace "test" annotated $ cat network-policy.yaml kind: NetworkPolicy apiVersion: extensions/v1beta1 metadata: name: access-nginx namespace: test spec: podSelector: matchLabels: run: nginx ingress: - from: - podSelector: matchLabels: access: "true" $ kubectl create -f network-policy.yaml networkpolicy "access-nginx" created $ kubectl --namespace=test run busybox --rm -ti --labels="access=true" --image=busybox /bin/sh Waiting for pod test/busybox-3554646944-irgeg to be running, status is Pending, pod ready: false If you don't see a command prompt, try pressing enter. / # wget nginx Connecting to nginx (10.247.114.187:80) index.html 100% |**********************************************************************************************************************************************************| 612 0:00:00 ETA / # Session ended, resume using 'kubectl attach busybox-3554646944-irgeg -c busybox -i -t' command when the pod is running deployment "busybox" deleted $ kubectl --namespace=test run busybox --rm -ti --image=busybox /bin/sh Waiting for pod test/busybox-3674381263-0solq to be running, status is Pending, pod ready: false If you don't see a command prompt, try pressing enter. / # wget --timeout=1 nginx Connecting to nginx (10.247.114.187:80) wget: download timed out / #