【K8s最佳实践】构建测试用的Nginx镜像并简单测试集群网络

K8s版本:v1.20

构建模板:Dockerfile

FROM nginx:1.20-alpine

MAINTAINER varden

# 修改安装源
RUN sed -i 's@dl-cdn.alpinelinux.org@mirrors.aliyun.com@g' /etc/apk/repositories
ENV LANG C.UTF-8
RUN apk update

# 安装必需软件和修改时区
RUN apk add --no-cache \
        bash \
        ca-certificates \
        tzdata \
        busybox-extras \
        net-tools \
        tcpdump \
        wget \
        bind-tools \
        curl \
        nmap \
        busybox \
        openssh-client \
        iproute2 \
        ethtool \
        && rm -rf /var/cache/apk/*
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo "Asia/Shanghai" > /etc/timezone

构建shell脚本:build.sh

#!/bin/bash
#
set -u

# 构建环境
DOCKER_REGISTRY=192.168.1.85:5000
PROJECT_NAME=library
APP_NAME=tools
APP_VERSION=1.0

# 构建镜像
docker build -t ${DOCKER_REGISTRY}/${PROJECT_NAME}/${APP_NAME}:${APP_VERSION} .

# 推送镜像
docker push ${DOCKER_REGISTRY}/${PROJECT_NAME}/${APP_NAME}:${APP_VERSION}

部署并简单测试

# kubectl create deployment nginx --image=192.168.1.85:5000/library/tools:1.0
# kubectl expose deployment nginx --port=80 --type=NodePort
# kubectl get pod,svc
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-54cd98b47c-2xm6k   1/1     Running   0          21m

NAME                 TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        107m
service/nginx        NodePort    10.110.122.229   <none>        80:30037/TCP   21m

# kubectl exec -it nginx-54cd98b47c-2xm6k -- bash
bash-5.1# nslookup kubernetes.default
Server:         10.96.0.10
Address:        10.96.0.10#53

Name:   kubernetes.default.svc.cluster.local
Address: 10.96.0.1
posted @ 2021-08-10 09:58  Varden  阅读(415)  评论(0编辑  收藏  举报