1. 程式人生 > >kubernetes 應用快速入門 部署nginx

kubernetes 應用快速入門 部署nginx

下面文章只是個人學習使用整理,不做過解釋。。。

kubectl get cs

kubectl get nodes

[[email protected] ~]# kubectl get pods -n kube-system

[[email protected] ~]# kubectl get ns

NAME STATUS AGE

default Active 24h

kube-public Active 24h

kube-system Active 24h

[[email protected] ~]# kubectl get pods -n kube-system -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE

coredns-576cbf47c7-jqcn8 1/1 Running 0 24h 10.244.0.3 k8m1 <none>

coredns-576cbf47c7-rqqvk 1/1 Running 0 24h 10.244.0.2 k8m1 <none>

etcd-k8m1 1/1 Running 0 24h 192.168.1.220 k8m1 <none>

kube-apiserver-k8m1 1/1 Running 0 24h 192.168.1.220 k8m1 <none>

kube-controller-manager-k8m1 1/1 Running 0 24h 192.168.1.220 k8m1 <none>

kube-flannel-ds-amd64-ck799 1/1 Running 1 24h 192.168.1.221 k8n1 <none>

kube-flannel-ds-amd64-nkdzg 1/1 Running 0 155m 192.168.1.205 k8n2 <none>

kube-flannel-ds-amd64-tjp27 1/1 Running 0 24h 192.168.1.220 k8m1 <none>

kube-proxy-8b7ds 1/1 Running 0 155m 192.168.1.205 k8n2 <none>

kube-proxy-dmswb 1/1 Running 0 24h 192.168.1.220 k8m1 <none>

kube-proxy-pm7wt 1/1 Running 0 24h 192.168.1.221 k8n1 <none>

kube-scheduler-k8m1 1/1 Running 0 24h 192.168.1.220 k8m1 <none>

pod, service,replicaset, deplovyment, statefulet, daemonset, job, cronjob, node

[email protected] ~]# kubectl

kubectl controls the Kubernetes cluster manager.

Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

Basic Commands (Beginner):

create Create a resource from a file or from stdin.

expose 使用 replication controller, service, deployment 或者 pod 並暴露它作為一個 新的

Kubernetes Service

run 在叢集中執行一個指定的映象

set 為 objects 設定一個指定的特徵

Basic Commands (Intermediate):

explain 檢視資源的文件

get 顯示一個或更多 resources

edit 在伺服器上編輯一個資源

delete Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:

rollout Manage the rollout of a resource

scale 為 Deployment, ReplicaSet, Replication Controller 或者 Job 設定一個新的副本數量

autoscale 自動調整一個 Deployment, ReplicaSet, 或者 ReplicationController 的副本數量

Cluster Management Commands:

certificate 修改 certificate 資源.

cluster-info 顯示叢集資訊

top Display Resource (CPU/Memory/Storage) usage.

cordon 標記 node 為 unschedulable

uncordon 標記 node 為 schedulable

drain Drain node in preparation for maintenance

taint 更新一個或者多個 node 上的 taints

Troubleshooting and Debugging Commands:

describe 顯示一個指定 resource 或者 group 的 resources 詳情

logs 輸出容器在 pod 中的日誌

attach Attach 到一個執行中的 container

exec 在一個 container 中執行一個命令

port-forward Forward one or more local ports to a pod

proxy 執行一個 proxy 到 Kubernetes API server

cp 複製 files 和 directories 到 containers 和從容器中複製 files 和 directories.

auth Inspect authorization

Advanced Commands:

apply 通過檔名或標準輸入流(stdin)對資源進行配置

patch 使用 strategic merge patch 更新一個資源的 field(s)

replace 通過 filename 或者 stdin替換一個資源

wait Experimental: Wait for a specific condition on one or many resources.

convert 在不同的 API versions 轉換配置檔案

Settings Commands:

label 更新在這個資源上的 labels

annotate 更新一個資源的註解

completion Output shell completion code for the specified shell (bash or zsh)

