您当前的位置: 首页 >  kubernetes

暂无认证

  • 0浏览

    0关注

    92582博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Kubernetes基础:使用Deployment创建Pod

发布时间:2020-02-04 20:16:49 ,浏览量:0

随着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   Ready2m25s   v1.17.2   192.168.163.131CentOS 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.18280: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! width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }  width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }             
关注
打赏
1653961664
查看更多评论
立即登录/注册

微信扫码登录

0.3626s