查看集群有多少节点

[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 2h v1.10.0
node1 Ready 1h v1.10.0
node2 Ready 1h v1.10.0

kubectl get nodes
kubectl get cs
kubectl get pods
kubectl get pods --all-namespaces 
kubectl get namespaces  或 ns
kubectl get pods
kubectl get pods --all-namespaces 
kubectl get pods --namespace kube-public
kubectl get pods --namespace=kube-public
kubectl get pods --namespace kube-public
kubectl get pods --namespace kube-system
查看版本

[root@master ~]# kubectl api-versions

kubectl get 查看某个类型的资源
kubectl describe 查看特定的资源显示详细信息
kubectl logs 查看某个pod的日志
kubectl exec 在容器内执行命令
kubectl scale 实现水平扩展或伸缩
kubectl rollout status 部署状态变更检查
kubectl rollout history 部署历史
Kubectl rollout undo 回滚部署到最近的某个版本
kubectl set images 升级某个deployment的image到新的版本
kubectl delete 删除某个资源
[root@master ~]# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
default httpd-app-77c9c8f99f-jtdd2 1/1 Running 0 1h
default httpd-app-77c9c8f99f-r5sfz 1/1 Running 0 1h
kube-system etcd-master 1/1 Running 1 2h
kube-system kube-apiserver-master 1/1 Running 0 2h
kube-system kube-controller-manager-master 1/1 Running 3 2h
kube-system kube-dns-86f4d74b45-2rhdr 3/3 Running 35 2h
kube-system kube-flannel-ds-h8sdj 1/1 Running 2 1h
kube-system kube-flannel-ds-x5mnm 1/1 Running 1 2h
kube-system kube-flannel-ds-xrzgz 1/1 Running 0 1h
kube-system kube-proxy-4kclc 1/1 Running 1 2h
kube-system kube-proxy-bc772 1/1 Running 0 1h
kube-system kube-proxy-gzt4c 1/1 Running 0 1h
kube-system kube-scheduler-master 1/1 Running 3 2h
kube-system kubernetes-dashboard-7d5dcdb6d9-pz6gl 1/1 Running
查看集群信息

[root@master ~]# kubectl cluster-info

Kubernetes master is running at https://192.168.0.200:6443
KubeDNS is running at https://192.168.0.200:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
查看组件的信息

[root@master ~]# kubectl get cs

NAME STATUS MESSAGE ERROR
controller-manager Healthy ok
scheduler Healthy ok
etcd-0 Healthy {"health": "true"}
创建pod ,deployment为2个

[root@master ~]# kubectl run httpd-app --image=httpd --replicas=2

查看httpd-app的deployment是否为2个

[root@master ~]# kubectl get deployment

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
httpd-app 2 2 2 2 1h
查看启动的pod信息

[root@master ~]# kubectl get pod

NAME READY STATUS RESTARTS AGE
httpd-app-77c9c8f99f-jtdd2 1/1 Running 0 1h
httpd-app-77c9c8f99f-r5sfz 1/1 Running 0 1h
查看更详细的信息,包含运行的node和ip地址

[root@master ~]# kubectl get pod -o wide

NAME READY STATUS RESTARTS AGE IP NODE
httpd-app-77c9c8f99f-jtdd2 1/1 Running 0 1h 10.244.2.2 node2
httpd-app-77c9c8f99f-r5sfz 1/1 Running 0 1h 10.244.1.2 node1
修改配置

[root@master tmp (⎈ |kubernetes-admin@kubernetes:default)]# kubectl edit deployment nginx-app
打开后修改参数,保存后即生效

[root@master ~]# kubectl get namespace

NAME STATUS AGE
default Active 2h
kube-public Active 2h
kube-system Active 2h
创建名称空间

[root@master ~]# kubectl create namespace demotest
namespace "demotest" created
[root@master ~]# kubectl get namespace

NAME STATUS AGE
default Active 2h
demotest Active 3s
kube-public Active 2h
kube-system Active 2h
在该空间内创建pod

[root@master ~]# kubectl run httpd-app --image=httpd --replicas=1 --namespace=demotest
deployment.apps "httpd-app" created

查看
[root@master ~]# kubectl get pod --namespace=demotest

NAME READY STATUS RESTARTS AGE
httpd-app-77c9c8f99f-fq6ks 0/1 ContainerCreating 0 53s

负载均衡

[root@master ~]# kubectl run httpd-app --image=httpd --replicas=2 --port=80

[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpd-app-586f898c8-4j2np 1/1 Running 0 5m
httpd-app-586f898c8-psvbj 1/1 Running 0 5m
[root@master ~]# kubectl get pod --namespace=default
NAME READY STATUS RESTARTS AGE
NAME READY STATUS RESTARTS AGE
httpd-app-586f898c8-4j2np 1/1 Running 0 5m
httpd-app-586f898c8-psvbj 1/1 Running 0 5m
[root@master ~]# kubectl expose deployment/httpd-app --type="ClusterIP" --port=80
service "httpd-app" exposed
[root@master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd-app ClusterIP 10.111.37.22 <none> 80/TCP 27s
[root@master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
httpd-app 10.244.1.2:80,10.244.2.2:80 1m
[root@master ~]# kubectl exec -it httpd-app-586f898c8-4j2np /bin/bash
root@httpd-app-586f898c8-4j2np:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@httpd-app-586f898c8-4j2np:/usr/local/apache2# cd htdocs/
root@httpd-app-586f898c8-4j2np:/usr/local/apache2/htdocs# cat index.html
<html><body><h1>It works!</h1></body></html>
root@httpd-app-586f898c8-4j2np:/usr/local/apache2/htdocs# echo "test1" > index.html
root@httpd-app-586f898c8-4j2np:/usr/local/apache2/htdocs# cat index.html
test1
[root@master ~]# kubectl exec -it httpd-app-586f898c8-psvbj /bin/bash
root@httpd-app-586f898c8-psvbj:/usr/local/apache2# cd htdocs/
root@httpd-app-586f898c8-psvbj:/usr/local/apache2/htdocs# ls
index.html
root@httpd-app-586f898c8-psvbj:/usr/local/apache2/htdocs# cat index.html
<html><body><h1>It works!</h1></body></html>
root@httpd-app-586f898c8-psvbj:/usr/local/apache2/htdocs# echo "test2" > index.html
root@httpd-app-586f898c8-psvbj:/usr/local/apache2/htdocs# cat index.html
test2

[root@master ~]# curl 10.98.236.206
test1
[root@master ~]# curl 10.98.236.206
test1
[root@master ~]# curl 10.98.236.206
test2
[root@master ~]# curl 10.98.236.206
test2

NodePort nodePort是kubernetes提供给集群外部客户访问service入口的一种方式(另一种方式是LoadBalancer)

[root@master ~]# kubectl delete service httpd-app
service "httpd-app" deleted
[root@master ~]# kubectl expose deployment/httpd-app --type="NodePort" --port=80
service "httpd-app" exposed

[root@master ~]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd-app NodePort 10.105.176.97 80:30232/TCP 16s

[root@master ~]# curl 10.105.176.97
test1
[root@master ~]# curl 10.105.176.97
test1
[root@master ~]# curl 10.105.176.97
test2
[root@master ~]# curl 10.105.176.97
test2

[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
httpd-app-586f898c8-4j2np 1/1 Running 0 12m 10.244.2.10 node01
httpd-app-586f898c8-psvbj 1/1 Running 0 12m 10.244.1.7 node02

[root@master ~]# curl 10.244.2.10
test1
[root@master ~]# curl 10.244.1.7
test2

[root@master ~]# kubectl get service

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
httpd-app NodePort 10.101.193.241 <none> 80:30367/TCP 3h
[root@master ~]# curl node01:30367
<html><body><h1>It works!</h1></body></html>    

kubenetes的弹性伸缩

[root@master ~]# kubectl scale deployment/httpd-app --replicas=3
deployment "httpd-app" scaled

[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
httpd-app-586f898c8-4j2np 1/1 Running 0 15m
httpd-app-586f898c8-mhvlk 1/1 Running 0 12s
httpd-app-586f898c8-psvbj 1/1 Running 0 15m

[root@master ~]# kubectl scale deployment/httpd-app --replicas=5

[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
httpd-app-586f898c8-4j2np 1/1 Running 0 16m
httpd-app-586f898c8-gn2jg 1/1 Running 0 10s
httpd-app-586f898c8-mhvlk 1/1 Running 0 1m
httpd-app-586f898c8-psvbj 1/1 Running 0 16m
httpd-app-586f898c8-zm8zx 1/1 Running 0 10s

[root@master ~]# kubectl scale deployment/httpd-app --replicas=2

[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
httpd-app-586f898c8-4j2np 1/1 Running 0 17m
httpd-app-586f898c8-psvbj 1/1 Running 0 17m

更具deployment的伸缩,service访问也会跟随deployment变化,kubernetes有一个hpa,自动伸缩

lable
[root@master ~]# kubectl get node --show-labels
NAME STATUS ROLES AGE VERSION LABELS
master Ready master 1d v1.9.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=master,node-role.kubernetes.io/master=
node01 Ready <none> 1d v1.9.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node01
node02 NotReady <none> 1d v1.9.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node02

[root@master ~]# kubectl label node node1 disktype=ssd
node "node01" labeled

[root@master ~]# kubectl get node node1 --show-labels
NAME STATUS ROLES AGE VERSION LABELS
node01 Ready <none> 1d v1.9.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/hostname=node01

kubectl get node -l "disktype=ssd" 根据标签筛选

[root@master ~]# kubectl label node node1 disktype-
node "node1" labeled

[root@master ~]# kubectl get node node1 --show-labels
NAME STATUS ROLES AGE VERSION LABELS
node1 Ready <none> 2h v1.10.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=node1
更新及回滚

[root@master ~]# kubectl run nginx --image=nginx:1.7.5 --replicas=4

[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-7d4bfb4d9d-lbg2s 1/1 Running 0 53s 10.244.1.46 node02
nginx-7d4bfb4d9d-rt6b6 1/1 Running 0 53s 10.244.2.42 node01
nginx-7d4bfb4d9d-tqfgz 1/1 Running 0 53s 10.244.2.43 node01
nginx-7d4bfb4d9d-vxlmh 1/1 Running 0 53s 10.244.1.45 node02

[root@master ~]# kubectl exec -it nginx-7d4bfb4d9d-tqfgz /bin/bash

root@nginx-7d4bfb4d9d-tqfgz:/# nginx -v
nginx version: nginx/1.7.5

[root@master ~]# kubectl set image deployment/nginx nginx=nginx:1.7.9
deployment "nginx" image updated

[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-6f8cf9fbc4-4w5cd 1/1 Running 0 7s 10.244.1.48 node02
nginx-6f8cf9fbc4-k9gz6 1/1 Running 0 32s 10.244.2.44 node01
nginx-6f8cf9fbc4-ncf6l 1/1 Running 0 32s 10.244.1.47 node02
nginx-6f8cf9fbc4-v5txl 1/1 Running 0 6s 10.244.2.45 node01

[root@master ~]# kubectl exec -it nginx-6f8cf9fbc4-4w5cd /bin/bash
root@nginx-6f8cf9fbc4-4w5cd:/# nginx -v
nginx version: nginx/1.7.9

[root@master ~]# kubectl rollout history deployment/nginx
deployments "nginx"
REVISION CHANGE-CAUSE
1 <none>
2 <none>

[root@master ~]# kubectl rollout undo deployment/nginx
deployment "nginx"

kubectl rollout undo deployment/nginx-test --to-revision=2

[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx-7d4bfb4d9d-9hd57 1/1 Running 0 17s 10.244.2.46 node01
nginx-7d4bfb4d9d-d9xjj 1/1 Running 0 15s 10.244.2.47 node01
nginx-7d4bfb4d9d-vkczl 1/1 Running 0 17s 10.244.1.49 node02
nginx-7d4bfb4d9d-z67gp 1/1 Running 0 15s 10.244.1.50 node02

[root@master ~]# kubectl exec -it nginx-7d4bfb4d9d-9hd57 /bin/bash
root@nginx-7d4bfb4d9d-9hd57:/# nginx -v
nginx version: nginx/1.7.5

使用yaml文件

[root@master ~]# kubectl get deployment bootcamp -o yaml ### 可以将你用命令行创建的资源以yaml的文件格式导出
[root@master ~]# kubectl get service httpd-app -o yaml > service.yaml

[root@master ~]# kubectl delete service httpd-app

[root@master ~]# kubectl create -f service.yaml
service "httpd-app" created

pod自动迁移

[root@master ~]# kubectl run httpd-app --image=httpd --replicas=4
deployment "httpd-app" created

[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
httpd-app-5fbccd7c6c-5vzqm 1/1 Running 0 37s 10.244.2.54 node01
httpd-app-5fbccd7c6c-jwlp5 1/1 Running 0 37s 10.244.2.55 node01
httpd-app-5fbccd7c6c-lkbq5 1/1 Running 0 37s 10.244.1.29 node02
httpd-app-5fbccd7c6c-xs2nv 1/1 Running 0 37s 10.244.1.30 node02

在node节点测试,关闭一台node节点
[root@node02 ~]# poweroff

[root@master ~]# watch -n1 kubectl get pod -o wide
虚拟环境比较慢,需要等一段时间会实现自动迁移