k8s快速入门

1.快速创建k8s集群

参考网站:https://kubernetes.io/docs/tutorials/kubernetes-basics

点击教程菜单 1. Create a Cluster -> Interactive Tutorial - Creating a Cluster

note:通过 Kubernetes 的命令行工具 kubectl 远程管理集群
  • minikube start 创建集群
  • kubectl cluster-info 查看集群信息
Kubernetes master is running at https://172.17.0.15:8443
KubeDNS is running at https://172.17.0.15:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
  • kubectl get nodes 查看节点信息
NAME       STATUS   ROLES    AGE     VERSION
minikube   Ready    master   2m53s   v1.13.3

2.部署应用

kubectl run kubernetes-bootcamp  --image=docker.io/jocatalin/kubernetes-bootcamp:v1 --port=8080

显示信息
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/kubernetes-bootcamp created

这里我们通过 kubectl run 部署了一个应用,命名为 kubernetes-bootcamp
--image 指定Docker 镜像
--port 设置应用对外服务的端口

3.访问应用

默认情况下,所有 Pod只能在集群内部访问。为了能够从外部访问应用,我们需要将容器的 8080 端口映射到节点的端口。

kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080

显示信息
service/kubernetes-bootcamp exposed

查看services及对应的映射端口

kubectl get services

NAME                  TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes            ClusterIP   10.96.0.1        <none>        443/TCP          9m47s
kubernetes-bootcamp   NodePort    10.105.140.245   <none>        8080:31048/TCP   82s

kubernetes 是默认的 service,暂时不用考虑。kubernetes-bootcamp 是我们应用的 service,8080 端口已经映射到 host01 的 31048 端口,端口号是随机分配的

通过curl命令进行访问

curl host01:30393
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c5cfd894b-ccqz7 | v=1

4.Scale 应用

默认情况下应用只会运行一个副本,可以通过 kubectl get deployments查看副本数。

kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   1/1     1            1           2m

增加副本个数

kubectl scale deployments/kubernetes-bootcamp --replicas=3
deployment.extensions/kubernetes-bootcamp scaled

$ kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   3/3     3            3           2m34s

减少副本个数

$ kubectl scale deployments/kubernetes-bootcamp --replicas=1
deployment.extensions/kubernetes-bootcamp scaled
$ kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   1/1     1            1           3m51s

$ kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-6c5cfd894b-9cn54   1/1     Running   0          41s
kubernetes-bootcamp-6c5cfd894b-ccqz7   1/1     Running   0          4m44s
kubernetes-bootcamp-6c5cfd894b-gnhgz   1/1     Running   0          41s
$ kubectl scale deployments/kubernetes-bootcamp --replicas=1
deployment.extensions/kubernetes-bootcamp scaled
$ kubectl get pods
NAME                                   READY   STATUS        RESTARTS   AGE
kubernetes-bootcamp-6c5cfd894b-9cn54   1/1     Terminating   0          57s
kubernetes-bootcamp-6c5cfd894b-ccqz7   1/1     Running       0          5m
kubernetes-bootcamp-6c5cfd894b-gnhgz   1/1     Terminating   0          57s

4.滚动更新

当前应用使用的 image 版本为 v1,执行如下命令将其升级到 v2:

$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.extensions/kubernetes-bootcamp image updated
$ kubectl get pods
NAME                                   READY   STATUS        RESTARTS   AGE
kubernetes-bootcamp-5bf4d5689b-4k5lb   1/1     Running       0          3s
kubernetes-bootcamp-5bf4d5689b-b9szr   1/1     Running       0          4s
kubernetes-bootcamp-5bf4d5689b-sxxs5   1/1     Running       0          4s
kubernetes-bootcamp-5bf4d5689b-zftkx   1/1     Running       0          3s
kubernetes-bootcamp-6c5cfd894b-ccqz7   1/1     Terminating   0          6m25s
kubernetes-bootcamp-6c5cfd894b-r4mtd   1/1     Terminating   0          6s
kubernetes-bootcamp-6c5cfd894b-r6txw   1/1     Terminating   0          6s
kubernetes-bootcamp-6c5cfd894b-vnbd6   1/1     Terminating   0          6s

$ curl host01:30393
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-zftkx | v=2

通过 kubectl get pods 可以观察滚动更新的过程:v1 的 Pod 被逐个删除,同时启动了新的 v2 Pod。更新完成后访问新版本应用。

如果要回退到 v1 版本也很容易,执行 kubectl rollout undo 命令:

$ kubectl rollout undo deployments/kubernetes-bootcamp
deployment.extensions/kubernetes-bootcamp rolled back
$ curl host01:30393
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6c5cfd894b-pq7kv | v=1

转载于:https://www.cnblogs.com/jiliguo/p/10907997.html

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