kubernetes集群发布Pod 端口
创建测试环境
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
验证服务启动
[[email protected] ~]# 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
[[email protected] ~]#
[[email protected] ~]# 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>
检查 Pod 的 IP 地址:
[[email protected] ~]# kubectl get pods -l run=my-nginx -o yaml | grep podIP
podIP: 10.244.3.101
podIP: 10.244.4.123
创建 Service
Kubernetes Service 从逻辑上定义了运行在集群中的一组 Pod,这些 Pod 提供了相同的功能。 当每个 Service 创建时,会被分配一个唯一的 IP 地址(也称为 clusterIP)。 这个 IP 地址与一个 Service 的生命周期绑定在一起,当 Service 存在的时候它也不会改变。 可以配置 Pod 使它与 Service 进行通信,Pod 知道与 Service 通信将被自动地负载均衡到该 Service 中的某些 Pod 上。
创建nginx 服务
kubectl expose 命令为 2个 Nginx 副本创建一个 Service:
[[email protected] ~]# kubectl expose deployment/my-nginx
service/my-nginx exposed
验证服务启动
[[email protected] ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-nginx ClusterIP 10.245.206.203 <none> 80/TCP 2m36s
查看详细信息
[[email protected]m-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的详细状态
访问 Service
Kubernetes 支持两种主要的服务发现模式 —— 环境变量和 DNS。前者在单个节点上可用使用,然而后者必须使用 kube-dns 集群插件。
[[email protected] ~]# 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
[[email protected] ~]# 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
DNS 解析测试
Kubernetes 提供 DNS ,如果它在集群中处于运行状态,可以通过如下命令来检查:
[[email protected] ~]# 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
验证服务解析
启动一个安装有curl 的镜像
[[email protected] ~]# kubectl run curl --image=radial/busyboxplus:curl -i --tty
测试解析 my-nginx
[ [email protected]:/ ]$ 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地址
[ [email protected]:/ ]$
curl 测试
[ [email protected]:/ ]$ curl my-nginx
........
<h1>Welcome to nginx!</h1>
.........
[ [email protected]:/ ]$ curl 10.245.206.203
........
<h1>Welcome to nginx!</h1>
......
服务资源扩容
收缩服务
[[email protected] ~]# kubectl scale deployment my-nginx --replicas=0
验证
[[email protected] ~]# kubectl get pods -o wide
No resources found.
扩容服务至2节点
[[email protected] ~]# kubectl scale deployment my-nginx --replicas=2
deployment.extensions/my-nginx scaled
验证
[[email protected] ~]# 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>
清理服务
#直接使用配置文件删除
[[email protected] ~]# kubectl delete -f nginx.yaml
deployment.apps "my-nginx" deleted
#删除服务
[[email protected] ~]# 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
[[email protected] ~]# kubectl delete svc my-nginx
service "my-nginx" deleted
[[email protected] ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.245.0.1 <none> 443/TCP 11d
原文地址:https://blog.51cto.com/michaelkang/2430830
时间: 2024-09-30 05:50:11