Other Commands:

alpha Commands for features in alpha

api-resources Print the supported API resources on the server

api-versions Print the supported API versions on the server, in the form of "group/version"

config 修改 kubeconfig 檔案

plugin Provides utilities for interacting with plugins.

version 輸出 client 和 server 的版本資訊

Usage:

kubectl [flags] [options]

Use "kubectl <command> --help" for more information about a given command.

Use "kubectl options" for a list of global command-line options (applies to all commands).

[[email protected] ~]# k8s run --help

-bash: k8s: 未找到命令

[[email protected] ~]# kubectl run --help

Create and run a particular image, possibly replicated.

Creates a deployment or job to manage the created container(s).

Examples:

# Start a single instance of nginx.

kubectl run nginx --image=nginx

# Start a single instance of hazelcast and let the container expose port 5701 .

kubectl run hazelcast --image=hazelcast --port=5701

# Start a single instance of hazelcast and set environment variables "DNS_DOMAIN=cluster" and

"POD_NAMESPACE=default" in the container.

kubectl run hazelcast --image=hazelcast --env="DNS_DOMAIN=cluster" --env="POD_NAMESPACE=default"

# Start a single instance of hazelcast and set labels "app=hazelcast" and "env=prod" in the

container.

kubectl run hazelcast --image=hazelcast --labels="app=hazelcast,env=prod"

# Start a replicated instance of nginx.

kubectl run nginx --image=nginx --replicas=5

# Dry run. Print the corresponding API objects without creating them.

kubectl run nginx --image=nginx --dry-run

# Start a single instance of nginx, but overload the spec of the deployment with a partial set of

values parsed from JSON.

kubectl run nginx --image=nginx --overrides='{ "apiVersion": "v1", "spec": { ... } }'

# Start a pod of busybox and keep it in the foreground, don't restart it if it exits.

kubectl run -i -t busybox --image=busybox --restart=Never

# Start the nginx container using the default command, but use custom arguments (arg1 .. argN) for

that command.

kubectl run nginx --image=nginx -- <arg1> <arg2> ... <argN>

# Start the nginx container using a different command and custom arguments.

kubectl run nginx --image=nginx --command -- <cmd> <arg1> ... <argN>

# Start the perl container to compute π to 2000 places and print it out.

kubectl run pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)'

# Start the cron job to compute π to 2000 places and print it out every 5 minutes.

kubectl run pi --schedule="0/5 * * * ?" --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle

'print bpi(2000)'

Options:

--allow-missing-template-keys=true: If true, ignore any errors in templates when a field or

map key is missing in the template. Only applies to golang and jsonpath output formats.

--attach=false: If true, wait for the Pod to start running, and then attach to the Pod as if

'kubectl attach ...' were called. Default false, unless '-i/--stdin' is set, in which case the

default is true. With '--restart=Never' the exit code of the container process is returned.

--cascade=true: If true, cascade the deletion of the resources managed by this resource (e.g.

Pods created by a ReplicationController). Default true.

--command=false: If true and extra arguments are present, use them as the 'command' field in

the container, rather than the 'args' field which is the default.

--dry-run=false: If true, only print the object that would be sent, without sending it.

--env=[]: Environment variables to set in the container

--expose=false: If true, a public, external service is created for the container(s) which are

run

-f, --filename=[]: to use to replace the resource.

--force=false: Only used when grace-period=0. If true, immediately remove resources from API

and bypass graceful deletion. Note that immediate deletion of some resources may result in

inconsistency or data loss and requires confirmation.

--generator='': 使用 API generator 的名字, 在

http://kubernetes.io/docs/user-guide/kubectl-conventions/#generators 檢視列表.

--grace-period=-1: Period of time in seconds given to the resource to terminate gracefully.

Ignored if negative. Set to 1 for immediate shutdown. Can only be set to 0 when --force is true

(force deletion).

--hostport=-1: The host port mapping for the container port. To demonstrate a single-machine

container.

