Kubernetes 的基本使用方法
原则:使用YAML文件描述你要部署的API对象!
以部署nginx静态站点为例,具体操作及内容如下
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到了吗?朋友
PS:文中服务器使用的是国内某☁️的机器
欢迎大家留言哦~~~