- $ kubectl create namespace ygq
- namespace/ygq created
在naemspace ygq下创建两个应用:nginx和nginx-test。
- $ cat nginx-svc.yaml
- apiVersion: v1
- kind: Service
- metadata:
- name: nginx
- namespace: ygq
- spec:
- selector:
- app: nginx
- ports:
- - port: 80
- type: ClusterIP
- $ cat deployment-nginx.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- creationTimestamp: null
- labels:
- app: nginx
- name: nginx
- namespace: ygq
- spec:
- replicas: 1
- selector:
- matchLabels:
- app: nginx
- template:
- metadata:
- creationTimestamp: null
- labels:
- app: nginx
- spec:
- containers:
- - image: docker.io/library/nginx:latest
- name: nginx
- imagePullPolicy: IfNotPresent
- imagePullSecrets:
- - name: harbor-login
- $ kubectl apply -f nginx-svc.yaml
- $ kubectl apply -f deployment-nginx.yaml
- $ kubectl get svc -n ygq
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
- nginx ClusterIP 192.168.245.168 <none> 80/TCP 3d
- $ kubectl get pod -n ygq
- NAME READY STATUS RESTARTS AGE
- nginx-547cc75cb7-j46zl 1/1 Running 0 2d22h
- $ cat nginx-test-svc.yaml
- apiVersion: v1
- kind: Service
- metadata:
- name: nginx-test
- namespace: ygq
- spec:
- selector:
- app: nginx-test
- ports:
- - port: 80
- type: ClusterIP
- $ cat deployment-nginx-test.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- creationTimestamp: null
- labels:
- app: nginx-test
- name: nginx-test
- namespace: ygq
- spec:
- replicas: 1
- selector:
- matchLabels:
- app: nginx-test
- template:
- metadata:
- creationTimestamp: null
- labels:
- app: nginx-test
- spec:
- containers:
- - image: docker.io/library/nginx:latest
- name: nginx
- imagePullPolicy: IfNotPresent
- imagePullSecrets:
- - name: harbor-login
- $ kubectl apply -f nginx-test-svc.yaml
- $ kubectl apply -f deployment-nginx-test.yaml
- $ kubectl get svc -n ygq
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
- nginx-test ClusterIP 192.168.97.154 <none> 80/TCP 3d
- $ kubectl get pod -n ygq
- NAME READY STATUS RESTARTS AGE
- nginx-test-6c5f4dfc79-2ldhg 1/1 Running 1 (2d23h ago) 3d
- $ kubectl exec -it nginx-547cc75cb7-j46zl /bin/bash -n ygq
- kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
- root@nginx-547cc75cb7-j46zl:/# curl nginx
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
- # kubectl get pod -n ygq -o wide
- NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
- nginx-test-6c5f4dfc79-2ldhg 1/1 Running 1 (2d23h ago) 3d 172.20.176.17 cn-shanghai.10.12.46.85 <none> <none>
pod ip是172.20.176.17。
- root@nginx-547cc75cb7-j46zl:/# curl http://172.20.176.17:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
k8s 中dns的组成结构为:service_name.namespace_name.svc.cluster.local:port,可简写为service_name.namespace_name.svc:port。
deployment nginx-test的端口为80,其dns为:nginx-test.ygq.svc.cluster.local:80,简写为:nginx-test.ygq.svc:80。
1)完整dns
- root@nginx-547cc75cb7-j46zl:/# curl http://nginx-test.ygq.svc.cluster.local:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
2)简写dns
- root@nginx-547cc75cb7-j46zl:/# curl http://nginx-test.ygq.svc:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
- $ kubectl exec -it nginx-test-6c5f4dfc79-2ldhg /bin/bash -n ygq
- kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
- root@nginx-test-6c5f4dfc79-2ldhg:/# curl nginx
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
- $ kubectl get pod -n ygq -o wide
- NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
- nginx-547cc75cb7-j46zl 1/1 Running 0 2d23h 172.20.176.24 cn-shanghai.10.12.46.85 <none> <none>
- root@nginx-test-6c5f4dfc79-2ldhg:/# curl http://172.20.176.24:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
deployment nginx的端口为80,其dns为:nginx.ygq.svc.cluster.local:80,简写为:nginx.ygq.svc:80。
1)完整dns
- root@nginx-test-6c5f4dfc79-2ldhg:/# curl nginx.ygq.svc.cluster.local:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
2)简写dns
- root@nginx-test-6c5f4dfc79-2ldhg:/# curl nginx.ygq.svc:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
同namespace下不同pod直接可通过svc name、pod ip及dns互相访问。
pod ip是不固定的,会伴随pod的状态变化发生改变,生产环境不建议使用pod ip作为请求地址。
- $ kubectl create namespace dev
- namespace/dev created
在naemspace dev下创建应用:nginx-dev。
- $ cat deployment-nginx-dev.yaml
- apiVersion: apps/v1
- kind: Deployment
- metadata:
- creationTimestamp: null
- labels:
- app: nginx-dev
- name: nginx-dev
- namespace: dev
- spec:
- replicas: 4
- selector:
- matchLabels:
- app: nginx-dev
- template:
- metadata:
- creationTimestamp: null
- labels:
- app: nginx-dev
- spec:
- containers:
- - image: docker.io/library/nginx:latest
- name: nginx
- imagePullPolicy: IfNotPresent
- imagePullSecrets:
- - name: harbor-login
- $ cat nginx-dev-svc.yaml
- apiVersion: v1
- kind: Service
- metadata:
- name: nginx-dev
- namespace: dev
- spec:
- selector:
- app: nginx-dev
- ports:
- - port: 80
- type: ClusterIP
- $ kubectl apply -f nginx-dev-svc.yaml
- $ kubectl apply -f deployment-nginx-dev.yaml
- # kubectl get svc -n dev
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
- nginx-dev ClusterIP 192.168.28.113 <none> 80/TCP 3d
- $ kubectl get pod -n dev
- NAME READY STATUS RESTARTS AGE
- nginx-dev-5966c9747d-gbdq4 1/1 Running 1 (3d ago) 3d
- $ kubectl exec -it nginx-547cc75cb7-j46zl /bin/bash -n ygq
- kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
- root@nginx-547cc75cb7-j46zl:/# curl nginx-dev
- curl: (6) Could not resolve host: nginx-dev
- $ kubectl get pod -n dev -o wide
- NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
- nginx-dev-5966c9747d-gbdq4 1/1 Running 1 (3d ago) 3d 172.20.176.9 cn-shanghai.10.12.46.85 <none> <none>
- root@nginx-547cc75cb7-j46zl:/# curl 172.20.176.9:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
deployment nginx-dev的端口为80,其dns为:nginx-dev.dev.svc.cluster.local:80,简写为:nginx-dev.dev.svc:80。
1)完整dns
- root@nginx-547cc75cb7-j46zl:/# curl nginx-dev.dev.svc.cluster.local:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
2)简写dns
- root@nginx-547cc75cb7-j46zl:/# curl nginx-dev.dev.svc:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
- $ kubectl exec -it nginx-dev-5966c9747d-gbdq4 /bin/bash -n dev
- kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
- root@nginx-dev-5966c9747d-gbdq4:/# curl nginx
- curl: (6) Could not resolve host: nginx
- $ kubectl get pod -n ygq -o wide
- NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
- nginx-547cc75cb7-j46zl 1/1 Running 0 2d23h 172.20.176.24 cn-shanghai.10.12.46.85 <none> <none>
- root@nginx-dev-5966c9747d-gbdq4:/# curl 172.20.176.24:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
deployment nginx的端口为80,其dns为:nginx.ygq.svc.cluster.local:80,简写为:nginx.ygq.svc:80。
1)完整dns
- root@nginx-dev-5966c9747d-gbdq4:/# curl nginx.ygq.svc.cluster.local:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
2)简写dns
- root@nginx-dev-5966c9747d-gbdq4:/# curl nginx.ygq.svc:80
- <!DOCTYPE html>
- <html>
- <head>
- <title>Welcome to nginx!</title>
- <style>
- html { color-scheme: light dark; }
- body { width: 35em; margin: 0 auto;
- font-family: Tahoma, Verdana, Arial, sans-serif; }
- </style>
- </head>
- <body>
- <h1>Welcome to nginx!</h1>
- <p>If you see this page, the nginx web server is successfully installed and
- working. Further configuration is required.</p>
-
- <p>For online documentation and support please refer to
- <a href="http://nginx.org/">nginx.org</a>.<br/>
- Commercial support is available at
- <a href="http://nginx.com/">nginx.com</a>.</p>
-
- <p><em>Thank you for using nginx.</em></p>
- </body>
- </html>
不同namespace下pod直接可通过pod ip及dns互相访问,但不能通过svc name进行访问
pod ip是不固定的,会伴随pod的状态变化发生改变,生产环境不建议使用pod ip作为请求地址。
- $ kubectl get pod -n ygq -o wide
- NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
- nginx-547cc75cb7-j46zl 1/1 Running 0 3d 172.20.176.24 cn-shanghai.10.12.46.85 <none> <none>
- nginx-test-6c5f4dfc79-2ldhg 1/1 Running 1 (3d ago) 3d2h 172.20.176.17 cn-shanghai.10.12.46.85 <none> <none>
- $ kubectl create -f deployment-nginx.yaml
- Error from server (AlreadyExists): error when creating "deployment-nginx.yaml": deployments.apps "nginx" already exists
- $ kubectl get svc -n ygq -o wide
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
- nginx ClusterIP 192.168.245.168 <none> 80/TCP 3d1h app=nginx
- nginx-test ClusterIP 192.168.97.154 <none> 80/TCP 3d1h app=nginx-test
- $ kubectl create -f nginx-svc.yaml
- Error from server (AlreadyExists): error when creating "nginx-svc.yaml": services "nginx" already exists
- $ kubectl get pod -n dev -o wide
- NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
- nginx-cfcb57f6d-vr79r 1/1 Running 0 10s 172.20.176.28 cn-shanghai.10.12.46.85 <none> <none>
- nginx-dev-5966c9747d-gbdq4 1/1 Running 1 (3d1h ago) 3d1h 172.20.176.9 cn-shanghai.10.12.46.85 <none> <none>
- $ kubectl get pod -n ygq -o wide
- NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
- nginx-547cc75cb7-j46zl 1/1 Running 0 3d 172.20.176.24 cn-shanghai.10.12.46.85 <none> <none>
- nginx-test-6c5f4dfc79-2ldhg 1/1 Running 1 (3d ago) 3d2h 172.20.176.17 cn-shanghai.10.12.46.85 <none> <none>
- $ kubectl get svc -n dev -o wide
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
- nginx ClusterIP 192.168.87.200 <none> 80/TCP 7s app=nginx
- nginx-dev ClusterIP 192.168.28.113 <none> 80/TCP 3d1h app=nginx-dev
- $ kubectl get svc -n ygq -o wide
- NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
- nginx ClusterIP 192.168.245.168 <none> 80/TCP 3d1h app=nginx
- nginx-test ClusterIP 192.168.97.154 <none> 80/TCP 3d1h app=nginx-test
不同namescpace下可以存在相同名称的资源,同一namespace下不允许有相同名称的资源。