1、常用指令如下
运行应用程序
[root@manager ~]# kubectl run hello-world --replicas=3 --labels="app=example" --image=nginx:1.10 --port=80
[root@manager ~]# kubectl get pods --selector="app=example" NAME READY STATUS RESTARTS AGE hello-world-cc85d4fb6-btsvz 0/1 ContainerCreating 0 33s hello-world-cc85d4fb6-mtg75 0/1 ContainerCreating 0 33s hello-world-cc85d4fb6-r57vx 0/1 ContainerCreating 0 33s [root@manager ~]# [root@manager ~]# kubectl describe pod hello-world-cc85d4fb6-btsvz Name: hello-world-cc85d4fb6-btsvz Namespace: default Node: 192.168.10.221/192.168.10.221 Start Time: Fri, 02 Feb 2018 10:13:01 +0800 Labels: app=example pod-template-hash=774180962 Annotations: kubernetes.io/created-by={"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicaSet","namespace":"default","name":"hello-world-cc85d4fb6","uid":"98254448-07be-11e8-af8c-5254002bf2... Status: Pending IP: Created By: ReplicaSet/hello-world-cc85d4fb6 Controlled By: ReplicaSet/hello-world-cc85d4fb6 Containers: hello-world: Container ID: Image: nginx:1.10 Image ID: Port: 80/TCP State: Waiting Reason: ContainerCreating Ready: False Restart Count: 0 Environment: <none> Mounts: <none> Conditions: Type Status Initialized True Ready False PodScheduled True Volumes: <none> QoS Class: BestEffort Node-Selectors: <none> Tolerations: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 1m default-scheduler Successfully assigned hello-world-cc85d4fb6-btsvz to 192.168.10.221 Normal Pulling 1m kubelet, 192.168.10.221 pulling image "nginx:1.10" Normal Pulled 6s kubelet, 192.168.10.221 Successfully pulled image "nginx:1.10"
查看pod所属标签 [root@manager ~]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS busybox 1/1 Running 47 1d <none> busybox2 1/1 Running 5 1d <none> hello-world-cc85d4fb6-btsvz 1/1 Running 0 2m app=example,pod-template-hash=774180962 hello-world-cc85d4fb6-mtg75 1/1 Running 0 2m app=example,pod-template-hash=774180962 hello-world-cc85d4fb6-r57vx 1/1 Running 0 2m app=example,pod-template-hash=774180962
根据标签查看pod
[root@manager ~]# kubectl get pods -l app=example -o wide
NAME READY STATUS RESTARTS AGE IP NODE
hello-world-cc85d4fb6-btsvz 1/1 Running 0 3m 10.0.91.5 192.168.10.221
hello-world-cc85d4fb6-mtg75 1/1 Running 0 3m 10.0.71.8 192.168.10.222
hello-world-cc85d4fb6-r57vx 1/1 Running 0 3m 10.0.91.6 192.168.10.221
显示有关deployment信息
[root@manager ~]# kubectl get deployments hello-world
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
hello-world 3 3 3 3 3m
[root@manager ~]#
[root@manager ~]# kubectl describe deployments hello-world
Name: hello-world
Namespace: default
CreationTimestamp: Fri, 02 Feb 2018 10:13:01 +0800
Labels: app=example
Annotations: deployment.kubernetes.io/revision=1
Selector: app=example
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 1 max unavailable, 1 max surge
Pod Template:
Labels: app=example
Containers:
hello-world:
Image: nginx:1.10
Port: 80/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: hello-world-cc85d4fb6 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 4m deployment-controller Scaled up replica set hello-world-cc85d4fb6 to 3
显示有关rs信息
[root@manager ~]# kubectl get replicasets
NAME DESIRED CURRENT READY AGE
hello-world-cc85d4fb6 3 3 3 4m
[root@manager ~]#
[root@manager ~]# kubectl describe replicasets
Name: hello-world-cc85d4fb6
Namespace: default
Selector: app=example,pod-template-hash=774180962
Labels: app=example
pod-template-hash=774180962
Annotations: deployment.kubernetes.io/desired-replicas=3
deployment.kubernetes.io/max-replicas=4
deployment.kubernetes.io/revision=1
Controlled By: Deployment/hello-world
Replicas: 3 current / 3 desired
Pods Status: 3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=example
pod-template-hash=774180962
Containers:
hello-world:
Image: nginx:1.10
Port: 80/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 4m replicaset-controller Created pod: hello-world-cc85d4fb6-btsvz
Normal SuccessfulCreate 4m replicaset-controller Created pod: hello-world-cc85d4fb6-mtg75
Normal SuccessfulCreate 4m replicaset-controller Created pod: hello-world-cc85d4fb6-r57vx
扩容pod数量
[root@manager ~]# kubectl scale deployment --replicas=5 hello-world
deployment "hello-world" scaled
[root@manager ~]#
[root@manager ~]# kubectl get pods --selector="app=example" -o wide
NAME READY STATUS RESTARTS AGE IP NODE
hello-world-cc85d4fb6-btsvz 1/1 Running 0 5m 10.0.91.5 192.168.10.221
hello-world-cc85d4fb6-mppx2 1/1 Running 0 17s 10.0.71.9 192.168.10.222
hello-world-cc85d4fb6-mtg75 1/1 Running 0 5m 10.0.71.8 192.168.10.222
hello-world-cc85d4fb6-r57vx 1/1 Running 0 5m 10.0.91.6 192.168.10.221
hello-world-cc85d4fb6-xgv4z 1/1 Running 0 17s 10.0.91.7 192.168.10.221
创建一个Service对象暴露Deployment(在88端口负载TCP流量) [root@manager ~]# kubectl expose deployment hello-world --port=88 --type=NodePort --target-port=80 --name=example-service service "example-service" exposed [root@manager ~]# [root@manager ~]# kubectl describe services example-service Name: example-service Namespace: default Labels: app=example Annotations: <none> Selector: app=example Type: NodePort IP: 10.10.10.11 Port: <unset> 88/TCP TargetPort: 80/TCP NodePort: <unset> 31916/TCP Endpoints: 10.0.71.8:80,10.0.71.9:80,10.0.91.5:80 + 2 more... Session Affinity: None External Traffic Policy: Cluster Events: <none> 使用节点IP和节点端口访问应用程序 [root@node1 ~]# curl 10.10.10.11:88 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { 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>
清理应用
kubectl delete services example-service
kubectl delete deployment hello-world
[root@manager ~]# kubectl get all NAME READY STATUS RESTARTS AGE po/busybox 1/1 Running 47 1d po/busybox2 1/1 Running 5 1d NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc/kubernetes ClusterIP 10.10.10.1 <none> 443/TCP 2d
yaml配置文件管理资源
配置文件说明:
定义配置时,指定最新稳定版API(当前为v1);
配置文件应该存储在集群之外的版本控制仓库中。如果需要,可以快速回滚配置、重新创建和恢复;
应该使用YAML格式编写配置文件,而不是JSON。尽管这些格式都可以使用,但YAML对用户更加友好;
可以将相关对象组合成单个文件,通常会更容易管理;
不要没必要的指定默认值,简单和最小配置减少错误;
在注释中说明一个对象描述更好维护。
[root@manager ~]# [root@manager ~]# cat nginx-deployment.yaml apiVersion: apps/v1beta2 kind: Deployment metadata: name: nginx-deployment spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.10 ports: - containerPort: 80 [root@manager ~]# kubectl create -f nginx-deployment.yaml deployment "nginx-deployment" created
[root@manager ~]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
ports:
- port: 88
targetPort: 80
selector:
app: nginx
[root@manager ~]#
[root@manager ~]# kubectl create -f nginx-service.yaml
service "nginx-service" created
[root@manager ~]#
[root@manager ~]#
[root@manager ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.10.10.1 <none> 443/TCP 2d
nginx-service ClusterIP 10.10.10.243 <none> 88/TCP 9s
[root@manager ~]# kubectl describe services nginx-service
Name: nginx-service
Namespace: default
Labels: app=nginx
Annotations: <none>
Selector: app=nginx
Type: ClusterIP
IP: 10.10.10.243
Port: <unset> 88/TCP
TargetPort: 80/TCP
Endpoints: 10.0.71.8:80,10.0.71.9:80,10.0.91.5:80
Session Affinity: None
Events: <none>