您的位置:首页 > 运维架构 > Kubernetes

Kubernetes应用快速入门

2018-10-01 23:29 603 查看

一、创建pod

1. kuberctl run命令

[root@master manifests]# kubectl run --help
Create and run a particular image, possibly replicated.

Creates a deployment or job to manage the created container(s).
......
......
Usage:
kubectl run NAME --image=image [--env="key=value"] 	[--port=port] [--replicas=replicas] [--dry-run=bool]
[--overrides=inline-json] [--command] -- [COMMAND] 	[args...] [options]

简而言之就是kuberctl run命令就是用来创建deployment或者job对象的

2. 创建nginx-deploy

[root@master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80
deployment.apps/nginx-deploy created
[root@master ~]# kubectl get pods
NAME                          READY     STATUS    RESTARTS   AGE
nginx-deploy-5b595999-49sct   1/1       Running   0          1m

这样就创建了一个nginx-deploy Pod
但是如何访问nginx-deploy提供的服务呢?
可以使用kubectl get pods -o wide查看nginx-deploy容器的IP

[root@master ~]# kubectl get pods nginx-deploy-5b595999-49sct -o wide
NAME                          READY     STATUS    RESTARTS   AGE       IP            NODE
nginx-deploy-5b595999-49sct   1/1       Running   0          9m        10.244.2.26   node2

然后在集群内部使用curl命令访问:

[root@master ~]# curl http://10.244.2.26


但是pod是有生命周期的,当pod宕掉之后,pod的IP会重新被分配,那么该如何访问pod的固定端点呢?好在kubernetes提供了service作为pod的固定端点用于用户访问pod

二、创建Service

1. Service访问后端Pod示意图

2. kubectl expose命令

[root@master ~]# kubectl expose --help
Expose a resource as a new Kubernetes service.

Looks up a deployment, service, replica set, replication controller or pod by name and uses the selector for that
resource as the selector for a new service on the specified port. A deployment or replica set will be exposed as a
service only if its selector is convertible to a selector that service supports, i.e. when the selector contains only
the matchLabels component. Note that if no port is specified via --port and the exposed resource has multiple ports, all
will be re-used by the new service. Also if no labels are specified, the new service will re-use the labels from the
resource it exposes.

Possible resources include (case insensitive):

pod (po), service (svc), replicationcontroller (rc), deployment (deploy), replicaset (rs)
......
......
Usage:
kubectl expose (-f FILENAME | TYPE NAME) [--	port=port] [--protocol=TCP|UDP] [--target-port=number-or-name]
[--name=name] [--external-ip=external-ip-of-service] [--type=type] [options]

简而言之,kubectl expose命令就是用来创建service资源对象的

3. 创建service

[root@master ~]# kubectl expose deployments nginx-deploy --name=nginx-deploy --port=80 --target-port=80 --protocol=TCP
service/nginx-deploy exposed

这样将nginx-deploy以service的形式暴露出去了

[root@master ~]# kubectl get svc -o wide
NAME           TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE       SELECTOR
nginx-deploy   ClusterIP   10.107.30.182   <none>        80/TCP         1m        run=nginx-deploy

4. 通过service访问pod

[root@master ~]# curl http://10.107.30.182


上面的IP是service的IP

三、扩缩容

1. 扩容

[root@master ~]# kubectl scale --replicas=2 deployment nginx-deploy
deployment.extensions/nginx-deploy scaled

2. 缩容

[root@master ~]# kubectl scale --replicas=1 deployment nginx-deploy
deployment.extensions/nginx-deploy scaled

四、滚动升级

[root@master ~]# kubectl set image deployment nginx-deploy nginx-deploy=nginx:latest
deployment.extensions/nginx-deploy image updated

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: