k8s 实操部署应用
Posted 看,未来
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了k8s 实操部署应用相关的知识,希望对你有一定的参考价值。
文章目录
使用 kubectl 创建 Deployment
前边咱搭建了集群环境,现在咱来部署一下应用,不然集群搭建来干嘛?
为此,我们需要先创建 Kubernetes Deployment 配置。Deployment 指挥 Kubernetes 如何创建和更新应用程序的实例。创建 Deployment 后,Kubernetes master 将应用程序实例调度到集群中的各个节点上。
创建应用程序实例后,Kubernetes Deployment 控制器会持续监视这些实例。 如果托管实例的节点关闭或被删除,则 Deployment 控制器会将该实例替换为群集中另一个节点上的实例。 这提供了一种自我修复机制来解决机器故障维护问题。
在没有 Kubernetes 这种编排系统之前,安装脚本通常用于启动应用程序,但它们不允许从机器故障中恢复。通过创建应用程序实例并使它们在节点之间运行, Kubernetes Deployments 提供了一种与众不同的应用程序管理方法。
kubectl命令的常见格式是:kubectl action resource。这将对指定的资源(如节点、容器)执行指定的操作(如创建、描述)。可以在命令之后使用–help来获取有关可能参数的其他信息。
通过运行kubectl version命令,检查kubectl是否已安装:
Client Version: version.InfoMajor:"1", Minor:"20", GitVersion:"v1.20.4", GitCommit:"e87da0bd6e03ec3fea7933c4b5263d151aafd07c", GitTreeState:"clean", BuildDate:"2021-02-18T16:12:00Z", GoVersion:"go1.15.8", Compiler:"gc", Platform:"linux/amd64"
Server Version: version.InfoMajor:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-13T13:20:00Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"
运行 kubectl get nodes 命令以查看集群中的节点:
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 15m v1.20.2
依旧是那倔强且孤单的一个节点哈。
Kubernetes将根据节点可用资源选择在何处部署我们的应用程序。
使用 kubectl create deployment 命令在 Kubernetes 上部署我们的第一个应用程序。我们需要提供部署名称和应用程序映像位置(包括Docker hub外部托管映像的完整存储库url)。
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
deployment.apps/kubernetes-bootcamp created
好了。
要列出以部署的应用可以:kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 2m3s
Kubernetes 内部运行的 Pod 运行在一个独立的专用网络上。默认情况下,它们在同一 kubernetes 集群内的其他 POD 和服务中可见,但在该网络之外不可见。当我们使用 kubectl 时,我们通过 API 端点与应用程序进行交互。
kubectl命令可以创建一个代理,将通信转发到集群范围的专用网络。可以通过按 control-C 终止代理,并且在代理运行时不会显示任何输出。
kubectl proxy
Starting to serve on 127.0.0.1:8001
现在,我们的主机(在线终端)和Kubernetes集群之间有了连接。代理允许从这些终端直接访问API。
看到通过代理端点托管的所有API。例如,我们可以使用curl命令直接通过API查询版本:
curl http://localhost:8001/version
"major": "1",
"minor": "20",
"gitVersion": "v1.20.2",
"gitCommit": "faecb196815e248d3ecfb03c680a4507229c2a56",
"gitTreeState": "clean",
"buildDate": "2021-01-13T13:20:00Z",
"goVersion": "go1.15.5",
"compiler": "gc",
"platform": "linux/amd64"
API服务器将根据pod名称自动为每个pod创建一个端点,该端点也可以通过代理访问。
首先,我们需要获取Pod名称,并将其存储在环境变量Pod_name中:
export POD_NAME=$(kubectl get pods -o go-template --template 'range .items.metadata.name"\\n"end')
echo Name of the Pod: $POD_NAME
接下来可以通过下面的 IP 访问 Pod:
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/
"kind": "Pod",
"apiVersion": "v1",
"metadata":
"name": "kubernetes-bootcamp-57978f5f5d-clzbr",
"generateName": "kubernetes-bootcamp-57978f5f5d-",
"namespace": "default",
"uid": "d28f7fbe-73be-4917-ae6c-9e1e77eb25e2",
"resourceVersion": "1292",
"creationTimestamp": "2022-01-28T08:24:01Z",
"labels":
"app": "kubernetes-bootcamp",
"pod-template-hash": "57978f5f5d"
,
"ownerReferences": [
"apiVersion": "apps/v1",
"kind": "ReplicaSet",
"name": "kubernetes-bootcamp-57978f5f5d",
"uid": "24323ea4-7b0c-4a09-9d0e-749f15c2052f",
"controller": true,
"blockOwnerDeletion": true
],
"managedFields": [
"manager": "kube-controller-manager",
"operation": "Update",
"apiVersion": "v1",
"time": "2022-01-28T08:24:01Z",
"fieldsType": "FieldsV1",
"fieldsV1": "f:metadata":"f:generateName":,"f:labels":".":,"f:app":,"f:pod-template-hash":,"f:ownerReferences":".":,"k:\\"uid\\":\\"24323ea4-7b0c-4a09-9d0e-749f15c2052f\\"":".":,"f:apiVersion":,"f:blockOwnerDeletion":,"f:controller":,"f:kind":,"f:name":,"f:uid":,"f:spec":"f:containers":"k:\\"name\\":\\"kubernetes-bootcamp\\"":".":,"f:image":,"f:imagePullPolicy":,"f:name":,"f:resources":,"f:terminationMessagePath":,"f:terminationMessagePolicy":,"f:dnsPolicy":,"f:enableServiceLinks":,"f:restartPolicy":,"f:schedulerName":,"f:securityContext":,"f:terminationGracePeriodSeconds":
,
"manager": "kubelet",
"operation": "Update",
"apiVersion": "v1",
"time": "2022-01-28T08:24:03Z",
"fieldsType": "FieldsV1",
"fieldsV1": "f:status":"f:conditions":"k:\\"type\\":\\"ContainersReady\\"":".":,"f:lastProbeTime":,"f:lastTransitionTime":,"f:status":,"f:type":,"k:\\"type\\":\\"Initialized\\"":".":,"f:lastProbeTime":,"f:lastTransitionTime":,"f:status":,"f:type":,"k:\\"type\\":\\"Ready\\"":".":,"f:lastProbeTime":,"f:lastTransitionTime":,"f:status":,"f:type":,"f:containerStatuses":,"f:hostIP":,"f:phase":,"f:podIP":,"f:podIPs":".":,"k:\\"ip\\":\\"172.18.0.6\\"":".":,"f:ip":,"f:startTime":
]
,
"spec":
"volumes": [
"name": "default-token-vwk4j",
"secret":
"secretName": "default-token-vwk4j",
"defaultMode": 420
],
"containers": [
"name": "kubernetes-bootcamp",
"image": "gcr.io/google-samples/kubernetes-bootcamp:v1",
"resources":
,
"volumeMounts": [
"name": "default-token-vwk4j",
"readOnly": true,
"mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
],
"terminationMessagePath": "/dev/termination-log",
"terminationMessagePolicy": "File",
"imagePullPolicy": "IfNotPresent"
],
"restartPolicy": "Always",
"terminationGracePeriodSeconds": 30,
"dnsPolicy": "ClusterFirst",
"serviceAccountName": "default",
"serviceAccount": "default",
"nodeName": "minikube",
"securityContext":
,
"schedulerName": "default-scheduler",
"tolerations": [
"key": "node.kubernetes.io/not-ready",
"operator": "Exists",
"effect": "NoExecute",
"tolerationSeconds": 300
,
"key": "node.kubernetes.io/unreachable",
"operator": "Exists",
"effect": "NoExecute",
"tolerationSeconds": 300
],
"priority": 0,
"enableServiceLinks": true,
"preemptionPolicy": "PreemptLowerPriority"
,
"status":
"phase": "Running",
"conditions": [
"type": "Initialized",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2022-01-28T08:24:01Z"
,
"type": "Ready",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2022-01-28T08:24:03Z"
,
"type": "ContainersReady",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2022-01-28T08:24:03Z"
,
"type": "PodScheduled",
"status": "True",
"lastProbeTime": null,
"lastTransitionTime": "2022-01-28T08:24:01Z"
],
"hostIP": "172.17.0.66",
"podIP": "172.18.0.6",
"podIPs": [
"ip": "172.18.0.6"
],
"startTime": "2022-01-28T08:24:01Z",
"containerStatuses": [
"name": "kubernetes-bootcamp",
"state":
"running":
"startedAt": "2022-01-28T08:24:03Z"
,
"lastState":
,
"ready": true,
"restartCount": 0,
"image": "jocatalin/kubernetes-bootcamp:v1",
"imageID": "docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af",
"containerID": "docker://5d812609bb54a4185efd69361084d6f9fa0cffa890d9f821410337b4941f5054",
"started": true
],
"qosClass": "BestEffort"
为了在不使用代理的情况下访问新部署,需要一个服务,后面再说。
以上是关于k8s 实操部署应用的主要内容,如果未能解决你的问题,请参考以下文章
实操教程丨如何在K8S集群中部署Traefik Ingress Controller