部署自己配置的nginx到kubernetes,并且能通过ingress访问

时间:2023-03-10 00:42:23
部署自己配置的nginx到kubernetes,并且能通过ingress访问

本文的环境介绍

[root@m-30-1 ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.2", GitCommit:"81753b10df112992bf51bbc2c2f85208aad78335", GitTreeState:"clean", BuildDate:"2018-04-27T09:22:21Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.2", GitCommit:"81753b10df112992bf51bbc2c2f85208aad78335", GitTreeState:"clean", BuildDate:"2018-04-27T09:10:24Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
[root@m-30-1 ~]# rpm -qf /etc/issue
centos-release-7-4.1708.el7.centos.x86_64

kubernetes集群是通过kubeadm安装的

首先创建镜像

[root@m-30-2 nginx-wis]# pwd
/opt/dockerfile/nginx-wis
[root@m-30-2 nginx-wis]# ls
Dockerfile index.html
[root@m-30-2 nginx-wis]# cat Dockerfile
FROM nginx MAINTAINER wis COPY index.html /usr/share/nginx/html/index.html
[root@m-30-2 nginx-wis]# cat index.html
hello from wis

然后创建镜像,我们可以先打一个简单的tag然后在打一个私仓的tag,不过我们的这个镜像最终也是要传到私仓,所以直接打私仓的tag

[root@m-30-2 nginx-wis]# docker build . -t harbor.my.domain/library/wis-nginx
Sending build context to Docker daemon 3.072 kB
Step 1/3 : FROM nginx
---> ae513a47849c
Step 2/3 : MAINTAINER wis
---> Running in 29aeb5fdae33
---> b68a66ae794c
Removing intermediate container 29aeb5fdae33
Step 3/3 : COPY index.html /usr/share/nginx/html/index.html
---> 81945be11b0c
Removing intermediate container be67728dad51
Successfully built 81945be11b0c

登录私仓 默认的用户名密码admin/Harbor12345,docker的systemd启动文件中要加入--insecure-registry harbor.my.domain,然后我是本机绑的hosts,我用的harbor私仓在172.16.30.1 harbor.my.domain,这个机器上

docker login harbor.my.domain

登录成功后

[root@m-30-2 nginx-wis]# docker push harbor.my.domain/library/wis-nginx
The push refers to a repository [harbor.my.domain/library/wis-nginx]
6a1acaa1b8e7: Pushed
7ab428981537: Pushed
82b81d779f83: Pushed
d626a8ad97a1: Pushed
latest: digest: sha256:974de0760e53a8d46c07ce7ff985e0aca55380e9ae134bbf82fcc1ac8b98324c size: 1155
[root@m-30-2 nginx-wis]#

创建wis-nginx的deployment和service

[root@m-30-2 ingress]# kubectl run wis-nginx --image=harbor.my.domain/library/wis-nginx --expose=true --port=80
service "wis-nginx" created
deployment.apps "wis-nginx" created

创建ingress

[root@m-30-2 ingress]# pwd
/opt/ingress
[root@m-30-2 ingress]# cat wis-nginx.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: wis-nginx
namespace: ops
spec:
rules:
- host: wis-nginx.com
http:
paths:
- backend:
serviceName: wis-nginx
servicePort: 80

本机绑定这个hosts

172.16.30.2 wis-nginx.com

然后本机就能访问这个页面了(在浏览器输入 wis-nginx.com