--image='': 指定容器要執行的映象.

--image-pull-policy='': 容器的映象拉取策略. 如果為空, 這個值將不會 被

client 指定且使用 server 端的預設值

-l, --labels='': Comma separated labels to apply to the pod(s). Will override previous values.

--leave-stdin-open=false: If the pod is started in interactive mode or with stdin, leave stdin

open after the first attach completes. By default, stdin will be closed after the first attach

completes.

--limits='': The resource requirement limits for this container. For example,

'cpu=200m,memory=512Mi'. Note that server side components may assign limits depending on the server

configuration, such as limit ranges.

-o, --output='': Output format. One of:

json|yaml|name|template|go-template|go-template-file|templatefile|jsonpath|jsonpath-file.

--overrides='': An inline JSON override for the generated object. If this is non-empty, it is

used to override the generated object. Requires that the object supply a valid apiVersion field.

--pod-running-timeout=1m0s: The length of time (like 5s, 2m, or 3h, higher than zero) to wait

until at least one pod is running

--port='': The port that this container exposes. If --expose is true, this is also the port

used by the service that is created.

--quiet=false: If true, suppress prompt messages.

--record=false: Record current kubectl command in the resource annotation. If set to false, do

not record the command. If set to true, record the command. If not set, default to updating the

existing annotation value only if one already exists.

-R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you

want to manage related manifests organized within the same directory.

-r, --replicas=1: Number of replicas to create for this container. Default is 1.

--requests='': 資源為 container 請求 requests . 例如, 'cpu=100m,memory=256Mi'.

注意服務端元件也許會賦予 requests, 這決定於伺服器端配置, 比如 limit ranges.

--restart='Always': 這個 Pod 的 restart policy. Legal values [Always, OnFailure, Never].

如果設定為 'Always' 一個 deployment 被建立, 如果設定為 ’OnFailure' 一個 job

被建立, 如果設定為 'Never', 一個普通的 pod 被建立. 對於後面兩個 --replicas

必須為 1. 預設 'Always', 為 CronJobs 設定為 `Never`.

--rm=false: If true, delete resources created in this command for attached containers.

--save-config=false: If true, the configuration of current object will be saved in its

annotation. Otherwise, the annotation will be unchanged. This flag is useful when you want to

perform kubectl apply on this object in the future.

--schedule='': A schedule in the Cron format the job should be run with.

--service-generator='service/v2': 使用 gnerator 的名稱建立一個 service. 只有在

--expose 為 true 的時候使用

--service-overrides='': An inline JSON override for the generated service object. If this is

non-empty, it is used to override the generated object. Requires that the object supply a valid

apiVersion field. Only used if --expose is true.

--serviceaccount='': Service account to set in the pod spec

-i, --stdin=false: Keep stdin open on the container(s) in the pod, even if nothing is attached.

--template='': Template string or path to template file to use when -o=go-template,

-o=go-template-file. The template format is golang templates

