您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。