上一节搭建了 k8s 集群环境,两台机器,一台 master, 一个 worker. 就像一个机器的池子一样,我的应用以 pod 的形式跑在某台机器上,这个应用所需要 cpu, memory 等资源都会由 k8s 来调度
启动两个 nginx 应用
k8s 描述一个 pod 都会用 yaml 配置,而不是裸用 docker run 去跑
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
上面定义一个 nginx-deployment.yaml, 其中 replicas: 2 表示启动两个的意思,其它暂时不管了,也看不懂
kubectl create -f nginx-deployment.yaml
然后执行查看命令
kubectl get pods --all-namespaces
或是
kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
nginx-deployment-76bf4969df-t5cj4 1/1 Running 0 36m
nginx-deployment-76bf4969df-zc8pc 1/1 Running 0 36m
可以看到两个 nginx 的 pod 正在运行
查看 nginx pod
此时己经运行了两个 nginx pod, 查看详细信息
kubectl describe pod nginx-deployment-76bf4969df-t5cj4
Name: nginx-deployment-76bf4969df-t5cj4
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: jjh-fusion-sre-backup3/10.20.76.23
Start Time: Wed, 27 Feb 2019 14:41:29 +0800
Labels: app=nginx
pod-template-hash=76bf4969df
Annotations: <none>
Status: Running
IP: 10.44.0.9
Controlled By: ReplicaSet/nginx-deployment-76bf4969df
Containers:
nginx:
Container ID: docker://5fc7612b01cb8f6148c4306db73565a30989bda932ec13aa82d07507a96ae33e
Image: nginx:1.7.9
Image ID: docker-pullable://nginx@sha256:e3456c851a152494c3e4ff5fcc26f240206abac0c9d794affb40e0714846c451
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 27 Feb 2019 14:41:54 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-r78v4 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-r78v4:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-r78v4
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 40m default-scheduler Successfully assigned default/nginx-deployment-76bf4969df-t5cj4 to jjh-fusion-sre-backup3
Normal Pulling 40m kubelet, jjh-fusion-sre-backup3 pulling image "nginx:1.7.9"
Normal Pulled 40m kubelet, jjh-fusion-sre-backup3 Successfully pulled image "nginx:1.7.9"
Normal Created 40m kubelet, jjh-fusion-sre-backup3 Created container
Normal Started 40m kubelet, jjh-fusion-sre-backup3 Started container
可以看到分配的 ip 是 10.44.0.9,那么直接访问就可以了
curl http://10.44.0.9
下面还有镜像的详细信息,运行在 worker 机器上面等等。一般来说,对一个应用做的所有操作,都会返映在 describe 信息里面
升级 nginx
修改 nginx-deployment.yaml 文件,将 nginx 版本变成 1.8
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.8
ports:
- containerPort: 80
执行以下命令应用
kubectl apply -f nginx-deployment.yaml
查看当前 pods
# kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
nginx-deployment-5896fbb489-8zjdg 1/1 Running 0 18s
nginx-deployment-5896fbb489-dczqn 1/1 Running 0 37s
查看详细某一个 pod
# kubectl describe pod nginx-deployment-5896fbb489-8zjdg
Name: nginx-deployment-5896fbb489-8zjdg
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: jjh-fusion-sre-backup3/10.20.76.23
Start Time: Wed, 27 Feb 2019 15:32:06 +0800
Labels: app=nginx
pod-template-hash=5896fbb489
Annotations: <none>
Status: Running
IP: 10.44.0.9
Controlled By: ReplicaSet/nginx-deployment-5896fbb489
Containers:
nginx:
Container ID: docker://dfeb8fed612173c94d0c48547d068025fa8e76d4c6e6fa39e24b9ceeae58de0d
Image: nginx:1.8
Image ID: docker-pullable://nginx@sha256:c97ee70c4048fe79765f7c2ec0931957c2898f47400128f4f3640d0ae5d60d10
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 27 Feb 2019 15:32:08 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-r78v4 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-r78v4:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-r78v4
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 57s default-scheduler Successfully assigned default/nginx-deployment-5896fbb489-8zjdg to jjh-fusion-sre-backup3
Normal Pulled 55s kubelet, jjh-fusion-sre-backup3 Container image "nginx:1.8" already present on machine
Normal Created 55s kubelet, jjh-fusion-sre-backup3 Created container
Normal Started 55s kubelet, jjh-fusion-sre-backup3 Started container
可以看到,己经变成了 nginx 1.8 的版本
挂载 volume 卷
现在对这个 nginx pod 增加 volume,挂载点是 /usr/share/nginx/html
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.8
ports:
- containerPort: 80
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: nginx-vol
volumes:
- name: nginx-vol
emptyDir: {}
挂载的卷是 nginx-vol,emptyDir 表示在宿主机临时生成一个目录。重新应用这个 yaml
kubectl apply -f nginx-deployment.yaml
然后立马查看 pods 状态,会发现在滚动升机重启
# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5896fbb489-dczqn 0/1 Terminating 0 20h
nginx-deployment-58bbdfd545-4rg8s 1/1 Running 0 10s
nginx-deployment-58bbdfd545-69kp5 1/1 Running 0 12s
通过 describe 查看其中一个 pod 详情
# kubectl describe pod nginx-deployment-58bbdfd545-4rg8s
Name: nginx-deployment-58bbdfd545-4rg8s
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: jjh-fusion-sre-backup3/10.20.76.23
Start Time: Thu, 28 Feb 2019 11:50:21 +0800
Labels: app=nginx
pod-template-hash=58bbdfd545
Annotations: <none>
Status: Running
IP: 10.44.0.9
Controlled By: ReplicaSet/nginx-deployment-58bbdfd545
Containers:
nginx:
Container ID: docker://fc26530dbc54377c7034700777498d6815ca8d578806c03c4175a33388cd3f9b
Image: nginx:1.8
Image ID: docker-pullable://nginx@sha256:c97ee70c4048fe79765f7c2ec0931957c2898f47400128f4f3640d0ae5d60d10
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 28 Feb 2019 11:55:49 +0800
Last State: Terminated
Reason: Completed
Exit Code: 0
Started: Thu, 28 Feb 2019 11:50:23 +0800
Finished: Thu, 28 Feb 2019 11:55:47 +0800
Ready: True
Restart Count: 1
Environment: <none>
Mounts:
/usr/share/nginx/html from nginx-vol (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-r78v4 (ro)
可以看到 Mounts 挂载信息,此时我们访问这个 nginx 会报错
# curl http://10.44.0.9
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.8.1</center>
</body>
</html>
因为我们此时目录虽然挂载了,但是是空的,所以需要进入容器中写一个 html,进到 node 机器上 docer ps 查看 nginx 容器的 ID
docker exec -it e3c3007ad67a /bin/bash
也可以直接用 kubectl 命令来操作,这样就完全屏敝了底层 docker
kubectl exec -it nginx-deployment-58bbdfd545-4rg8s -- /bin/bash
然后随变写一个 hello world
echo "hello world" >> /usr/share/nginx/html/index.html
此时再访问就会看到 hello world
小结
至此,己经成功用 k8s 集群创建了第一个应用,还有很多概念与功能不太熟悉。万事开头难,搞起~~~
网友评论