[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对象的
[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
[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资源对象的
[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 80/TCP 1m run=nginx-deploy
[root@master ~]# curl http://10.107.30.182
[root@master ~]# kubectl scale --replicas=2 deployment nginx-deploy
deployment.extensions/nginx-deploy scaled
[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