kubernetes

最困难的耗时间 | kubernetes-the-hard-way
工具:

kubectl

1
2
3
4
5
6
7
8
9
10
# 节点
kubectl get node
# 节点详细信息
kubectl get node -o wide
# 上下文
kubectl context
# 获取不同的 context
kubectl config get-contexts
# 查看集群情况
kubectl cluster-info
1
2
# 查看当前 minikube 生成集群的基本信息
kubectl config view

minikube

使用minikube创建虚拟机,在VirtualBox里面
需要安装kubernetskubectl brew install kubectl

1
2
3
4
5
6
7
8
# 创建单节点的 K8s 集群
minikube start
minikube start --bootstrapper=localkube # 上面运行不成功就这个
minikube delete
# 进入 minikube 创建的 virtualbox 虚拟机
minikube ssh
# minikube web
minikube dashboard
  • 阿里云安装minikube
1
2
3
4
# Mac OSX 验证
curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v0.28.1/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/
# Linux
curl -Lo minikube http://kubernetes.oss-cn-hangzhou.aliyuncs.com/minikube/releases/v0.28.1/minikube-linux-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/

k8s

pod

最小的调度单位

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: nginx
spec:
# 定义容器 最重要的部分
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80 # 暴露的端口
1
2
3
4
5
6
kubectl create -f pod_nginx.yml
kubectl delete -f pod_ngins.yml
kubectl get pods
kubectl get pods -o wide # 显示容器的详细信息
kubectl exec -it nginx sh # 进入容器
kubectl port-forward nginx 8080:80 # 映射 8080 到容器里面的 80
1
2
3
~ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 1m
1
2
3
~ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
nginx 1/1 Running 0 4m 172.17.0.4 minikube

横向扩展

  • ReplicsSetv1 版本不支持,扩展
  • ReplicationController
1
2
3
4
5
6
kubectl get pods
# 删除 pod 之后 k8s 会自动重启一个 pod
kubectl delete pods <pods name>
# 调整 nginx pod 为 2
kubectl scale rc nginx --replicas=2
kubectl get rc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
apiVersion: apps/v1
kind: ReplicaSet # ReplicationController
metadata:
name: nginx
labels:
tier: frontend
spec:
replicas: 3 # 扩展 3 个 nginx
selector:
matchLabels:
tier: frontend
template:
metadata:
name: nginx
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80

Deployments

  • example yaml file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: apps/v1
kind: Deployment # 指定的类型
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3 # 保证这个 pods 里面至少是 3 个
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.12.2
ports:
- containerPort: 80
1
2
3
4
5
6
7
# 操作示例
# 创建
kubectl create -f deployment_nginx.yml
kubectl get deployment # 查看数量
kubectl get rs # 有 dep 也会有这个
# 查看详细信息
kubectl get deployment -o wide

deployment
看 NAME 会一层一层的加

升级 image

实现平滑升级,过渡

1
2
# 对 image 升级
kubectl set image deployment nginx-deployment nginx=nginx:1.13

回滚

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-7498dc98f8 0 0 0 17m
nginx-deployment-86cd46c4d9 3 3 3 7m
#----------------------------
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-86cd46c4d9-8zvtk 1/1 Running 0 7m
nginx-deployment-86cd46c4d9-bhn2d 1/1 Running 0 7m
nginx-deployment-86cd46c4d9-ltmn9 1/1 Running 0 7m
#----------------------------
# 历史
$ kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
1 <none>
2 <none>
#----------------------------
# 回滚
$ kubectl rollout undo deployment nginx-deployment
deployment "nginx-deployment"
$ kubectl get deployment -o wide
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-deployment 3 3 3 3 20m nginx nginx:1.12.2 app=nginx

Service

port simple example

1
2
3
4
5
6
7
8
# 创建
kubectl expose deployment nginx-deployment --type=NodePort
# --------------------
# 监听了 minikube 的端口 32117
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1h
nginx-deployment NodePort 10.102.175.227 <none> 80:32117/TCP 18s

对外服务的三种方式(端口)

  • kubectl expose
  • yaml文件定义service
    • ClusterIP(外界无法访问)
    • NodePort(绑定到Node,对外提供访问的)
    • LoadBalancer(云服务商提供)
  • DNS

ClusterIP

内部网络

1
2
3
4
5
6
7
# ClusterIP
# 创建
kubectl expose deployment service-test
# 单行 shell
while true; do curl 10.3.120.168:80080; done
# 无宕机更新? 这个会有一小段的中断
kubectl edit deployment service-test

Rolling Update todo,task

NodePort

对外提供访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# pod_nginx.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx-container
image: nginx
ports:
- name: nginx-port
containerPort: 80
  • example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 创建 pod
kubectl create -f pod_nginx.yml
# 创建 NodePort
kubectl expose pods nginx-pods --type=NodePort
# 查看 node 详细信息
kubectl describe node minikube
# ----------------------------
# 查看 NodePort
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19h
nginx-deployment NodePort 10.102.175.227 <none> 80:32117/TCP 17h
# ---------------------------
$ kubectl get node
NAME STATUS ROLES AGE VERSION
minikube Ready master 19h v1.10.0

通过yaml文件创建service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# service_nginx.yml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
ports:
- port: 33333 # 8080
nodePort: 32333 # 8080
targetPort: nginx-port
protocol: TCP
selector: # 过滤 选择暴露哪个 pod
app: nginx
type: NodePort
1
2
3
4
# lables
$ kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-pod 1/1 Running 0 41m app=nginx

lable

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 先创建 pod
kubectl create -f
# 设置lable
bukectl label node minikube hardware=good
# -------------------------------
# 执行前
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox-pod 0/1 Pending 0 16s
nginx-pod 1/1 Running 0 1h
# -----------------------------
# 执行后
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
busybox-pod 1/1 Running 0 1m
nginx-pod 1/1 Running 0 1h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# pod_busybox.yml
# 创建之后它会是 pending
# 它会寻找 lable 是 hardware: good 节点来运行
apiVersion: v1
kind: Pod
metadata:
name: busybox-pod
labels:
app: busybox
spec:
nodeSelector:
hardware: good
containers:
- name: busybox-container
image: busybox
command:
- sleep
- "360000"

生产环境一般不用ClusterIPNodePort,而是LoadBalanceExternalName

LoadBalance

external-dns