vi nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
selector:
matchLabels:
run: my-nginx
replicas: 2
template:
metadata:
labels:
run: my-nginx
spec:
containers:
- name: my-nginx
image: nginx
ports:
- containerPort: 80
[root@kubm-02 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-756fb87568-jwgp8 1/1 Running 0 64s
my-nginx-756fb87568-vshxc 1/1 Running 0 64s
[root@kubm-02 ~]#
[root@kubm-02 ~]# kubectl get pods -l run=my-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-756fb87568-jwgp8 1/1 Running 0 2m12s 10.244.3.101 kubnode-01 <none> <none>
my-nginx-756fb87568-vshxc 1/1 Running 0 2m12s 10.244.4.123 kubnode-02 <none> <none>
[root@kubm-02 ~]# kubectl get pods -l run=my-nginx -o yaml | grep podIP
podIP: 10.244.3.101
podIP: 10.244.4.123
Kubernetes Service 从逻辑上定义了运行在集群中的一组 Pod,这些 Pod 提供了相同的功能。 当每个 Service 创建时,会被分配一个唯一的 IP 地址(也称为 clusterIP)。 这个 IP 地址与一个 Service 的生命周期绑定在一起,当 Service 存在的时候它也不会改变。 可以配置 Pod 使它与 Service 进行通信,Pod 知道与 Service 通信将被自动地负载均衡到该 Service 中的某些 Pod 上。
kubectl expose 命令为 2个 Nginx 副本创建一个 Service:
[root@kubm-02 ~]# kubectl expose deployment/my-nginx
service/my-nginx exposed
[root@kubm-02 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-nginx ClusterIP 10.245.206.203 <none> 80/TCP 2m36s
[root@kubm-02 ~]# kubectl describe svc my-nginx
Name: my-nginx
Namespace: default
Labels: <none>
Annotations: <none>
Selector: run=my-nginx
Type: ClusterIP
IP: 10.245.206.203
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.3.101:80,10.244.4.123:80
Session Affinity: None
Events: <none>
kubectl describe po my-nginx # 查看my-nginx pod的详细状态
kubectl describe rs my-nginx # 查看my-nginx replica set的详细状态
kubectl describe deployment my-nginx # 查看my-nginx deployment的详细状态
Kubernetes 支持两种主要的服务发现模式 —— 环境变量和 DNS。前者在单个节点上可用使用,然而后者必须使用 kube-dns 集群插件。
[root@kubm-02 ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
my-nginx-756fb87568-jwgp8 1/1 Running 0 23m
my-nginx-756fb87568-vshxc 1/1 Running 0 23m
[root@kubm-02 ~]# kubectl exec my-nginx-756fb87568-jwgp8 -- printenv | grep SERVICE
KUBERNETES_SERVICE_HOST=10.245.0.1
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_PORT_HTTPS=443
Kubernetes 提供 DNS ,如果它在集群中处于运行状态,可以通过如下命令来检查:
[root@kubm-02 ~]# kubectl get services kube-dns --namespace=kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.245.0.10 <none> 53/UDP,53/TCP,9153/TCP 11d
[root@kubm-02 ~]# kubectl run curl --image=radial/busyboxplus:curl -i --tty
[ root@curl-6bf6db5c4f-96nhg:/ ]$ nslookup my-nginx
Server: 10.245.0.10 《==== dns 服务器
Address 1: 10.245.0.10 kube-dns.kube-system.svc.cluster.local
Name: my-nginx
Address 1: 10.245.206.203 my-nginx.default.svc.cluster.local 《==== my-nginx服务的集群IP地址
[ root@curl-6bf6db5c4f-96nhg:/ ]$
[ root@curl-6bf6db5c4f-96nhg:/ ]$ curl my-nginx
........
<h2>Welcome to nginx!</h2>
.........
[ root@curl-6bf6db5c4f-96nhg:/ ]$ curl 10.245.206.203
........
<h2>Welcome to nginx!</h2>
......
收缩服务
[root@kubm-02 ~]# kubectl scale deployment my-nginx --replicas=0
验证
[root@kubm-02 ~]# kubectl get pods -o wide
No resources found.
扩容服务至2节点
[root@kubm-02 ~]# kubectl scale deployment my-nginx --replicas=2
deployment.extensions/my-nginx scaled
验证
[root@kubm-02 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-nginx-756fb87568-gmgfq 0/1 ContainerCreating 0 1s <none> kubnode-02 <none> <none>
my-nginx-756fb87568-gvhbm 0/1 ContainerCreating 0 1s <none> kubnode-01 <none> <none>
#直接使用配置文件删除
[root@kubm-02 ~]# kubectl delete -f nginx.yaml
deployment.apps "my-nginx" deleted
#删除服务
[root@kubm-02 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 11d
my-nginx ClusterIP 10.245.206.203 <none> 80/TCP 48m
[root@kubm-02 ~]# kubectl delete svc my-nginx
service "my-nginx" deleted
[root@kubm-02 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 11d
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。