3. 使用 Kubectl 启动容器
在此场景中,您将学习如何使用 Kubectl 创建和启动部署、复制控制器并通过服务公开它们,而无需编写yaml定义。
3.1 启动集群
$ minikube start --wait=false * minikube v1.8.1 on Ubuntu 18.04 * Using the none driver based on user configuration * Running on localhost (CPUs=2, Memory=2460MB, Disk=145651MB) ... * OS release is Ubuntu 18.04.4 LTS * Preparing Kubernetes v1.17.3 on Docker 19.03.6 ... - kubelet.resolv-conf=/run/systemd/resolve/resolv.conf * Launching Kubernetes ... * Enabling addons: default-storageclass, storage-provisioner * Configuring local host environment ... * Done! kubectl is now configured to use "minikube" $ kubectl get nodes NAME STATUS ROLES AGE VERSION minikube Ready master 2m13s v1.17.3
3.2 Kubectl 运行
启动一个基于 Docker 镜像katacoda/docker-http-server:latest
的容器
$ kubectl run http --image=katacoda/docker-http-server:latest --replicas=1 kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead. deployment.apps/http created $ kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE http 1/1 1 1 25s $ kubectl describe deployment http Name: http Namespace: default CreationTimestamp: Sun, 07 Nov 2021 04:53:59 +0000 Labels: run=http Annotations: deployment.kubernetes.io/revision: 1 Selector: run=http Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: run=http Containers: http: Image: katacoda/docker-http-server:latest Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: http-774bb756bb (1/1 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 31s deployment-controller Scaled up replica set http-774bb756bb to 1
3.3 Kubectl 暴露
创建部署后,我们可以使用kubectl创建一个服务,该服务在特定端口上公开Pod。
通过kubectl 暴露新部署的http部署。该命令允许您定义服务的不同参数以及如何公开部署。
使用下面的命令暴露主机8000上的容器端口80绑定到主机的external-ip
。
$ kubectl expose deployment http --external-ip="172.17.0.34" --port=8000 --target-port=80 service/http exposed $ curl http://172.17.0.34:8000 <h1>This request was processed by host: http-774bb756bb-thlz6</h1>
3.4 Kubectl 运行和公开
使用kubectl run可以创建部署并将其公开为单个命令。
$ kubectl run httpexposed --image=katacoda/docker-http-server:latest --replicas=1 --port=80 --hostport=8001 kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead. deployment.apps/httpexposed created $ curl http://172.17.0.34:8001 <h1>This request was processed by host: httpexposed-68cb8c8d4-7jxl5</h1> $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE http ClusterIP 10.99.57.91 172.17.0.34 8000/TCP 5m26s kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 21m $ docker ps | grep httpexposed 305d27b7ef0a katacoda/docker-http-server "/app" About a minute ago Up About a minute k8s_httpexposed_httpexposed-68cb8c8d4-7jxl5_default_d8535fb7-f2d9-4a50-a4b3-abc5d3166f2e_0 928f66157451 k8s.gcr.io/pause:3.1 "/pause" About a minute ago Up About a minute 0.0.0.0:8001->80/tcp k8s_POD_httpexposed-68cb8c8d4-7jxl5_default_d8535fb7-f2d9-4a50-a4b3-abc5d3166f2e_0
您应该能够使用它访问它 curl http://172.17.0.34:8001
在幕后,这通过 Docker 端口映射公开了 Pod。因此,您不会看到使用列出的服务kubectl get svc
要查找您可以使用的详细信息 docker ps | grep httpexposed
运行上面的命令,你会注意到端口暴露在 Pod 上,而不是 http 容器本身。Pause 容器负责为 Pod 定义网络。Pod 中的其他容器共享相同的网络命名空间。这提高了网络性能并允许多个容器通过同一网络接口进行通信。
3.5 扩展容器
随着我们的部署运行,我们现在可以使用kubectl来扩展副本的数量。
扩展部署将请求 Kubernetes 启动额外的 Pod。然后,这些 Pod 将使用公开的服务自动进行负载平衡。
kubectl scale
命令允许我们调整为特定部署或复制控制器运行的Pod数量。
$ kubectl scale --replicas=3 deployment http deployment.apps/http scaled
列出所有 pod,您应该会看到三个正在运行的http部署
$ kubectl get pods NAME READY STATUS RESTARTS AGE http-774bb756bb-b79wn 1/1 Running 0 34s http-774bb756bb-bspt4 1/1 Running 0 34s http-774bb756bb-thlz6 1/1 Running 0 12m httpexposed-68cb8c8d4-7jxl5 1/1 Running 0 4m34s
一旦每个 Pod 启动,它就会被添加到负载均衡器服务中。通过描述服务,您可以查看包含的端点和关联的 Pod。
$ kubectl describe svc http Name: http Namespace: default Labels: run=http Annotations: <none> Selector: run=http Type: ClusterIP IP: 10.99.57.91 External IPs: 172.17.0.34 Port: <unset> 8000/TCP TargetPort: 80/TCP Endpoints: 172.18.0.4:80,172.18.0.6:80,172.18.0.7:80 Session Affinity: None Events: <none>
向服务发出请求将请求在不同的节点中处理请求。
$ curl http://172.17.0.34:8000 <h1>This request was processed by host: http-774bb756bb-thlz6</h1>
4. YAML 部署容器
在此场景中,您将学习如何使用 Kubectl 创建和启动部署、复制控制器,并通过编写yaml定义通过服务公开它们。
YAML 定义定义了计划部署的 Kubernetes 对象。可以更新对象并将其重新部署到集群以更改配置。
4.1 创建deployment
deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: webapp1 spec: replicas: 1 selector: matchLabels: app: webapp1 template: metadata: labels: app: webapp1 spec: containers: - name: webapp1 image: katacoda/docker-http-server:latest ports: - containerPort: 80
$ kubectl create -f deployment.yaml deployment.apps/webapp1 created $ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE webapp1 1/1 1 1 29s $ kubectl describe deployment webapp1 Name: webapp1 Namespace: default CreationTimestamp: Sun, 07 Nov 2021 10:05:15 +0000 Labels: <none> Annotations: deployment.kubernetes.io/revision: 1 Selector: app=webapp1 Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=webapp1 Containers: webapp1: Image: katacoda/docker-http-server:latest Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: webapp1-6b54fb89d9 (1/1 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 32s deployment-controller Scaled up replica set webapp1-6b54fb89d9 to 1
4.2 创建服务
Kubernetes 具有强大的网络功能,可以控制应用程序的通信方式。这些网络配置也可以通过 YAML 进行控制。
service.yaml
apiVersion: v1 kind: Service metadata: name: webapp1-svc labels: app: webapp1 spec: type: NodePort ports: - port: 80 nodePort: 30080 selector: app: webapp1
$ kubectl create -f service.yaml service/webapp1-svc created $ kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3m3s webapp1-svc NodePort 10.107.139.217 <none> 80:30080/TCP 2s $ kubectl describe svc webapp1-svc Name: webapp1-svc Namespace: default Labels: app=webapp1 Annotations: <none> Selector: app=webapp1 Type: NodePort IP: 10.107.139.217 Port: <unset> 80/TCP TargetPort: 80/TCP NodePort: <unset> 30080/TCP Endpoints: 172.18.0.4:80 Session Affinity: None External Traffic Policy: Cluster Events: <none> $ curl host01:30080 <h1>This request was processed by host: webapp1-6b54fb89d9-hfc88</h1>
4.3 扩容
更改replicas: 4
$ kubectl apply -f deployment.yaml deployment.apps/webapp1 configured $ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE webapp1 1/4 4 1 4m27s $ kubectl get pods NAME READY STATUS RESTARTS AGE webapp1-6b54fb89d9-hfc88 1/1 Running 0 4m31s webapp1-6b54fb89d9-j9gdt 1/1 Running 0 6s webapp1-6b54fb89d9-w7j2w 1/1 Running 0 6s webapp1-6b54fb89d9-xczm7 1/1 Running 0 6s $ curl host01:30080 <h1>This request was processed by host: webapp1-6b54fb89d9-hfc88</h1>