kubernetes usage

K8S 用法简要教程

概念简要释义

  • pod/po kubernetes集群中的容器,对应docker中的container

  • service kubernetes集群中对外公开的服务

  • replicaset 副本集,即期望部署的资源份数,对于集群内自动负载及伸缩

  • replicacontroller 副本控制器(早期版本),现在建议使用replicaset

  • deployment A Deployment controller provides declarative updates for Pods and ReplicaSets (为po和replica提供声明式更新,推荐)

  • namespace 命名空间(用于环境隔离)

  • secret 集群服务分发用途的安全密钥(如用户名密码、ssh key、认证、其他不想被别人看到的数据)

  • volume 容器挂载的本地目录及共享目录

常用配置

namespace

1
2
3
4
5
6
apiVersion: v1
kind: Namespace
metadata:
name: cela
labels:
name: cela

deployment & service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
apiVersion: apps/v1
kind: Deployment
metadata:
name: gateway-app-test
namespace: cela
labels:
app: gateway-app-test
spec:
replicas: 1
selector:
matchLabels:
app: gateway-app-test
template:
metadata:
labels:
app: gateway-app-test
spec:
nodeName: cela-k8s-node-01
containers:
- name: gateway-app-test
image: hub.facemeta.cn:88/cela/service-gateway
imagePullPolicy: IfNotPresent
env:
- name: JVM_PARAMS
value: ""
- name: APP_PARAMS
value: "test"
ports:
- containerPort: 8091
hostAliases:
- ip: 10.25.10.161
hostnames:
- odp-test-1

---

apiVersion: v1
kind: Service
metadata:
name: gateway-service-test
namespace: cela
labels:
app: gateway-service-test
spec:
type: NodePort
ports:
- port: 8091
nodePort: 8091
selector:
app: gateway-app-test

获取集群所有的节点

1
2
3
4
5
6
[root@cela-k8s-master-01 cela]# kubectl get node 
NAME STATUS ROLES AGE VERSION
cela-k8s-master-01 Ready master 2d3h v1.13.4
cela-k8s-master-02 Ready master 2d3h v1.13.4
cela-k8s-node-01 Ready <none> 2d2h v1.13.4
cela-k8s-node-02 Ready <none> 2d2h v1.13.4

支持 -o wide参数

查看K8S中的…

1
2
3
[root@cela-k8s-master-01 cela]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 20.18.0.1 <none> 443/TCP 2d3h
  • -n 命令空间,所有则为 –all-namespaces
  • -o wide 更多输出

其他

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@cela-k8s-master-01 cela]# kubectl get po -n cela
NAME READY STATUS RESTARTS AGE
cas-app-test-74cb4f69dc-5qz6h 1/1 Running 0 4h53m
gateway-app-test-5b8f445667-b7wtq 1/1 Running 0 4h58m
manager-app-test-7bc97bdb6-cfqzw 1/1 Running 0 4h
service-app-test-5dd98f65dd-5jfwg 1/1 Running 1 4h55m
[root@cela-k8s-master-01 cela]# kubectl get replicaset -n cela
NAME DESIRED CURRENT READY AGE
cas-app-test-74cb4f69dc 1 1 1 4h49m
gateway-app-test-5b8f445667 1 1 1 4h54m
manager-app-test-6f6496cc9f 0 0 0 4h5m
manager-app-test-7565bdd45c 0 0 0 3h58m
manager-app-test-7bc97bdb6 1 1 1 4h1m
service-app-test-5dd98f65dd 1 1 1 4h51m
[root@cela-k8s-master-01 cela]# kubectl get service -n cela
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
cas-service-test NodePort 20.18.194.63 <none> 8092:8092/TCP 4h57m
gateway-service-test NodePort 20.18.73.211 <none> 8091:8091/TCP 5h17m
manager-service-test NodePort 20.18.127.254 <none> 8080:8080/TCP 4h5m
service-service-test NodePort 20.18.27.106 <none> 8093:8093/TCP 4h59m
[root@cela-k8s-master-01 cela]# kubectl get deployment -n cela
NAME READY UP-TO-DATE AVAILABLE AGE
cas-app-test 1/1 1 1 4h53m
gateway-app-test 1/1 1 1 5h14m
manager-app-test 1/1 1 1 4h1m
service-app-test 1/1 1 1 4h55m
[root@cela-k8s-master-01 cela]# kubectl get secret -n kube-system
NAME TYPE DATA AGE
attachdetach-controller-token-t6zkk kubernetes.io/service-account-token 3 2d3h

查看指定…的描述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
[root@cela-k8s-master-01 cela]# kubectl describe deployment.apps/gateway-app-test -n cela
Name: gateway-app-test
Namespace: cela
CreationTimestamp: Sun, 24 Mar 2019 15:06:22 +0800
Labels: app=gateway-app-test
Annotations: deployment.kubernetes.io/revision: 2
kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"labels":{"app":"gateway-app-test"},"name":"gateway-app-test","na...
Selector: app=gateway-app-test
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: app=gateway-app-test
Containers:
gateway-app-test:
Image: hub.facemeta.cn:88/cela/service-gateway:0.3.4
Port: 8091/TCP
Host Port: 0/TCP
Environment:
JVM_PARAMS:
APP_PARAMS: test
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: gateway-app-test-5b8f445667 (1/1 replicas created)
Events: <none>

同样,支持po,service等

查看pod日志

1
2
3
4
[root@cela-k8s-master-01 cela]# kubectl logs pod/gateway-app-test-5b8f445667-b7wtq -n cela
setSystemEnvironment java.net.preferIPv4Stack=true
setSystemEnvironment java.net.preferIPv6Addresses=false
日志较多其余略去......

在容器内执行命令

1
kubectl exec -it <pod-name> -c <container-name> bash

节点管理

节点标签

1
kubectl label nodes cela-k8s-node-02 node=node-test

pod节点选中器

1
2
3
4
spec:
nodeSelector:
node: node-test
nodeName: cela-k8s-node-01

资源管理常用命令

资源配置(包含更新)

1
kubectl apply -f <yaml>

资源删除

1
kubectl delete -f <yaml>

通过指定的资源生成yaml格式(常用于参考快速配置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@cela-k8s-master-01 cela]# kubectl run service-gateway --image=hub.facemeta.cn:88/cela/service-gateway:0.3.4 --dry-run -o yaml
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
run: service-gateway
name: service-gateway
spec:
replicas: 1
selector:
matchLabels:
run: service-gateway
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
run: service-gateway
spec:
containers:
- image: hub.facemeta.cn:88/cela/service-gateway:0.3.4
name: service-gateway
resources: {}
status: {}

镜像替换

1
kubectl set image deployment.apps/gateway-app-test gateway-app-test=hub.facemeta.cn:88/cela/service-gateway:0.3.4 -n cela

回滚常用命令

1
2
[root@cela-k8s-master-01 cela]# kubectl rollout status deployment.apps/gateway-app-test -n cela
deployment "gateway-app-test" successfully rolled out

pause 中断、resume恢复、history历史、undo撤销

删除指定指定状态

1
2
kubectl get pods | grep Evicted | awk '{print $1}' | xargs kubectl delete pod
kubectl get rs -n cela | grep cas | awk '{print $1}' | xargs kubectl delete rs -n cela

推荐书籍

《Kubernetes经典实例》(美)Sébastien Goasguen,(美)Michael Hausenblas

《Kubernetes in Action》(美)Marko Luksa

《Kubernetes权威指南》龚正等