其他分享
首页 > 其他分享> > kubernetes云平台管理实战:namespace的使用(十六)

kubernetes云平台管理实战:namespace的使用(十六)

作者:互联网

一、namespace的作用

Namespace(命名空间)是Kubernetes系统中的另一个非常重要的概念,Namespace在很多情况下用于实现多租户的资源隔离。阿里云

cms项目用到数据库、商城项目也需要数据库,同一个namespace下面不允许出现两个service叫mysql

二、namespace管理命令

创建namespace

root@master dashboard]# kubectl create namespace luoahong
namespace "luoahong" created

查看namespace

[root@master dashboard]# kubectl get namespace 
NAME STATUS AGE
default Active 1d
kube-system Active 1d
luoahong Active 9s

删除namespace

[root@master dashboard]# kubectl delete namespace luoahong
namespace "luoahong" deleted

查看所有namespace

[root@master ~]# kubectl get all --all-namespaces 
NAMESPACE NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kube-system deploy/kube-dns 1 1 1 1 3h
kube-system deploy/kubernetes-dashboard-latest 1 1 1 1 2h

NAMESPACE NAME DESIRED CURRENT READY AGE
default rc/mysql 1 1 1 20h
default rc/readiness 2 2 0 2h

NAMESPACE NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default svc/kubernetes 10.254.0.1 <none> 443/TCP 1d
default svc/mysql 10.254.39.137 <none> 3306/TCP 20h
default svc/nginx-deployment 10.254.250.109 <nodes> 80:32263/TCP 21h
default svc/readiness 10.254.202.52 <none> 80/TCP 2h
kube-system svc/kube-dns 10.254.230.254 <none> 53/UDP,53/TCP 3h
kube-system svc/kubernetes-dashboard 10.254.245.38 <none> 80/TCP 2h

NAMESPACE NAME DESIRED CURRENT READY AGE
default rs/nginx-1734779636 0 0 0 14m
kube-system rs/kube-dns-1835838994 1 1 1 3h
kube-system rs/kubernetes-dashboard-latest-2728556226 1 1 1 2h

NAMESPACE NAME READY STATUS RESTARTS AGE
default po/busybox2 1/1 Running 3 3h
default po/exec 1/1 Running 54 3h
default po/httpget 1/1 Running 1 2h
default po/mysql-3qkf1 1/1 Running 0 20h
default po/readiness-9rc3s 0/1 Running 0 2h
default po/readiness-jhzk5 0/1 Running 0 2h
kube-system po/kube-dns-1835838994-jm5bk 4/4 Running 0 3h
kube-system po/kubernetes-dashboard-latest-2728556226-fc2pc 1/1 Running 0 2h

三、使用namespace

 1、结构与组成

[root@master namespace]# ll
total 8
-rw-r--r-- 1 root root 351 May 13 14:23 nginx-rc.yaml
-rw-r--r-- 1 root root 205 May 13 14:32 nginx-svc.yaml

nginx-rc.yaml

[root@master namespace]# cat nginx-rc.yaml 
apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
  namespace: luoahong
spec:
  replicas: 2
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
      - name: myweb
        image: 192.168.118.18:5000/nginx:1.13
        ports:
        - containerPort: 80

重点看namespace: luoahong

apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
  namespace: luoahong

2、部署运行

[root@master namespace]# kubectl create -f nginx-rc.yaml
replicationcontroller "myweb" created
[root@master namespace]# kubectl get all --namespace=luoahong 
NAME       DESIRED   CURRENT   READY     AGE
rc/myweb   2         2         2         5m

NAME             READY     STATUS    RESTARTS   AGE
po/myweb-qsml7   1/1       Running   0          5m
po/myweb-sbsw2   1/1       Running   0          5m

3、让外界访问

nginx-svc.yaml

[root@master namespace]# cat nginx-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: myweb
  namespace: luoahong
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 30000
      targetPort: 80
  selector:
    app: myweb

部署运行

[root@master namespace]# kubectl create -f nginx-svc.yaml
service "myweb" created
[root@master namespace]# kubectl get all --namespace=luoahong 
NAME       DESIRED   CURRENT   READY     AGE
rc/myweb   2         2         2         8m

NAME        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
svc/myweb   10.254.252.47   <nodes>       80:30000/TCP   29s

NAME             READY     STATUS    RESTARTS   AGE
po/myweb-qsml7   1/1       Running   0          8m
po/myweb-sbsw2   1/1       Running   0          8m

4、测试访问

[root@master namespace]# curl -I 192.168.118.19:30000
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Wed, 13 May 2020 06:33:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 09 Apr 2018 16:01:09 GMT
Connection: keep-alive
ETag: "5acb8e45-264"
Accept-Ranges: bytes

四、通过反向代理访问k8s中的应用

1、访问k8s中应用的方式

第一种:NodePort类型

type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30008

第二种:ClusterIP类型

type: ClusterIP
ports:
- port: 80
targetPort: 80

2、更新nginx-svc.yaml

[root@k8s-master namespace]# cat nginx-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: test
  namespace: luoahong
spec:
  type: NodePort

2、访问演示

http://192.168.118.18:8080/api/v1/proxy/namespaces/luoahong/services/myweb/

 

标签:实战,myweb,kubernetes,default,namespace,nginx,master,root
来源: https://www.cnblogs.com/luoahong/p/12936869.html