09-kubernetes创建一个简单的Nginx Service

1.0 节点环境

ip地址主机名称角色
10.0.0.70master1.lec.orgK8s 集群主节点 1,Master和etcd
10.0.0.71node1.lec.orgK8s 集群工作节点 1
10.0.0.72node2.lec.orgK8s 集群工作节点 2
10.0.0.73node3.lec.orgK8s 集群工作节点 3

1.1 创建命名空间namespace

1.1.1 创建命名空间的yaml文件 nginx-namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: nginx
  labels:
    name: nginx

1.1.2 常用命令

#创建
root@master1:~/yaml# kubectl apply -f nginx-namespace.yaml
namespace/nginx created
#查询
root@master1:~/yaml# kubectl get namespace
NAME              STATUS   AGE
default           Active   23d
kube-flannel      Active   23d
kube-node-lease   Active   23d
kube-public       Active   23d
kube-system       Active   23d
nginx             Active   6s

# 删除
kubectl delete -f nginx-namespace.yaml

1.2 创建deployment(用于pod发布)

1.2.1 创建deployment的yaml文件 nginx-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment  # Deployment名称
  namespace: nginx   # 命名空间
spec:
  replicas: 1   # 启动几个pod节点
  selector: 	# 标签选择器
    matchLabels:	#选择包含标签app:nginx的资源
      app: nginx 
  template:		#这是选择或创建的Pod的模板
    metadata:
      labels:	#Pod的标签,上面的selector即选择包含标签app:nginx的Pod
        app: nginx
    spec:
      containers:	#期望Pod实现的功能(即在pod中部署)
      - name: nginx-deploy	#container的名称
        image: nginx:1.20.0 # 镜像版本
        imagePullPolicy: IfNotPresent  #本地有镜像就不在拉取

1.2.2 常用命令

#创建  
root@master1:~/yaml# kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created

# 查看 Deployment
root@master1:~/yaml# kubectl get deployment -n nginx -o wide 
NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS     IMAGES         SELECTOR
nginx-deployment   1/1     1            1           28s   nginx-deploy   nginx:1.20.0   app=nginx
# 查看 Pod

root@master1:~/yaml# kubectl get pods -n nginx -o wide
NAME                                READY   STATUS    RESTARTS   AGE    IP            NODE            NOMINATED NODE   READINESS GATES
nginx-deployment-766496f884-c679g   1/1     Running   0          2m6s   10.244.2.22   node2.lec.org   <none>           <none>

# 删除
kubectl delete -f nginx-deployment.yaml

1.3 创建nginx的服务Service

  • 通过labelselector选择一组pod,把这些pod指定端口公布到集群外部,并支持负载均衡和服务发现。

1.3.1 创建Service的yaml文件 nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service # service 名称
  namespace: nginx	# 命名空间
  labels:
    app: nginx
spec:
  selector:
    app: nginx
  ports:
  - port: 80 			# 集群内的其他容器组可通过 80 端口访问 Service
    name: nginx-port 	# 端口的名字
    protocol: TCP		# 协议类型 TCP/UDP
    targetPort: 80		# 将请求转发到匹配 Pod 的 80 端口
    nodePort: 32600 	# 通过任意节点的 32600 端口访问 Service(对外发布的端口)
  type: NodePort		# Serive的类型,ClusterIP/NodePort/LoaderBalancer

1.3.2 常用命令

# 创建

root@master1:~/yaml# kubectl apply -f nginx-service.yaml
service/nginx-service created

# 查看
root@master1:~/yaml# kubectl get svc nginx-service -o wide  -n nginx
NAME            TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE   SELECTOR
nginx-service   NodePort   10.102.65.85   <none>        80:32600/TCP   32s   app=nginx

# 删除
kubectl delete -f nginx-service.yaml

1.4 进入容器nginx并修改index.html

# 进入容器nginx
root@master1:~/yaml# kubectl exec -it nginx-deployment-766496f884-c679g -n nginx -- /bin/sh

# cd /usr/share/nginx/html/
# echo "nginx-services node2.lec.org test html" > index.html 
# cat index.html
nginx-services node2.lec.org test html

1.5 测试访问nginx,任意节点的IP(master和node都行)

  • curl <任意节点的IP>:31098
master
root@master1:~/yaml# curl 10.0.0.70:32600
nginx-services node2.lec.org test html

node 
root@master1:~/yaml# curl 10.0.0.71:32600
nginx-services node2.lec.org test html
root@master1:~/yaml# curl 10.0.0.73:32600
nginx-services node2.lec.org test html
root@master1:~/yaml# curl 10.0.0.72:32600
nginx-services node2.lec.org test html

浏览器访问结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值