[root@master1 yamldir]# kubectl apply -f 03_create_deployment_nginx_app2.yaml
deployment.apps/nginx-app2 created
[root@master1 yamldir]# kubectl get deployment.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app2 1/1 1 1 32s
[root@master1 yamldir]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app2-7b78bfb8c8-wzvc9 1/1 Running 0 70s
[root@master1 yamldir]#
[root@master1 yamldir]# kubectl expose deployment.apps nginx-app2 --type=ClusterIP --target-port=80 --port=80
service/nginx-app2 exposed
[root@master1 yamldir]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8h
nginx-app2 ClusterIP 10.106.82.170 <none> 80/TCP 31s
[root@master1 yamldir]# curl http://10.106.82.170
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
[root@master1 yamldir]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.216.100:6443 8h
nginx-app2 172.16.235.131:80 90s
[root@master1 yamldir]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app2-7b78bfb8c8-wzvc9 1/1 Running 0 4m49s 172.16.235.131 worker1 <none> <none>
[root@master1 yamldir]#
按资源清单方式
[root@master1 yamldir]# cat 04_create_deployment_nginx_app3_service.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app3
spec:
replicas: 2
selector:
matchLabels:
apps: nginx
template:
metadata:
labels:
apps: nginx
spec:
containers:
- name: nginxapp3
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-app3-svc
spec:
type: ClusterIP
ports:
- protocol: TCP
port: 80
targetPort: 80
selector:
apps: nginx
[root@master1 yamldir]# kubectl apply -f 04_create_deployment_nginx_app3_service.yaml
deployment.apps/nginx-app3 created
service/nginx-app3-svc created
[root@master1 yamldir]# kubectl get deployment.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app2 1/1 1 1 31m
nginx-app3 2/2 2 2 39s
[root@master1 yamldir]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8h
nginx-app2 ClusterIP 10.106.82.170 <none> 80/TCP 28m
nginx-app3-svc ClusterIP 10.110.142.231 <none> 80/TCP 11m
[root@master1 yamldir]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 8h
nginx-app2 ClusterIP 10.106.82.170 <none> 80/TCP 29m
nginx-app3-svc ClusterIP 10.110.142.231 <none> 80/TCP 12m
[root@master1 yamldir]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.216.100:6443 8h
nginx-app2 172.16.235.131:80 29m
nginx-app3-svc 172.16.189.69:80,172.16.235.132:80 12m
[root@master1 yamldir]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app2-7b78bfb8c8-wzvc9 1/1 Running 0 33m
nginx-app3-86984c5bb9-f8dg2 1/1 Running 0 2m50s
nginx-app3-86984c5bb9-t8w4x 1/1 Running 0 2m50s
[root@master1 yamldir]# kubectl exec -it nginx-app3-86984c5bb9-f8dg2 sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# cd /usr/share/nginx/html
# ls
50x.html index.html
# echo "xiaobu pod1" >index.html
# cat index.html
xiaobu pod1
# exit
You have new mail in /var/spool/mail/root
[root@master1 yamldir]# kubectl exec -it nginx-app3-86984c5bb9-t8w4x sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
# cd /usr/share/nginx/html
# ls
50x.html index.html
# echo "xiaobu pod2">index.html
# cat index.html
xiaobu pod2
# exit
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod2
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]# curl http://10.110.142.231
xiaobu pod1
[root@master1 yamldir]#
[root@worker1 ~]# ss -anput|grep 30844
tcp LISTEN 0 128 *:30844 *:* users:(("kube-proxy",pid=17785,fd=11))
You have new mail in /var/spool/mail/root
[root@worker1 ~]#