Kubernetes(k8s)-Depolyment方式部署应用
作者:myluzh 分类: Kubernetes 长度:4294 阅读:398
0x01 命令行部署
[root@master ~]# kubectl run testapp --image=ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v1
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
testapp 0/1 ContainerCreating 0 16s
0x02 pod部署
1.编写pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-pod
spec:
# 定义容器,可以多个
containers:
- name: test-k8s # 容器名字
image: ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v1 # 镜像
2.部署
[root@master ~]# kubectl apply -f ./pod.yaml
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-pod 1/1 Running 0 11s
testapp 1/1 Running 0 8m16s
0x03 Depolyment部署
Depolyment通过lables关联pods
1.编写hellohttp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
# 部署名字
name: hellohttp
spec:
replicas: 5
# 用来查找关联的 Pod,所有标签都匹配才行
selector:
matchLabels:
app: hellohttp
# 定义 Pod 相关数据
template:
metadata:
labels:
app: hellohttp
spec:
# 定义容器,可以多个
containers:
- name: hellohttp # 容器名字
image: registry.cn-hangzhou.aliyuncs.com/myluzh/hellohttp:v1 # 镜像
2.部署
[root@master hellohttp]# kubectl apply -f app.yaml
[root@master hellohttp]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hellohttp-78c5857bbd-5v6m6 1/1 Running 0 15m 10.32.0.3 node2 <none> <none>
hellohttp-78c5857bbd-b4jg4 1/1 Running 0 15m 10.40.0.4 node1 <none> <none>
hellohttp-78c5857bbd-pfdkv 1/1 Running 0 15m 10.40.0.3 node1 <none> <none>
hellohttp-78c5857bbd-xjkvr 1/1 Running 0 15m 10.32.0.2 node2 <none> <none>
hellohttp-78c5857bbd-z4n4q 1/1 Running 0 15m 10.32.0.4 node2 <none> <none>
3.使用 describe查看pod状态
[root@master hellohttp]# kubectl describe pod/hellohttp-78c5857bbd-5v6m6
注意:如果发现 Events 中有如下报错:"network: open /run/flannel/subnet.env: no such file or directory",解决方案
在每个节点创建文件/run/flannel/subnet.env写入以下内容,配置后等待一会就好了。
FLANNEL_NETWORK=10.244.0.0/16
FLANNEL_SUBNET=10.244.0.1/24
FLANNEL_MTU=1450
FLANNEL_IPMASQ=true