前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >二进制安装k8s集群(18)-安装示例nginx application

二进制安装k8s集群(18)-安装示例nginx application

作者头像
TA码字
发布2020-04-01 15:00:18
4640
发布2020-04-01 15:00:18
举报
文章被收录于专栏:TA码字

上一篇文章中我们安装了pod的调试工具kubectl-debug,在这里我们安装示例nginx application,包括deployment,service,ingress等。对于这个示例application我们安装在default namespace里,然后再用上一篇文章中介绍的kubectl-debug调试一下这个application。

创建配置文件目录:

由于nginx application是以deployment的方式部署在k8s集群里的,一般都会有yaml部署文件,目前都放在此目录里。

代码语言:javascript
复制
mkdir -p /opt/application/k8s/nginx-app
cd /opt/application/k8s/nginx-app

patch service account:

k8s需要把image pull下来,我们这里用的是private repo(以前文章介绍过的harbor),需要得到private repo的认证才可以pull private project的image。对于k8s来说把private repo的认证信息存储在secret对象里。一般每个pod都属于一个namespace,每个namesapce都有一个默认的service account(即在声明的yaml中不指定service-account就会用这个默认的service-account)。我们可以把这个pull image的secret关联到这个默认的service-account,那么在声明的时候就不需要指定image pull secret了。

代码语言:javascript
复制
kubectl create secret docker-registry container-registry --docker-server=172.20.11.41:1034 \
--docker-username=admin --docker-password=abc123_ --namespace=default

kubectl describe secret container-registry -n default

kubectl patch serviceaccount default --namespace=default  -p '{"imagePullSecrets": [{"name": "container-registry"}]}'

创建nginx application的deployment:

代码语言:javascript
复制
cat > /opt/application/k8s/nginx-app/nginx-deployment.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-nginx-app
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      k8s-app: nginx-app
  template:
    metadata:
      labels:
        k8s-app: nginx-app
    spec:
      containers:
        - name: nginx-app
          image: 172.20.11.41:1034/library/nginx:latest
          imagePullPolicy: IfNotPresent
          ports:
            - name: http
              containerPort: 80
          livenessProbe:
            failureThreshold: 3
            httpGet:
              path: /index.html
              port: 80
              scheme: HTTP
            initialDelaySeconds: 60
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 15
          readinessProbe:
            failureThreshold: 3
            httpGet:
              path: /index.html
              port: 80
              scheme: HTTP
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 15
EOF

kubectl create -f /opt/application/k8s/nginx-app/nginx-deployment.yaml
kubectl describe deployment deployment-nginx-app -n default

创建nginx application的service:

这里把service定义成cluster-ip类型,后面通过定义ingress rule来把这个服务暴露给外部调用。

代码语言:javascript
复制
cat > /opt/application/k8s/nginx-app/nginx-app-service.yaml <<EOF
apiVersion: v1
kind: Service
metadata:
  name: service-nginx-app
  namespace: default
spec:
  selector:
    k8s-app: nginx-app
  type: ClusterIP
  ports:
  - name: nginx-app-http
    port: 80
    targetPort: 80
    protocol: TCP
EOF

kubectl create -f  /opt/application/k8s/nginx-app/nginx-app-service.yaml 
kubectl describe service service-nginx-app -n default

创建nginx application的ingress规则:

代码语言:javascript
复制
cat > /opt/application/k8s/nginx-app/nginx-app-ingress.yaml <<EOF
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-of-nginx-app
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /nginx
        backend:
          serviceName: service-nginx-app
          servicePort: 80
EOF

kubectl create -f  /opt/application/k8s/nginx-app/nginx-app-ingress.yaml
kubectl describe ingress ingress-of-nginx-app -n default

外部访问nginx application:

这里我们在创建nginx-ingress-controller的时候,这个nginx-ingress-controller会默认有http到https的跳转,所以虽然我们在container里面没有声明https,我们依然可以访问到。另外在证书详情里我们也可以看到这个证书就是我们以前文章中给nginx-ingress-controller配置的默认ssl证书。

查看nginx application的pods开启并调试:

这里我们看到nginx application一共2个pod,分别是10.1.79.4(在node 172.20.11.43上)和10.1.27.2(在node 172.20.11.42上)

代码语言:javascript
复制
kubectl get pods -n default

调试nginx pod:

这里我们debug进入10.1.79.4(在node 172.20.11.43上)中,分别用ping去ping 10.1.27.2(在node 172.20.11.42上)。用curl访问10.1.27.2(在node 172.20.11.42上)的nginx application,用tracroute去跟踪10.1.27.2(在node 172.20.11.42上),用nslookup去查询内部service的fqdn和外部域名的fqdn。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-11-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 TA码字 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档