Kubernetes基础:使用Deployment创建Pod

随着RC的退出,Deployment作为Pod的控制器之一使用地越来越广泛,这篇文章以nginx服务为例,介绍一下使用的方式。

事前准备

本文使用Kubernetes 1.17.2,可参看下文进行快速环境搭建:

  • 单机版本或者集群版本环境搭建
[root@host131 ansible]# kubectl get node -o wide
NAME              STATUS   ROLES    AGE     VERSION   INTERNAL-IP       EXTERNAL-IP   OS-IMAGE                KERNEL-VERSION          CONTAINER-RUNTIME
192.168.163.131   Ready       2m25s   v1.17.2   192.168.163.131           CentOS Linux 7 (Core)   3.10.0-957.el7.x86_64   docker://19.3.5
[root@host131 ansible]# 

Deployment配置文件

使用如下yaml文件用于设定Deployment

[root@host131 nginx]# cat deployment.yml 
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx-host
        image: nginx:1.17.8-alpine
        ports:
        - containerPort: 80
...
[root@host131 nginx]# 
  • 注意事项:matchLabels的内容需要与template中metadata的labels内容匹配,不过不用担心,在创建时会进行检查,比如将matchLabels改为app: nginx-app1,创建时会提示如下错误信息
[root@host131 nginx]# kubectl create -f deployment.yml 
The Deployment "nginx-deployment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"nginx-app1"}: `selector` does not match template `labels`
[root@host131 nginx]#

Service配置文件

使用如下Service配置文件

[root@host131 nginx]# cat service.yml 
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    app: nginx-service-app
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30000
  selector:
    app: nginx-app
...
[root@host131 nginx]# 
  • 注意事项:selector关联的需要是Deployment中template定义的label。

生成Deployment

使用Deployment的配置文件生成Deployment

[root@host131 nginx]# kubectl create -f deployment.yml 
deployment.apps/nginx-deployment created
[root@host131 nginx]#

结果确认

[root@host131 nginx]# kubectl get deployment -o wide |grep nginx
nginx-deployment   1/1     1            1           34s    nginx-host   nginx:1.17.8-alpine                  app=nginx-app
[root@host131 nginx]# 
[root@host131 nginx]# kubectl get pods -o wide |grep nginx
nginx-deployment-7c5b8fc568-wkh9b   1/1     Running   0          51s    10.254.152.6   192.168.163.131              
[root@host131 nginx]# 
[root@host131 nginx]# 
[root@host131 nginx]# kubectl exec -it nginx-deployment-7c5b8fc568-wkh9b sh
/ # hostname
nginx-deployment-7c5b8fc568-wkh9b
/ #

生成Service

[root@host131 nginx]# kubectl create -f service.yml 
service/nginx-service created
[root@host131 nginx]#

结果确认

[root@host131 nginx]# kubectl get service -o wide |grep nginx
nginx-service   NodePort    10.254.77.182           80:30000/TCP   9s      app=nginx-app
[root@host131 nginx]# 

nginx使用:集群地址访问

在集群所在的集群中,可以使用集群地址+80端口进行访问

[root@host131 nginx]# curl http://10.254.77.182

<html>
<head>
<title>Welcome to nginx!title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
style>
head>
<body>
<h1>Welcome to nginx!h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.orga>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.coma>.p>

<p><em>Thank you for using nginx.em>p>
body>
html>
[root@host131 nginx]#

也可以利用NodePort端口结合宿主机IP进行访问

[root@host131 nginx]# curl http://192.168.163.131:30000

<html>
<head>
<title>Welcome to nginx!title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
style>
head>
<body>
<h1>Welcome to nginx!h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.orga>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.coma>.p>

<p><em>Thank you for using nginx.em>p>
body>
html>
[root@host131 nginx]# 

当然这种方式也可以直接使用浏览器进行确认
Kubernetes基础:使用Deployment创建Pod_第1张图片

总结

Deployment作为Pod的控制器,可以对Pod进行创建、滚动升级等常见操作,这篇文章以nginx服务为例进行说明,介绍了Deployment和Service在Kubernetes中最常见的使用方式之一。

你可能感兴趣的:(#,深入浅出kubernetes,#,Nginx)