[http://golang.org/pkg/text/template/#pkg-overview].

--timeout=0s: The length of time to wait before giving up on a delete, zero means determine a

timeout from the size of the object

-t, --tty=false: Allocated a TTY for each container in the pod.

--wait=false: If true, wait for resources to be gone before returning. This waits for

finalizers.

Usage:

kubectl run NAME --image=image [--env="key=value"] [--port=port] [--replicas=replicas]

[--dry-run=bool] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

(如果初始化叢集出現問題,可以使用下面命令進行清理進行重新初始化:kubeadm reset)

檢視監控資訊

kubectl componentstatus #kubectl get cs

[[email protected] ~]# kubectl get cs

NAME STATUS MESSAGE ERROR

controller-manager Healthy ok

scheduler Healthy ok

etcd-0 Healthy {"health": "true"}

kubectl get pods #獲取當前都pod

[[email protected] ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1

kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.

deployment.apps/nginx-deploy created

[[email protected] ~]# kubectl get deployment

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE

nginx-deploy 1 1 1 0 12s

[[email protected] ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --replicas=1

kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.

deployment.apps/nginx-deploy created

[[email protected] ~]# kubectl get deployment

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE

nginx-deploy 1 1 1 0 12s

[[email protected] ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 71s

[[email protected] ~]# kubectl pods -o wide

Error: unknown command "pods" for "kubectl"

Did you mean this?

logs

Run 'kubectl --help' for usage.

unknown command "pods" for "kubectl"

Did you mean this?

logs

[[email protected] ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 117s

[[email protected] ~]# kubectl get pods -o wide

NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 2m8s 10.244.2.3 k8node2 <none>

[[email protected] ~]# curl 10.244.2.3

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

body {

width: 35em;

margin: 0 auto;

font-family: Tahoma, Verdana, Arial, sans-serif;

}

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

<p>For online documentation and support please refer to

<a href="http://nginx.org/">nginx.org</a>.<br/>

Commercial support is available at

<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

[[email protected] ~]# 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)

Examples:

# Create a service for a replicated nginx, which serves on port 80 and connects to the containers

on port 8000.

kubectl expose rc nginx --port=80 --target-port=8000

# Create a service for a replication controller identified by type and name specified in

"nginx-controller.yaml", which serves on port 80 and connects to the containers on port 8000.

kubectl expose -f nginx-controller.yaml --port=80 --target-port=8000

# Create a service for a pod valid-pod, which serves on port 444 with the name "frontend"

kubectl expose pod valid-pod --port=444 --name=frontend

# Create a second service based on the above service, exposing the container port 8443 as port 443

with the name "nginx-https"

kubectl expose service nginx --port=443 --target-port=8443 --name=nginx-https

# Create a service for a replicated streaming application on port 4100 balancing UDP traffic and

named 'video-stream'.

kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream

# Create a service for a replicated nginx using replica set, which serves on port 80 and connects

to the containers on port 8000.

kubectl expose rs nginx --port=80 --target-port=8000

# Create a service for an nginx deployment, which serves on port 80 and connects to the containers

on port 8000.

kubectl expose deployment nginx --port=80 --target-port=8000

Options:

--allow-missing-template-keys=true: If true, ignore any errors in templates when a field or

map key is missing in the template. Only applies to golang and jsonpath output formats.

--cluster-ip='': ClusterIP to be assigned to the service. Leave empty to auto-allocate, or set

to 'None' to create a headless service.

--dry-run=false: If true, only print the object that would be sent, without sending it.

--external-ip='': Additional external IP address (not managed by Kubernetes) to accept for the

service. If this IP is routed to a node, the service can be accessed by this IP in addition to its

generated service IP.

-f, --filename=[]: Filename, directory, or URL to files identifying the resource to expose a

service

--generator='service/v2': 使用 generator 的名稱. 這裡有 2 個 generators: 'service/v1'

和 'service/v2'. 為一個不同地方是服務埠在 v1 的情況下叫 'default', 如果在 v2

中沒有指定名稱. 預設的名稱是 'service/v2'.

-l, --labels='': Labels to apply to the service created by this call.

--load-balancer-ip='': IP to assign to the LoadBalancer. If empty, an ephemeral IP will be

created and used (cloud-provider specific).

--name='': 名稱為最新建立的物件.

-o, --output='': Output format. One of:

json|yaml|name|go-template-file|templatefile|template|go-template|jsonpath|jsonpath-file.

--overrides='': An inline JSON override for the generated object. If this is non-empty, it is

used to override the generated object. Requires that the object supply a valid apiVersion field.

--port='': 服務的埠應該被指定. 如果沒有指定, 從被建立的資源中複製

--protocol='': 建立 service 的時候伴隨著一個網路協議被建立. 預設是 'TCP'.

--record=false: Record current kubectl command in the resource annotation. If set to false, do

not record the command. If set to true, record the command. If not set, default to updating the

existing annotation value only if one already exists.

-R, --recursive=false: Process the directory used in -f, --filename recursively. Useful when you

want to manage related manifests organized within the same directory.

--save-config=false: If true, the configuration of current object will be saved in its

annotation. Otherwise, the annotation will be unchanged. This flag is useful when you want to

perform kubectl apply on this object in the future.

--selector='': A label selector to use for this service. Only equality-based selector

requirements are supported. If empty (the default) infer the selector from the replication

controller or replica set.)

--session-affinity='': If non-empty, set the session affinity for the service to this; legal

values: 'None', 'ClientIP'

--target-port='': Name or number for the port on the container that the service should direct

traffic to. Optional.

--template='': Template string or path to template file to use when -o=go-template,

-o=go-template-file. The template format is golang templates

[http://golang.org/pkg/text/template/#pkg-overview].

--type='': Type for this service: ClusterIP, NodePort, LoadBalancer, or ExternalName. Default

is 'ClusterIP'.

Usage:

kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP]

[--target-port=number-or-name] [--name=name] [--external-ip=external-ip-of-service] [--type=type]

[options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

[[email protected] ~]#

暴露埠 ip 舉例:

[[email protected] ~]# kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP

service/nginx exposed

[[email protected] ~]# kubectl get svc

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 84m

nginx ClusterIP 10.105.147.135 <none> 80/TCP 19s

訪問試試:

[email protected] ~]# kubectl get svc

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 84m

nginx ClusterIP 10.105.147.135 <none> 80/TCP 19s

[[email protected] ~]# curl 10.105.147.135

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

body {

width: 35em;

margin: 0 auto;

font-family: Tahoma, Verdana, Arial, sans-serif;

}

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

<p>For online documentation and support please refer to

<a href="http://nginx.org/">nginx.org</a>.<br/>

Commercial support is available at

<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

[[email protected] ~]# kubectl get svc -n kube-system

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP 87m

[[email protected] ~]# yum install bind-utils -y

[[email protected] ~]# kubectl run client --image=busybox --replicas=1 -it --restart=Never

[[email protected] ~]# kubectl run client --image=busybox --replicas=1 -it --restart=Never

If you don't see a command prompt, try pressing enter.

另外開啟終端檢視:

[[email protected] ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

client 1/1 Running 0 2m24s

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 40m

[[email protected] ~]#

/ #

/ # cat /etc/resolv.conf

nameserver 10.96.0.10

search default.svc.cluster.local svc.cluster.local cluster.local

options ndots:5

[email protected] ~]# dig -t A nginx.default.svc.cluster.local @10.96.0.1

; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7_5.1 <<>> -t A nginx.default.svc.cluster.local @10.96.0.1

/ # wget nginx

Connecting to nginx (10.105.147.135:80)

index.html 100% |******************************************************************| 612 0:00:00 ETA

/ # wget -O - -q http://nginx:80/

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

body {

width: 35em;

margin: 0 auto;

font-family: Tahoma, Verdana, Arial, sans-serif;

}

</style>

</head>

<body>

<h1>Welcome to nginx!</h1>

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

<p>For online documentation and support please refer to

<a href="http://nginx.org/">nginx.org</a>.<br/>

Commercial support is available at

<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

/ #

[[email protected] ~]# kubectl get svc

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 109m

nginx ClusterIP 10.105.147.135 <none> 80/TCP 25m

[[email protected] ~]# kubectl describe svc nginx

Name: nginx

Namespace: default

Labels: run=nginx-deploy

Annotations: <none>

Selector: run=nginx-deploy

Type: ClusterIP

IP: 10.105.147.135

Port: <unset> 80/TCP

TargetPort: 80/TCP

Endpoints: 10.244.2.3:80

Session Affinity: None

Events: <none>

[[email protected] ~]#

[[email protected] ~]# kubectl get pods --show-labels

NAME READY STATUS RESTARTS AGE LABELS

client 1/1 Running 0 17m run=client

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 54m pod-template-hash=86bf78c77,run=nginx-deploy

怎麼修改nginx對外ip地址呢:

[[email protected] ~]# kubectl describe deployment nginx-deploy

Name: nginx-deploy

Namespace: default

CreationTimestamp: Tue, 13 Nov 2018 23:53:09 +0800

Labels: run=nginx-deploy

Annotations: deployment.kubernetes.io/revision: 1

Selector: run=nginx-deploy

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=nginx-deploy

Containers:

nginx-deploy:

Image: nginx:1.14-alpine

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: nginx-deploy-86bf78c77 (1/1 replicas created)

Events: <none>

[email protected] ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2

kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.

[[email protected] ~]# kubectl get deployment -w

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE

myapp 2 2 2 1 42s

nginx-deploy 1 1 1 1 64m

myapp 2 2 2 2 43s

部署多個:

[[email protected] ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2

kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.

deployment.apps/myapp created

[[email protected] ~]# kubectl get deployment -w

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE

myapp 2 2 2 1 42s

nginx-deploy 1 1 1 1 64m

myapp 2 2 2 2 43s

^C[[email protected] ~]# kubectl expose deployment myapp --name=myapp --port=88

service/myapp exposed

[[email protected] ~]# kubectl get svc

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 124m

myapp ClusterIP 10.99.69.37 <none> 88/TCP 9s

nginx ClusterIP 10.105.147.135 <none> 80/TCP 39m

擴充套件:kubectl scale --replicas=3 ......

[email protected] ~]# kubectl scale --replicas=5 deployment myapp

deployment.extensions/myapp scaled

[[email protected] ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

client 1/1 Running 0 34m

myapp-6946649ccd-2nrn9 1/1 Running 0 14s

myapp-6946649ccd-4xjrs 1/1 Running 0 14s

myapp-6946649ccd-5ccwr 1/1 Running 0 8m7s

myapp-6946649ccd-7gzbj 1/1 Running 0 14s

myapp-6946649ccd-vw9dv 1/1 Running 0 8m7s

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 71m

縮減:

[[email protected] ~]# kubectl scale --replicas=3 deployment myapp

deployment.extensions/myapp scaled

[[email protected] ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

client 1/1 Running 0 35m

myapp-6946649ccd-2nrn9 0/1 Terminating 0 103s

myapp-6946649ccd-4xjrs 1/1 Running 0 103s

myapp-6946649ccd-5ccwr 1/1 Running 0 9m36s

myapp-6946649ccd-7gzbj 0/1 Terminating 0 103s

myapp-6946649ccd-vw9dv 1/1 Running 0 9m36s

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 73m

[[email protected] ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

client 1/1 Running 0 35m

myapp-6946649ccd-4xjrs 1/1 Running 0 112s

myapp-6946649ccd-5ccwr 1/1 Running 0 9m45s

myapp-6946649ccd-vw9dv 1/1 Running 0 9m45s

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 73m

[[email protected] ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

client 1/1 Running 0 35m

myapp-6946649ccd-4xjrs 1/1 Running 0 2m2s

myapp-6946649ccd-5ccwr 1/1 Running 0 9m55s

myapp-6946649ccd-vw9dv 1/1 Running 0 9m55s

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 73m

更新升級,改變映象版本,

[[email protected] ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2

deployment.extensions/myapp image updated

[[email protected] ~]# kubectl rollout status deployment myapp 顯示更新過程

[[email protected] ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

client 1/1 Running 0 41m

myapp-867c8bf765-5twnb 1/1 Running 0 2m25s

myapp-867c8bf765-7r2m7 1/1 Running 0 119s

myapp-867c8bf765-sbz9f 1/1 Running 0 2m1s

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 79m

回滾:rollout undo

[[email protected] ~]# kubectl get pods

NAME READY STATUS RESTARTS AGE

client 1/1 Running 0 43m

myapp-6946649ccd-t8mmn 1/1 Running 0 16s

myapp-6946649ccd-tgqp6 1/1 Running 0 13s

myapp-6946649ccd-zhpnc 1/1 Running 0 15s

nginx-deploy-86bf78c77-j7wqm 1/1 Running 0 81m

[[email protected] ~]# iptables -vnL

Chain INPUT (policy ACCEPT 1412 packets, 316K bytes)

pkts bytes target prot opt in out source destination

3648 226K KUBE-EXTERNAL-SERVICES all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate NEW /* kubernetes externally-visible service portals */

866K 211M KUBE-FIREWALL all -- * * 0.0.0.0/0 0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

2 245 KUBE-FORWARD all -- * * 0.0.0.0/0 0.0.0.0/0 /* kubernetes forwarding rules */

1 85 DOCKER-USER all -- * * 0.0.0.0/0 0.0.0.0/0

1 85 DOCKER-ISOLATION-STAGE-1 all -- * * 0.0.0.0/0 0.0.0.0/0

0 0 ACCEPT all -- * docker0 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED

0 0 DOCKER all -- * docker0 0.0.0.0/0 0.0.0.0/0

0 0 ACCEPT all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0

0 0 ACCEPT all -- docker0 docker0 0.0.0.0/0 0.0.0.0/0

1 85 ACCEPT all -- * * 10.244.0.0/16 0.0.0.0/0

0 0 ACCEPT all -- * * 0.0.0.0/0 10.244.0.0/16

Chain OUTPUT (policy ACCEPT 1426 packets, 372K bytes)

pkts bytes target prot opt in out source destination

4244 259K KUBE-SERVICES all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate NEW /* kubernetes service portals */

867K 245M KUBE-FIREWALL all -- * * 0.0.0.0/0 0.0.0.0/0

Chain DOCKER (1 references)

pkts bytes target prot opt in out source destination

Chain DOCKER-ISOLATION-STAGE-1 (1 references)

pkts bytes target prot opt in out source destination

0 0 DOCKER-ISOLATION-STAGE-2 all -- docker0 !docker0 0.0.0.0/0 0.0.0.0/0

1 85 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0

Chain DOCKER-ISOLATION-STAGE-2 (1 references)

pkts bytes target prot opt in out source destination

0 0 DROP all -- * docker0 0.0.0.0/0 0.0.0.0/0

0 0 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0

Chain DOCKER-USER (1 references)

pkts bytes target prot opt in out source destination

1 85 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0

Chain KUBE-EXTERNAL-SERVICES (1 references)

pkts bytes target prot opt in out source destination

Chain KUBE-FIREWALL (2 references)

pkts bytes target prot opt in out source destination

0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 /* kubernetes firewall for dropping marked packets */ mark match 0x8000/0x8000

Chain KUBE-FORWARD (1 references)

pkts bytes target prot opt in out source destination

0 0 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 /* kubernetes forwarding rules */ mark match 0x4000/0x4000

0 0 ACCEPT all -- * * 10.244.0.0/16 0.0.0.0/0 /* kubernetes forwarding conntrack pod source rule */ ctstate RELATED,ESTABLISHED

0 0 ACCEPT all -- * * 0.0.0.0/0 10.244.0.0/16 /* kubernetes forwarding conntrack pod destination rule */ ctstate RELATED,ESTABLISHED

Chain KUBE-SERVICES (1 references)

pkts bytes target prot opt in out source destination

[[email protected] ~]# iptables -vnL -t nat

[[email protected] ~]# kubectl edit svc myapp

# Plese edit the object below. Lines beginning with a '#' will be ignored,

# and an empty file will abort the edit. If an error occurs while saving this file will be

# reopened with the relevant failures.

#

apiVersion: v1

kind: Service

metadata:

creationTimestamp: 2018-11-13T17:00:31Z

labels:

run: myapp

name: myapp

namespace: default

resourceVersion: "10887"

selfLink: /api/v1/namespaces/default/services/myapp

uid: a1163e03-e765-11e8-9c0c-000c29a474fa

spec:

clusterIP: 10.99.69.37

ports: