Kubernetes的基本使用方法
原则:使用YAML文件描述你要部署的API对象!
1.编写YAML文件
[root@kubernetes01 ~]# cat Nginx-staticwebsite.yaml
apiVersion: apps/v1
kind: Deployment
Metadata:
name: Nginx-staticwebsite
spec:
selector:
matchLabels:
app: Nginx
replicas: 2
template:
Metadata:
labels:
app: Nginx
spec:
containers:
- name: Nginx
image: Nginx:1.9.1
ports:
- containerPort: 80
volumeMounts:
- mountPath: "/usr/share/Nginx/html"
name: Nginx-staticwebsite-vol
volumes:
- name: Nginx-staticwebsite-vol
hostPath:
path: "/home/data/kubernetes/Nginx-staticwebsite"
2.YAML文件说明
apiVersion: apps/v1
kind: Deployment //指定了API对象的类型,Deployment是一个可以定义多副本应用的对象
Metadata: //API对象的标示,用来存放元数据
name: Nginx-staticwebsite //定义元数据名
spec: //对象的独有定义
selector: //选择器
matchLabels: //匹配的标签
app: Nginx //app名称
replicas: 2 //定义副本数量
template: //定义Pod的模版用来描述细节,需要理解Pod就是Kubernetes世界中的应用
Metadata: //模版的元数据
labels: //标签
app: Nginx //app名称
spec:
containers: //Pod中只定义了一个容器
- name: Nginx //名称
image: Nginx:1.9.1 //镜像的名称
ports:
- containerPort: 80 //容器监听的端口
volumeMounts: //Pod中的容器声明自己挂载哪个Volume
- mountPath: "/usr/share/Nginx/html" //定义容器的中的Volume的目录
name: Nginx-staticwebsite-vol //Volume的名字
volumes: //定义这个Pod声明的所有Volume显式定义
- name: Nginx-staticwebsite-vol //Volume的名字
hostPath:
path: "/home/data/kubernetes/Nginx-staticwebsite" //显式定义,Volume挂载的宿主机目录
//隐式定义,我们没有用到
volumes: 定义这个Pod声明的所有卷
- name: Nginx-vol
emptyDir: {} 隐式定义
3.操作YAML文件
1.运行这个YAML文件
kubelctl create -f Nginx-staticwebsite.yaml
2.GET指定的API对象
[root@kubernetes01 ~]# kubectl get pods -l app=Nginx
NAME READY STATUS RESTARTS AGE
Nginx-staticwebsite-8479f8997f-2mh4j 1/1 Running 0 153m
Nginx-staticwebsite-8479f8997f-s526z 1/1 Running 0 153m
3.查看API对象的细节
[root@kubernetes01 ~]# kubectl describe pod Nginx-staticwebsite-8479f8997f-2mh4j
Name: Nginx-staticwebsite-8479f8997f-2mh4j
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: kubernetes09/10.5.0.219
Start Time: Mon, 11 Mar 2019 11:35:53 +0800
Labels: app=Nginx
pod-template-hash=8479f8997f
Annotations: <none>
Status: Running
IP: 10.39.0.2
Controlled By: replicaset/Nginx-staticwebsite-8479f8997f
Containers:
Nginx:
Container ID: docker://08e49e4a3f253e317b7e4a9603e9398bca9f1762daa7dd10ddf885e4e20f2078
Image: Nginx:1.9.1
Image ID: docker-pullable://Nginx@sha256:2f68b99bc0d6d25d0c56876b924ec20418544ff28e1fb89a4c27679a40da811b
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 11 Mar 2019 11:36:09 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/usr/share/Nginx/html from Nginx-staticwebsite-vol (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-8j8dl (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
Nginx-staticwebsite-vol:
Type: HostPath (bare host directory volume)
Path: /home/data/kubernetes/Nginx-staticwebsite
HostPathType:
default-token-8j8dl:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-8j8dl
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: <none>
4.尝试更新镜像
修改YAML文件中的Nginx的镜像版本
[root@kubernetes01 ~]# cat Nginx-staticwebsite.yaml | grep "image"
image: Nginx:1.9.2
[root@kubernetes01 ~]# kubectl apply -f Nginx-staticwebsite.yaml
[root@kubernetes01 ~]# kubectl get pods -l app=Nginx
NAME READY STATUS RESTARTS AGE
Nginx-deployment-7f987f7889-8tbbt 1/1 Running 0 3h23m
Nginx-deployment-7f987f7889-rh8xc 1/1 Running 0 3h23m
Nginx-staticwebsite-648bc64544-snjpg 0/1 ContainerCreating 0 23s
Nginx-staticwebsite-8479f8997f-2mh4j 1/1 Running 0 160m
Nginx-staticwebsite-8479f8997f-s526z 1/1 Running 0 160m
[root@kubernetes01 ~]# kubectl describe pod Nginx-staticwebsite-648bc64544-snjpg
Name: Nginx-staticwebsite-648bc64544-snjpg
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: kubernetes05/10.5.0.210
Start Time: Mon, 11 Mar 2019 14:15:36 +0800
Labels: app=Nginx
pod-template-hash=648bc64544
Annotations: <none>
Status: Running
IP: 10.45.0.3
Controlled By: replicaset/Nginx-staticwebsite-648bc64544
Containers:
Nginx:
Container ID: docker://b3fb6b0aa095990482027158c4bce84dbe023f2ae7f2ccb6b4d521d3274a93cf
Image: Nginx:1.9.2
Image ID: docker-pullable://Nginx@sha256:7ffb2ccf2de82b0afeee46ae9d7542e9643587a8d4dbe2901cf466369e3c10f5
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 11 Mar 2019 14:16:00 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/usr/share/Nginx/html from Nginx-staticwebsite-vol (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-8j8dl (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
Nginx-staticwebsite-vol:
Type: HostPath (bare host directory volume)
Path: /home/data/kubernetes/Nginx-staticwebsite
HostPathType:
default-token-8j8dl:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-8j8dl
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 70s default-scheduler Successfully assigned default/Nginx-staticwebsite-648bc64544-snjpg to kubernetes05
normal Pulling 69s kubelet, kubernetes05 pulling image "Nginx:1.9.2"
normal Pulled 46s kubelet, kubernetes05 Successfully pulled image "Nginx:1.9.2"
normal Created 46s kubelet, kubernetes05 Created container
normal Started 46s kubelet, kubernetes05 Started container
可以看到Events中的信息!这块信息很重要因为我们可以用来排错!
5.静态网站访问
[root@kubernetes01 ~]# curl 10.35.0.3
Nginx static website!
[root@kubernetes01 ~]# curl 10.39.0.2
Nginx static website!
4.总结
通过对Kubenetes的基本操作和体验,不难发现,YAML文件我们可以做版本化管理,当热这也是发布应用的最基本操作!你GET到了吗?朋友
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。